@omnific/atelier 0.5.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/README.md +201 -0
  2. package/dist/button/LoadingIcon.js +5 -5
  3. package/dist/button/types.d.ts +1 -1
  4. package/dist/list/ListItemIcon.d.ts +5 -0
  5. package/dist/list/ListItemIcon.js +15 -0
  6. package/dist/list/ListItemText.d.ts +5 -0
  7. package/dist/list/ListItemText.js +15 -0
  8. package/dist/list/constants.js +11 -0
  9. package/dist/list/index.d.ts +3 -0
  10. package/dist/list/index.js +3 -0
  11. package/dist/main.d.ts +22 -2
  12. package/dist/main.js +15 -1
  13. package/dist/menu/MenuItem.d.ts +5 -0
  14. package/dist/menu/MenuItem.js +57 -0
  15. package/dist/menu/MenuList.d.ts +5 -0
  16. package/dist/menu/MenuList.js +67 -0
  17. package/dist/menu/constants.js +11 -0
  18. package/dist/menu/index.d.ts +9 -0
  19. package/dist/menu/index.js +5 -0
  20. package/dist/menu/types.d.ts +62 -0
  21. package/dist/navigation/ListCollection.d.ts +19 -0
  22. package/dist/navigation/ListCollection.js +21 -0
  23. package/dist/navigation/ListCollectionStore.d.ts +17 -0
  24. package/dist/navigation/ListCollectionStore.js +88 -0
  25. package/dist/navigation/ListState.d.ts +19 -0
  26. package/dist/navigation/ListState.js +21 -0
  27. package/dist/navigation/ListStateStore.d.ts +31 -0
  28. package/dist/navigation/ListStateStore.js +56 -0
  29. package/dist/navigation/index.d.ts +12 -0
  30. package/dist/navigation/index.js +11 -0
  31. package/dist/navigation/useInteractions.d.ts +23 -0
  32. package/dist/navigation/useInteractions.js +35 -0
  33. package/dist/navigation/useListCollection.d.ts +5 -0
  34. package/dist/navigation/useListCollection.js +11 -0
  35. package/dist/navigation/useListItem.d.ts +14 -0
  36. package/dist/navigation/useListItem.js +42 -0
  37. package/dist/navigation/useListNavigation.d.ts +39 -0
  38. package/dist/navigation/useListNavigation.js +88 -0
  39. package/dist/navigation/useListState.d.ts +24 -0
  40. package/dist/navigation/useListState.js +47 -0
  41. package/dist/navigation/useTypeahead.d.ts +34 -0
  42. package/dist/navigation/useTypeahead.js +95 -0
  43. package/dist/navigation/utils.d.ts +9 -0
  44. package/dist/navigation/utils.js +71 -0
  45. package/dist/styles.css +122 -3
  46. package/dist/system/index.d.ts +2 -1
  47. package/dist/system/index.js +2 -1
  48. package/dist/system/themes/antd.d.ts +63 -0
  49. package/dist/system/themes/antd.js +49 -0
  50. package/dist/system/types.d.ts +1 -1
  51. package/dist/transition/types.d.ts +2 -2
  52. package/dist/transition/useTransitionStatus.d.ts +1 -1
  53. package/dist/transition/useTransitionStatus.js +24 -14
  54. package/package.json +21 -6
@@ -0,0 +1,17 @@
1
+ /**
2
+ * 创建列表集合 store,维护节点的 DOM 顺序、索引、标签和索引订阅。
3
+ *
4
+ * 节点注册与注销会在同一微任务内合并为一次通知。
5
+ */
6
+ export declare const createListCollectionStore: () => {
7
+ readonly register: (node: HTMLElement) => void;
8
+ readonly unregister: (node: HTMLElement) => void;
9
+ readonly setLabel: (index: number, node: HTMLElement, label: string | null | undefined) => void;
10
+ readonly getIndex: (node: HTMLElement | null) => number;
11
+ readonly getElement: (index: number) => HTMLElement | undefined;
12
+ readonly getSize: () => number;
13
+ readonly getLabels: () => (string | null)[];
14
+ readonly subscribe: (listener: import("@omnific/utils").EmitterListener<void>) => () => void;
15
+ };
16
+ /** 列表节点集合 store。 */
17
+ export type ListCollectionStore = ReturnType<typeof createListCollectionStore>;
@@ -0,0 +1,88 @@
1
+ import { createEmitter, isNil, isUndefined } from "@omnific/utils";
2
+ // navigation/ListCollectionStore.ts
3
+ const NO_INDEX = -1;
4
+ const sortByDocumentPosition = (a, b) => {
5
+ const position = a.compareDocumentPosition(b);
6
+ if (position & Node.DOCUMENT_POSITION_FOLLOWING || position & Node.DOCUMENT_POSITION_CONTAINED_BY) return -1;
7
+ if (position & Node.DOCUMENT_POSITION_PRECEDING || position & Node.DOCUMENT_POSITION_CONTAINS) return 1;
8
+ return 0;
9
+ };
10
+ /**
11
+ * 创建列表集合 store,维护节点的 DOM 顺序、索引、标签和索引订阅。
12
+ *
13
+ * 节点注册与注销会在同一微任务内合并为一次通知。
14
+ */
15
+ const createListCollectionStore = () => {
16
+ const nodes = /* @__PURE__ */ new Set();
17
+ const indices = /* @__PURE__ */ new Map();
18
+ const labelsRef = [];
19
+ const { subscribe, emit } = createEmitter();
20
+ let orderedNodes = [];
21
+ let indicesDirty = false;
22
+ let notificationPending = false;
23
+ let flushScheduled = false;
24
+ const updateIndices = () => {
25
+ if (!indicesDirty) return;
26
+ orderedNodes = [...nodes].sort(sortByDocumentPosition);
27
+ indices.clear();
28
+ for (const [index, node] of orderedNodes.entries()) indices.set(node, index);
29
+ indicesDirty = false;
30
+ };
31
+ const flush = () => {
32
+ updateIndices();
33
+ if (!notificationPending) return;
34
+ notificationPending = false;
35
+ emit();
36
+ };
37
+ const scheduleFlush = () => {
38
+ indicesDirty = true;
39
+ notificationPending = true;
40
+ if (flushScheduled) return;
41
+ flushScheduled = true;
42
+ queueMicrotask(() => {
43
+ flushScheduled = false;
44
+ flush();
45
+ });
46
+ };
47
+ const register = (node) => {
48
+ if (nodes.has(node)) return;
49
+ nodes.add(node);
50
+ scheduleFlush();
51
+ };
52
+ const unregister = (node) => {
53
+ if (!nodes.delete(node)) return;
54
+ scheduleFlush();
55
+ };
56
+ const getIndex = (node) => {
57
+ if (isNil(node)) return NO_INDEX;
58
+ updateIndices();
59
+ const index = indices.get(node);
60
+ return isNil(index) ? NO_INDEX : index;
61
+ };
62
+ const getElement = (index) => {
63
+ updateIndices();
64
+ return orderedNodes[index];
65
+ };
66
+ const getSize = () => {
67
+ updateIndices();
68
+ return orderedNodes.length;
69
+ };
70
+ const setLabel = (index, node, label) => {
71
+ labelsRef[index] = isUndefined(label) ? node.textContent ?? null : label;
72
+ };
73
+ const getLabels = () => {
74
+ updateIndices();
75
+ return labelsRef.slice(0, orderedNodes.length);
76
+ };
77
+ return {
78
+ register,
79
+ unregister,
80
+ setLabel,
81
+ getIndex,
82
+ getElement,
83
+ getSize,
84
+ getLabels,
85
+ subscribe
86
+ };
87
+ };
88
+ export { createListCollectionStore };
@@ -0,0 +1,19 @@
1
+ import { ListStateStore } from "./ListStateStore.js";
2
+ import { ReactNode } from "react";
3
+ /** `ListState` 组件属性。 */
4
+ export interface ListStateProps {
5
+ /** 列表内容。 */
6
+ children: ReactNode;
7
+ /** 列表交互状态 store。 */
8
+ store: ListStateStore;
9
+ }
10
+ /**
11
+ * 读取最近的列表交互状态 store。
12
+ *
13
+ * 不在 ListState 中时返回 `null`。
14
+ */
15
+ export declare const useListStateContext: () => ListStateStore | null;
16
+ /**
17
+ * 向子树提供列表交互状态 store。
18
+ */
19
+ export declare const ListState: (props: ListStateProps) => import("react").JSX.Element;
@@ -0,0 +1,21 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { createContext, use } from "react";
3
+ // navigation/ListState.tsx
4
+ const ListStateContext = createContext(null);
5
+ /**
6
+ * 读取最近的列表交互状态 store。
7
+ *
8
+ * 不在 ListState 中时返回 `null`。
9
+ */
10
+ const useListStateContext = () => use(ListStateContext);
11
+ /**
12
+ * 向子树提供列表交互状态 store。
13
+ */
14
+ const ListState = (props) => {
15
+ const { children, store } = props;
16
+ return /* @__PURE__ */ jsx(ListStateContext, {
17
+ value: store,
18
+ children
19
+ });
20
+ };
21
+ export { ListState, useListStateContext };
@@ -0,0 +1,31 @@
1
+ import { InteractionProps } from "./useInteractions.js";
2
+ /** ListStateStore 初始化选项。 */
3
+ export type ListStateStoreOptions = {
4
+ /** 初始 active item 索引。 */
5
+ activeIndex?: number;
6
+ /** 初始化选中项,只在创建 store 时读取一次。 */
7
+ defaultSelectedKey?: string;
8
+ };
9
+ /** `ListStateStore.getItemProps` 的 item 属性。 */
10
+ export type ListStateItemProps = InteractionProps & {
11
+ /** item 的唯一值;缺省时不参与选中。 */
12
+ value?: string;
13
+ };
14
+ /**
15
+ * 创建列表交互状态 store。
16
+ *
17
+ * 与 ListCollectionStore 分开:
18
+ * - ListCollectionStore 负责 DOM 顺序、索引和标签。
19
+ * - ListStateStore 负责保存 active item 与选中项;受控/非受控策略由 useListState 决定。
20
+ */
21
+ export declare const createListStateStore: (options?: ListStateStoreOptions) => {
22
+ readonly getActiveIndex: () => number | null;
23
+ readonly getItemProps: (index: number, itemProps?: ListStateItemProps) => InteractionProps;
24
+ readonly getSelectedKey: () => string | undefined;
25
+ readonly setActiveIndex: (index: number | null) => void;
26
+ readonly setSelectedKey: (nextKey: string | undefined) => void;
27
+ readonly subscribe: (listener: import("@omnific/utils").EmitterListener<void>) => () => void;
28
+ readonly subscribeSelection: (listener: import("@omnific/utils").EmitterListener<string>) => () => void;
29
+ };
30
+ /** 列表交互状态 store。 */
31
+ export type ListStateStore = ReturnType<typeof createListStateStore>;
@@ -0,0 +1,56 @@
1
+ import { createChainedFunction, createEmitter, hasValue } from "@omnific/utils";
2
+ // navigation/ListStateStore.ts
3
+ /**
4
+ * 创建列表交互状态 store。
5
+ *
6
+ * 与 ListCollectionStore 分开:
7
+ * - ListCollectionStore 负责 DOM 顺序、索引和标签。
8
+ * - ListStateStore 负责保存 active item 与选中项;受控/非受控策略由 useListState 决定。
9
+ */
10
+ const createListStateStore = (options = {}) => {
11
+ let activeIndex = options.activeIndex ?? null;
12
+ let selectedKey = options.defaultSelectedKey;
13
+ const { subscribe, emit } = createEmitter();
14
+ const { subscribe: subscribeSelection, emit: emitSelection } = createEmitter();
15
+ const getActiveIndex = () => activeIndex;
16
+ const getSelectedKey = () => selectedKey;
17
+ const setActiveIndex = (index) => {
18
+ if (Object.is(activeIndex, index)) return;
19
+ activeIndex = index;
20
+ emit();
21
+ };
22
+ const setSelectedKey = (nextKey) => {
23
+ if (Object.is(selectedKey, nextKey)) return;
24
+ selectedKey = nextKey;
25
+ emit();
26
+ };
27
+ const selectKey = (value) => {
28
+ if (Object.is(selectedKey, value)) return;
29
+ emitSelection(value);
30
+ };
31
+ /**
32
+ * 生成 item 的交互 props,并在 focus / click 时同步 activeIndex。
33
+ */
34
+ const getItemProps = (index, itemProps = {}) => {
35
+ const { value, ...userProps } = itemProps;
36
+ if (index < 0) return userProps;
37
+ return {
38
+ ...userProps,
39
+ onFocus: createChainedFunction((_event) => setActiveIndex(index), userProps.onFocus),
40
+ onClick: createChainedFunction((_event) => {
41
+ setActiveIndex(index);
42
+ if (hasValue(value)) selectKey(value);
43
+ }, userProps.onClick)
44
+ };
45
+ };
46
+ return {
47
+ getActiveIndex,
48
+ getItemProps,
49
+ getSelectedKey,
50
+ setActiveIndex,
51
+ setSelectedKey,
52
+ subscribe,
53
+ subscribeSelection
54
+ };
55
+ };
56
+ export { createListStateStore };
@@ -0,0 +1,12 @@
1
+ import { ListCollection, ListCollectionProps, useListCollectionContext } from "./ListCollection.js";
2
+ import { ListState, ListStateProps, useListStateContext } from "./ListState.js";
3
+ import { ListCollectionStore, createListCollectionStore } from "./ListCollectionStore.js";
4
+ import { ListStateItemProps, ListStateStore, ListStateStoreOptions, createListStateStore } from "./ListStateStore.js";
5
+ import { Interaction, InteractionProps, useInteractions } from "./useInteractions.js";
6
+ import { useListCollection } from "./useListCollection.js";
7
+ import { UseListItemOptions, useListItem } from "./useListItem.js";
8
+ import { ListNavigationItemProps, UseListNavigationOptions, useListNavigation } from "./useListNavigation.js";
9
+ import { ListIndexGetter, UseListStateOptions, useActiveIndex, useListState, useSelectedKey } from "./useListState.js";
10
+ import { ListLabelsGetter, TypeaheadFindMatch, UseTypeaheadOptions, useTypeahead } from "./useTypeahead.js";
11
+ import { DisabledIndices, ListElementGetter, ListNavigationOrientation, ListSizeGetter } from "./utils.js";
12
+ export { type DisabledIndices, type Interaction, type InteractionProps, ListCollection, type ListCollectionProps, type ListCollectionStore, type ListElementGetter, type ListIndexGetter, type ListLabelsGetter, type ListNavigationItemProps, type ListNavigationOrientation, type ListSizeGetter, ListState, type ListStateItemProps, type ListStateProps, type ListStateStore, type ListStateStoreOptions, type TypeaheadFindMatch, type UseListItemOptions, type UseListNavigationOptions, type UseListStateOptions, type UseTypeaheadOptions, createListCollectionStore, createListStateStore, useActiveIndex, useInteractions, useListCollection, useListCollectionContext, useListItem, useListNavigation, useListState, useListStateContext, useSelectedKey, useTypeahead };
@@ -0,0 +1,11 @@
1
+ import { ListCollection, useListCollectionContext } from "./ListCollection.js";
2
+ import { ListState, useListStateContext } from "./ListState.js";
3
+ import { createListCollectionStore } from "./ListCollectionStore.js";
4
+ import { createListStateStore } from "./ListStateStore.js";
5
+ import { useInteractions } from "./useInteractions.js";
6
+ import { useListCollection } from "./useListCollection.js";
7
+ import { useListItem } from "./useListItem.js";
8
+ import { useListNavigation } from "./useListNavigation.js";
9
+ import { useActiveIndex, useListState, useSelectedKey } from "./useListState.js";
10
+ import { useTypeahead } from "./useTypeahead.js";
11
+ export { ListCollection, ListState, createListCollectionStore, createListStateStore, useActiveIndex, useInteractions, useListCollection, useListCollectionContext, useListItem, useListNavigation, useListState, useListStateContext, useSelectedKey, useTypeahead };
@@ -0,0 +1,23 @@
1
+ import { FocusEventHandler, KeyboardEventHandler, MouseEventHandler } from "react";
2
+ /** 可合并的交互事件 props。 */
3
+ export type InteractionProps = {
4
+ /** focus 事件处理器。 */
5
+ onFocus?: FocusEventHandler<HTMLElement>;
6
+ /** keydown 事件处理器。 */
7
+ onKeyDown?: KeyboardEventHandler<HTMLElement>;
8
+ /** keyup 事件处理器。 */
9
+ onKeyUp?: KeyboardEventHandler<HTMLElement>;
10
+ /** click 事件处理器。 */
11
+ onClick?: MouseEventHandler<HTMLElement>;
12
+ };
13
+ /** 单个交互对象。 */
14
+ export type Interaction = InteractionProps;
15
+ /**
16
+ * 合并多个交互对象的事件 props。
17
+ *
18
+ * 事件处理器按传入顺序执行,调用方 props 最后执行。
19
+ */
20
+ export declare const useInteractions: (interactions?: ReadonlyArray<Interaction | null | undefined>) => {
21
+ getReferenceProps: (userProps?: InteractionProps) => InteractionProps;
22
+ getFloatingProps: (userProps?: InteractionProps) => InteractionProps;
23
+ };
@@ -0,0 +1,35 @@
1
+ import { createChainedFunction, hasValue, isNil } from "@omnific/utils";
2
+ import { useEventCallback } from "@omnific/hooks";
3
+ // navigation/useInteractions.ts
4
+ const mergeInteractionProps = (...propsList) => {
5
+ const focusHandlers = [];
6
+ const keyDownHandlers = [];
7
+ const keyUpHandlers = [];
8
+ const clickHandlers = [];
9
+ for (const props of propsList) {
10
+ if (isNil(props)) continue;
11
+ if (hasValue(props.onFocus)) focusHandlers.push(props.onFocus);
12
+ if (hasValue(props.onKeyDown)) keyDownHandlers.push(props.onKeyDown);
13
+ if (hasValue(props.onKeyUp)) keyUpHandlers.push(props.onKeyUp);
14
+ if (hasValue(props.onClick)) clickHandlers.push(props.onClick);
15
+ }
16
+ const merged = {};
17
+ if (focusHandlers.length > 0) merged.onFocus = createChainedFunction(...focusHandlers);
18
+ if (keyDownHandlers.length > 0) merged.onKeyDown = createChainedFunction(...keyDownHandlers);
19
+ if (keyUpHandlers.length > 0) merged.onKeyUp = createChainedFunction(...keyUpHandlers);
20
+ if (clickHandlers.length > 0) merged.onClick = createChainedFunction(...clickHandlers);
21
+ return merged;
22
+ };
23
+ /**
24
+ * 合并多个交互对象的事件 props。
25
+ *
26
+ * 事件处理器按传入顺序执行,调用方 props 最后执行。
27
+ */
28
+ const useInteractions = (interactions = []) => {
29
+ const getReferenceProps = useEventCallback((userProps = {}) => mergeInteractionProps(...interactions, userProps));
30
+ return {
31
+ getReferenceProps,
32
+ getFloatingProps: getReferenceProps
33
+ };
34
+ };
35
+ export { useInteractions };
@@ -0,0 +1,5 @@
1
+ import { ListCollectionStore } from "./ListCollectionStore.js";
2
+ /**
3
+ * 创建并保持一个与组件生命周期一致的列表集合 store。
4
+ */
5
+ export declare const useListCollection: () => ListCollectionStore;
@@ -0,0 +1,11 @@
1
+ import { createListCollectionStore } from "./ListCollectionStore.js";
2
+ import { useState } from "react";
3
+ // navigation/useListCollection.ts
4
+ /**
5
+ * 创建并保持一个与组件生命周期一致的列表集合 store。
6
+ */
7
+ const useListCollection = () => {
8
+ const [store] = useState(() => createListCollectionStore());
9
+ return store;
10
+ };
11
+ export { useListCollection };
@@ -0,0 +1,14 @@
1
+ /** `useListItem` 选项。 */
2
+ export type UseListItemOptions = {
3
+ /** typeahead 标签;`undefined` 时读取 DOM 文本,`null` 表示跳过该项。 */
4
+ label?: string | null | undefined;
5
+ };
6
+ /**
7
+ * 将当前 item 注册到最近的 ListCollection,并返回 DOM 顺序索引。
8
+ *
9
+ * 不在 ListCollection 中时返回 `index: -1`。
10
+ */
11
+ export declare const useListItem: <T extends HTMLElement = HTMLElement>(options?: UseListItemOptions) => {
12
+ index: number;
13
+ ref: (node: T | null) => void;
14
+ };
@@ -0,0 +1,42 @@
1
+ import { useListCollectionContext } from "./ListCollection.js";
2
+ import { isNil, noop } from "@omnific/utils";
3
+ import { useCallback, useLayoutEffect, useRef, useSyncExternalStore } from "react";
4
+ // navigation/useListItem.ts
5
+ const NO_INDEX = -1;
6
+ const getServerSnapshot = () => NO_INDEX;
7
+ /**
8
+ * 将当前 item 注册到最近的 ListCollection,并返回 DOM 顺序索引。
9
+ *
10
+ * 不在 ListCollection 中时返回 `index: -1`。
11
+ */
12
+ const useListItem = (options = {}) => {
13
+ const { label } = options;
14
+ const store = useListCollectionContext();
15
+ const nodeRef = useRef(null);
16
+ const subscribe = useCallback((listener) => isNil(store) ? noop : store.subscribe(listener), [store]);
17
+ const getSnapshot = useCallback(() => isNil(store) ? NO_INDEX : store.getIndex(nodeRef.current), [store]);
18
+ const index = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
19
+ const ref = useCallback((node) => {
20
+ nodeRef.current = node;
21
+ if (isNil(node) || isNil(store) || index < 0) return;
22
+ store.setLabel(index, node, label);
23
+ }, [
24
+ index,
25
+ label,
26
+ store
27
+ ]);
28
+ useLayoutEffect(() => {
29
+ if (isNil(store)) return;
30
+ const node = nodeRef.current;
31
+ if (isNil(node)) return;
32
+ store.register(node);
33
+ return () => {
34
+ store.unregister(node);
35
+ };
36
+ }, [store]);
37
+ return {
38
+ index,
39
+ ref
40
+ };
41
+ };
42
+ export { useListItem };
@@ -0,0 +1,39 @@
1
+ import { ListIndexGetter } from "./useListState.js";
2
+ import { DisabledIndices, ListElementGetter, ListNavigationOrientation, ListSizeGetter } from "./utils.js";
3
+ import { FocusEvent, FocusEventHandler, KeyboardEvent, MouseEventHandler } from "react";
4
+ /** `useListNavigation` 选项。 */
5
+ export type UseListNavigationOptions = {
6
+ /** 读取当前 active item 索引。 */
7
+ getActiveIndex: ListIndexGetter;
8
+ /** 按索引读取列表节点。 */
9
+ getElement: ListElementGetter;
10
+ /** 返回当前列表节点数量。 */
11
+ getSize: ListSizeGetter;
12
+ /** active item 变化时回调。 */
13
+ onNavigate?: (index: number | null) => void;
14
+ /** 是否启用导航,默认 `true`。 */
15
+ enabled?: boolean;
16
+ /** 导航方向,默认 `vertical`。 */
17
+ orientation?: ListNavigationOrientation;
18
+ /** 到达边界后是否循环,默认 `false`。 */
19
+ loop?: boolean;
20
+ /** 不可聚焦的索引;传入后覆盖 DOM `disabled` / `aria-disabled` 的默认判断。 */
21
+ disabledIndices?: DisabledIndices;
22
+ };
23
+ /** `useListNavigation` 返回的 item props。 */
24
+ export type ListNavigationItemProps = {
25
+ onFocus: FocusEventHandler<HTMLElement>;
26
+ onClick: MouseEventHandler<HTMLElement>;
27
+ };
28
+ /**
29
+ * 为列表提供 roving tabindex 风格的方向键导航。
30
+ *
31
+ * 与 Floating UI 的同名 hook 保持核心选项与行为一致,但不包含浮层打开、
32
+ * virtual focus、网格和嵌套菜单逻辑。
33
+ */
34
+ export declare const useListNavigation: (options: UseListNavigationOptions) => {
35
+ focusIndex: (index: number) => boolean;
36
+ getItemProps: (index: number) => ListNavigationItemProps;
37
+ onFocus: (event: FocusEvent<HTMLElement>) => void;
38
+ onKeyDown: (event: KeyboardEvent<HTMLElement>) => void;
39
+ };
@@ -0,0 +1,88 @@
1
+ import { findEnabledIndex, getMaxEnabledIndex, getMinEnabledIndex, isBackwardKey, isForwardKey, isIndexAvailable, shouldSkipKeyboardEvent } from "./utils.js";
2
+ import { hasValue, isFunction, isNil } from "@omnific/utils";
3
+ import { useEventCallback } from "@omnific/hooks";
4
+ import { useRef } from "react";
5
+ // navigation/useListNavigation.ts
6
+ /**
7
+ * 为列表提供 roving tabindex 风格的方向键导航。
8
+ *
9
+ * 与 Floating UI 的同名 hook 保持核心选项与行为一致,但不包含浮层打开、
10
+ * virtual focus、网格和嵌套菜单逻辑。
11
+ */
12
+ const useListNavigation = (options) => {
13
+ const { disabledIndices, enabled = true, getActiveIndex, getElement, getSize, loop = false, onNavigate, orientation = "vertical" } = options;
14
+ const indexRef = useRef(getActiveIndex());
15
+ const readCurrentIndex = () => {
16
+ const current = getActiveIndex();
17
+ indexRef.current = current;
18
+ return current;
19
+ };
20
+ const focusIndex = useEventCallback((index) => {
21
+ const element = getElement(index);
22
+ if (isNil(element)) return false;
23
+ element.focus({ preventScroll: true });
24
+ return true;
25
+ });
26
+ const scrollIndexIntoView = (index) => {
27
+ const element = getElement(index);
28
+ if (isNil(element)) return;
29
+ if (isFunction(element.scrollIntoView)) element.scrollIntoView({
30
+ block: "nearest",
31
+ inline: "nearest"
32
+ });
33
+ };
34
+ const syncIndex = (index) => {
35
+ if (indexRef.current === index) return;
36
+ indexRef.current = index;
37
+ onNavigate?.(index);
38
+ };
39
+ const navigateTo = (index) => {
40
+ syncIndex(index);
41
+ if (focusIndex(index)) scrollIndexIntoView(index);
42
+ };
43
+ const handleKeyDown = useEventCallback((event) => {
44
+ if (!enabled || shouldSkipKeyboardEvent(event)) return;
45
+ const currentIndex = readCurrentIndex() ?? -1;
46
+ const minIndex = getMinEnabledIndex(getElement, getSize, disabledIndices);
47
+ if (minIndex < 0) return;
48
+ const maxIndex = getMaxEnabledIndex(getElement, getSize, disabledIndices);
49
+ if (maxIndex < 0) return;
50
+ let nextIndex = null;
51
+ if (event.key === "Home") nextIndex = minIndex;
52
+ else if (event.key === "End") nextIndex = maxIndex;
53
+ else if (isForwardKey(event.key, orientation)) {
54
+ if (currentIndex >= maxIndex) nextIndex = loop ? minIndex : maxIndex;
55
+ else {
56
+ const foundIndex = findEnabledIndex(getElement, getSize, disabledIndices, currentIndex);
57
+ nextIndex = loop ? foundIndex : Math.min(maxIndex, foundIndex);
58
+ }
59
+ } else if (isBackwardKey(event.key, orientation)) {
60
+ if (currentIndex <= minIndex) nextIndex = loop ? maxIndex : minIndex;
61
+ else {
62
+ const foundIndex = findEnabledIndex(getElement, getSize, disabledIndices, currentIndex, true);
63
+ nextIndex = loop ? foundIndex : Math.max(minIndex, foundIndex);
64
+ }
65
+ }
66
+ if (isNil(nextIndex)) return;
67
+ const targetIndex = Math.min(maxIndex, Math.max(minIndex, nextIndex));
68
+ event.preventDefault();
69
+ navigateTo(targetIndex);
70
+ });
71
+ const handleContainerFocus = useEventCallback((event) => {
72
+ if (!enabled || event.target !== event.currentTarget) return;
73
+ const current = readCurrentIndex();
74
+ const targetIndex = hasValue(current) && isIndexAvailable(getElement, getSize, current, disabledIndices) ? current : getMinEnabledIndex(getElement, getSize, disabledIndices);
75
+ if (targetIndex < 0) return;
76
+ navigateTo(targetIndex);
77
+ });
78
+ return {
79
+ focusIndex,
80
+ getItemProps: useEventCallback((index) => ({
81
+ onFocus: () => syncIndex(index),
82
+ onClick: () => syncIndex(index)
83
+ })),
84
+ onFocus: handleContainerFocus,
85
+ onKeyDown: handleKeyDown
86
+ };
87
+ };
88
+ export { useListNavigation };
@@ -0,0 +1,24 @@
1
+ import { ListStateStore, ListStateStoreOptions } from "./ListStateStore.js";
2
+ /** 读取列表当前 active item 索引。 */
3
+ export type ListIndexGetter = () => number | null;
4
+ /** `useListState` 选项。 */
5
+ export type UseListStateOptions = ListStateStoreOptions & {
6
+ /** 受控选中项;传入时进入受控模式。 */
7
+ selectedKey?: string;
8
+ /** 非受控模式的初始选中项;缺省表示无选中项。 */
9
+ defaultSelectedKey?: string;
10
+ /** 用户选择 item 时的回调。 */
11
+ onSelect?: (selectedKey: string) => void;
12
+ };
13
+ /**
14
+ * 创建并保持一个与组件生命周期一致的列表交互状态 store。
15
+ */
16
+ export declare const useListState: (options?: UseListStateOptions) => ListStateStore;
17
+ /**
18
+ * 订阅 active item 索引。
19
+ */
20
+ export declare const useActiveIndex: (state: ListStateStore | null) => number | null;
21
+ /**
22
+ * 订阅当前选中项。
23
+ */
24
+ export declare const useSelectedKey: (state: ListStateStore | null) => string | undefined;
@@ -0,0 +1,47 @@
1
+ import { createListStateStore } from "./ListStateStore.js";
2
+ import { isNil, isUndefined, noop } from "@omnific/utils";
3
+ import { useEventCallback, useMergedState } from "@omnific/hooks";
4
+ import { useCallback, useEffect, useLayoutEffect, useState, useSyncExternalStore } from "react";
5
+ // navigation/useListState.ts
6
+ /**
7
+ * 创建并保持一个与组件生命周期一致的列表交互状态 store。
8
+ */
9
+ const useListState = (options = {}) => {
10
+ const [mergedSelectedKey, setSelectedKey] = useMergedState({
11
+ defaultValue: options.defaultSelectedKey,
12
+ value: options.selectedKey
13
+ });
14
+ const [store] = useState(() => createListStateStore({
15
+ activeIndex: options.activeIndex,
16
+ defaultSelectedKey: mergedSelectedKey
17
+ }));
18
+ const handleSelect = useEventCallback((nextSelectedKey) => {
19
+ if (isUndefined(options.selectedKey)) store.setSelectedKey(nextSelectedKey);
20
+ setSelectedKey(nextSelectedKey);
21
+ options.onSelect?.(nextSelectedKey);
22
+ });
23
+ useEffect(() => {
24
+ return store.subscribeSelection(handleSelect);
25
+ }, [handleSelect, store]);
26
+ useLayoutEffect(() => {
27
+ store.setSelectedKey(mergedSelectedKey);
28
+ }, [mergedSelectedKey, store]);
29
+ return store;
30
+ };
31
+ /**
32
+ * 订阅 active item 索引。
33
+ */
34
+ const useActiveIndex = (state) => {
35
+ const subscribe = useCallback((listener) => isNil(state) ? noop : state.subscribe(listener), [state]);
36
+ const getSnapshot = useCallback(() => isNil(state) ? null : state.getActiveIndex(), [state]);
37
+ return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
38
+ };
39
+ /**
40
+ * 订阅当前选中项。
41
+ */
42
+ const useSelectedKey = (state) => {
43
+ const subscribe = useCallback((listener) => isNil(state) ? noop : state.subscribe(listener), [state]);
44
+ const getSnapshot = useCallback(() => isNil(state) ? void 0 : state.getSelectedKey(), [state]);
45
+ return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
46
+ };
47
+ export { useActiveIndex, useListState, useSelectedKey };
@@ -0,0 +1,34 @@
1
+ import { ListIndexGetter } from "./useListState.js";
2
+ import { KeyboardEvent } from "react";
3
+ /** 返回与列表索引对齐的标签数组。 */
4
+ export type ListLabelsGetter = () => Array<string | null>;
5
+ /** typeahead 自定义匹配函数。 */
6
+ export type TypeaheadFindMatch = (labels: Array<string | null>, search: string) => string | null | undefined;
7
+ /** `useTypeahead` 选项。 */
8
+ export type UseTypeaheadOptions = {
9
+ /** 读取当前 active item 索引。 */
10
+ getActiveIndex: ListIndexGetter;
11
+ /** 读取当前 selected item 索引,用于派生 typeahead 的起始位置。 */
12
+ getSelectedIndex?: ListIndexGetter;
13
+ /** 按列表索引返回标签数组。 */
14
+ getLabels: ListLabelsGetter;
15
+ /** 命中项时回调。 */
16
+ onMatch: (index: number) => void;
17
+ /** 是否启用 typeahead,默认 `true`。 */
18
+ enabled?: boolean;
19
+ /** 输入缓冲重置时间,默认 `750ms`。 */
20
+ resetMs?: number;
21
+ /** 自定义匹配函数。 */
22
+ findMatch?: TypeaheadFindMatch | null;
23
+ /** 忽略的按键。 */
24
+ ignoreKeys?: ReadonlyArray<string>;
25
+ /** 输入状态变化时回调。 */
26
+ onTypingChange?: (typing: boolean) => void;
27
+ };
28
+ /**
29
+ * 提供菜单列表的字符搜索能力。
30
+ */
31
+ export declare const useTypeahead: (options: UseTypeaheadOptions) => {
32
+ onKeyDown: (event: KeyboardEvent<HTMLElement>) => void;
33
+ onKeyUp: (event: KeyboardEvent<HTMLElement>) => void;
34
+ };