@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/button.tsx CHANGED
@@ -9,17 +9,21 @@
9
9
  * cancelled by dragging off, and arrives late on touch.
10
10
  */
11
11
 
12
- import { type Accessor, type Child, type Incoming } from "@barqjs/core";
12
+ import { cells, createEffect, provide, type Accessor, type Child } from "@barqjs/core";
13
13
  import { ref as makeRef, mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
14
14
  import { focusRing } from "./focus.ts";
15
15
  import { focusable, type FocusableOptions } from "./interactions/focusable.ts";
16
- import { hover } from "./interactions/hover.ts";
16
+ import { hover, type HoverEvent } from "./interactions/hover.ts";
17
17
  import { press, type ElementRef, type PressEvent } from "./interactions/press.ts";
18
+ import { ProgressBarContext } from "./progress-bar-context.ts";
19
+ import { announce } from "./live.ts";
20
+ import { SelectionIndicatorProvider } from "./selection-indicator.tsx";
18
21
  import { controllable } from "./utils.ts";
19
22
  import {
20
23
  access,
21
24
  filterDOMProps,
22
25
  fromProps,
26
+ id,
23
27
  mergeProps,
24
28
  styleProps,
25
29
  triggerSlot,
@@ -29,6 +33,7 @@ import {
29
33
  } from "./utils.ts";
30
34
 
31
35
  export interface ButtonOptions extends FocusableOptions {
36
+ id?: MaybeAccessor<string | undefined>;
32
37
  isDisabled?: MaybeAccessor<boolean | undefined>;
33
38
  /**
34
39
  * What the element actually is.
@@ -47,8 +52,20 @@ export interface ButtonOptions extends FocusableOptions {
47
52
  target?: MaybeAccessor<string | undefined>;
48
53
  rel?: MaybeAccessor<string | undefined>;
49
54
  form?: MaybeAccessor<string | undefined>;
55
+ /** Where a submit button sends the form, in place of the form's own `action`. */
56
+ formAction?: MaybeAccessor<string | undefined>;
57
+ formEncType?: MaybeAccessor<string | undefined>;
58
+ formMethod?: MaybeAccessor<string | undefined>;
59
+ formNoValidate?: MaybeAccessor<boolean | undefined>;
60
+ formTarget?: MaybeAccessor<string | undefined>;
50
61
  name?: MaybeAccessor<string | undefined>;
51
62
  value?: MaybeAccessor<string | undefined>;
63
+ /**
64
+ * Keep a `tabindex` of -1 while disabled, so a control that moves focus to
65
+ * the button itself (a number field's stepper) can still do so.
66
+ */
67
+ allowFocusWhenDisabled?: MaybeAccessor<boolean | undefined>;
68
+ "aria-disabled"?: MaybeAccessor<boolean | "true" | "false" | undefined>;
52
69
  "aria-label"?: MaybeAccessor<string | undefined>;
53
70
  "aria-labelledby"?: MaybeAccessor<string | undefined>;
54
71
  "aria-describedby"?: MaybeAccessor<string | undefined>;
@@ -90,7 +107,14 @@ export function button(options: ButtonOptions, ref?: ElementRef): ButtonResult {
90
107
  const isDisabled = (): boolean => access(options.isDisabled) === true;
91
108
 
92
109
  const { pressProps, isPressed } = press({ ...options, ref });
93
- const { focusableProps } = focusable(options, ref);
110
+ const { focusableProps: focusableBase } = focusable(options, ref);
111
+ const focusableProps = mergeProps(focusableBase, {
112
+ tabIndex: () =>
113
+ access(options.allowFocusWhenDisabled) === true && isDisabled()
114
+ ? -1
115
+ : access(focusableBase.tabIndex as MaybeAccessor<number | undefined>),
116
+ });
117
+ const isFormControl = (): boolean => elementType() === "button" || elementType() === "input";
94
118
 
95
119
  const nativeProps: DOMProps = {
96
120
  type: () =>
@@ -103,14 +127,20 @@ export function button(options: ButtonOptions, ref?: ElementRef): ButtonResult {
103
127
  elementType() === "button" || elementType() === "input" ? isDisabled() : undefined,
104
128
  role: () => (elementType() === "button" ? undefined : "button"),
105
129
  "aria-disabled": () =>
106
- elementType() === "button" || elementType() === "input"
130
+ access(options["aria-disabled"]) ??
131
+ (elementType() === "button" || elementType() === "input"
107
132
  ? undefined
108
- : isDisabled() || undefined,
133
+ : isDisabled() || undefined),
109
134
  // A disabled link must not navigate, and `disabled` does not exist on `<a>`.
110
135
  href: () => (elementType() === "a" && !isDisabled() ? access(options.href) : undefined),
111
136
  target: () => (elementType() === "a" ? access(options.target) : undefined),
112
137
  rel: () => (elementType() === "a" ? access(options.rel) : undefined),
113
138
  form: () => access(options.form),
139
+ formAction: () => (isFormControl() ? access(options.formAction) : undefined),
140
+ formEncType: () => (isFormControl() ? access(options.formEncType) : undefined),
141
+ formMethod: () => (isFormControl() ? access(options.formMethod) : undefined),
142
+ formNoValidate: () => (isFormControl() ? access(options.formNoValidate) : undefined),
143
+ formTarget: () => (isFormControl() ? access(options.formTarget) : undefined),
114
144
  name: () => access(options.name),
115
145
  value: () => access(options.value),
116
146
  };
@@ -213,12 +243,25 @@ export function toggleButton(
213
243
  export interface ButtonComponentProps extends StyleProps {
214
244
  children?: Child;
215
245
  isDisabled?: boolean;
246
+ /**
247
+ * Waiting on the action the button started. Presses and hover are ignored,
248
+ * focus stays, a submit button stops submitting, and a focused button
249
+ * announces the change. A `<ProgressBar>` inside joins the button's name.
250
+ */
251
+ isPending?: boolean;
216
252
  type?: "button" | "submit" | "reset";
217
253
  autoFocus?: boolean;
218
254
  excludeFromTabOrder?: boolean;
255
+ id?: string;
219
256
  form?: string;
257
+ formAction?: string;
258
+ formEncType?: string;
259
+ formMethod?: string;
260
+ formNoValidate?: boolean;
261
+ formTarget?: string;
220
262
  name?: string;
221
263
  value?: string;
264
+ "aria-disabled"?: boolean | "true" | "false";
222
265
  "aria-label"?: string;
223
266
  "aria-labelledby"?: string;
224
267
  "aria-describedby"?: string;
@@ -229,20 +272,46 @@ export interface ButtonComponentProps extends StyleProps {
229
272
  onPress?: (event: PressEvent) => void;
230
273
  onPressStart?: (event: PressEvent) => void;
231
274
  onPressEnd?: (event: PressEvent) => void;
275
+ onPressUp?: (event: PressEvent) => void;
232
276
  onPressChange?: (isPressed: boolean) => void;
277
+ onHoverStart?: (event: HoverEvent) => void;
278
+ onHoverEnd?: (event: HoverEvent) => void;
279
+ onHoverChange?: (isHovered: boolean) => void;
280
+ onFocus?: (event: FocusEvent) => void;
281
+ onBlur?: (event: FocusEvent) => void;
233
282
  onClick?: (event: MouseEvent) => void;
234
283
  }
235
284
 
236
285
  /**
237
286
  * `press` already delivers `onClick`, including the one a keyboard or a touch
238
- * activation would have produced. Bound on the element as well, a caller's
239
- * handler runs twice for every mouse click.
287
+ * activation would have produced, and `focusable` delivers `onFocus` and
288
+ * `onBlur`. Bound on the element as well, a caller's handler runs twice.
240
289
  */
241
290
  function withoutClick(props: DOMProps): DOMProps {
242
- const { onClick: _, ...rest } = props;
291
+ const { onClick: _click, onFocus: _focus, onBlur: _blur, ...rest } = props;
243
292
  return rest;
244
293
  }
245
294
 
295
+ /** What a pending button still answers: focus, and what a tooltip listens to. */
296
+ const LIVE_WHILE_PENDING =
297
+ /Focus|Blur|Hover|Pointer(Enter|Leave|Over|Out)|Mouse(Enter|Leave|Over|Out)/;
298
+
299
+ function inertWhilePending(props: DOMProps, isPending: () => boolean): DOMProps {
300
+ const result: DOMProps = {};
301
+ for (const key in props) {
302
+ const value = props[key];
303
+ if (key.startsWith("on") && typeof value === "function" && !LIVE_WHILE_PENDING.test(key)) {
304
+ const handler = value as (event: Event) => void;
305
+ result[key] = (event: Event): void => {
306
+ if (!isPending()) handler(event);
307
+ };
308
+ } else {
309
+ result[key] = value;
310
+ }
311
+ }
312
+ return result;
313
+ }
314
+
246
315
  /**
247
316
  * A button, with the interaction states as data attributes.
248
317
  *
@@ -254,7 +323,7 @@ function withoutClick(props: DOMProps): DOMProps {
254
323
  * <Button onPress={() => save()}>Save</Button>
255
324
  * ```
256
325
  */
257
- export function Button(props: Incoming<ButtonComponentProps>) {
326
+ export function Button(props: ButtonComponentProps) {
258
327
  const domRef = makeRef<HTMLButtonElement>();
259
328
  // What a tooltip, a menu or a dialog wrapped around this button wants on it,
260
329
  // merged into the button's OPTIONS rather than onto its element.
@@ -266,36 +335,86 @@ export function Button(props: Incoming<ButtonComponentProps>) {
266
335
  // saw a value and kept it. Through the options both arrive where `button()`
267
336
  // already knows what to do with them, and `mergeProps` chains the two
268
337
  // `onPress` handlers instead of one replacing the other.
269
- const options = mergeProps(fromProps(props), triggerSlot().props) as ButtonOptions;
338
+ const options = mergeProps(fromProps(cells(props)), triggerSlot().props) as ButtonOptions;
270
339
  const slot = triggerSlot();
271
340
 
341
+ const isPending = (): boolean => props.isPending === true;
342
+
272
343
  const { buttonProps, isPressed } = button(options, domRef);
273
- const { hoverProps, isHovered } = hover({ isDisabled: options.isDisabled });
344
+ const { hoverProps, isHovered } = hover({
345
+ isDisabled: () => access(options.isDisabled) === true || isPending(),
346
+ onHoverStart: (event) => props.onHoverStart?.(event),
347
+ onHoverEnd: (event) => props.onHoverEnd?.(event),
348
+ onHoverChange: (hovered) => props.onHoverChange?.(hovered),
349
+ });
274
350
  const { focusProps, isFocused, isFocusVisible } = focusRing();
275
351
 
352
+ const buttonId = id(options.id);
353
+ const progressId = id();
354
+ // `aria-labelledby` outranks `aria-label` in the name computation, so a
355
+ // labelled button that wants its progress bar read has to name itself.
356
+ const labelledBy = (): string | undefined => {
357
+ const given = access(options["aria-labelledby"]);
358
+ if (!isPending()) return given;
359
+ if (given) return `${given} ${progressId()}`;
360
+ if (access(options["aria-label"])) {
361
+ return `${buttonId()} ${progressId()}`;
362
+ }
363
+ return given;
364
+ };
365
+
366
+ let wasPending = isPending();
367
+ createEffect(
368
+ () => ({ pending: isPending(), focused: isFocused(), by: labelledBy() || buttonId() }),
369
+ ({ pending, focused, by }) => {
370
+ if (focused && pending !== wasPending) announce({ "aria-labelledby": by }, "assertive");
371
+ wasPending = pending;
372
+ },
373
+ );
374
+
276
375
  const elementProps = mergeProps(
277
- buttonProps,
376
+ inertWhilePending(buttonProps, isPending),
278
377
  hoverProps,
279
378
  focusProps,
280
379
  withoutClick(filterDOMProps(options, { global: true })),
281
- styleProps(props),
380
+ styleProps(cells(props)),
282
381
  {
283
- "data-pressed": isPressed,
382
+ id: buttonId,
383
+ // A pending submit button must not submit, implicitly from a text field
384
+ // on Enter included.
385
+ type: () => {
386
+ const type = access(buttonProps.type as MaybeAccessor<string | undefined>);
387
+ return type === "submit" && isPending() ? "button" : type;
388
+ },
389
+ "aria-labelledby": labelledBy,
390
+ "aria-disabled": () =>
391
+ isPending()
392
+ ? "true"
393
+ : access(buttonProps["aria-disabled"] as MaybeAccessor<string | boolean | undefined>),
394
+ "data-pressed": () => isPressed() && !isPending(),
284
395
  "data-hovered": isHovered,
285
396
  "data-focused": isFocused,
286
397
  "data-focus-visible": isFocusVisible,
287
- "data-disabled": () => props.isDisabled?.() === true,
398
+ "data-disabled": () => props.isDisabled === true,
399
+ "data-pending": isPending,
288
400
  },
289
401
  );
290
402
 
291
403
  return (
292
- <button {...elementProps} ref={mergeRefs(domRef.set, slot.ref, props.ref?.())}>
293
- {props.children}
404
+ <button {...elementProps} ref={mergeRefs(domRef.set, slot.ref, props.ref)}>
405
+ {provide(
406
+ ProgressBarContext,
407
+ () => ({ id: progressId }),
408
+ () => props.children as unknown,
409
+ )}
294
410
  </button>
295
411
  );
296
412
  }
297
413
 
298
- export interface ToggleButtonComponentProps extends Omit<ButtonComponentProps, "aria-pressed"> {
414
+ export interface ToggleButtonComponentProps extends Omit<
415
+ ButtonComponentProps,
416
+ "aria-pressed" | "isPending"
417
+ > {
299
418
  isSelected?: boolean;
300
419
  defaultSelected?: boolean;
301
420
  onChange?: (isSelected: boolean) => void;
@@ -306,16 +425,21 @@ export interface ToggleButtonComponentProps extends Omit<ButtonComponentProps, "
306
425
  * <ToggleButton defaultSelected onChange={(on) => bold.set(on)}>Bold</ToggleButton>
307
426
  * ```
308
427
  */
309
- export function ToggleButton(props: Incoming<ToggleButtonComponentProps>) {
428
+ export function ToggleButton(props: ToggleButtonComponentProps) {
310
429
  const domRef = makeRef<HTMLButtonElement>();
311
430
  // See `Button` for why the slot goes into the options and not onto the
312
431
  // element.
313
- const options = mergeProps(fromProps(props), triggerSlot().props) as ToggleButtonOptions;
432
+ const options = mergeProps(fromProps(cells(props)), triggerSlot().props) as ToggleButtonOptions;
314
433
  const slot = triggerSlot();
315
434
 
316
435
  const state = toggleState(options);
317
436
  const { buttonProps, isPressed } = toggleButton(options, state, domRef);
318
- const { hoverProps, isHovered } = hover({ isDisabled: options.isDisabled });
437
+ const { hoverProps, isHovered } = hover({
438
+ isDisabled: options.isDisabled,
439
+ onHoverStart: (event) => props.onHoverStart?.(event),
440
+ onHoverEnd: (event) => props.onHoverEnd?.(event),
441
+ onHoverChange: (hovered) => props.onHoverChange?.(hovered),
442
+ });
319
443
  const { focusProps, isFocused, isFocusVisible } = focusRing();
320
444
 
321
445
  const elementProps = mergeProps(
@@ -323,20 +447,22 @@ export function ToggleButton(props: Incoming<ToggleButtonComponentProps>) {
323
447
  hoverProps,
324
448
  focusProps,
325
449
  withoutClick(filterDOMProps(options, { global: true })),
326
- styleProps(props),
450
+ styleProps(cells(props)),
327
451
  {
328
452
  "data-pressed": isPressed,
329
453
  "data-hovered": isHovered,
330
454
  "data-focused": isFocused,
331
455
  "data-focus-visible": isFocusVisible,
332
456
  "data-selected": state.isSelected,
333
- "data-disabled": () => props.isDisabled?.() === true,
457
+ "data-disabled": () => props.isDisabled === true,
334
458
  },
335
459
  );
336
460
 
337
461
  return (
338
- <button {...elementProps} ref={mergeRefs(domRef.set, slot.ref, props.ref?.())}>
339
- {props.children}
462
+ <button {...elementProps} ref={mergeRefs(domRef.set, slot.ref, props.ref)}>
463
+ <SelectionIndicatorProvider isSelected={state.isSelected()}>
464
+ {props.children}
465
+ </SelectionIndicatorProvider>
340
466
  </button>
341
467
  );
342
468
  }