MCPcopy Create free account
hub / github.com/algolia/docsearch / DocSearchButtonKey

Function DocSearchButtonKey

packages/docsearch-react/src/DocSearchButton.tsx:73–119  ·  view source on GitHub ↗
({ reactsToKey, children }: React.PropsWithChildren<DocSearchButtonKeyProps>)

Source from the content-addressed store, hash-verified

71};
72
73function 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}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected