@omnific/atelier 0.5.0 → 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.
- package/README.md +201 -0
- package/dist/button/LoadingIcon.js +5 -5
- package/dist/button/types.d.ts +1 -1
- package/dist/list/ListItemIcon.d.ts +5 -0
- package/dist/list/ListItemIcon.js +15 -0
- package/dist/list/ListItemText.d.ts +5 -0
- package/dist/list/ListItemText.js +15 -0
- package/dist/list/constants.js +11 -0
- package/dist/list/index.d.ts +3 -0
- package/dist/list/index.js +3 -0
- package/dist/main.d.ts +22 -2
- package/dist/main.js +15 -1
- package/dist/menu/MenuItem.d.ts +5 -0
- package/dist/menu/MenuItem.js +57 -0
- package/dist/menu/MenuList.d.ts +5 -0
- package/dist/menu/MenuList.js +67 -0
- package/dist/menu/constants.js +11 -0
- package/dist/menu/index.d.ts +9 -0
- package/dist/menu/index.js +5 -0
- package/dist/menu/types.d.ts +62 -0
- package/dist/navigation/ListCollection.d.ts +19 -0
- package/dist/navigation/ListCollection.js +21 -0
- package/dist/navigation/ListCollectionStore.d.ts +17 -0
- package/dist/navigation/ListCollectionStore.js +88 -0
- package/dist/navigation/ListState.d.ts +19 -0
- package/dist/navigation/ListState.js +21 -0
- package/dist/navigation/ListStateStore.d.ts +31 -0
- package/dist/navigation/ListStateStore.js +56 -0
- package/dist/navigation/index.d.ts +12 -0
- package/dist/navigation/index.js +11 -0
- package/dist/navigation/useInteractions.d.ts +23 -0
- package/dist/navigation/useInteractions.js +35 -0
- package/dist/navigation/useListCollection.d.ts +5 -0
- package/dist/navigation/useListCollection.js +11 -0
- package/dist/navigation/useListItem.d.ts +14 -0
- package/dist/navigation/useListItem.js +42 -0
- package/dist/navigation/useListNavigation.d.ts +39 -0
- package/dist/navigation/useListNavigation.js +88 -0
- package/dist/navigation/useListState.d.ts +24 -0
- package/dist/navigation/useListState.js +47 -0
- package/dist/navigation/useTypeahead.d.ts +34 -0
- package/dist/navigation/useTypeahead.js +95 -0
- package/dist/navigation/utils.d.ts +9 -0
- package/dist/navigation/utils.js +71 -0
- package/dist/styles.css +122 -3
- package/dist/system/index.d.ts +2 -1
- package/dist/system/index.js +2 -1
- package/dist/system/themes/antd.d.ts +63 -0
- package/dist/system/themes/antd.js +49 -0
- package/dist/system/types.d.ts +1 -1
- package/dist/transition/types.d.ts +2 -2
- package/dist/transition/useTransitionStatus.d.ts +1 -1
- package/dist/transition/useTransitionStatus.js +24 -14
- package/package.json +21 -6
package/README.md
CHANGED
|
@@ -77,6 +77,207 @@ scope.
|
|
|
77
77
|
Spacing, radii, fonts, control heights, shadows, body defaults, border-box sizing
|
|
78
78
|
and component styles ship via `@omnific/atelier/styles.css`.
|
|
79
79
|
|
|
80
|
+
## Menus
|
|
81
|
+
|
|
82
|
+
### Menu items
|
|
83
|
+
|
|
84
|
+
`MenuItem` renders a standalone native button with menu semantics. Compose its
|
|
85
|
+
content with `ListItemIcon` and `ListItemText` from `@omnific/atelier/list`.
|
|
86
|
+
|
|
87
|
+
```tsx
|
|
88
|
+
import { MenuItem } from '@omnific/atelier';
|
|
89
|
+
import { ListItemIcon, ListItemText } from '@omnific/atelier/list';
|
|
90
|
+
|
|
91
|
+
<MenuItem aria-current='page' selected>
|
|
92
|
+
<ListItemIcon aria-hidden='true'>H</ListItemIcon>
|
|
93
|
+
<ListItemText>Home</ListItemText>
|
|
94
|
+
</MenuItem>;
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
`selected`, `disabled`, `size`, and `divider` map to menu item state, data
|
|
98
|
+
attributes, and accessible attributes. A `MenuItem` works without `MenuList`;
|
|
99
|
+
inside a list it registers itself for navigation. Use `label` to provide an
|
|
100
|
+
explicit typeahead label when the children contain icons or the displayed text
|
|
101
|
+
should not be used for search.
|
|
102
|
+
|
|
103
|
+
### MenuList
|
|
104
|
+
|
|
105
|
+
`MenuList` composes `useListNavigation` and `useTypeahead` to provide roving
|
|
106
|
+
tabindex navigation and typeahead for a collection of `MenuItem` components.
|
|
107
|
+
|
|
108
|
+
```tsx
|
|
109
|
+
import { MenuItem, MenuList } from '@omnific/atelier';
|
|
110
|
+
import { ListItemText } from '@omnific/atelier/list';
|
|
111
|
+
import '@omnific/atelier/styles.css';
|
|
112
|
+
|
|
113
|
+
function NavigationMenu() {
|
|
114
|
+
return (
|
|
115
|
+
<MenuList aria-label='Main navigation' tabIndex={0}>
|
|
116
|
+
<MenuItem selected>
|
|
117
|
+
<ListItemText>Home</ListItemText>
|
|
118
|
+
</MenuItem>
|
|
119
|
+
<MenuItem>
|
|
120
|
+
<ListItemText>Projects</ListItemText>
|
|
121
|
+
</MenuItem>
|
|
122
|
+
<MenuItem disabled>
|
|
123
|
+
<ListItemText>Billing</ListItemText>
|
|
124
|
+
</MenuItem>
|
|
125
|
+
</MenuList>
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
`MenuList` uses a roving tabindex. Add `tabIndex={0}` when the list itself is
|
|
131
|
+
the keyboard entry point; when it lives in a popup, the trigger layer should
|
|
132
|
+
focus it instead.
|
|
133
|
+
|
|
134
|
+
`selected` only controls the selected visual state and `data-selected`. For
|
|
135
|
+
navigation menus, pass `aria-current='page'` explicitly. For checkable menus,
|
|
136
|
+
use `role='menuitemradio'` or `role='menuitemcheckbox'`; `selected` then maps to
|
|
137
|
+
`aria-checked`. Keyboard navigation keeps its active item in `MenuList` state
|
|
138
|
+
and does not require a selected index.
|
|
139
|
+
|
|
140
|
+
For single selection, give each item a `value` and use `selectedKey` or
|
|
141
|
+
`defaultSelectedKey`. `onSelect` receives the next selected key.
|
|
142
|
+
|
|
143
|
+
```tsx
|
|
144
|
+
import { useState } from 'react';
|
|
145
|
+
|
|
146
|
+
function ProjectMenu() {
|
|
147
|
+
const [selectedKey, setSelectedKey] = useState('overview');
|
|
148
|
+
|
|
149
|
+
return (
|
|
150
|
+
<MenuList selectedKey={selectedKey} onSelect={setSelectedKey}>
|
|
151
|
+
<MenuItem value='overview' role='menuitemradio'>
|
|
152
|
+
<ListItemText>Overview</ListItemText>
|
|
153
|
+
</MenuItem>
|
|
154
|
+
<MenuItem value='activity' role='menuitemradio'>
|
|
155
|
+
<ListItemText>Activity</ListItemText>
|
|
156
|
+
</MenuItem>
|
|
157
|
+
</MenuList>
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
When `value` is provided inside `MenuList`, the list selection overrides the
|
|
163
|
+
individual `selected` prop. Clicking the currently selected item does not call
|
|
164
|
+
`onSelect`. Multi-selection is not supported yet; `selectedKey` and
|
|
165
|
+
`defaultSelectedKey` intentionally stay singular until that API is added.
|
|
166
|
+
|
|
167
|
+
### Ant Design menu themes
|
|
168
|
+
|
|
169
|
+
The system subpath exports `antdLightSystem` and `antdDarkSystem`. Both systems
|
|
170
|
+
only declare the menu colors that differ from the Atelier default theme, so
|
|
171
|
+
undeclared variables continue to inherit from `:root`.
|
|
172
|
+
|
|
173
|
+
```tsx
|
|
174
|
+
import { MenuItem } from '@omnific/atelier';
|
|
175
|
+
import { antdDarkSystem, useTheme } from '@omnific/atelier/system';
|
|
176
|
+
|
|
177
|
+
function DarkMenu() {
|
|
178
|
+
const themeRef = useTheme(antdDarkSystem);
|
|
179
|
+
|
|
180
|
+
return (
|
|
181
|
+
<div ref={themeRef}>
|
|
182
|
+
<MenuItem>Dark item</MenuItem>
|
|
183
|
+
</div>
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
### Sizes
|
|
189
|
+
|
|
190
|
+
`MenuList` accepts `size="small" | "default" | "large"`. The list size is
|
|
191
|
+
propagated through inherited CSS custom properties: `--menu-item-height`,
|
|
192
|
+
`--menu-item-padding-inline`, and `--menu-item-font-size`. An individual
|
|
193
|
+
`MenuItem` can override the list size with its own `size` prop.
|
|
194
|
+
|
|
195
|
+
```tsx
|
|
196
|
+
<MenuList size='small'>
|
|
197
|
+
<MenuItem>Compact item</MenuItem>
|
|
198
|
+
<MenuItem size='large'>Large override</MenuItem>
|
|
199
|
+
</MenuList>
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Keyboard behavior
|
|
203
|
+
|
|
204
|
+
- `ArrowUp` / `ArrowDown` move focus in vertical lists; `ArrowLeft` /
|
|
205
|
+
`ArrowRight` move focus when `aria-orientation='horizontal'`. `MenuList`
|
|
206
|
+
wraps around at the ends.
|
|
207
|
+
- `Home` / `End` move focus to the first or last enabled item.
|
|
208
|
+
- Printable characters start typeahead. The search buffer resets after 750ms,
|
|
209
|
+
and repeating the same first letter cycles through matching items.
|
|
210
|
+
- Disabled items are skipped. Menus use a roving tabindex: the active item has
|
|
211
|
+
`tabIndex=0` and every other item has `tabIndex=-1`.
|
|
212
|
+
- The initial render has no active item. Focusing the `MenuList` container
|
|
213
|
+
activates the first enabled item; focusing or clicking an item synchronizes
|
|
214
|
+
the active index.
|
|
215
|
+
|
|
216
|
+
### Focus boundary
|
|
217
|
+
|
|
218
|
+
`MenuList` only manages navigation inside the list. It does not open or close a
|
|
219
|
+
popup, position an overlay, trap Tab focus, close on Escape or outside focus, or
|
|
220
|
+
restore focus to a trigger. Those responsibilities belong to a future
|
|
221
|
+
Menu/Popover layer.
|
|
222
|
+
|
|
223
|
+
### Navigation hooks
|
|
224
|
+
|
|
225
|
+
The navigation subpath exports the building blocks used by `MenuList`:
|
|
226
|
+
|
|
227
|
+
```tsx
|
|
228
|
+
import {
|
|
229
|
+
ListCollection,
|
|
230
|
+
useInteractions,
|
|
231
|
+
useListItem,
|
|
232
|
+
useListNavigation,
|
|
233
|
+
useTypeahead,
|
|
234
|
+
} from '@omnific/atelier/navigation';
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
For menu-focused imports, `@omnific/atelier/menu` also re-exports
|
|
238
|
+
`useListNavigation` and `useTypeahead` with their option types.
|
|
239
|
+
|
|
240
|
+
`useListNavigation` owns arrow-key navigation and imperative focus.
|
|
241
|
+
`useListNavigation` and `useTypeahead` read list data through getters so they can
|
|
242
|
+
work with the `ListCollection` store or custom refs:
|
|
243
|
+
|
|
244
|
+
- `useListNavigation` uses `getActiveIndex`, `getElement`, and `getSize`.
|
|
245
|
+
- `useTypeahead` uses `getActiveIndex`, `getLabels`, and optionally
|
|
246
|
+
`getSelectedIndex`.
|
|
247
|
+
|
|
248
|
+
`useTypeahead` follows Floating UI's label-array contract: `getLabels` returns an
|
|
249
|
+
array of strings and `null` entries are skipped. It only reports the matched
|
|
250
|
+
index through `onMatch`; it does not change the active index or move focus. A
|
|
251
|
+
controlled composition must pass the match to both the active index state and
|
|
252
|
+
the navigation focus API:
|
|
253
|
+
|
|
254
|
+
```tsx
|
|
255
|
+
const elementsRef = useRef<Array<HTMLElement | null>>([]);
|
|
256
|
+
const labelsRef = useRef<Array<string | null>>([]);
|
|
257
|
+
|
|
258
|
+
const navigation = useListNavigation({
|
|
259
|
+
getActiveIndex: () => activeIndex,
|
|
260
|
+
getElement: index => elementsRef.current[index] ?? undefined,
|
|
261
|
+
getSize: () => elementsRef.current.length,
|
|
262
|
+
onNavigate: setActiveIndex,
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
const typeahead = useTypeahead({
|
|
266
|
+
getActiveIndex: () => activeIndex,
|
|
267
|
+
getLabels: () => labelsRef.current,
|
|
268
|
+
onMatch: index => {
|
|
269
|
+
setActiveIndex(index);
|
|
270
|
+
navigation.focusIndex(index);
|
|
271
|
+
},
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
const interactions = useInteractions([navigation, typeahead]);
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
Use `ListCollection` with `useListItem` to register item elements in DOM order,
|
|
278
|
+
or provide your own getters. Disabled `MenuItem` components write `null` to the
|
|
279
|
+
label array, so typeahead never matches them.
|
|
280
|
+
|
|
80
281
|
## Storybook
|
|
81
282
|
|
|
82
283
|
Curated component examples live under `packages/atelier/__stories__` and
|
|
@@ -3,19 +3,19 @@ import "../transition/types.js";
|
|
|
3
3
|
import { useTransitionStatus } from "../transition/useTransitionStatus.js";
|
|
4
4
|
import { clsx } from "clsx";
|
|
5
5
|
import { jsx } from "react/jsx-runtime";
|
|
6
|
-
import { LoadingIcon } from "@omnific/icons";
|
|
6
|
+
import { LoadingIcon as LoadingIcon$1 } from "@omnific/icons";
|
|
7
7
|
// button/LoadingIcon.tsx
|
|
8
8
|
/**
|
|
9
9
|
* 渲染按钮内部的加载图标。
|
|
10
10
|
*/
|
|
11
|
-
const LoadingIcon
|
|
11
|
+
const LoadingIcon = (props) => {
|
|
12
12
|
const { className, iconOnly = false, loading = false, style } = props;
|
|
13
13
|
const { handleRef, isMounted, status } = useTransitionStatus({
|
|
14
14
|
appear: true,
|
|
15
15
|
enter: {
|
|
16
16
|
opacity: 1,
|
|
17
17
|
scale: 1,
|
|
18
|
-
width:
|
|
18
|
+
width: 16,
|
|
19
19
|
marginRight: iconOnly ? 0 : 8
|
|
20
20
|
},
|
|
21
21
|
exit: {
|
|
@@ -38,7 +38,7 @@ const LoadingIcon$1 = (props) => {
|
|
|
38
38
|
width: 0,
|
|
39
39
|
...style
|
|
40
40
|
},
|
|
41
|
-
children: /* @__PURE__ */ jsx(LoadingIcon, { size: "1em" })
|
|
41
|
+
children: /* @__PURE__ */ jsx(LoadingIcon$1, { size: "1em" })
|
|
42
42
|
});
|
|
43
43
|
};
|
|
44
|
-
export { LoadingIcon
|
|
44
|
+
export { LoadingIcon as default };
|
package/dist/button/types.d.ts
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { listItemIconPrefixCls } from "./constants.js";
|
|
2
|
+
import { clsx } from "clsx";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
// list/ListItemIcon.tsx
|
|
5
|
+
/**
|
|
6
|
+
* 列表项左侧图标容器。
|
|
7
|
+
*/
|
|
8
|
+
const ListItemIcon = (props) => {
|
|
9
|
+
const { className, ...rest } = props;
|
|
10
|
+
return /* @__PURE__ */ jsx("span", {
|
|
11
|
+
...rest,
|
|
12
|
+
className: clsx(listItemIconPrefixCls, className)
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
export { ListItemIcon };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { listItemTextPrefixCls } from "./constants.js";
|
|
2
|
+
import { clsx } from "clsx";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
// list/ListItemText.tsx
|
|
5
|
+
/**
|
|
6
|
+
* 列表项主内容容器。
|
|
7
|
+
*/
|
|
8
|
+
const ListItemText = (props) => {
|
|
9
|
+
const { className, ...rest } = props;
|
|
10
|
+
return /* @__PURE__ */ jsx("span", {
|
|
11
|
+
...rest,
|
|
12
|
+
className: clsx(listItemTextPrefixCls, className)
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
export { ListItemText };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { getPrefixCls } from "../utils/getPrefixCls.js";
|
|
2
|
+
// list/constants.ts
|
|
3
|
+
/**
|
|
4
|
+
* ListItemIcon 节点的 CSS 类名前缀。
|
|
5
|
+
*/
|
|
6
|
+
const listItemIconPrefixCls = getPrefixCls("list-item-icon");
|
|
7
|
+
/**
|
|
8
|
+
* ListItemText 节点的 CSS 类名前缀。
|
|
9
|
+
*/
|
|
10
|
+
const listItemTextPrefixCls = getPrefixCls("list-item-text");
|
|
11
|
+
export { listItemIconPrefixCls, listItemTextPrefixCls };
|
package/dist/main.d.ts
CHANGED
|
@@ -1,10 +1,30 @@
|
|
|
1
|
-
import "./
|
|
1
|
+
import { ComponentSize } from "./types.js";
|
|
2
2
|
import { Box } from "./box/Box.js";
|
|
3
|
+
import "./box/index.js";
|
|
3
4
|
import { BoxProps } from "./box/types.js";
|
|
4
5
|
import "./button/index.js";
|
|
5
6
|
import { Button } from "./button/Button.js";
|
|
6
7
|
import { ButtonProps } from "./button/types.js";
|
|
8
|
+
import { ListItemIcon } from "./list/ListItemIcon.js";
|
|
9
|
+
import { ListItemText } from "./list/ListItemText.js";
|
|
10
|
+
import "./list/index.js";
|
|
11
|
+
import { ListCollectionStore, createListCollectionStore } from "./navigation/ListCollectionStore.js";
|
|
12
|
+
import { ListCollection, ListCollectionProps, useListCollectionContext } from "./navigation/ListCollection.js";
|
|
13
|
+
import { Interaction, InteractionProps, useInteractions } from "./navigation/useInteractions.js";
|
|
14
|
+
import { ListStateItemProps, ListStateStore, ListStateStoreOptions, createListStateStore } from "./navigation/ListStateStore.js";
|
|
15
|
+
import { ListState, ListStateProps, useListStateContext } from "./navigation/ListState.js";
|
|
16
|
+
import { useListCollection } from "./navigation/useListCollection.js";
|
|
17
|
+
import { UseListItemOptions, useListItem } from "./navigation/useListItem.js";
|
|
18
|
+
import { ListIndexGetter, UseListStateOptions, useActiveIndex, useListState, useSelectedKey } from "./navigation/useListState.js";
|
|
19
|
+
import { DisabledIndices, ListElementGetter, ListNavigationOrientation, ListSizeGetter } from "./navigation/utils.js";
|
|
20
|
+
import { ListNavigationItemProps, UseListNavigationOptions, useListNavigation } from "./navigation/useListNavigation.js";
|
|
21
|
+
import { ListLabelsGetter, TypeaheadFindMatch, UseTypeaheadOptions, useTypeahead } from "./navigation/useTypeahead.js";
|
|
22
|
+
import "./menu/index.js";
|
|
23
|
+
import { MenuItemProps, MenuListProps } from "./menu/types.js";
|
|
24
|
+
import { MenuItem } from "./menu/MenuItem.js";
|
|
25
|
+
import { MenuList } from "./menu/MenuList.js";
|
|
26
|
+
import "./navigation/index.js";
|
|
7
27
|
import "./transition/index.js";
|
|
8
28
|
import { useTransitionStatus } from "./transition/useTransitionStatus.js";
|
|
9
29
|
import { ENTERED, ENTERING, EXITED, EXITING, TransitionOptions, UNMOUNTED } from "./transition/types.js";
|
|
10
|
-
export { Box, type BoxProps, Button, type ButtonProps, ENTERED, ENTERING, EXITED, EXITING, type TransitionOptions, UNMOUNTED, useTransitionStatus };
|
|
30
|
+
export { Box, type BoxProps, Button, type ButtonProps, type ComponentSize, type DisabledIndices, ENTERED, ENTERING, EXITED, EXITING, type Interaction, type InteractionProps, ListCollection, type ListCollectionProps, type ListCollectionStore, type ListElementGetter, type ListIndexGetter, ListItemIcon, ListItemText, type ListLabelsGetter, type ListNavigationItemProps, type ListNavigationOrientation, type ListSizeGetter, ListState, type ListStateItemProps, type ListStateProps, type ListStateStore, type ListStateStoreOptions, MenuItem, type MenuItemProps, MenuList, type MenuListProps, type TransitionOptions, type TypeaheadFindMatch, UNMOUNTED, type UseListItemOptions, type UseListNavigationOptions, type UseListStateOptions, type UseTypeaheadOptions, createListCollectionStore, createListStateStore, useActiveIndex, useInteractions, useListCollection, useListCollectionContext, useListItem, useListNavigation, useListState, useListStateContext, useSelectedKey, useTransitionStatus, useTypeahead };
|
package/dist/main.js
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
import { Box } from "./box/Box.js";
|
|
2
2
|
import { Button } from "./button/Button.js";
|
|
3
|
+
import { ListItemIcon } from "./list/ListItemIcon.js";
|
|
4
|
+
import { ListItemText } from "./list/ListItemText.js";
|
|
5
|
+
import { MenuItem } from "./menu/MenuItem.js";
|
|
6
|
+
import { MenuList } from "./menu/MenuList.js";
|
|
7
|
+
import { ListCollection, useListCollectionContext } from "./navigation/ListCollection.js";
|
|
8
|
+
import { ListState, useListStateContext } from "./navigation/ListState.js";
|
|
9
|
+
import { createListCollectionStore } from "./navigation/ListCollectionStore.js";
|
|
10
|
+
import { createListStateStore } from "./navigation/ListStateStore.js";
|
|
11
|
+
import { useActiveIndex, useListState, useSelectedKey } from "./navigation/useListState.js";
|
|
12
|
+
import { useInteractions } from "./navigation/useInteractions.js";
|
|
13
|
+
import { useListCollection } from "./navigation/useListCollection.js";
|
|
14
|
+
import { useListItem } from "./navigation/useListItem.js";
|
|
15
|
+
import { useListNavigation } from "./navigation/useListNavigation.js";
|
|
16
|
+
import { useTypeahead } from "./navigation/useTypeahead.js";
|
|
3
17
|
import { ENTERED, ENTERING, EXITED, EXITING, UNMOUNTED } from "./transition/types.js";
|
|
4
18
|
import { useTransitionStatus } from "./transition/useTransitionStatus.js";
|
|
5
|
-
export { Box, Button, ENTERED, ENTERING, EXITED, EXITING, UNMOUNTED, useTransitionStatus };
|
|
19
|
+
export { Box, Button, ENTERED, ENTERING, EXITED, EXITING, ListCollection, ListItemIcon, ListItemText, ListState, MenuItem, MenuList, UNMOUNTED, createListCollectionStore, createListStateStore, useActiveIndex, useInteractions, useListCollection, useListCollectionContext, useListItem, useListNavigation, useListState, useListStateContext, useSelectedKey, useTransitionStatus, useTypeahead };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { menuItemPrefixCls } from "./constants.js";
|
|
2
|
+
import { useListStateContext } from "../navigation/ListState.js";
|
|
3
|
+
import { useListItem } from "../navigation/useListItem.js";
|
|
4
|
+
import { useActiveIndex, useSelectedKey } from "../navigation/useListState.js";
|
|
5
|
+
import { clsx } from "clsx";
|
|
6
|
+
import { hasValue } from "@omnific/utils";
|
|
7
|
+
import { jsx } from "react/jsx-runtime";
|
|
8
|
+
import { useForkRef } from "@omnific/hooks";
|
|
9
|
+
// menu/MenuItem.tsx
|
|
10
|
+
/**
|
|
11
|
+
* 纵向菜单中的单个菜单项。
|
|
12
|
+
*/
|
|
13
|
+
const MenuItem = (props) => {
|
|
14
|
+
const { "aria-checked": ariaChecked, "aria-current": ariaCurrent, "aria-disabled": ariaDisabled, children, className, disabled = false, divider = false, value, label, onClick, onFocus, ref, role = "menuitem", selected = false, size = "default", tabIndex, type = "button", ...rest } = props;
|
|
15
|
+
const state = useListStateContext();
|
|
16
|
+
const activeIndex = useActiveIndex(state);
|
|
17
|
+
const selectedKey = useSelectedKey(state);
|
|
18
|
+
const { index, ref: listItemRef } = useListItem({ label: disabled ? null : label });
|
|
19
|
+
const handleRef = useForkRef(listItemRef, ref);
|
|
20
|
+
const isInList = index >= 0 && hasValue(state);
|
|
21
|
+
let isSelected = selected;
|
|
22
|
+
if (hasValue(state) && hasValue(value)) isSelected = Object.is(selectedKey, value);
|
|
23
|
+
const itemProps = hasValue(state) && index >= 0 ? state.getItemProps(index, {
|
|
24
|
+
value,
|
|
25
|
+
onClick,
|
|
26
|
+
onFocus
|
|
27
|
+
}) : {
|
|
28
|
+
onClick,
|
|
29
|
+
onFocus
|
|
30
|
+
};
|
|
31
|
+
const isCheckableRole = role === "menuitemradio" || role === "menuitemcheckbox";
|
|
32
|
+
let resolvedTabIndex = tabIndex;
|
|
33
|
+
if (isInList) resolvedTabIndex = index === activeIndex ? 0 : -1;
|
|
34
|
+
const classes = clsx(menuItemPrefixCls, `${menuItemPrefixCls}-size-${size}`, {
|
|
35
|
+
[`${menuItemPrefixCls}-disabled`]: disabled,
|
|
36
|
+
[`${menuItemPrefixCls}-divider`]: divider,
|
|
37
|
+
[`${menuItemPrefixCls}-selected`]: isSelected
|
|
38
|
+
}, className);
|
|
39
|
+
return /* @__PURE__ */ jsx("button", {
|
|
40
|
+
...rest,
|
|
41
|
+
"aria-checked": isCheckableRole ? isSelected : ariaChecked,
|
|
42
|
+
"aria-current": ariaCurrent,
|
|
43
|
+
"aria-disabled": disabled ? true : ariaDisabled,
|
|
44
|
+
className: classes,
|
|
45
|
+
"data-size": size,
|
|
46
|
+
"data-divider": divider || void 0,
|
|
47
|
+
"data-selected": isSelected || void 0,
|
|
48
|
+
disabled,
|
|
49
|
+
...itemProps,
|
|
50
|
+
ref: handleRef,
|
|
51
|
+
role,
|
|
52
|
+
tabIndex: disabled ? void 0 : resolvedTabIndex,
|
|
53
|
+
type,
|
|
54
|
+
children
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
export { MenuItem };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { menuListPrefixCls } from "./constants.js";
|
|
2
|
+
import { ListCollection } from "../navigation/ListCollection.js";
|
|
3
|
+
import { ListState } from "../navigation/ListState.js";
|
|
4
|
+
import { useInteractions } from "../navigation/useInteractions.js";
|
|
5
|
+
import { useListCollection } from "../navigation/useListCollection.js";
|
|
6
|
+
import { useListNavigation } from "../navigation/useListNavigation.js";
|
|
7
|
+
import { useListState } from "../navigation/useListState.js";
|
|
8
|
+
import { useTypeahead } from "../navigation/useTypeahead.js";
|
|
9
|
+
import { clsx } from "clsx";
|
|
10
|
+
import { jsx } from "react/jsx-runtime";
|
|
11
|
+
import { useForkRef } from "@omnific/hooks";
|
|
12
|
+
import { useRef } from "react";
|
|
13
|
+
// menu/MenuList.tsx
|
|
14
|
+
/**
|
|
15
|
+
* 菜单列表容器,组合 item 注册、active 状态、选中状态和键盘导航。
|
|
16
|
+
*/
|
|
17
|
+
const MenuList = (props) => {
|
|
18
|
+
const { "aria-orientation": ariaOrientation = "vertical", children, className, defaultSelectedKey, onClick, onFocus, onKeyDown, onKeyUp, onSelect, ref, role = "menu", selectedKey, size = "default", tabIndex = -1, ...rest } = props;
|
|
19
|
+
const containerRef = useRef(null);
|
|
20
|
+
const handleRef = useForkRef(containerRef, ref);
|
|
21
|
+
const collection = useListCollection();
|
|
22
|
+
const state = useListState({
|
|
23
|
+
defaultSelectedKey,
|
|
24
|
+
onSelect,
|
|
25
|
+
selectedKey
|
|
26
|
+
});
|
|
27
|
+
const navigation = useListNavigation({
|
|
28
|
+
getActiveIndex: state.getActiveIndex,
|
|
29
|
+
getElement: collection.getElement,
|
|
30
|
+
getSize: collection.getSize,
|
|
31
|
+
loop: true,
|
|
32
|
+
onNavigate: state.setActiveIndex,
|
|
33
|
+
orientation: ariaOrientation
|
|
34
|
+
});
|
|
35
|
+
const typeahead = useTypeahead({
|
|
36
|
+
getActiveIndex: state.getActiveIndex,
|
|
37
|
+
getLabels: collection.getLabels,
|
|
38
|
+
onMatch: (index) => {
|
|
39
|
+
state.setActiveIndex(index);
|
|
40
|
+
navigation.focusIndex(index);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
const interactions = useInteractions([navigation, typeahead]);
|
|
44
|
+
return /* @__PURE__ */ jsx(ListCollection, {
|
|
45
|
+
store: collection,
|
|
46
|
+
children: /* @__PURE__ */ jsx(ListState, {
|
|
47
|
+
store: state,
|
|
48
|
+
children: /* @__PURE__ */ jsx("div", {
|
|
49
|
+
...rest,
|
|
50
|
+
...interactions.getFloatingProps({
|
|
51
|
+
onClick,
|
|
52
|
+
onFocus,
|
|
53
|
+
onKeyDown,
|
|
54
|
+
onKeyUp
|
|
55
|
+
}),
|
|
56
|
+
"aria-orientation": ariaOrientation,
|
|
57
|
+
className: clsx(menuListPrefixCls, className),
|
|
58
|
+
"data-size": size,
|
|
59
|
+
ref: handleRef,
|
|
60
|
+
role,
|
|
61
|
+
tabIndex,
|
|
62
|
+
children
|
|
63
|
+
})
|
|
64
|
+
})
|
|
65
|
+
});
|
|
66
|
+
};
|
|
67
|
+
export { MenuList };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { getPrefixCls } from "../utils/getPrefixCls.js";
|
|
2
|
+
// menu/constants.ts
|
|
3
|
+
/**
|
|
4
|
+
* MenuItem 节点的 CSS 类名前缀。
|
|
5
|
+
*/
|
|
6
|
+
const menuItemPrefixCls = getPrefixCls("menu-item");
|
|
7
|
+
/**
|
|
8
|
+
* MenuList 节点的 CSS 类名前缀。
|
|
9
|
+
*/
|
|
10
|
+
const menuListPrefixCls = getPrefixCls("menu-list");
|
|
11
|
+
export { menuItemPrefixCls, menuListPrefixCls };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ListIndexGetter } from "../navigation/useListState.js";
|
|
2
|
+
import { DisabledIndices, ListElementGetter, ListNavigationOrientation, ListSizeGetter } from "../navigation/utils.js";
|
|
3
|
+
import { ListNavigationItemProps, UseListNavigationOptions, useListNavigation } from "../navigation/useListNavigation.js";
|
|
4
|
+
import { ListLabelsGetter, TypeaheadFindMatch, UseTypeaheadOptions, useTypeahead } from "../navigation/useTypeahead.js";
|
|
5
|
+
import "../navigation/index.js";
|
|
6
|
+
import { MenuItem } from "./MenuItem.js";
|
|
7
|
+
import { MenuList } from "./MenuList.js";
|
|
8
|
+
import { MenuItemProps, MenuListProps } from "./types.js";
|
|
9
|
+
export { type DisabledIndices, type ListElementGetter, type ListIndexGetter, type ListLabelsGetter, type ListNavigationItemProps, type ListNavigationOrientation, type ListSizeGetter, MenuItem, type MenuItemProps, MenuList, type MenuListProps, type TypeaheadFindMatch, type UseListNavigationOptions, type UseTypeaheadOptions, useListNavigation, useTypeahead };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { useListNavigation } from "../navigation/useListNavigation.js";
|
|
2
|
+
import { useTypeahead } from "../navigation/useTypeahead.js";
|
|
3
|
+
import { MenuItem } from "./MenuItem.js";
|
|
4
|
+
import { MenuList } from "./MenuList.js";
|
|
5
|
+
export { MenuItem, MenuList, useListNavigation, useTypeahead };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { ComponentSize } from "../types.js";
|
|
2
|
+
import { ComponentPropsWithRef } from "react";
|
|
3
|
+
/**
|
|
4
|
+
* MenuItem 组件属性。
|
|
5
|
+
*/
|
|
6
|
+
export type MenuItemProps = Omit<ComponentPropsWithRef<'button'>, 'value'> & {
|
|
7
|
+
/**
|
|
8
|
+
* 菜单项的唯一值,在 MenuList 中用于选中。
|
|
9
|
+
*/
|
|
10
|
+
value?: string;
|
|
11
|
+
/**
|
|
12
|
+
* 是否禁用菜单项。
|
|
13
|
+
*/
|
|
14
|
+
disabled?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* 是否处于选中状态。
|
|
17
|
+
*
|
|
18
|
+
* 在 MenuList 中且提供 `value` 时,由 MenuList 的 `selectedKey` 覆盖。
|
|
19
|
+
*/
|
|
20
|
+
selected?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* 菜单项尺寸。
|
|
23
|
+
*
|
|
24
|
+
* @default 'default'
|
|
25
|
+
*/
|
|
26
|
+
size?: ComponentSize;
|
|
27
|
+
/**
|
|
28
|
+
* 是否在菜单项底部显示分隔线。
|
|
29
|
+
*/
|
|
30
|
+
divider?: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* typeahead 使用的文本标签;缺省时读取菜单项 DOM 文本。
|
|
33
|
+
*
|
|
34
|
+
* 传入 `null` 可让该项不参与 typeahead;禁用项始终使用 `null`。
|
|
35
|
+
*/
|
|
36
|
+
label?: string | null;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* MenuList 组件属性。
|
|
40
|
+
*/
|
|
41
|
+
export type MenuListProps = Omit<ComponentPropsWithRef<'div'>, 'onSelect'> & {
|
|
42
|
+
/**
|
|
43
|
+
* 受控选中项;不传表示非受控模式。
|
|
44
|
+
*/
|
|
45
|
+
selectedKey?: string;
|
|
46
|
+
/**
|
|
47
|
+
* 非受控模式的初始选中项;缺省表示无选中项。
|
|
48
|
+
*/
|
|
49
|
+
defaultSelectedKey?: string;
|
|
50
|
+
/**
|
|
51
|
+
* 选中项变化时触发,返回新的选中 key。
|
|
52
|
+
*/
|
|
53
|
+
onSelect?: (selectedKey: string) => void;
|
|
54
|
+
/**
|
|
55
|
+
* 菜单列表的统一尺寸,通过 CSS 变量继承给内部 MenuItem。
|
|
56
|
+
*
|
|
57
|
+
* MenuItem 自身的 `size` 覆盖该值;`default` 保留列表继承值。
|
|
58
|
+
*
|
|
59
|
+
* @default 'default'
|
|
60
|
+
*/
|
|
61
|
+
size?: ComponentSize;
|
|
62
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ListCollectionStore } from "./ListCollectionStore.js";
|
|
2
|
+
import { ReactNode } from "react";
|
|
3
|
+
/** `ListCollection` 组件属性。 */
|
|
4
|
+
export interface ListCollectionProps {
|
|
5
|
+
/** 列表内容。 */
|
|
6
|
+
children: ReactNode;
|
|
7
|
+
/** 列表集合 store。 */
|
|
8
|
+
store: ListCollectionStore;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* 读取最近的列表集合 store。
|
|
12
|
+
*
|
|
13
|
+
* 不在 ListCollection 中时返回 `null`。
|
|
14
|
+
*/
|
|
15
|
+
export declare const useListCollectionContext: () => ListCollectionStore | null;
|
|
16
|
+
/**
|
|
17
|
+
* 向子树提供列表集合 store。
|
|
18
|
+
*/
|
|
19
|
+
export declare const ListCollection: (props: ListCollectionProps) => import("react").JSX.Element;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, use } from "react";
|
|
3
|
+
// navigation/ListCollection.tsx
|
|
4
|
+
const ListCollectionContext = createContext(null);
|
|
5
|
+
/**
|
|
6
|
+
* 读取最近的列表集合 store。
|
|
7
|
+
*
|
|
8
|
+
* 不在 ListCollection 中时返回 `null`。
|
|
9
|
+
*/
|
|
10
|
+
const useListCollectionContext = () => use(ListCollectionContext);
|
|
11
|
+
/**
|
|
12
|
+
* 向子树提供列表集合 store。
|
|
13
|
+
*/
|
|
14
|
+
const ListCollection = (props) => {
|
|
15
|
+
const { children, store } = props;
|
|
16
|
+
return /* @__PURE__ */ jsx(ListCollectionContext, {
|
|
17
|
+
value: store,
|
|
18
|
+
children
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
export { ListCollection, useListCollectionContext };
|