@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/radio.tsx CHANGED
@@ -14,6 +14,7 @@
14
14
  */
15
15
 
16
16
  import {
17
+ cells,
17
18
  type Accessor,
18
19
  type Child,
19
20
  type Incoming,
@@ -32,10 +33,13 @@ import { focusable } from "./interactions/focusable.ts";
32
33
  import { press, type ElementRef, type PressEvent } from "./interactions/press.ts";
33
34
  import { useLocale } from "./i18n.ts";
34
35
  import { field, type FieldOptions } from "./label.ts";
35
- import { formReset, toggleElementType, type ToggleElementType } from "./toggle.ts";
36
- import { toggleInput } from "./toggle-input.tsx";
36
+ import { focusInput, formReset, toggleElementType, type ToggleElementType } from "./toggle.ts";
37
+ import { SelectionIndicatorProvider } from "./selection-indicator.tsx";
38
+ import { SharedElementTransition } from "./shared-element-transition.tsx";
39
+ import { toggleInput, withHelpText } from "./toggle-input.tsx";
37
40
  import {
38
41
  fieldValidation,
42
+ formValidation,
39
43
  type FormValidationState,
40
44
  type ValidateFunction,
41
45
  type ValidationBehavior,
@@ -74,7 +78,8 @@ export interface RadioGroupStateOptions {
74
78
 
75
79
  export interface RadioGroupState {
76
80
  selectedValue: Accessor<string | null>;
77
- setSelectedValue(value: string): void;
81
+ /** `null` clears the choice, which only a form reset does. */
82
+ setSelectedValue(value: string | null): void;
78
83
  /**
79
84
  * The radio Tab last landed on.
80
85
  *
@@ -88,6 +93,8 @@ export interface RadioGroupState {
88
93
  isReadOnly: Accessor<boolean>;
89
94
  isRequired: Accessor<boolean>;
90
95
  isInvalid: Accessor<boolean>;
96
+ /** The group's own behaviour, or the form's, which every radio follows. */
97
+ validationBehavior: Accessor<ValidationBehavior | undefined>;
91
98
  /**
92
99
  * On the STATE rather than on `radioGroup`, because each radio reads it:
93
100
  * an invalid group marks every button in it, and a radio has no way back to
@@ -112,6 +119,7 @@ export function radioGroupState(options: RadioGroupStateOptions): RadioGroupStat
112
119
 
113
120
  const {
114
121
  state: validation,
122
+ behavior,
115
123
  isInvalid,
116
124
  errors,
117
125
  errorMessage,
@@ -138,6 +146,7 @@ export function radioGroupState(options: RadioGroupStateOptions): RadioGroupStat
138
146
  isReadOnly,
139
147
  isRequired: () => access(options.isRequired) === true,
140
148
  isInvalid,
149
+ validationBehavior: behavior,
141
150
  validation,
142
151
  errors,
143
152
  errorMessage,
@@ -146,6 +155,8 @@ export function radioGroupState(options: RadioGroupStateOptions): RadioGroupStat
146
155
 
147
156
  export interface RadioGroupOptions extends FieldOptions {
148
157
  name?: MaybeAccessor<string | undefined>;
158
+ /** The id of the `<form>` the radios belong to, when they are not inside it. */
159
+ form?: MaybeAccessor<string | undefined>;
149
160
  /** @default "vertical" */
150
161
  orientation?: MaybeAccessor<"horizontal" | "vertical" | undefined>;
151
162
  isDisabled?: MaybeAccessor<boolean | undefined>;
@@ -160,19 +171,30 @@ export interface RadioGroupResult {
160
171
  errorMessageProps: DOMProps;
161
172
  /** The shared `name`, which is what makes them one group to the platform. */
162
173
  name: Accessor<string>;
174
+ /** For each radio's `aria-describedby`: the group's help text and error. */
175
+ descriptionIds: Accessor<string | undefined>;
163
176
  }
164
177
 
178
+ /** What a radio needs from its group's hook, which only the hook knows. */
179
+ interface RadioGroupData {
180
+ form: Accessor<string | undefined>;
181
+ descriptionIds: Accessor<string | undefined>;
182
+ }
183
+
184
+ const radioGroupData = new WeakMap<RadioGroupState, RadioGroupData>();
185
+
165
186
  export function radioGroup(options: RadioGroupOptions, state: RadioGroupState): RadioGroupResult {
166
187
  const locale = useLocale();
167
188
  const groupName = id(options.name);
168
189
 
169
- const { labelProps, fieldProps, descriptionProps, errorMessageProps } = field({
190
+ const { labelProps, fieldProps, descriptionProps, errorMessageProps, descriptionIds } = field({
170
191
  ...options,
171
192
  // A radiogroup is not a labelable element.
172
193
  labelElementType: "span",
173
194
  isInvalid: state.isInvalid,
174
195
  errorMessage: state.errorMessage,
175
196
  });
197
+ radioGroupData.set(state, { form: () => access(options.form), descriptionIds });
176
198
 
177
199
  const orientation = (): "horizontal" | "vertical" => access(options.orientation) ?? "vertical";
178
200
 
@@ -229,6 +251,7 @@ export function radioGroup(options: RadioGroupOptions, state: RadioGroupState):
229
251
 
230
252
  return {
231
253
  name: groupName,
254
+ descriptionIds,
232
255
  radioGroupProps: mergeProps(filterDOMProps(options, { labelable: true }), fieldProps, {
233
256
  role: "radiogroup",
234
257
  "aria-orientation": orientation,
@@ -255,6 +278,8 @@ export function radioGroup(options: RadioGroupOptions, state: RadioGroupState):
255
278
  export interface RadioOptions {
256
279
  value: MaybeAccessor<string>;
257
280
  isDisabled?: MaybeAccessor<boolean | undefined>;
281
+ /** Help text for this one radio, which describes it when there is any. */
282
+ description?: MaybeAccessor<unknown>;
258
283
  autoFocus?: MaybeAccessor<boolean | undefined>;
259
284
  children?: MaybeAccessor<unknown>;
260
285
  "aria-label"?: MaybeAccessor<string | undefined>;
@@ -268,6 +293,8 @@ export interface RadioOptions {
268
293
  export interface RadioResult {
269
294
  labelProps: DOMProps;
270
295
  inputProps: DOMProps;
296
+ /** For this radio's help text, outside its label. */
297
+ descriptionProps: DOMProps;
271
298
  isSelected: Accessor<boolean>;
272
299
  isPressed: Accessor<boolean>;
273
300
  isDisabled: Accessor<boolean>;
@@ -308,6 +335,7 @@ export function radio(
308
335
  return;
309
336
  }
310
337
  options.onPress?.(event);
338
+ focusInput(ref);
311
339
  },
312
340
  });
313
341
 
@@ -322,9 +350,17 @@ export function radio(
322
350
  ref,
323
351
  );
324
352
 
325
- formReset(ref, state.defaultValue(), (next) => {
326
- if (next !== null) state.setSelectedValue(next);
327
- });
353
+ formReset(ref, state.defaultValue(), (next) => state.setSelectedValue(next));
354
+
355
+ const group = radioGroupData.get(state);
356
+ const isNative = (): boolean => state.validationBehavior() === "native";
357
+ formValidation({ validationBehavior: state.validationBehavior }, state.validation, ref);
358
+
359
+ const descriptionId = id();
360
+ const describedBy = (): string | undefined =>
361
+ [present(access(options.description)) ? descriptionId() : undefined, group?.descriptionIds()]
362
+ .filter((part): part is string => part !== undefined)
363
+ .join(" ") || undefined;
328
364
 
329
365
  /**
330
366
  * The roving tab index: exactly one radio in the group is tabbable.
@@ -356,11 +392,15 @@ export function radio(
356
392
  tabIndex,
357
393
  checked: isSelected,
358
394
  value,
395
+ form: () => group?.form(),
359
396
  disabled: isDisabled,
360
- required: () => state.isRequired(),
397
+ // The browser refuses the submit for `required`, so only when the page
398
+ // asked for that.
399
+ required: () => state.isRequired() && isNative(),
361
400
  // On each radio as well as on the group: a screen reader moving
362
401
  // through the radios reads theirs, not the group's.
363
402
  "aria-invalid": () => state.isInvalid() || undefined,
403
+ "aria-describedby": describedBy,
364
404
  onChange: (event: Event) => {
365
405
  event.stopPropagation();
366
406
  state.setSelectedValue(value());
@@ -371,12 +411,30 @@ export function radio(
371
411
  },
372
412
  ),
373
413
 
414
+ descriptionProps: { id: descriptionId },
374
415
  isSelected,
375
416
  isPressed: () => isPressed() || labelPressed(),
376
417
  isDisabled,
377
418
  };
378
419
  }
379
420
 
421
+ /**
422
+ * A radio's children, told whether it is the chosen one, so a
423
+ * `<SelectionIndicator>` among them is shown only there.
424
+ */
425
+ function offeringSelection(
426
+ isSelected: Accessor<boolean>,
427
+ children: Incoming<{ children?: Child }>["children"],
428
+ ): Incoming<{ children?: Child }>["children"] {
429
+ return () => (
430
+ <SelectionIndicatorProvider isSelected={isSelected()}>{children}</SelectionIndicatorProvider>
431
+ );
432
+ }
433
+
434
+ function present(value: unknown): boolean {
435
+ return value !== undefined && value !== null && value !== false && value !== "";
436
+ }
437
+
380
438
  // ---------------------------------------------------------------------------
381
439
  // Components
382
440
  // ---------------------------------------------------------------------------
@@ -405,6 +463,8 @@ export interface RadioGroupComponentProps extends StyleProps {
405
463
  value?: string | null;
406
464
  defaultValue?: string | null;
407
465
  name?: string;
466
+ /** The id of the `<form>` the radios belong to, when they are not inside it. */
467
+ form?: string;
408
468
  orientation?: "horizontal" | "vertical";
409
469
  isDisabled?: boolean;
410
470
  isReadOnly?: boolean;
@@ -427,12 +487,12 @@ export interface RadioGroupComponentProps extends StyleProps {
427
487
  * </RadioGroup>
428
488
  * ```
429
489
  */
430
- export function RadioGroup(props: Incoming<RadioGroupComponentProps>) {
431
- const options = fromProps(props);
490
+ export function RadioGroup(props: RadioGroupComponentProps) {
491
+ const options = fromProps(cells(props));
432
492
  const state = radioGroupState({
433
493
  ...(options as RadioGroupStateOptions),
434
- validate: callback(props.validate),
435
- validationBehavior: () => props.validationBehavior?.(),
494
+ validate: callback(cells(props).validate),
495
+ validationBehavior: () => props.validationBehavior,
436
496
  });
437
497
  const { radioGroupProps, labelProps, descriptionProps, errorMessageProps, name } = radioGroup(
438
498
  options,
@@ -448,21 +508,23 @@ export function RadioGroup(props: Incoming<RadioGroupComponentProps>) {
448
508
  const outerProps = mergeProps(
449
509
  radioGroupProps,
450
510
  filterDOMProps(options, { global: true }),
451
- styleProps(props),
511
+ styleProps(cells(props)),
452
512
  {
453
513
  "data-disabled": state.isDisabled,
454
514
  "data-readonly": state.isReadOnly,
455
515
  "data-invalid": state.isInvalid,
456
516
  "data-required": state.isRequired,
457
- "data-orientation": () => props.orientation?.() ?? "vertical",
458
- "data-testid": () => props["data-testid"]?.(),
517
+ "data-orientation": () => props.orientation ?? "vertical",
518
+ "data-testid": () => props["data-testid"],
459
519
  },
460
520
  );
461
521
 
462
522
  return (
463
523
  <div {...outerProps}>
464
524
  <span {...labelProps}>{props.label}</span>
465
- {props.children}
525
+ {/* One transition for the group, so an indicator drawn in the chosen
526
+ radio slides to the next one rather than appearing there. */}
527
+ <SharedElementTransition>{props.children}</SharedElementTransition>
466
528
  <span {...descriptionProps}>{props.description}</span>
467
529
  <span {...errorMessageProps}>
468
530
  {() => (state.isInvalid() ? (state.errorMessage() as Child) : null)}
@@ -481,6 +543,8 @@ export interface RadioComponentProps extends StyleProps {
481
543
  */
482
544
  elementType?: ToggleElementType;
483
545
  isDisabled?: boolean;
546
+ /** Help text for this radio, rendered after it and announced with it. */
547
+ description?: Child;
484
548
  autoFocus?: boolean;
485
549
  "aria-label"?: string;
486
550
  "aria-labelledby"?: string;
@@ -516,7 +580,7 @@ export function Radio(props: Incoming<RadioComponentProps>) {
516
580
  const options = fromProps(props);
517
581
  const group = useRadioGroup();
518
582
 
519
- const { labelProps, inputProps, isSelected, isPressed, isDisabled } = radio(
583
+ const { labelProps, inputProps, descriptionProps, isSelected, isPressed, isDisabled } = radio(
520
584
  options as RadioOptions,
521
585
  group.state,
522
586
  inputRef,
@@ -549,19 +613,29 @@ export function Radio(props: Incoming<RadioComponentProps>) {
549
613
  "data-focused": isFocused,
550
614
  "data-focus-visible": isFocusVisible,
551
615
  "data-disabled": isDisabled,
616
+ "data-readonly": group.state.isReadOnly,
552
617
  "data-invalid": group.state.isInvalid,
618
+ "data-required": group.state.isRequired,
553
619
  "data-testid": () => props["data-testid"]?.(),
554
620
  },
555
621
  );
556
622
 
557
- return toggleInput({
558
- elementType: toggleElementType(props.elementType?.(), props.children),
559
- outerProps,
560
- inputProps,
561
- focusProps,
562
- inputRef,
563
- ref: props.ref?.(),
564
- children: props.children,
565
- isDisabled,
566
- });
623
+ return withHelpText(
624
+ toggleInput({
625
+ elementType: toggleElementType(props.elementType?.(), props.children),
626
+ outerProps,
627
+ inputProps,
628
+ focusProps,
629
+ inputRef,
630
+ ref: props.ref?.(),
631
+ children: offeringSelection(isSelected, props.children),
632
+ isDisabled,
633
+ }),
634
+ {
635
+ descriptionProps,
636
+ errorMessageProps: {},
637
+ description: () => props.description?.(),
638
+ errorMessage: () => undefined,
639
+ },
640
+ );
567
641
  }