@@ -8,20 +8,22 @@ import {
88 CommandLoading ,
99} from '@/features/common/components/command'
1010import { cn } from '@/features/common/utils'
11- import { useCallback } from 'react'
11+ import { useCallback , useEffect , useRef } from 'react'
1212import { RenderLoadable } from '@/features/common/components/render-loadable'
1313import { useNavigate } from 'react-router-dom'
1414import { useSearch } from '../data'
1515import { Loader } from 'lucide-react'
1616import { Badge } from '@/features/common/components/badge'
1717import { useLocationChange } from '@/features/common/hooks/use-location-change'
18+ import { isMacOs } from '@/utils/is-mac-platform.ts'
1819
19- export const searchPlaceholderLabel = ' Search by ID or Address'
20+ export const searchPlaceholderLabel = ` Search by ID or Address ${ isMacOs ? '(⌘K)' : '(Ctrl+K)' } `
2021export const noSearchResultsMessage = 'No results.'
2122
2223export function Search ( ) {
2324 const navigate = useNavigate ( )
2425 const [ term , setTerm , loadableResults ] = useSearch ( )
26+ const searchInputRef = useRef < HTMLInputElement > ( null )
2527
2628 const handleInput = useCallback (
2729 ( id : string ) => {
@@ -41,9 +43,24 @@ export function Search() {
4143
4244 useLocationChange ( clearTerm )
4345
46+ useEffect ( ( ) => {
47+ const down = ( e : KeyboardEvent ) => {
48+ if ( e . key === 'k' && ( e . metaKey || e . ctrlKey ) ) {
49+ e . preventDefault ( )
50+ searchInputRef . current ?. focus ( )
51+ }
52+ if ( e . key === 'Escape' ) {
53+ e . preventDefault ( )
54+ setTerm ( '' )
55+ }
56+ }
57+ document . addEventListener ( 'keydown' , down )
58+ return ( ) => document . removeEventListener ( 'keydown' , down )
59+ } , [ setTerm ] )
60+
4461 return (
4562 < Command className = { cn ( 'bg-card text-card-foreground w-80 h-auto z-20 border' ) } shouldFilter = { false } loop >
46- < CommandInput placeholder = { searchPlaceholderLabel } value = { term } onValueChange = { handleInput } />
63+ < CommandInput placeholder = { searchPlaceholderLabel } value = { term } onValueChange = { handleInput } ref = { searchInputRef } />
4764 < CommandList >
4865 < RenderLoadable
4966 loadable = { loadableResults }
0 commit comments