UNPKG

@wordpress/components

Version:
109 lines (108 loc) • 3.96 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_element = require("@wordpress/element"); let _wordpress_compose = require("@wordpress/compose"); let _wordpress_keycodes = require("@wordpress/keycodes"); let react_jsx_runtime = require("react/jsx-runtime"); // packages/components/src/higher-order/navigate-regions/index.tsx const defaultShortcuts = { previous: [ { modifier: "ctrlShift", character: "`" }, { modifier: "ctrlShift", character: "~" }, { modifier: "access", character: "p" } ], next: [{ modifier: "ctrl", character: "`" }, { modifier: "access", character: "n" }] }; function useNavigateRegions(shortcuts = defaultShortcuts) { const ref = (0, _wordpress_element.useRef)(null); const [isFocusingRegions, setIsFocusingRegions] = (0, _wordpress_element.useState)(false); function focusRegion(offset) { const regions = Array.from(ref.current?.querySelectorAll("[role=\"region\"][tabindex=\"-1\"]") ?? []); if (!regions.length) return; let nextRegion = regions[0]; const wrappingRegion = ref.current?.ownerDocument?.activeElement?.closest("[role=\"region\"][tabindex=\"-1\"]"); const selectedIndex = wrappingRegion ? regions.indexOf(wrappingRegion) : -1; if (selectedIndex !== -1) { let nextIndex = selectedIndex + offset; nextIndex = nextIndex === -1 ? regions.length - 1 : nextIndex; nextIndex = nextIndex === regions.length ? 0 : nextIndex; nextRegion = regions[nextIndex]; } nextRegion.focus(); setIsFocusingRegions(true); } const clickRef = (0, _wordpress_compose.useRefEffect)((element) => { function onClick() { setIsFocusingRegions(false); } element.addEventListener("click", onClick); return () => { element.removeEventListener("click", onClick); }; }, [setIsFocusingRegions]); return { ref: (0, _wordpress_compose.useMergeRefs)([ref, clickRef]), className: isFocusingRegions ? "is-focusing-regions" : "", onKeyDown(event) { if (shortcuts.previous.some(({ modifier, character }) => { return _wordpress_keycodes.isKeyboardEvent[modifier](event, character); })) focusRegion(-1); else if (shortcuts.next.some(({ modifier, character }) => { return _wordpress_keycodes.isKeyboardEvent[modifier](event, character); })) focusRegion(1); } }; } /** * `navigateRegions` is a React [higher-order component](https://facebook.github.io/react/docs/higher-order-components.html) * adding keyboard navigation to switch between the different DOM elements marked as "regions" (role="region"). * These regions should be focusable (By adding a tabIndex attribute for example). For better accessibility, * these elements must be properly labelled to briefly describe the purpose of the content in the region. * For more details, see "Landmark Roles" in the [WAI-ARIA specification](https://www.w3.org/TR/wai-aria/) * and "Landmark Regions" in the [ARIA Authoring Practices Guide](https://www.w3.org/WAI/ARIA/apg/practices/landmark-regions/). * * ```jsx * import { navigateRegions } from '@wordpress/components'; * * const MyComponentWithNavigateRegions = navigateRegions( () => ( * <div> * <div role="region" tabIndex="-1" aria-label="Header"> * Header * </div> * <div role="region" tabIndex="-1" aria-label="Content"> * Content * </div> * <div role="region" tabIndex="-1" aria-label="Sidebar"> * Sidebar * </div> * </div> * ) ); * ``` */ var navigate_regions_default = (0, _wordpress_compose.createHigherOrderComponent)((Component) => function NavigateRegions({ shortcuts, ...props }) { return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { ...useNavigateRegions(shortcuts), children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Component, { ...props }) }); }, "navigateRegions"); exports.default = navigate_regions_default; exports.useNavigateRegions = useNavigateRegions; //# sourceMappingURL=index.cjs.map