@pdftron/webviewer-react-toolkit
Version:
A React component library for integrating with PDFTron WebViewer API.
31 lines (30 loc) • 1.38 kB
JavaScript
import { useCallback } from 'react';
import { generateClickEventFromKeyboardEvent } from '../utils';
/**
* Returns the handler for onKeyPress. If it hears a space or Enter key, it will
* fire onClick. If you provide a ref, will compare the target and make sure it
* is the same as the ref, then will fire onClick on the ref. Otherwise will
* call it on the event target.
* @param onKeyPress The onKeyPress prop if it's available.
* @param ref If given, will compare event target to prevent any bubbling events.
*/
export function useKeyForClick(onKeyPress, ref) {
var handler = useCallback(function (event) {
// Fire click on space or enter press.
if (event.key === ' ' || event.key === 'Enter') {
var clickEvent = generateClickEventFromKeyboardEvent(event);
// If ref is provided and it matches the event target, click ref.
if (ref && event.target === ref.current) {
ref.current.dispatchEvent(clickEvent);
// Stop scrolling if space is pressed.
if (event.key === ' ')
event.preventDefault();
}
else if (!ref) {
event.target.dispatchEvent(clickEvent);
}
}
onKeyPress === null || onKeyPress === void 0 ? void 0 : onKeyPress(event);
}, [ref, onKeyPress]);
return handler;
}