@wordpress/components
Version:
UI components for WordPress.
84 lines (83 loc) • 3.13 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let _wordpress_element = require("@wordpress/element");
let _wordpress_compose = require("@wordpress/compose");
let _wordpress_dom = require("@wordpress/dom");
let react_jsx_runtime = require("react/jsx-runtime");
// packages/components/src/navigable-container/container.tsx
const noop = () => {};
const MENU_ITEM_ROLES = [
"menuitem",
"menuitemradio",
"menuitemcheckbox"
];
function cycleValue(value, total, offset) {
const nextValue = value + offset;
if (nextValue < 0) return total + nextValue;
else if (nextValue >= total) return nextValue - total;
return nextValue;
}
function getFocusableContext(container, target, tabbableOnly) {
const focusables = (tabbableOnly ? _wordpress_dom.focus.tabbable : _wordpress_dom.focus.focusable).find(container);
const index = focusables.indexOf(target);
if (index > -1 || !tabbableOnly && target === container) return {
index,
target,
focusables
};
return null;
}
function UnforwardedNavigableContainer({ children, stopNavigationEvents, eventToOffset, onNavigate = noop, onKeyDown, cycle = true, onlyBrowserTabstops, ...restProps }, ref) {
const containerRef = (0, _wordpress_element.useRef)(null);
(0, _wordpress_element.useEffect)(() => {
const container = containerRef.current;
if (!container) return;
function handleKeyDown(event) {
if (onKeyDown) onKeyDown(event);
const offset = eventToOffset(event);
if (offset !== void 0 && stopNavigationEvents) {
event.stopImmediatePropagation();
const targetRole = event.target?.getAttribute("role");
if (!!targetRole && MENU_ITEM_ROLES.includes(targetRole) || !onlyBrowserTabstops && event.target === container) event.preventDefault();
}
if (!offset) return;
const activeElement = event.target?.ownerDocument?.activeElement;
if (!activeElement) return;
const context = getFocusableContext(container, activeElement, !!onlyBrowserTabstops);
if (!context) return;
const { index, focusables } = context;
let nextIndex = index + offset;
if (index === -1) nextIndex = offset > 0 ? 0 : focusables.length - 1;
else if (cycle) nextIndex = cycleValue(index, focusables.length, offset);
if (nextIndex >= 0 && nextIndex < focusables.length) {
focusables[nextIndex].focus();
onNavigate(nextIndex, focusables[nextIndex]);
if (event.code === "Tab") event.preventDefault();
}
}
container.addEventListener("keydown", handleKeyDown);
return () => {
container.removeEventListener("keydown", handleKeyDown);
};
}, [
onKeyDown,
eventToOffset,
stopNavigationEvents,
cycle,
onNavigate,
onlyBrowserTabstops
]);
const mergedRef = (0, _wordpress_compose.useMergeRefs)([containerRef, ref]);
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
ref: mergedRef,
...restProps,
children
});
}
const NavigableContainer = (0, _wordpress_element.forwardRef)(UnforwardedNavigableContainer);
NavigableContainer.displayName = "NavigableContainer";
exports.default = NavigableContainer;
//# sourceMappingURL=container.cjs.map