@barqjs/aria 0.2.0-next.0 → 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.
Files changed (78) hide show
  1. package/README.md +24 -13
  2. package/package.json +127 -37
  3. package/src/autocomplete.tsx +581 -0
  4. package/src/breadcrumbs.tsx +21 -20
  5. package/src/button.tsx +151 -25
  6. package/src/calendar.tsx +1019 -92
  7. package/src/checkbox.tsx +214 -52
  8. package/src/collections.ts +88 -5
  9. package/src/color.ts +1580 -23
  10. package/src/colorpicker.tsx +698 -153
  11. package/src/colorswatchpicker.tsx +240 -0
  12. package/src/combobox.tsx +635 -113
  13. package/src/command.tsx +78 -59
  14. package/src/date.ts +31 -9
  15. package/src/datefield.tsx +1451 -336
  16. package/src/datepicker.tsx +465 -114
  17. package/src/dialog.tsx +302 -115
  18. package/src/disclosure.tsx +81 -29
  19. package/src/dnd/clipboard.ts +125 -0
  20. package/src/dnd/collection.ts +1265 -0
  21. package/src/dnd/data.ts +409 -0
  22. package/src/dnd/drag-drop.ts +801 -0
  23. package/src/dnd/index.ts +133 -0
  24. package/src/dnd/manager.ts +639 -0
  25. package/src/dnd/strings.ts +1279 -0
  26. package/src/dnd/targets.ts +726 -0
  27. package/src/dnd/types.ts +212 -0
  28. package/src/dom.ts +3 -6
  29. package/src/drag-and-drop.tsx +669 -0
  30. package/src/dropzone.tsx +208 -0
  31. package/src/file-trigger.tsx +103 -0
  32. package/src/form.tsx +18 -18
  33. package/src/gridlist.tsx +912 -121
  34. package/src/group.tsx +146 -0
  35. package/src/index.ts +402 -0
  36. package/src/interactions/focus-events.ts +13 -5
  37. package/src/interactions/keyboard.ts +195 -2
  38. package/src/interactions/modality.ts +18 -6
  39. package/src/interactions/open-link.ts +46 -13
  40. package/src/interactions/press.ts +35 -18
  41. package/src/label.ts +18 -6
  42. package/src/link.tsx +146 -50
  43. package/src/listbox.tsx +615 -89
  44. package/src/live.ts +4 -0
  45. package/src/menu.tsx +182 -89
  46. package/src/menubar.tsx +21 -17
  47. package/src/navigation-tree.tsx +357 -0
  48. package/src/numberfield.tsx +164 -60
  49. package/src/overlay-arrow.tsx +157 -0
  50. package/src/overlays.ts +364 -62
  51. package/src/platform.ts +2 -1
  52. package/src/preview-trigger.tsx +582 -0
  53. package/src/progress-bar-context.ts +15 -0
  54. package/src/questionnaire.tsx +109 -104
  55. package/src/radio.tsx +101 -27
  56. package/src/select.tsx +225 -145
  57. package/src/selection-indicator.tsx +74 -0
  58. package/src/selection.ts +73 -16
  59. package/src/shared-element-transition.tsx +316 -0
  60. package/src/sheet.tsx +1461 -0
  61. package/src/slider.tsx +139 -44
  62. package/src/switch.tsx +47 -21
  63. package/src/table.tsx +0 -0
  64. package/src/tabs.tsx +198 -89
  65. package/src/tag.tsx +217 -32
  66. package/src/textarea.tsx +186 -0
  67. package/src/textfield.tsx +103 -37
  68. package/src/toast-components.tsx +589 -0
  69. package/src/toggle-button-group.tsx +175 -0
  70. package/src/toggle-input.tsx +38 -0
  71. package/src/toggle.ts +68 -5
  72. package/src/tokenfield.tsx +1673 -0
  73. package/src/toolbar.tsx +15 -10
  74. package/src/tooltip.tsx +46 -29
  75. package/src/tree.tsx +2075 -0
  76. package/src/utils.ts +30 -1
  77. package/src/validation.ts +79 -22
  78. package/src/virtualizer.tsx +186 -23
package/src/menubar.tsx CHANGED
@@ -25,6 +25,7 @@
25
25
  */
26
26
 
27
27
  import {
28
+ cells,
28
29
  type Accessor,
29
30
  type Child,
30
31
  createContext,
@@ -153,16 +154,16 @@ export interface MenubarComponentProps extends StyleProps {
153
154
  * </Menubar>
154
155
  * ```
155
156
  */
156
- export function Menubar(props: Incoming<MenubarComponentProps>) {
157
+ export function Menubar(props: MenubarComponentProps) {
157
158
  const locale = useLocale();
158
159
  const entries = new Map<string, MenubarEntry>();
159
160
  const [registered, setRegistered] = createSignal(0, { ownedWrite: true });
160
161
  const [focused, setFocused] = createSignal<string | null>(null);
161
162
 
162
163
  const [value, setValue] = controllable<string>(
163
- () => props.value?.(),
164
- () => props.defaultValue?.() ?? "",
165
- (next) => props.onValueChange?.()?.(next),
164
+ () => props.value,
165
+ () => props.defaultValue ?? "",
166
+ (next) => props.onValueChange?.(next),
166
167
  );
167
168
 
168
169
  const ordered = (): MenubarEntry[] =>
@@ -224,7 +225,7 @@ export function Menubar(props: Incoming<MenubarComponentProps>) {
224
225
  const at = all.findIndex((entry) => entry.value === from);
225
226
  const to = at + (step === "next" ? 1 : -1);
226
227
  if (to >= 0 && to < all.length) return all[to] ?? null;
227
- if (props.loop?.() === false) return null;
228
+ if (props.loop === false) return null;
228
229
  return all[(to + all.length) % all.length] ?? null;
229
230
  },
230
231
  matching: (from, character) => {
@@ -232,7 +233,10 @@ export function Menubar(props: Incoming<MenubarComponentProps>) {
232
233
  forget = setTimeout(() => {
233
234
  typed = "";
234
235
  }, TYPEAHEAD_RESET);
235
- typed += character.toLowerCase();
236
+ // The same letter again steps on to the next trigger starting with it,
237
+ // rather than searching for the letter twice over.
238
+ const letter = character.toLowerCase();
239
+ typed = typed === "" || typed === letter ? letter : typed + letter;
236
240
 
237
241
  const all = ordered();
238
242
  const at = all.findIndex((entry) => entry.value === from);
@@ -266,19 +270,19 @@ export function Menubar(props: Incoming<MenubarComponentProps>) {
266
270
  };
267
271
 
268
272
  const elementProps = mergeProps(
269
- filterDOMProps(fromProps(props), { global: true }),
270
- styleProps(props),
273
+ filterDOMProps(fromProps(cells(props)), { global: true }),
274
+ styleProps(cells(props)),
271
275
  {
272
276
  role: "menubar",
273
- "aria-label": () => props["aria-label"]?.(),
274
- "aria-labelledby": () => props["aria-labelledby"]?.(),
277
+ "aria-label": () => props["aria-label"],
278
+ "aria-labelledby": () => props["aria-labelledby"],
275
279
  "data-orientation": "horizontal",
276
- "data-testid": () => props["data-testid"]?.(),
280
+ "data-testid": () => props["data-testid"],
277
281
  },
278
282
  );
279
283
 
280
284
  return (
281
- <div {...elementProps} ref={mergeRefs(props.ref?.())}>
285
+ <div {...elementProps} ref={mergeRefs(props.ref)}>
282
286
  {provide(
283
287
  MenubarContext,
284
288
  () => bar,
@@ -556,19 +560,19 @@ export interface MenubarTriggerComponentProps extends StyleProps {
556
560
  }
557
561
 
558
562
  /** The button in the bar that opens the enclosing {@link MenubarMenu}. */
559
- export function MenubarTrigger(props: Incoming<MenubarTriggerComponentProps>) {
560
- const options = fromProps(props);
563
+ export function MenubarTrigger(props: MenubarTriggerComponentProps) {
564
+ const options = fromProps(cells(props));
561
565
  const { triggerProps, ref } = menubarTrigger(options);
562
566
 
563
567
  const elementProps = mergeProps(
564
568
  triggerProps,
565
569
  filterDOMProps(options, { global: true }),
566
- styleProps(props),
567
- { "data-testid": () => props["data-testid"]?.() },
570
+ styleProps(cells(props)),
571
+ { "data-testid": () => props["data-testid"] },
568
572
  );
569
573
 
570
574
  return (
571
- <button {...elementProps} ref={mergeRefs(ref, props.ref?.())}>
575
+ <button {...elementProps} ref={mergeRefs(ref, props.ref)}>
572
576
  {props.children}
573
577
  </button>
574
578
  );
@@ -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
+ }