@barqjs/aria 0.0.0-stage → 0.2.0-next.1
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/LICENSE +21 -0
- package/README.md +304 -2
- package/package.json +437 -4
- package/src/autocomplete.tsx +581 -0
- package/src/breadcrumbs.tsx +297 -0
- package/src/button.tsx +468 -0
- package/src/calendar.tsx +2316 -0
- package/src/checkbox.tsx +732 -0
- package/src/collections.ts +1059 -0
- package/src/color.ts +2119 -0
- package/src/colorpicker.tsx +2023 -0
- package/src/colorswatchpicker.tsx +240 -0
- package/src/combobox.tsx +3003 -0
- package/src/command.tsx +1203 -0
- package/src/date.ts +674 -0
- package/src/datefield.tsx +2066 -0
- package/src/datepicker.tsx +907 -0
- package/src/dialog.tsx +1077 -0
- package/src/disclosure.tsx +711 -0
- package/src/dnd/clipboard.ts +125 -0
- package/src/dnd/collection.ts +1265 -0
- package/src/dnd/data.ts +409 -0
- package/src/dnd/drag-drop.ts +801 -0
- package/src/dnd/index.ts +133 -0
- package/src/dnd/manager.ts +639 -0
- package/src/dnd/strings.ts +1279 -0
- package/src/dnd/targets.ts +726 -0
- package/src/dnd/types.ts +212 -0
- package/src/dom.ts +703 -0
- package/src/drag-and-drop.tsx +669 -0
- package/src/drawer.ts +674 -0
- package/src/dropzone.tsx +208 -0
- package/src/file-trigger.tsx +103 -0
- package/src/focus.ts +1370 -0
- package/src/form.tsx +94 -0
- package/src/gridlist.tsx +1354 -0
- package/src/group.tsx +146 -0
- package/src/i18n.ts +823 -0
- package/src/index.ts +1672 -0
- package/src/interactions/description.ts +73 -0
- package/src/interactions/flags.ts +35 -0
- package/src/interactions/focus-events.ts +226 -0
- package/src/interactions/focusable.ts +86 -0
- package/src/interactions/hover.ts +213 -0
- package/src/interactions/interact-outside.ts +123 -0
- package/src/interactions/keyboard.ts +298 -0
- package/src/interactions/listeners.ts +78 -0
- package/src/interactions/long-press.ts +160 -0
- package/src/interactions/modality.ts +428 -0
- package/src/interactions/move.ts +257 -0
- package/src/interactions/open-link.ts +226 -0
- package/src/interactions/press.ts +915 -0
- package/src/interactions/prevent-focus.ts +102 -0
- package/src/interactions/scroll-wheel.ts +43 -0
- package/src/interactions/text-selection.ts +77 -0
- package/src/interactions.ts +81 -0
- package/src/label.ts +140 -0
- package/src/link.tsx +477 -0
- package/src/listbox.tsx +1247 -0
- package/src/live.ts +208 -0
- package/src/menu.tsx +2383 -0
- package/src/menubar.tsx +579 -0
- package/src/navigation-tree.tsx +357 -0
- package/src/navigation.ts +735 -0
- package/src/numberfield.tsx +942 -0
- package/src/otp.ts +430 -0
- package/src/overlay-arrow.tsx +157 -0
- package/src/overlays.ts +1549 -0
- package/src/platform.ts +75 -0
- package/src/presence.ts +164 -0
- package/src/preview-trigger.tsx +582 -0
- package/src/progress-bar-context.ts +15 -0
- package/src/questionnaire.tsx +1717 -0
- package/src/radio.tsx +641 -0
- package/src/register-dom.ts +12 -0
- package/src/scroller.ts +1165 -0
- package/src/select.tsx +1745 -0
- package/src/selection-indicator.tsx +74 -0
- package/src/selection.ts +1472 -0
- package/src/shared-element-transition.tsx +316 -0
- package/src/sheet.tsx +1461 -0
- package/src/slider.tsx +1137 -0
- package/src/splitter.ts +1152 -0
- package/src/static-collection.ts +211 -0
- package/src/switch.tsx +212 -0
- package/src/table.tsx +2218 -0
- package/src/tabs.tsx +1127 -0
- package/src/tag.tsx +637 -0
- package/src/textarea.tsx +186 -0
- package/src/textfield.tsx +483 -0
- package/src/toast-components.tsx +589 -0
- package/src/toast.ts +658 -0
- package/src/toggle-button-group.tsx +175 -0
- package/src/toggle-input.tsx +86 -0
- package/src/toggle.ts +384 -0
- package/src/togglegroup.ts +365 -0
- package/src/tokenfield.tsx +1673 -0
- package/src/toolbar.tsx +184 -0
- package/src/tooltip.tsx +674 -0
- package/src/tree.tsx +2075 -0
- package/src/utils.ts +787 -0
- package/src/validation.ts +504 -0
- package/src/virtualizer.tsx +837 -0
|
@@ -0,0 +1,357 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A navigation tree: a site's or an app's sections as a tree of links.
|
|
3
|
+
*
|
|
4
|
+
* It is a {@link Tree} with the choices a navigation makes already made. There
|
|
5
|
+
* is no selection, because the page is what is "selected", and that is
|
|
6
|
+
* `selectedRoute`, matched against each item's `href`. The row hands focus to
|
|
7
|
+
* its link, so Enter follows it and a screen reader announces a link; the arrow
|
|
8
|
+
* keys still move between rows and expand and collapse them from the link,
|
|
9
|
+
* and Tab leaves the tree from any row.
|
|
10
|
+
*
|
|
11
|
+
* ```tsx
|
|
12
|
+
* <NavigationTree aria-label="Docs" items={pages()} selectedRoute={location.pathname}>
|
|
13
|
+
* {(page) => (
|
|
14
|
+
* <NavigationTreeItem>
|
|
15
|
+
* <NavigationTreeItemContent>
|
|
16
|
+
* <NavigationTreeLink>{page.title}</NavigationTreeLink>
|
|
17
|
+
* </NavigationTreeItemContent>
|
|
18
|
+
* </NavigationTreeItem>
|
|
19
|
+
* )}
|
|
20
|
+
* </NavigationTree>
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* The current item's link carries `aria-current="page"` and its row
|
|
24
|
+
* `data-current`; every row above it, open or not, carries
|
|
25
|
+
* `data-current-ancestor`. Keyboard focus starts on the current item, or on
|
|
26
|
+
* the closest of its ancestors that is showing.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import {
|
|
30
|
+
cells,
|
|
31
|
+
type Accessor,
|
|
32
|
+
type Child,
|
|
33
|
+
createContext,
|
|
34
|
+
createEffect,
|
|
35
|
+
getContext,
|
|
36
|
+
omit,
|
|
37
|
+
provide,
|
|
38
|
+
untrack,
|
|
39
|
+
} from "@barqjs/core";
|
|
40
|
+
import { isServer } from "@barqjs/web";
|
|
41
|
+
import { ref as makeRef, mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
|
|
42
|
+
import type { Key } from "./collections.ts";
|
|
43
|
+
import { contains, scrollIntoView, scrollParents } from "./dom.ts";
|
|
44
|
+
import { focusRing } from "./focus.ts";
|
|
45
|
+
import { hover } from "./interactions/hover.ts";
|
|
46
|
+
import { link } from "./link.tsx";
|
|
47
|
+
import {
|
|
48
|
+
Tree,
|
|
49
|
+
type TreeCollection,
|
|
50
|
+
type TreeComponentProps,
|
|
51
|
+
TreeHeader,
|
|
52
|
+
type TreeHeaderProps,
|
|
53
|
+
TreeItem,
|
|
54
|
+
type TreeItemComponentProps,
|
|
55
|
+
TreeItemContent,
|
|
56
|
+
type TreeItemRenderState,
|
|
57
|
+
TreeSection,
|
|
58
|
+
type TreeSectionProps,
|
|
59
|
+
useTree,
|
|
60
|
+
useTreeItem,
|
|
61
|
+
useTreeNode,
|
|
62
|
+
} from "./tree.tsx";
|
|
63
|
+
import { filterDOMProps, fromProps, mergeProps, type StyleProps, styleProps } from "./utils.ts";
|
|
64
|
+
|
|
65
|
+
interface NavigationTreeContextValue {
|
|
66
|
+
selectedRoute: Accessor<string | null | undefined>;
|
|
67
|
+
/** The route focus was last moved to, so the rows move it once per change between them. */
|
|
68
|
+
synced: { route: string | null | undefined };
|
|
69
|
+
element: Accessor<HTMLDivElement | null>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const NavigationTreeContext = createContext<NavigationTreeContextValue | null>(null);
|
|
73
|
+
|
|
74
|
+
function useNavigationTree(): NavigationTreeContextValue {
|
|
75
|
+
const value = getContext(NavigationTreeContext);
|
|
76
|
+
if (value === null || value === undefined) {
|
|
77
|
+
throw new Error("A NavigationTreeItem must be rendered inside a NavigationTree.");
|
|
78
|
+
}
|
|
79
|
+
return value;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface NavigationTreeProps<T> extends Omit<
|
|
83
|
+
TreeComponentProps<T>,
|
|
84
|
+
| "selectionMode"
|
|
85
|
+
| "selectionBehavior"
|
|
86
|
+
| "selectedKeys"
|
|
87
|
+
| "defaultSelectedKeys"
|
|
88
|
+
| "onSelectionChange"
|
|
89
|
+
| "disallowEmptySelection"
|
|
90
|
+
| "disabledBehavior"
|
|
91
|
+
| "keyboardNavigationBehavior"
|
|
92
|
+
| "escapeKeyBehavior"
|
|
93
|
+
| "shouldSelectOnPressUp"
|
|
94
|
+
| "renderEmptyState"
|
|
95
|
+
| "onAction"
|
|
96
|
+
> {
|
|
97
|
+
/** The route that is showing, matched against each item's `href`. */
|
|
98
|
+
selectedRoute?: string | null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function NavigationTree<T>(props: NavigationTreeProps<T>) {
|
|
102
|
+
const treeRef = makeRef<HTMLDivElement>();
|
|
103
|
+
const value: NavigationTreeContextValue = {
|
|
104
|
+
selectedRoute: () => props.selectedRoute,
|
|
105
|
+
synced: { route: undefined },
|
|
106
|
+
element: treeRef,
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
return provide(
|
|
110
|
+
NavigationTreeContext,
|
|
111
|
+
() => value,
|
|
112
|
+
() => (
|
|
113
|
+
<Tree
|
|
114
|
+
{...omit(props, "selectedRoute", "ref")}
|
|
115
|
+
ref={mergeRefs(treeRef.set, props.ref)}
|
|
116
|
+
selectionMode="none"
|
|
117
|
+
keyboardNavigationBehavior="tab"
|
|
118
|
+
/>
|
|
119
|
+
),
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// ---------------------------------------------------------------------------
|
|
124
|
+
// Items
|
|
125
|
+
// ---------------------------------------------------------------------------
|
|
126
|
+
|
|
127
|
+
/** The key of the item whose `href` is the route, hidden or not. */
|
|
128
|
+
function keyForRoute(collection: TreeCollection<unknown>, route: string): Key | null {
|
|
129
|
+
for (const key of collection.getKeys()) {
|
|
130
|
+
if (collection.getItem(key)?.props?.href === route) return key;
|
|
131
|
+
}
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const ancestorsCache = new WeakMap<
|
|
136
|
+
TreeCollection<unknown>,
|
|
137
|
+
{ route: string; ancestors: Set<Key> }
|
|
138
|
+
>();
|
|
139
|
+
|
|
140
|
+
/** Every row above the route's item, cached so each row does not walk the tree. */
|
|
141
|
+
function ancestorsOfRoute(collection: TreeCollection<unknown>, route: string): Set<Key> {
|
|
142
|
+
const cached = ancestorsCache.get(collection);
|
|
143
|
+
if (cached?.route === route) return cached.ancestors;
|
|
144
|
+
|
|
145
|
+
const ancestors = new Set<Key>();
|
|
146
|
+
const match = keyForRoute(collection, route);
|
|
147
|
+
let node = match === null ? null : collection.getItem(match);
|
|
148
|
+
while (node?.parentKey !== undefined && !ancestors.has(node.parentKey)) {
|
|
149
|
+
ancestors.add(node.parentKey);
|
|
150
|
+
node = collection.getItem(node.parentKey);
|
|
151
|
+
}
|
|
152
|
+
ancestorsCache.set(collection, { route, ancestors });
|
|
153
|
+
return ancestors;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
interface NavigationTreeItemContextValue {
|
|
157
|
+
href: Accessor<string | undefined>;
|
|
158
|
+
isCurrent: Accessor<boolean>;
|
|
159
|
+
isCurrentAncestor: Accessor<boolean>;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const NavigationTreeItemContext = createContext<NavigationTreeItemContextValue | null>(null);
|
|
163
|
+
|
|
164
|
+
function useNavigationTreeItem(): NavigationTreeItemContextValue {
|
|
165
|
+
const value = getContext(NavigationTreeItemContext);
|
|
166
|
+
if (value === null || value === undefined) {
|
|
167
|
+
throw new Error("This must be rendered inside a NavigationTreeItem.");
|
|
168
|
+
}
|
|
169
|
+
return value;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export interface NavigationTreeItemProps extends Omit<
|
|
173
|
+
TreeItemComponentProps,
|
|
174
|
+
"onAction" | "allowsArrowNavigation" | "focusMode"
|
|
175
|
+
> {}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* One row. Its `href` is its item's, read from the data like a tree row's.
|
|
179
|
+
*
|
|
180
|
+
* The current row is scrolled to the middle of the tree when the tree is its
|
|
181
|
+
* own scrolling container, and never scrolls the page around it.
|
|
182
|
+
*/
|
|
183
|
+
export function NavigationTreeItem(props: NavigationTreeItemProps) {
|
|
184
|
+
const navigation = useNavigationTree();
|
|
185
|
+
const root = useTree();
|
|
186
|
+
const node = useTreeNode();
|
|
187
|
+
const rowRef = makeRef<HTMLDivElement>();
|
|
188
|
+
|
|
189
|
+
const href = (): string | undefined => {
|
|
190
|
+
const given = node.props?.href;
|
|
191
|
+
return typeof given === "string" && given.length > 0 ? given : undefined;
|
|
192
|
+
};
|
|
193
|
+
const isCurrent = (): boolean => href() !== undefined && href() === navigation.selectedRoute();
|
|
194
|
+
const isCurrentAncestor = (): boolean => {
|
|
195
|
+
const route = navigation.selectedRoute();
|
|
196
|
+
if (route === null || route === undefined) return false;
|
|
197
|
+
return ancestorsOfRoute(root.state.collection(), route).has(node.key);
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
createEffect(
|
|
201
|
+
() => ({
|
|
202
|
+
route: navigation.selectedRoute(),
|
|
203
|
+
collection: root.state.collection(),
|
|
204
|
+
}),
|
|
205
|
+
({ route, collection }) => {
|
|
206
|
+
if (route === null || route === undefined || navigation.synced.route === route) return;
|
|
207
|
+
const key = keyForRoute(collection, route);
|
|
208
|
+
if (key === null) return;
|
|
209
|
+
navigation.synced.route = route;
|
|
210
|
+
untrack(() => root.state.selectionManager().setFocusedKey(collection.closestShowingKey(key)));
|
|
211
|
+
},
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
if (!isServer) {
|
|
215
|
+
createEffect(
|
|
216
|
+
() => (isCurrent() ? rowRef() : null),
|
|
217
|
+
(element) => {
|
|
218
|
+
if (element === null) return;
|
|
219
|
+
const scroller = scrollParents(element.parentElement ?? element, true)[0];
|
|
220
|
+
const tree = untrack(navigation.element);
|
|
221
|
+
if (scroller instanceof HTMLElement && tree !== null && contains(tree, scroller)) {
|
|
222
|
+
scrollIntoView(scroller, element, { block: "center" });
|
|
223
|
+
}
|
|
224
|
+
},
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const item: NavigationTreeItemContextValue = { href, isCurrent, isCurrentAncestor };
|
|
229
|
+
|
|
230
|
+
return provide(
|
|
231
|
+
NavigationTreeItemContext,
|
|
232
|
+
() => item,
|
|
233
|
+
() => (
|
|
234
|
+
<TreeItem
|
|
235
|
+
{...omit(props, "children", "ref")}
|
|
236
|
+
ref={mergeRefs(rowRef.set, props.ref)}
|
|
237
|
+
focusMode={href() === undefined ? "row" : "child"}
|
|
238
|
+
allowsArrowNavigation
|
|
239
|
+
data-current={isCurrent() || undefined}
|
|
240
|
+
data-current-ancestor={isCurrentAncestor() || undefined}
|
|
241
|
+
>
|
|
242
|
+
{props.children}
|
|
243
|
+
</TreeItem>
|
|
244
|
+
),
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** A row's state, with where it stands relative to the current route. */
|
|
249
|
+
export interface NavigationTreeItemRenderState extends TreeItemRenderState {
|
|
250
|
+
isCurrent: Accessor<boolean>;
|
|
251
|
+
isCurrentAncestor: Accessor<boolean>;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
export interface NavigationTreeItemContentProps {
|
|
255
|
+
children?: Child | ((row: NavigationTreeItemRenderState) => Child);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** The content of a row, with the row's state and its relation to the route. */
|
|
259
|
+
export function NavigationTreeItemContent(props: NavigationTreeItemContentProps) {
|
|
260
|
+
const row = useTreeItem();
|
|
261
|
+
const item = useNavigationTreeItem();
|
|
262
|
+
const state: NavigationTreeItemRenderState = {
|
|
263
|
+
...row.render,
|
|
264
|
+
isCurrent: item.isCurrent,
|
|
265
|
+
isCurrentAncestor: item.isCurrentAncestor,
|
|
266
|
+
};
|
|
267
|
+
const children = props.children;
|
|
268
|
+
return (
|
|
269
|
+
<TreeItemContent>
|
|
270
|
+
{typeof children === "function" && children.length > 0
|
|
271
|
+
? (children as (state: NavigationTreeItemRenderState) => Child)(state)
|
|
272
|
+
: children}
|
|
273
|
+
</TreeItemContent>
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export interface NavigationTreeLinkProps extends StyleProps {
|
|
278
|
+
children?: Child;
|
|
279
|
+
"aria-label"?: string;
|
|
280
|
+
ref?: RefTarget<HTMLElement>;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* The row's link, to its item's `href`, with `aria-current="page"` on the
|
|
285
|
+
* current one.
|
|
286
|
+
*
|
|
287
|
+
* On an item with no `href` it is still a link to a screen reader, and
|
|
288
|
+
* pressing it opens and closes the row: a heading that groups pages.
|
|
289
|
+
*/
|
|
290
|
+
export function NavigationTreeLink(props: NavigationTreeLinkProps) {
|
|
291
|
+
const domRef = makeRef<HTMLElement>();
|
|
292
|
+
const row = useTreeItem();
|
|
293
|
+
const item = useNavigationTreeItem();
|
|
294
|
+
const options = fromProps(cells(props));
|
|
295
|
+
const asAnchor = item.href() !== undefined;
|
|
296
|
+
|
|
297
|
+
const { linkProps, isPressed } = link(
|
|
298
|
+
{
|
|
299
|
+
...options,
|
|
300
|
+
elementType: asAnchor ? "a" : "span",
|
|
301
|
+
href: item.href,
|
|
302
|
+
"aria-current": () => (item.isCurrent() ? "page" : undefined),
|
|
303
|
+
onPress: () => {
|
|
304
|
+
if (item.href() === undefined && row.render.hasChildItems()) {
|
|
305
|
+
row.render.state.toggleKey(row.render.id);
|
|
306
|
+
}
|
|
307
|
+
},
|
|
308
|
+
},
|
|
309
|
+
domRef,
|
|
310
|
+
);
|
|
311
|
+
const { hoverProps, isHovered } = hover({});
|
|
312
|
+
const { focusProps, isFocused, isFocusVisible } = focusRing();
|
|
313
|
+
|
|
314
|
+
const elementProps = mergeProps(
|
|
315
|
+
linkProps,
|
|
316
|
+
hoverProps,
|
|
317
|
+
focusProps,
|
|
318
|
+
filterDOMProps(options, { global: true }),
|
|
319
|
+
styleProps(cells(props)),
|
|
320
|
+
{
|
|
321
|
+
"data-current": item.isCurrent,
|
|
322
|
+
"data-pressed": isPressed,
|
|
323
|
+
"data-hovered": isHovered,
|
|
324
|
+
"data-focused": isFocused,
|
|
325
|
+
"data-focus-visible": isFocusVisible,
|
|
326
|
+
},
|
|
327
|
+
);
|
|
328
|
+
|
|
329
|
+
const ref = mergeRefs(domRef.set, props.ref);
|
|
330
|
+
return asAnchor ? (
|
|
331
|
+
<a {...elementProps} ref={ref}>
|
|
332
|
+
{props.children}
|
|
333
|
+
</a>
|
|
334
|
+
) : (
|
|
335
|
+
<span {...elementProps} ref={ref}>
|
|
336
|
+
{props.children}
|
|
337
|
+
</span>
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// ---------------------------------------------------------------------------
|
|
342
|
+
// Sections
|
|
343
|
+
// ---------------------------------------------------------------------------
|
|
344
|
+
|
|
345
|
+
export interface NavigationTreeSectionProps extends TreeSectionProps {}
|
|
346
|
+
|
|
347
|
+
/** A group of top-level rows; see {@link TreeSection}. */
|
|
348
|
+
export function NavigationTreeSection(props: NavigationTreeSectionProps) {
|
|
349
|
+
return <TreeSection {...props} />;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
export interface NavigationTreeHeaderProps extends TreeHeaderProps {}
|
|
353
|
+
|
|
354
|
+
/** The header of a {@link NavigationTreeSection}. */
|
|
355
|
+
export function NavigationTreeHeader(props: NavigationTreeHeaderProps) {
|
|
356
|
+
return <TreeHeader {...props} />;
|
|
357
|
+
}
|