@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
@@ -19,11 +19,13 @@
19
19
  */
20
20
 
21
21
  import {
22
+ cells,
22
23
  type Accessor,
23
24
  type Child,
24
25
  For,
25
26
  createContext,
26
27
  createEffect,
28
+ createMemo,
27
29
  getContext,
28
30
  getOwner,
29
31
  type Incoming,
@@ -116,7 +118,7 @@ export interface DisclosureGroupState {
116
118
  export function disclosureGroupState(
117
119
  options: DisclosureGroupStateOptions = {},
118
120
  ): DisclosureGroupState {
119
- const [expandedKeys, setExpandedKeys] = controllable<Set<Key>>(
121
+ const [stored, setExpandedKeys] = controllable<Set<Key>>(
120
122
  () => {
121
123
  const given = access(options.expandedKeys);
122
124
  return given === undefined ? undefined : new Set(given);
@@ -126,6 +128,12 @@ export function disclosureGroupState(
126
128
  );
127
129
 
128
130
  const allowsMultiple = (): boolean => access(options.allowsMultiple) === true;
131
+ const expandedKeys = createMemo((): Set<Key> => {
132
+ const keys = stored();
133
+ if (allowsMultiple() || keys.size <= 1) return keys;
134
+ const [first] = keys;
135
+ return new Set([first as Key]);
136
+ });
129
137
  const isLocked = (key: Key): boolean =>
130
138
  !allowsMultiple() && access(options.isCollapsible) === false && expandedKeys().has(key);
131
139
 
@@ -206,20 +214,55 @@ export function disclosure(options: DisclosureOptions, state: DisclosureState):
206
214
  * one with no transition, and one under `prefers-reduced-motion` — so
207
215
  * nothing flashes open on the first render.
208
216
  */
209
- let hasOpened = false;
217
+ let previous: boolean | null = null;
210
218
  createEffect(
211
219
  () => [access(options.panelRef) as HTMLElement | null, state.isExpanded()] as const,
212
220
  ([panel, expanded]) => {
213
221
  if (panel === null) return undefined;
214
222
 
223
+ const first = previous === null;
224
+ previous = expanded;
215
225
  const hide = (): void => panel.setAttribute("hidden", "until-found");
226
+ const size = (width: string, height: string): void => {
227
+ panel.style.setProperty("--disclosure-panel-width", width);
228
+ panel.style.setProperty("--disclosure-panel-height", height);
229
+ };
230
+ const measured = (): void => size(`${panel.scrollWidth}px`, `${panel.scrollHeight}px`);
231
+
216
232
  if (expanded) {
217
- hasOpened = true;
218
233
  panel.removeAttribute("hidden");
234
+ if (first || typeof panel.getAnimations !== "function") {
235
+ size("auto", "auto");
236
+ return undefined;
237
+ }
238
+ // Pixels, so a transition has two lengths to run between; `auto`
239
+ // again once it is done, so the content can grow and shrink.
240
+ measured();
241
+ let settled = false;
242
+ Promise.all(panel.getAnimations().map((animation) => animation.finished)).then(
243
+ () => {
244
+ if (!settled) size("auto", "auto");
245
+ },
246
+ () => {},
247
+ );
248
+ return () => {
249
+ settled = true;
250
+ };
251
+ }
252
+
253
+ if (first) {
254
+ size("0px", "0px");
255
+ hide();
219
256
  return undefined;
220
257
  }
221
258
 
222
- const closing = hasOpened ? exitDuration(panel) : 0;
259
+ measured();
260
+ // Layout is forced between the two lengths, or the browser coalesces
261
+ // them and the transition starts from zero.
262
+ panel.getBoundingClientRect();
263
+ size("0px", "0px");
264
+
265
+ const closing = exitDuration(panel);
223
266
  if (closing <= 0) {
224
267
  hide();
225
268
  return undefined;
@@ -252,6 +295,9 @@ export function disclosure(options: DisclosureOptions, state: DisclosureState):
252
295
  id: panelId,
253
296
  role: "group",
254
297
  "aria-labelledby": triggerId,
298
+ // At once, while `hidden` waits for the collapse to finish: a panel on
299
+ // its way out is still painted, and still read, unless it says so.
300
+ "aria-hidden": () => !state.isExpanded() || undefined,
255
301
  // On the SERVER the attribute is all there is: the effects above have
256
302
  // not run, and a panel rendered without it would flash open.
257
303
  hidden: () => (isServer ? (state.isExpanded() ? undefined : "until-found") : undefined),
@@ -341,19 +387,19 @@ export interface DisclosureButtonComponentProps extends StyleProps {
341
387
  ref?: RefTarget<HTMLButtonElement>;
342
388
  }
343
389
 
344
- export function DisclosureButton(props: Incoming<DisclosureButtonComponentProps>) {
390
+ export function DisclosureButton(props: DisclosureButtonComponentProps) {
345
391
  const domRef = makeRef<HTMLButtonElement>();
346
392
  const value = useDisclosure();
347
393
 
348
394
  const { buttonProps, isPressed } = button(
349
- { ...(fromProps(props) as ButtonOptions), ...value.buttonProps },
395
+ { ...(fromProps(cells(props)) as ButtonOptions), ...value.buttonProps },
350
396
  domRef,
351
397
  );
352
- const { hoverProps, isHovered } = hover({ isDisabled: () => props.isDisabled?.() });
398
+ const { hoverProps, isHovered } = hover({ isDisabled: () => props.isDisabled });
353
399
  const { focusProps, isFocusVisible } = focusRing();
354
400
  // `button()` delivers `onClick` itself; bound on the element as well, a
355
401
  // caller's handler runs twice for every mouse click.
356
- const { onClick: _, ...forwarded } = filterDOMProps(fromProps(props), { global: true });
402
+ const { onClick: _, ...forwarded } = filterDOMProps(fromProps(cells(props)), { global: true });
357
403
 
358
404
  const elementProps = mergeProps(
359
405
  buttonProps,
@@ -361,7 +407,7 @@ export function DisclosureButton(props: Incoming<DisclosureButtonComponentProps>
361
407
  hoverProps,
362
408
  focusProps,
363
409
  forwarded,
364
- styleProps(props),
410
+ styleProps(cells(props)),
365
411
  {
366
412
  "aria-disabled": () => value.isLocked?.() === true || undefined,
367
413
  "data-expanded": value.state.isExpanded,
@@ -370,8 +416,8 @@ export function DisclosureButton(props: Incoming<DisclosureButtonComponentProps>
370
416
  "data-pressed": isPressed,
371
417
  "data-hovered": isHovered,
372
418
  "data-focus-visible": isFocusVisible,
373
- "data-disabled": () => props.isDisabled?.() === true || value.isDisabled(),
374
- "data-testid": () => props["data-testid"]?.(),
419
+ "data-disabled": () => props.isDisabled === true || value.isDisabled(),
420
+ "data-testid": () => props["data-testid"],
375
421
  },
376
422
  );
377
423
 
@@ -381,7 +427,7 @@ export function DisclosureButton(props: Incoming<DisclosureButtonComponentProps>
381
427
  };
382
428
 
383
429
  return (
384
- <button {...elementProps} ref={mergeRefs(domRef.set, enlist, props.ref?.())}>
430
+ <button {...elementProps} ref={mergeRefs(domRef.set, enlist, props.ref)}>
385
431
  {props.children}
386
432
  </button>
387
433
  );
@@ -389,6 +435,8 @@ export function DisclosureButton(props: Incoming<DisclosureButtonComponentProps>
389
435
 
390
436
  export interface DisclosurePanelComponentProps extends StyleProps {
391
437
  children?: Child;
438
+ /** @default "group" */
439
+ role?: string;
392
440
  ref?: RefTarget<HTMLDivElement>;
393
441
  }
394
442
 
@@ -396,24 +444,28 @@ export interface DisclosurePanelComponentProps extends StyleProps {
396
444
  * The panel. Still in the document while collapsed, so find-in-page can reach
397
445
  * it and open the disclosure it belongs to.
398
446
  */
399
- export function DisclosurePanel(props: Incoming<DisclosurePanelComponentProps>) {
447
+ export function DisclosurePanel(props: DisclosurePanelComponentProps) {
400
448
  const value = useDisclosure();
449
+ const { focusProps, isFocusVisible } = focusRing({ within: true });
401
450
 
402
451
  const elementProps = mergeProps(
403
452
  value.panelProps,
404
- filterDOMProps(fromProps(props), { global: true, labelable: true }),
405
- styleProps(props),
453
+ filterDOMProps(fromProps(cells(props)), { global: true, labelable: true }),
454
+ styleProps(cells(props)),
455
+ focusProps,
406
456
  {
457
+ role: () => props.role ?? "group",
458
+ "data-focus-visible-within": isFocusVisible,
407
459
  "data-expanded": value.state.isExpanded,
408
460
  "data-state": () => (value.state.isExpanded() ? "open" : "closed"),
409
461
  "data-orientation": () => value.group?.orientation(),
410
462
  "data-disabled": value.isDisabled,
411
- "data-testid": () => props["data-testid"]?.(),
463
+ "data-testid": () => props["data-testid"],
412
464
  },
413
465
  );
414
466
 
415
467
  return (
416
- <div {...elementProps} ref={mergeRefs(value.panelRef.set, props.ref?.())}>
468
+ <div {...elementProps} ref={mergeRefs(value.panelRef.set, props.ref)}>
417
469
  {props.children}
418
470
  </div>
419
471
  );
@@ -507,19 +559,19 @@ export interface DisclosureGroupComponentProps<T> extends StyleProps, ItemAccess
507
559
  * practices call that optional; it is here because a long accordion is
508
560
  * otherwise a Tab stop per section with nothing faster.
509
561
  */
510
- export function DisclosureGroup<T>(props: Incoming<DisclosureGroupComponentProps<T>>) {
511
- const options = fromProps(props as unknown as Incoming<Record<string, unknown>>);
562
+ export function DisclosureGroup<T>(props: DisclosureGroupComponentProps<T>) {
563
+ const options = fromProps(cells(props) as unknown as Incoming<Record<string, unknown>>);
512
564
  const state = disclosureGroupState({
513
565
  ...(options as DisclosureGroupStateOptions),
514
- onExpandedChange: (keys) => props.onExpandedChange?.()?.(keys),
566
+ onExpandedChange: (keys) => props.onExpandedChange?.(keys),
515
567
  });
516
568
 
517
- const items = props.items;
569
+ const items = cells(props).items;
518
570
  const collection = (): ListCollection<T> =>
519
- buildCollection(items?.() ?? [], props as unknown as ItemAccessors<T>);
571
+ buildCollection(items?.() ?? [], cells(props) as unknown as ItemAccessors<T>);
520
572
 
521
573
  const locale = useLocale();
522
- const orientation = (): "vertical" | "horizontal" => props.orientation?.() ?? "vertical";
574
+ const orientation = (): "vertical" | "horizontal" => props.orientation ?? "vertical";
523
575
  const group: DisclosureGroupContextValue = { state, orientation };
524
576
 
525
577
  const owner = getOwner();
@@ -530,7 +582,7 @@ export function DisclosureGroup<T>(props: Incoming<DisclosureGroupComponentProps
530
582
  const from = (event.target as Element | null)?.closest("button") ?? null;
531
583
  if (root === null || from === null || headerGroups.get(from) !== group) return;
532
584
 
533
- const rtl = (props.dir?.() ?? locale().direction) === "rtl";
585
+ const rtl = (props.dir ?? locale().direction) === "rtl";
534
586
  const horizontal = orientation() === "horizontal";
535
587
  const forward = horizontal ? (rtl ? "ArrowLeft" : "ArrowRight") : "ArrowDown";
536
588
  const back = horizontal ? (rtl ? "ArrowRight" : "ArrowLeft") : "ArrowUp";
@@ -560,13 +612,13 @@ export function DisclosureGroup<T>(props: Incoming<DisclosureGroupComponentProps
560
612
  };
561
613
 
562
614
  const elementProps = mergeProps(
563
- filterDOMProps(fromProps(props), { global: true }),
564
- styleProps(props),
615
+ filterDOMProps(fromProps(cells(props)), { global: true }),
616
+ styleProps(cells(props)),
565
617
  {
566
618
  onKeyDown,
567
619
  "data-orientation": orientation,
568
620
  "data-disabled": state.isDisabled,
569
- "data-testid": () => props["data-testid"]?.(),
621
+ "data-testid": () => props["data-testid"],
570
622
  },
571
623
  );
572
624
 
@@ -575,7 +627,7 @@ export function DisclosureGroup<T>(props: Incoming<DisclosureGroupComponentProps
575
627
  // key its sections by the outer section's node.
576
628
  if (owner !== null) install(owner, DisclosureNodeContext, () => null);
577
629
  return (
578
- <div {...elementProps} ref={mergeRefs(props.ref?.())}>
630
+ <div {...elementProps} ref={mergeRefs(props.ref)}>
579
631
  {props.children}
580
632
  </div>
581
633
  );
@@ -584,7 +636,7 @@ export function DisclosureGroup<T>(props: Incoming<DisclosureGroupComponentProps
584
636
  const render = props.children as unknown as (item: T) => Child;
585
637
 
586
638
  return (
587
- <div {...elementProps} ref={mergeRefs(props.ref?.())}>
639
+ <div {...elementProps} ref={mergeRefs(props.ref)}>
588
640
  <For each={() => [...collection()]}>
589
641
  {(node: Node<T>) => {
590
642
  const rowOwner = getOwner();
@@ -0,0 +1,125 @@
1
+ /** Copy, cut and paste, through the system clipboard. */
2
+
3
+ import { createEffect } from "@barqjs/core";
4
+ import { isServer } from "@barqjs/web";
5
+ import { focused } from "../interactions/focus-events.ts";
6
+ import { access, type DOMProps, type MaybeAccessor } from "../utils.ts";
7
+ import { readFromDataTransfer, writeToDataTransfer } from "./data.ts";
8
+ import type { DragItem, DropItem } from "./types.ts";
9
+
10
+ export interface ClipboardOptions {
11
+ /** What a copy or a cut writes. Without it, nothing can be copied. */
12
+ getItems?: (details: { action: "cut" | "copy" }) => DragItem[];
13
+ onCopy?: () => void;
14
+ /** Without it, nothing can be cut. */
15
+ onCut?: () => void;
16
+ /** Without it, nothing can be pasted. */
17
+ onPaste?: (items: DropItem[]) => void;
18
+ isDisabled?: MaybeAccessor<boolean | undefined>;
19
+ }
20
+
21
+ export interface ClipboardResult {
22
+ clipboardProps: DOMProps;
23
+ }
24
+
25
+ type ClipboardListener = (event: ClipboardEvent) => void;
26
+
27
+ const clipboardHandlers = new Map<
28
+ string,
29
+ { listener: (event: Event) => void; handlers: Set<ClipboardListener> }
30
+ >();
31
+
32
+ /** One document listener per event, shared by every element that wants it. */
33
+ function addClipboardListener(type: string, handler: ClipboardListener): () => void {
34
+ let entry = clipboardHandlers.get(type);
35
+ if (entry === undefined) {
36
+ const handlers = new Set<ClipboardListener>();
37
+ const listener = (event: Event): void => {
38
+ for (const each of handlers) each(event as ClipboardEvent);
39
+ };
40
+ entry = { listener, handlers };
41
+ clipboardHandlers.set(type, entry);
42
+ document.addEventListener(type, listener);
43
+ }
44
+ const registered = entry;
45
+ registered.handlers.add(handler);
46
+ return () => {
47
+ registered.handlers.delete(handler);
48
+ if (registered.handlers.size === 0) {
49
+ document.removeEventListener(type, registered.listener);
50
+ clipboardHandlers.delete(type);
51
+ }
52
+ };
53
+ }
54
+
55
+ /**
56
+ * Copy, cut and paste for a focusable element, through the system clipboard.
57
+ *
58
+ * The clipboard events fire on the document whatever is focused, so they are
59
+ * heard there and acted on only while this element has focus.
60
+ */
61
+ export function clipboard(options: ClipboardOptions): ClipboardResult {
62
+ let isFocused = false;
63
+ const { focusProps } = focused({
64
+ onFocusChange: (value) => {
65
+ isFocused = value;
66
+ },
67
+ });
68
+
69
+ // Each `before*` event enables the menu item in Safari.
70
+ const onBeforeCopy = (event: ClipboardEvent): void => {
71
+ if (isFocused && options.getItems !== undefined) event.preventDefault();
72
+ };
73
+ const onCopy = (event: ClipboardEvent): void => {
74
+ if (!isFocused || options.getItems === undefined) return;
75
+ event.preventDefault();
76
+ if (event.clipboardData === null) return;
77
+ writeToDataTransfer(event.clipboardData, options.getItems({ action: "copy" }));
78
+ options.onCopy?.();
79
+ };
80
+ const onBeforeCut = (event: ClipboardEvent): void => {
81
+ if (isFocused && options.onCut !== undefined && options.getItems !== undefined) {
82
+ event.preventDefault();
83
+ }
84
+ };
85
+ const onCut = (event: ClipboardEvent): void => {
86
+ if (!isFocused || options.onCut === undefined || options.getItems === undefined) return;
87
+ event.preventDefault();
88
+ if (event.clipboardData === null) return;
89
+ writeToDataTransfer(event.clipboardData, options.getItems({ action: "cut" }));
90
+ options.onCut();
91
+ };
92
+ // The types are not readable here, so Paste is enabled whatever is on the
93
+ // clipboard.
94
+ const onBeforePaste = (event: ClipboardEvent): void => {
95
+ if (isFocused && options.onPaste !== undefined) event.preventDefault();
96
+ };
97
+ const onPaste = (event: ClipboardEvent): void => {
98
+ if (!isFocused || options.onPaste === undefined) return;
99
+ event.preventDefault();
100
+ if (event.clipboardData === null) return;
101
+ options.onPaste(readFromDataTransfer(event.clipboardData));
102
+ };
103
+
104
+ if (!isServer) {
105
+ createEffect(
106
+ () => access(options.isDisabled) === true,
107
+ (disabled) => {
108
+ if (disabled) return undefined;
109
+ const undo = [
110
+ addClipboardListener("beforecopy", onBeforeCopy),
111
+ addClipboardListener("copy", onCopy),
112
+ addClipboardListener("beforecut", onBeforeCut),
113
+ addClipboardListener("cut", onCut),
114
+ addClipboardListener("beforepaste", onBeforePaste),
115
+ addClipboardListener("paste", onPaste),
116
+ ];
117
+ return () => {
118
+ for (const fn of undo) fn();
119
+ };
120
+ },
121
+ );
122
+ }
123
+
124
+ return { clipboardProps: focusProps };
125
+ }