UNPKG

@wordpress/components

Version:
30 lines (29 loc) • 1.24 kB
import { forwardRef, useRef } from "@wordpress/element"; import { useRovingTabIndexContext } from "./roving-tab-index-context.mjs"; import { jsx } from "react/jsx-runtime"; // packages/components/src/tree-grid/roving-tab-index-item.tsx const RovingTabIndexItem = forwardRef(function UnforwardedRovingTabIndexItem({ children, as: Component, ...props }, forwardedRef) { const localRef = useRef(null); const ref = forwardedRef || localRef; const context = useRovingTabIndexContext(); if (!context) throw new Error("TreeGridItem can only be rendered inside a TreeGrid component."); const { lastFocusedElement, setLastFocusedElement } = context; let tabIndex; if (lastFocusedElement) tabIndex = lastFocusedElement === ("current" in ref ? ref.current : void 0) ? 0 : -1; const onFocus = (event) => setLastFocusedElement?.(event.target); const allProps = { ref, tabIndex, onFocus, ...props }; if (typeof children === "function") return children(allProps); if (!Component) return null; return /* @__PURE__ */ jsx(Component, { ...allProps, children }); }); RovingTabIndexItem.displayName = "RovingTabIndexItem"; export { RovingTabIndexItem, RovingTabIndexItem as default }; //# sourceMappingURL=roving-tab-index-item.mjs.map