- Introduced a new CartButton component to encapsulate cart summary functionality, improving code reusability and maintainability. - Updated MarketPage.vue and StallView.vue to utilize the CartButton component, enhancing user navigation to the cart. - Removed redundant cart summary code from both views, streamlining the component structure. These changes provide a unified and consistent user experience for accessing the cart across different market views.
53 lines
No EOL
1.5 KiB
TypeScript
53 lines
No EOL
1.5 KiB
TypeScript
import { onMounted, onUnmounted, type Ref } from 'vue'
|
|
|
|
export function useSearchKeyboardShortcuts(searchInputRef: Ref<any>) {
|
|
const focusSearchInput = () => {
|
|
if (searchInputRef.value?.$el) {
|
|
// Access the underlying HTML input element from the Shadcn Input component
|
|
const inputElement = searchInputRef.value.$el.querySelector('input') || searchInputRef.value.$el
|
|
if (inputElement && typeof inputElement.focus === 'function') {
|
|
inputElement.focus()
|
|
}
|
|
}
|
|
}
|
|
|
|
const blurSearchInput = () => {
|
|
if (searchInputRef.value?.$el) {
|
|
const inputElement = searchInputRef.value.$el.querySelector('input') || searchInputRef.value.$el
|
|
if (inputElement && typeof inputElement.blur === 'function') {
|
|
inputElement.blur()
|
|
}
|
|
}
|
|
}
|
|
|
|
const handleGlobalKeydown = (event: KeyboardEvent) => {
|
|
// ⌘K or Ctrl+K to focus search
|
|
if ((event.metaKey || event.ctrlKey) && event.key === 'k') {
|
|
event.preventDefault()
|
|
focusSearchInput()
|
|
}
|
|
}
|
|
|
|
const handleSearchKeydown = (event: KeyboardEvent) => {
|
|
// Escape key clears search or blurs input
|
|
if (event.key === 'Escape') {
|
|
event.preventDefault()
|
|
return true // Signal to clear search
|
|
}
|
|
return false
|
|
}
|
|
|
|
onMounted(() => {
|
|
document.addEventListener('keydown', handleGlobalKeydown)
|
|
})
|
|
|
|
onUnmounted(() => {
|
|
document.removeEventListener('keydown', handleGlobalKeydown)
|
|
})
|
|
|
|
return {
|
|
focusSearchInput,
|
|
blurSearchInput,
|
|
handleSearchKeydown
|
|
}
|
|
} |