MCPcopy Create free account
hub / github.com/primer/react / useOnEscapePress

Function useOnEscapePress

packages/react/src/hooks/useOnEscapePress.ts:53–81  ·  view source on GitHub ↗
(
  onEscape: (e: KeyboardEvent) => void,
  callbackDependencies: React.DependencyList = [onEscape],
)

Source from the content-addressed store, hash-verified

51 * memoized. See `React.useCallback` for more info on memoization.
52 */
53export const useOnEscapePress = (
54 onEscape: (e: KeyboardEvent) => void,
55 callbackDependencies: React.DependencyList = [onEscape],
56): void => {
57 // eslint-disable-next-line react-hooks/exhaustive-deps
58 const escapeCallback = useCallback(onEscape, callbackDependencies)
59
60 const handler = useCallback<KeyboardEventCallback>(
61 event => {
62 if (event.key === 'Escape') escapeCallback(event)
63 },
64 [escapeCallback],
65 )
66
67 const id = useMemo(() => handlerId++, [])
68 useEffect(() => {
69 if (Object.keys(registry).length === 0) {
70 document.addEventListener('keydown', handleEscape)
71 }
72 register(id, handler)
73
74 return () => {
75 deregister(id)
76 if (Object.keys(registry).length === 0) {
77 document.removeEventListener('keydown', handleEscape)
78 }
79 }
80 }, [id, handler])
81}

Callers 7

Tooltip.tsxFile · 0.90
ActionBarFunction · 0.90
Dialog.tsxFile · 0.90
UnderlineNav.tsxFile · 0.90
useOverlayFunction · 0.90
useDialogFunction · 0.90
ComponentFunction · 0.90

Calls 2

registerFunction · 0.70
deregisterFunction · 0.70

Tested by 1

ComponentFunction · 0.72