UNPKG

@awsui/components-react

Version:

AWS UI is a collection of [React](https://reactjs.org/) components that help create intuitive, responsive, and accessible user experiences for web applications. It is developed by Amazon Web Services (AWS). This work is available under the terms of the [A

75 lines (74 loc) 2.83 kB
import { useCallback, useRef } from 'react'; import { KeyCode } from '../../../keycode'; var HOME = 36; var END = 35; export var useMenuKeyboard = function (_a) { var moveHighlight = _a.moveHighlight, selectOption = _a.selectOption, goHome = _a.goHome, goEnd = _a.goEnd, closeDropdown = _a.closeDropdown, isKeyboard = _a.isKeyboard, isSelectingUsingSpace = _a.isSelectingUsingSpace, _b = _a.preventNativeSpace, preventNativeSpace = _b === void 0 ? false : _b; return useCallback(function (e) { isKeyboard.current = true; switch (e.detail.keyCode) { case KeyCode.up: e.preventDefault(); moveHighlight(-1); break; case KeyCode.down: e.preventDefault(); moveHighlight(1); break; case HOME: goHome(); break; case END: goEnd(); break; case KeyCode.escape: closeDropdown(); break; case KeyCode.enter: e.preventDefault(); selectOption(); break; case KeyCode.space: if (preventNativeSpace) { e.preventDefault(); selectOption(); isSelectingUsingSpace.current = true; } } }, [moveHighlight, selectOption, goHome, goEnd, closeDropdown, isKeyboard, isSelectingUsingSpace, preventNativeSpace]); }; export var useTriggerKeyboard = function (_a) { var openDropdown = _a.openDropdown, goHome = _a.goHome, isKeyboard = _a.isKeyboard; return useCallback(function (e) { isKeyboard.current = true; switch (e.detail.keyCode) { case KeyCode.up: case KeyCode.down: e.preventDefault(); goHome(); openDropdown(); break; case KeyCode.space: e.preventDefault(); openDropdown(); break; } }, [openDropdown, goHome, isKeyboard]); }; export var useSpaceAwareClickHandler = function (_a) { var isSelectingUsingSpace = _a.isSelectingUsingSpace, toggleDropdown = _a.toggleDropdown; var skipImmediateClick = useRef(false); return { triggerClickHandler: useCallback(function () { if (!skipImmediateClick.current) { toggleDropdown(); } }, [toggleDropdown]), triggerKeyUpHandler: function (e) { if (e.detail.keyCode === KeyCode.space && isSelectingUsingSpace.current) { skipImmediateClick.current = true; setTimeout(function () { return (skipImmediateClick.current = false); }, 0); } } }; };