@ariakit/react-core
Version:
Ariakit React core
327 lines (324 loc) • 11.5 kB
JavaScript
"use client";
import {
findFirstEnabledItem,
getEnabledItem,
groupItemsByRows,
isItem,
silentlyFocused
} from "./5VQZOHHZ.js";
import {
CompositeContextProvider,
useCompositeProviderContext
} from "./APTFW6PT.js";
import {
useFocusable
} from "./OE2EFRVA.js";
import {
createElement,
createHook,
forwardRef
} from "./VOQWLFSQ.js";
import {
useBooleanEvent,
useEvent,
useMergeRefs,
useSafeLayoutEffect,
useTransactionState,
useWrapElement
} from "./5GGHRIN3.js";
import {
__objRest,
__spreadProps,
__spreadValues
} from "./3YLGPPWQ.js";
// src/composite/composite.tsx
import { flatten2DArray, reverseArray } from "@ariakit/core/utils/array";
import { getActiveElement, isTextField } from "@ariakit/core/utils/dom";
import {
fireBlurEvent,
fireKeyboardEvent,
isSelfTarget
} from "@ariakit/core/utils/events";
import { focusIntoView, hasFocus } from "@ariakit/core/utils/focus";
import { invariant } from "@ariakit/core/utils/misc";
import { useCallback, useEffect, useRef, useState } from "react";
import { jsx } from "react/jsx-runtime";
var TagName = "div";
function isGrid(items) {
return items.some((item) => !!item.rowId);
}
function isPrintableKey(event) {
const target = event.target;
if (target && !isTextField(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 useEvent((event) => {
var _a;
onKeyboardEvent == null ? void 0 : onKeyboardEvent(event);
if (event.defaultPrevented) return;
if (event.isPropagationStopped()) return;
if (!isSelfTarget(event)) return;
if (isModifierKey(event)) return;
if (isPrintableKey(event)) return;
const state = store.getState();
const activeElement = (_a = getEnabledItem(store, state.activeId)) == null ? void 0 : _a.element;
if (!activeElement) return;
const _b = event, { view } = _b, eventInit = __objRest(_b, ["view"]);
const previousElement = previousElementRef == null ? void 0 : previousElementRef.current;
if (activeElement !== previousElement) {
activeElement.focus();
}
if (!fireKeyboardEvent(activeElement, event.type, eventInit)) {
event.preventDefault();
}
if (event.currentTarget.contains(activeElement)) {
event.stopPropagation();
}
});
}
function findFirstEnabledItemInTheLastRow(items) {
return findFirstEnabledItem(
flatten2DArray(reverseArray(groupItemsByRows(items)))
);
}
function useScheduleFocus(store) {
const [scheduled, setScheduled] = useState(false);
const schedule = useCallback(() => setScheduled(true), []);
const activeItem = store.useState(
(state) => getEnabledItem(store, state.activeId)
);
useEffect(() => {
const activeElement = activeItem == null ? void 0 : activeItem.element;
if (!scheduled) return;
if (!activeElement) return;
setScheduled(false);
activeElement.focus({ preventScroll: true });
}, [activeItem, scheduled]);
return schedule;
}
var useComposite = createHook(
function useComposite2(_a) {
var _b = _a, {
store,
composite = true,
focusOnMove = composite,
moveOnKeyPress = true
} = _b, props = __objRest(_b, [
"store",
"composite",
"focusOnMove",
"moveOnKeyPress"
]);
const context = useCompositeProviderContext();
store = store || context;
invariant(
store,
process.env.NODE_ENV !== "production" && "Composite must receive a `store` prop or be wrapped in a CompositeProvider component."
);
const ref = useRef(null);
const previousElementRef = useRef(null);
const scheduleFocus = useScheduleFocus(store);
const moves = store.useState("moves");
const [, setBaseElement] = useTransactionState(
composite ? store.setBaseElement : null
);
useEffect(() => {
var _a2;
if (!store) return;
if (!moves) return;
if (!composite) return;
if (!focusOnMove) return;
const { activeId: activeId2 } = store.getState();
const itemElement = (_a2 = getEnabledItem(store, activeId2)) == null ? void 0 : _a2.element;
if (!itemElement) return;
focusIntoView(itemElement);
}, [store, moves, composite, focusOnMove]);
useSafeLayoutEffect(() => {
if (!store) return;
if (!moves) return;
if (!composite) return;
const { baseElement, activeId: activeId2 } = store.getState();
const isSelfAcive = activeId2 === null;
if (!isSelfAcive) return;
if (!baseElement) return;
const previousElement = previousElementRef.current;
previousElementRef.current = null;
if (previousElement) {
fireBlurEvent(previousElement, { relatedTarget: baseElement });
}
if (!hasFocus(baseElement)) {
baseElement.focus();
}
}, [store, moves, composite]);
const activeId = store.useState("activeId");
const virtualFocus = store.useState("virtualFocus");
useSafeLayoutEffect(() => {
var _a2;
if (!store) return;
if (!composite) return;
if (!virtualFocus) return;
const previousElement = previousElementRef.current;
previousElementRef.current = null;
if (!previousElement) return;
const activeElement = (_a2 = getEnabledItem(store, activeId)) == null ? void 0 : _a2.element;
const relatedTarget = activeElement || getActiveElement(previousElement);
if (relatedTarget === previousElement) return;
fireBlurEvent(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 = useEvent((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 = silentlyFocused(event.currentTarget);
if (isSelfTarget(event) && isSilentlyFocused) {
event.stopPropagation();
previousElementRef.current = previousActiveElement;
}
});
const onFocusProp = props.onFocus;
const onFocus = useEvent((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 (isSelfTarget(event) && !isItem(store, relatedTarget)) {
queueMicrotask(scheduleFocus);
}
} else if (isSelfTarget(event)) {
store.setActiveId(null);
}
});
const onBlurCaptureProp = props.onBlurCapture;
const onBlurCapture = useEvent((event) => {
var _a2;
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 = (_a2 = getEnabledItem(store, activeId2)) == null ? void 0 : _a2.element;
const nextActiveElement = event.relatedTarget;
const nextActiveElementIsItem = isItem(store, nextActiveElement);
const previousElement = previousElementRef.current;
previousElementRef.current = null;
if (isSelfTarget(event) && nextActiveElementIsItem) {
if (nextActiveElement === activeElement) {
if (previousElement && previousElement !== nextActiveElement) {
fireBlurEvent(previousElement, event);
}
} else if (activeElement) {
fireBlurEvent(activeElement, event);
} else if (previousElement) {
fireBlurEvent(previousElement, event);
}
event.stopPropagation();
} else {
const targetIsItem = isItem(store, event.target);
if (!targetIsItem && activeElement) {
fireBlurEvent(activeElement, event);
}
}
});
const onKeyDownProp = props.onKeyDown;
const moveOnKeyPressProp = useBooleanEvent(moveOnKeyPress);
const onKeyDown = useEvent((event) => {
var _a2;
onKeyDownProp == null ? void 0 : onKeyDownProp(event);
if (event.nativeEvent.isComposing) return;
if (event.defaultPrevented) return;
if (!store) return;
if (!isSelfTarget(event)) return;
const { orientation, renderedItems, activeId: activeId2 } = store.getState();
const activeItem = getEnabledItem(store, activeId2);
if ((_a2 = activeItem == null ? void 0 : activeItem.element) == null ? void 0 : _a2.isConnected) return;
const isVertical = orientation !== "horizontal";
const isHorizontal = orientation !== "vertical";
const grid = isGrid(renderedItems);
const isHorizontalKey = event.key === "ArrowLeft" || event.key === "ArrowRight" || event.key === "Home" || event.key === "End";
if (isHorizontalKey && isTextField(event.currentTarget)) return;
const up = () => {
if (grid) {
const item = findFirstEnabledItemInTheLastRow(renderedItems);
return item == null ? void 0 : item.id;
}
return store == null ? void 0 : store.last();
};
const keyMap = {
ArrowUp: (grid || isVertical) && up,
ArrowRight: (grid || isHorizontal) && store.first,
ArrowDown: (grid || isVertical) && store.first,
ArrowLeft: (grid || isHorizontal) && store.last,
Home: store.first,
End: store.last,
PageUp: store.first,
PageDown: store.last
};
const action = keyMap[event.key];
if (action) {
const id = action();
if (id !== void 0) {
if (!moveOnKeyPressProp(event)) return;
event.preventDefault();
store.move(id);
}
}
});
props = useWrapElement(
props,
(element) => /* @__PURE__ */ jsx(CompositeContextProvider, { value: store, children: element }),
[store]
);
const activeDescendant = store.useState((state) => {
var _a2;
if (!store) return;
if (!composite) return;
if (!state.virtualFocus) return;
return (_a2 = getEnabledItem(store, state.activeId)) == null ? void 0 : _a2.id;
});
props = __spreadProps(__spreadValues({
"aria-activedescendant": activeDescendant
}, props), {
ref: useMergeRefs(ref, setBaseElement, props.ref),
onKeyDownCapture,
onKeyUpCapture,
onFocusCapture,
onFocus,
onBlurCapture,
onKeyDown
});
const focusable = store.useState(
(state) => composite && (state.virtualFocus || state.activeId === null)
);
props = useFocusable(__spreadValues({ focusable }, props));
return props;
}
);
var Composite = forwardRef(function Composite2(props) {
const htmlProps = useComposite(props);
return createElement(TagName, htmlProps);
});
export {
useComposite,
Composite
};