@@ -8,13 +8,23 @@ import { createPortal } from 'react-dom';
88
99import { DocSearchButton } from './DocSearchButton' ;
1010import { DocSearchModal } from './DocSearchModal' ;
11+ import { ControlKeyIcon } from './icons/ControlKeyIcon' ;
1112import {
1213 DocSearchHit ,
1314 InternalDocSearchHit ,
1415 StoredDocSearchHit ,
1516} from './types' ;
1617import { useDocSearchKeyboardEvents } from './useDocSearchKeyboardEvents' ;
1718
19+ const { useState, useEffect, useCallback, useRef } = React ;
20+
21+ const ACTION_KEY_APPLE = '⌘' as const ;
22+ const ACTION_KEY_DEFAULT = 'Ctrl' as const ;
23+
24+ function isAppleDevice ( ) {
25+ return / ( M a c | i P h o n e | i P o d | i P a d ) / i. test ( navigator . platform ) ;
26+ }
27+
1828export interface DocSearchProps {
1929 appId ?: string ;
2030 apiKey : string ;
@@ -36,21 +46,31 @@ export interface DocSearchProps {
3646}
3747
3848export function DocSearch ( props : DocSearchProps ) {
39- const searchButtonRef = React . useRef < HTMLButtonElement > ( null ) ;
40- const [ isOpen , setIsOpen ] = React . useState ( false ) ;
41- const [ initialQuery , setInitialQuery ] = React . useState < string | undefined > (
49+ const searchButtonRef = useRef < HTMLButtonElement > ( null ) ;
50+ const [ isOpen , setIsOpen ] = useState ( false ) ;
51+ const [ initialQuery , setInitialQuery ] = useState < string | undefined > (
4252 undefined
4353 ) ;
4454
45- const onOpen = React . useCallback ( ( ) => {
55+ const [ key , setKey ] = useState <
56+ typeof ACTION_KEY_APPLE | typeof ACTION_KEY_DEFAULT | null
57+ > ( null ) ;
58+
59+ useEffect ( ( ) => {
60+ if ( typeof navigator !== 'undefined' ) {
61+ setKey ( isAppleDevice ( ) ? ACTION_KEY_APPLE : ACTION_KEY_DEFAULT ) ;
62+ }
63+ } , [ ] ) ;
64+
65+ const onOpen = useCallback ( ( ) => {
4666 setIsOpen ( true ) ;
4767 } , [ setIsOpen ] ) ;
4868
49- const onClose = React . useCallback ( ( ) => {
69+ const onClose = useCallback ( ( ) => {
5070 setIsOpen ( false ) ;
5171 } , [ setIsOpen ] ) ;
5272
53- const onInput = React . useCallback (
73+ const onInput = useCallback (
5474 ( event : KeyboardEvent ) => {
5575 setIsOpen ( true ) ;
5676 setInitialQuery ( event . key ) ;
@@ -68,7 +88,16 @@ export function DocSearch(props: DocSearchProps) {
6888
6989 return (
7090 < >
71- < DocSearchButton onClick = { onOpen } ref = { searchButtonRef } />
91+ < DocSearchButton onClick = { onOpen } ref = { searchButtonRef } >
92+ { key !== null ? (
93+ < span className = "DocSearch-Button-Keys" >
94+ < span className = "DocSearch-Button-Key" >
95+ { key === ACTION_KEY_DEFAULT ? < ControlKeyIcon /> : key }
96+ </ span >
97+ < span className = "DocSearch-Button-Key" > K</ span >
98+ </ span >
99+ ) : undefined }
100+ </ DocSearchButton >
72101
73102 { isOpen &&
74103 createPortal (
0 commit comments