@ariakit/react
Version:
Toolkit for building accessible web apps with React
330 lines (282 loc) • 13.6 kB
JavaScript
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
var _FHQLDZSWcjs = require('./FHQLDZSW.cjs');
var _XM764FDRcjs = require('./XM764FDR.cjs');
var _5FQZLBJ7cjs = require('./5FQZLBJ7.cjs');
var _MTBSKKM4cjs = require('./MTBSKKM4.cjs');
var _LZ6L3ECGcjs = require('./LZ6L3ECG.cjs');
// ../ariakit-react-components/dist/composite/composite.js
var _react = require('react');
var _jsxruntime = require('react/jsx-runtime');
var TagName = "div";
function isGrid(items) {
return items.some((item) => !!item.rowId);
}
function isPrintableKey(event) {
const target = event.target;
if (target && !_LZ6L3ECGcjs.isTextField.call(void 0, target)) return false;
return event.key.length === 1 && !event.ctrlKey && !event.metaKey;
}
function isModifierKey(event) {
return event.key === "Shift" || event.key === "Control" || event.key === "Alt" || event.key === "Meta";
}
function useKeyboardEventProxy(store, onKeyboardEvent, previousElementRef) {
return _LZ6L3ECGcjs.useEvent.call(void 0, (event) => {
var _a;
onKeyboardEvent == null ? void 0 : onKeyboardEvent(event);
if (event.defaultPrevented) return;
if (event.isPropagationStopped()) return;
if (!_LZ6L3ECGcjs.isSelfTarget.call(void 0, event)) return;
if (isModifierKey(event)) return;
if (isPrintableKey(event)) return;
const activeElement = (_a = _FHQLDZSWcjs.getEnabledItem.call(void 0, store, store.getState().activeId)) == null ? void 0 : _a.element;
if (!activeElement) return;
const { view, ...eventInit } = event;
if (activeElement !== (previousElementRef == null ? void 0 : previousElementRef.current)) activeElement.focus();
if (!_LZ6L3ECGcjs.fireKeyboardEvent.call(void 0, activeElement, event.type, eventInit)) event.preventDefault();
if (event.currentTarget.contains(activeElement)) event.stopPropagation();
});
}
function findFirstEnabledItemInTheLastRow(items) {
return _FHQLDZSWcjs.findFirstEnabledItem.call(void 0, _LZ6L3ECGcjs.flatten2DArray.call(void 0, _LZ6L3ECGcjs.reverseArray.call(void 0, _FHQLDZSWcjs.groupItemsByRows.call(void 0, items))));
}
function withBaseScrollPreserved(store, callback) {
const { virtualFocus, baseElement } = store.getState();
if (!virtualFocus || !baseElement || !_LZ6L3ECGcjs.isTextField.call(void 0, baseElement)) {
callback();
return;
}
const savedScrollLeft = baseElement.scrollLeft;
const savedScrollTop = baseElement.scrollTop;
callback();
baseElement.scrollLeft = savedScrollLeft;
baseElement.scrollTop = savedScrollTop;
}
function useScheduleFocus(store) {
const [scheduled, setScheduled] = _react.useState.call(void 0, false);
const schedule = _react.useCallback.call(void 0, () => setScheduled(true), []);
const activeItem = _5FQZLBJ7cjs.useStoreState.call(void 0, store, (state) => scheduled ? _FHQLDZSWcjs.getEnabledItem.call(void 0, store, state.activeId) : null);
_react.useEffect.call(void 0, () => {
const activeElement = activeItem == null ? void 0 : activeItem.element;
if (!scheduled) return;
if (!activeElement) return;
setScheduled(false);
withBaseScrollPreserved(store, () => {
activeElement.focus({ preventScroll: true });
});
}, [
store,
activeItem,
scheduled
]);
return schedule;
}
var CompositeFocusOnMove = _LZ6L3ECGcjs.memo.call(void 0, function CompositeFocusOnMove2({ store, focusOnMove, previousElementRef }) {
const moves = _5FQZLBJ7cjs.useStoreState.call(void 0, store, "moves");
const baseElement = _5FQZLBJ7cjs.useStoreState.call(void 0, store, "baseElement");
_react.useEffect.call(void 0, () => {
var _a;
if (!moves) return;
if (!focusOnMove) return;
const { activeId } = store.getState();
const itemElement = (_a = _FHQLDZSWcjs.getEnabledItem.call(void 0, store, activeId)) == null ? void 0 : _a.element;
if (!itemElement) return;
withBaseScrollPreserved(store, () => _LZ6L3ECGcjs.focusIntoView.call(void 0, itemElement));
}, [
store,
moves,
focusOnMove
]);
_LZ6L3ECGcjs.useSafeLayoutEffect.call(void 0, () => {
if (!moves) return;
if (!baseElement) return;
const { activeId } = store.getState();
if (!(activeId === null)) return;
const previousElement = previousElementRef.current;
previousElementRef.current = null;
if (previousElement) _LZ6L3ECGcjs.fireBlurEvent.call(void 0, previousElement, { relatedTarget: baseElement });
if (!_LZ6L3ECGcjs.hasFocus.call(void 0, baseElement)) baseElement.focus();
}, [
store,
moves,
baseElement
]);
return null;
});
var useComposite = _LZ6L3ECGcjs.createHook.call(void 0, function useComposite2({ store, composite = true, focusOnMove = composite, moveOnKeyPress = true, ...props }) {
const context = _FHQLDZSWcjs.useCompositeProviderContext.call(void 0, );
store = store || context;
_LZ6L3ECGcjs.invariant.call(void 0, store, process.env.NODE_ENV !== "production" && "Composite must receive a `store` prop or be wrapped in a CompositeProvider component.");
const ref = _react.useRef.call(void 0, null);
const previousElementRef = _react.useRef.call(void 0, null);
const scheduleFocus = useScheduleFocus(store);
const [, setBaseElement] = _LZ6L3ECGcjs.useTransactionState.call(void 0, composite ? store.setBaseElement : null);
const virtualFocus = _5FQZLBJ7cjs.useStoreState.call(void 0, store, "virtualFocus");
const activeId = _5FQZLBJ7cjs.useStoreState.call(void 0, store, (state) => state.virtualFocus ? state.activeId : null);
_LZ6L3ECGcjs.useSafeLayoutEffect.call(void 0, () => {
var _a;
if (!store) return;
if (!composite) return;
if (!virtualFocus) return;
const previousElement = previousElementRef.current;
previousElementRef.current = null;
if (!previousElement) return;
const relatedTarget = ((_a = _FHQLDZSWcjs.getEnabledItem.call(void 0, store, activeId)) == null ? void 0 : _a.element) || _LZ6L3ECGcjs.getActiveElement.call(void 0, previousElement);
if (relatedTarget === previousElement) return;
_LZ6L3ECGcjs.fireBlurEvent.call(void 0, previousElement, { relatedTarget });
}, [
store,
activeId,
virtualFocus,
composite
]);
const onKeyDownCapture = useKeyboardEventProxy(store, props.onKeyDownCapture, previousElementRef);
const onKeyUpCapture = useKeyboardEventProxy(store, props.onKeyUpCapture, previousElementRef);
const onFocusCaptureProp = props.onFocusCapture;
const onFocusCapture = _LZ6L3ECGcjs.useEvent.call(void 0, (event) => {
onFocusCaptureProp == null ? void 0 : onFocusCaptureProp(event);
if (event.defaultPrevented) return;
if (!store) return;
const { virtualFocus: virtualFocus2 } = store.getState();
if (!virtualFocus2) return;
const previousActiveElement = event.relatedTarget;
const isSilentlyFocused = _FHQLDZSWcjs.silentlyFocused.call(void 0, event.currentTarget);
if (_LZ6L3ECGcjs.isSelfTarget.call(void 0, event) && isSilentlyFocused) {
event.stopPropagation();
previousElementRef.current = previousActiveElement;
}
});
const onFocusProp = props.onFocus;
const onFocus = _LZ6L3ECGcjs.useEvent.call(void 0, (event) => {
onFocusProp == null ? void 0 : onFocusProp(event);
if (event.defaultPrevented) return;
if (!composite) return;
if (!store) return;
const { relatedTarget } = event;
const { virtualFocus: virtualFocus2 } = store.getState();
if (virtualFocus2) {
if (_LZ6L3ECGcjs.isSelfTarget.call(void 0, event) && !_FHQLDZSWcjs.isItem.call(void 0, store, relatedTarget)) queueMicrotask(scheduleFocus);
} else if (_LZ6L3ECGcjs.isSelfTarget.call(void 0, event)) store.setActiveId(null);
});
const onBlurCaptureProp = props.onBlurCapture;
const onBlurCapture = _LZ6L3ECGcjs.useEvent.call(void 0, (event) => {
var _a;
onBlurCaptureProp == null ? void 0 : onBlurCaptureProp(event);
if (event.defaultPrevented) return;
if (!store) return;
const { virtualFocus: virtualFocus2, activeId: activeId2 } = store.getState();
if (!virtualFocus2) return;
const activeElement = (_a = _FHQLDZSWcjs.getEnabledItem.call(void 0, store, activeId2)) == null ? void 0 : _a.element;
const nextActiveElement = event.relatedTarget;
const nextActiveElementIsItem = _FHQLDZSWcjs.isItem.call(void 0, store, nextActiveElement);
const previousElement = previousElementRef.current;
previousElementRef.current = null;
if (_LZ6L3ECGcjs.isSelfTarget.call(void 0, event) && nextActiveElementIsItem) {
if (nextActiveElement === activeElement) {
if (previousElement && previousElement !== nextActiveElement) _LZ6L3ECGcjs.fireBlurEvent.call(void 0, previousElement, event);
} else if (activeElement) _LZ6L3ECGcjs.fireBlurEvent.call(void 0, activeElement, event);
else if (previousElement) _LZ6L3ECGcjs.fireBlurEvent.call(void 0, previousElement, event);
event.stopPropagation();
} else if (!_FHQLDZSWcjs.isItem.call(void 0, store, event.target) && activeElement) _LZ6L3ECGcjs.fireBlurEvent.call(void 0, activeElement, event);
});
const onKeyDownProp = props.onKeyDown;
const moveOnKeyPressProp = _LZ6L3ECGcjs.useBooleanEvent.call(void 0, moveOnKeyPress);
const onKeyDown = _LZ6L3ECGcjs.useEvent.call(void 0, (event) => {
var _a, _b;
onKeyDownProp == null ? void 0 : onKeyDownProp(event);
if (event.nativeEvent.isComposing) return;
if (event.defaultPrevented) return;
if (!store) return;
if (!_LZ6L3ECGcjs.isSelfTarget.call(void 0, event)) return;
const { orientation, renderedItems, activeId: activeId2, rtl } = store.getState();
if ((_b = (_a = _FHQLDZSWcjs.getEnabledItem.call(void 0, store, activeId2)) == null ? void 0 : _a.element) == null ? void 0 : _b.isConnected) return;
const isVertical = orientation !== "horizontal";
const isHorizontal = orientation !== "vertical";
const grid = isGrid(renderedItems);
if ((event.key === "ArrowLeft" || event.key === "ArrowRight" || event.key === "Home" || event.key === "End") && _LZ6L3ECGcjs.isTextField.call(void 0, event.currentTarget)) return;
const up = () => {
var _a2;
if (grid) return (_a2 = findFirstEnabledItemInTheLastRow(renderedItems)) == null ? void 0 : _a2.id;
return store == null ? void 0 : store.last();
};
const action = {
ArrowUp: (grid || isVertical) && up,
ArrowRight: (grid || isHorizontal) && (rtl ? store.last : store.first),
ArrowDown: (grid || isVertical) && store.first,
ArrowLeft: (grid || isHorizontal) && (rtl ? store.first : store.last),
Home: store.first,
End: store.last,
PageUp: store.first,
PageDown: store.last
}[event.key];
if (action) {
const id = action();
if (id !== void 0) {
if (!moveOnKeyPressProp(event)) return;
event.preventDefault();
store.move(id);
}
}
});
props = _LZ6L3ECGcjs.useWrapElement.call(void 0, props, (element) => /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _FHQLDZSWcjs.CompositeScopedContextProvider, {
value: store,
children: [element, composite && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, CompositeFocusOnMove, {
store,
focusOnMove,
previousElementRef
})]
}), [
store,
composite,
focusOnMove
]);
props = {
"aria-activedescendant": _5FQZLBJ7cjs.useStoreState.call(void 0, store, (state) => {
var _a;
if (!store) return;
if (!composite) return;
if (!state.virtualFocus) return;
return (_a = _FHQLDZSWcjs.getEnabledItem.call(void 0, store, state.activeId)) == null ? void 0 : _a.id;
}),
...props,
ref: _LZ6L3ECGcjs.useMergeRefs.call(void 0, ref, setBaseElement, props.ref),
onKeyDownCapture,
onKeyUpCapture,
onFocusCapture,
onFocus,
onBlurCapture,
onKeyDown
};
props = _MTBSKKM4cjs.useFocusable.call(void 0, {
focusable: _5FQZLBJ7cjs.useStoreState.call(void 0, store, (state) => composite && (state.virtualFocus || state.activeId === null)),
...props
});
return props;
});
var Composite = _LZ6L3ECGcjs.forwardRef.call(void 0, function Composite2(props) {
return _LZ6L3ECGcjs.createElement.call(void 0, TagName, useComposite(props));
});
// ../ariakit-react-components/dist/composite/composite-store.js
function useCompositeStoreOptions(props) {
return {
id: _LZ6L3ECGcjs.useId.call(void 0, props.id),
...props
};
}
function useCompositeStoreProps(store, update, props) {
store = _XM764FDRcjs.useCollectionStoreProps.call(void 0, store, update, props);
_5FQZLBJ7cjs.useStoreProps.call(void 0, store, props, "activeId", "setActiveId");
_5FQZLBJ7cjs.useStoreProps.call(void 0, store, props, "includesBaseElement");
_5FQZLBJ7cjs.useStoreProps.call(void 0, store, props, "virtualFocus");
_5FQZLBJ7cjs.useStoreProps.call(void 0, store, props, "orientation");
_5FQZLBJ7cjs.useStoreProps.call(void 0, store, props, "rtl");
_5FQZLBJ7cjs.useStoreProps.call(void 0, store, props, "focusLoop");
_5FQZLBJ7cjs.useStoreProps.call(void 0, store, props, "focusWrap");
_5FQZLBJ7cjs.useStoreProps.call(void 0, store, props, "focusShift");
return store;
}
function useCompositeStore(props = {}) {
props = useCompositeStoreOptions(props);
const [store, update] = _5FQZLBJ7cjs.useStore.call(void 0, _FHQLDZSWcjs.createCompositeStore, props);
return useCompositeStoreProps(store, update, props);
}
exports.useComposite = useComposite; exports.Composite = Composite; exports.useCompositeStoreOptions = useCompositeStoreOptions; exports.useCompositeStoreProps = useCompositeStoreProps; exports.useCompositeStore = useCompositeStore;