UNPKG

@ariakit/react

Version:

Toolkit for building accessible web apps with React

56 lines (42 loc) 1.83 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); var _MOTSR5FYcjs = require('./MOTSR5FY.cjs'); var _LZ6L3ECGcjs = require('./LZ6L3ECG.cjs'); // ../ariakit-react-components/dist/focus-trap/focus-trap-region.js var _react = require('react'); var _jsxruntime = require('react/jsx-runtime'); var TagName = "div"; var useFocusTrapRegion = _LZ6L3ECGcjs.createHook.call(void 0, function useFocusTrapRegion2({ enabled = false, ...props }) { const ref = _react.useRef.call(void 0, null); props = _LZ6L3ECGcjs.useWrapElement.call(void 0, props, (element) => { const renderFocusTrap = () => { if (!enabled) return null; return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _MOTSR5FYcjs.FocusTrap, { onFocus: (event) => { const container = ref.current; if (!container) return; const tabbables = _LZ6L3ECGcjs.getAllTabbableIn.call(void 0, container, true); const first = tabbables[0]; const last = tabbables[tabbables.length - 1]; if (!tabbables.length) { container.focus(); return; } if (event.relatedTarget === first) last == null ? void 0 : last.focus(); else first == null ? void 0 : first.focus(); } }); }; return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _jsxruntime.Fragment, { children: [ renderFocusTrap(), element, renderFocusTrap() ] }); }, [enabled]); props = { ...props, ref: _LZ6L3ECGcjs.useMergeRefs.call(void 0, ref, props.ref) }; return _LZ6L3ECGcjs.removeUndefinedValues.call(void 0, props); }); var FocusTrapRegion = _LZ6L3ECGcjs.forwardRef.call(void 0, function FocusTrapRegion2(props) { return _LZ6L3ECGcjs.createElement.call(void 0, TagName, useFocusTrapRegion(props)); }); exports.FocusTrapRegion = FocusTrapRegion;