@acusti/use-keyboard-events
Version:
React hook that takes keyboard event handlers and attaches them to the document
48 lines • 2.09 kB
JavaScript
import { useEffect } from 'react';
import { addHandler, addHandlers } from './handlers.js';
export { isEventTargetUsingKeyEvent, isPrimaryModifierPressed, usesKeyEvents, } from './handlers.js';
export default function useKeyboardEvents({ ignoreUsedKeyboardEvents, onKeyDown, onKeyPress, onKeyUp, priority, }) {
useEffect(() => {
addHandlers(document);
document.querySelectorAll('iframe').forEach((iframe) => {
if (!isSameOriginFrame(iframe) || !iframe.contentDocument)
return;
addHandlers(iframe.contentDocument);
});
}, []);
useEffect(() => addHandler({
eventType: 'keydown',
handler: onKeyDown,
ignoreUsedKeyboardEvents,
priority,
}), [ignoreUsedKeyboardEvents, onKeyDown, priority]);
useEffect(() => addHandler({
eventType: 'keypress',
handler: onKeyPress,
ignoreUsedKeyboardEvents,
priority,
}), [ignoreUsedKeyboardEvents, onKeyPress, priority]);
useEffect(() => addHandler({
eventType: 'keyup',
handler: onKeyUp,
ignoreUsedKeyboardEvents,
priority,
}), [ignoreUsedKeyboardEvents, onKeyUp, priority]);
}
// https://github.com/facebook/react/blob/main/packages/react-dom-bindings/src/client/ReactInputSelection.js
function isSameOriginFrame(iframe) {
try {
// Accessing the contentDocument of an HTMLIframeElement can cause the
// browser to throw, e.g. if it has a cross-origin src attribute.
// Safari will show a “Blocked a frame with origin…” error in the
// console on e.g. iframe.contentDocument.defaultView if cross-origin.
// To avoid this, try a cross-origin property, like location, which can
// throw a “SecurityError” DOM Exception that’s compatible with Safari.
// https://html.spec.whatwg.org/multipage/browsers.html#integration-with-idl
return typeof iframe.contentWindow.location.href === 'string';
}
catch (err) {
return false;
}
}
//# sourceMappingURL=useKeyboardEvents.js.map