({ reactsToKey, children }: React.PropsWithChildren<DocSearchButtonKeyProps>)
| 71 | }; |
| 72 | |
| 73 | function DocSearchButtonKey({ reactsToKey, children }: React.PropsWithChildren<DocSearchButtonKeyProps>): JSX.Element { |
| 74 | const [isKeyDown, setIsKeyDown] = useState(false); |
| 75 | |
| 76 | useEffect(() => { |
| 77 | if (!reactsToKey) { |
| 78 | return undefined; |
| 79 | } |
| 80 | |
| 81 | function handleKeyDown(e: KeyboardEvent): void { |
| 82 | if (e.key === reactsToKey) { |
| 83 | setIsKeyDown(true); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | function handleKeyUp(e: KeyboardEvent): void { |
| 88 | if ( |
| 89 | e.key === reactsToKey || |
| 90 | // keyup doesn't fire when Command is held down, |
| 91 | // workaround is to mark key as also released when Command is released |
| 92 | // See https://stackoverflow.com/a/73419500 |
| 93 | e.key === 'Meta' |
| 94 | ) { |
| 95 | setIsKeyDown(false); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | window.addEventListener('keydown', handleKeyDown); |
| 100 | window.addEventListener('keyup', handleKeyUp); |
| 101 | |
| 102 | return (): void => { |
| 103 | window.removeEventListener('keydown', handleKeyDown); |
| 104 | window.removeEventListener('keyup', handleKeyUp); |
| 105 | }; |
| 106 | }, [reactsToKey]); |
| 107 | |
| 108 | return ( |
| 109 | <kbd |
| 110 | className={ |
| 111 | isKeyDown |
| 112 | ? 'DocSearch-Button-Key DocSearch-Button-Key--pressed' |
| 113 | : 'DocSearch-Button-Key' + (reactsToKey === 'Ctrl' ? ' DocSearch-Button-Key--ctrl' : '') |
| 114 | } |
| 115 | > |
| 116 | {children} |
| 117 | </kbd> |
| 118 | ); |
| 119 | } |
nothing calls this directly
no outgoing calls
no test coverage detected