@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.
Files changed (103) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +304 -2
  3. package/package.json +437 -4
  4. package/src/autocomplete.tsx +581 -0
  5. package/src/breadcrumbs.tsx +297 -0
  6. package/src/button.tsx +468 -0
  7. package/src/calendar.tsx +2316 -0
  8. package/src/checkbox.tsx +732 -0
  9. package/src/collections.ts +1059 -0
  10. package/src/color.ts +2119 -0
  11. package/src/colorpicker.tsx +2023 -0
  12. package/src/colorswatchpicker.tsx +240 -0
  13. package/src/combobox.tsx +3003 -0
  14. package/src/command.tsx +1203 -0
  15. package/src/date.ts +674 -0
  16. package/src/datefield.tsx +2066 -0
  17. package/src/datepicker.tsx +907 -0
  18. package/src/dialog.tsx +1077 -0
  19. package/src/disclosure.tsx +711 -0
  20. package/src/dnd/clipboard.ts +125 -0
  21. package/src/dnd/collection.ts +1265 -0
  22. package/src/dnd/data.ts +409 -0
  23. package/src/dnd/drag-drop.ts +801 -0
  24. package/src/dnd/index.ts +133 -0
  25. package/src/dnd/manager.ts +639 -0
  26. package/src/dnd/strings.ts +1279 -0
  27. package/src/dnd/targets.ts +726 -0
  28. package/src/dnd/types.ts +212 -0
  29. package/src/dom.ts +703 -0
  30. package/src/drag-and-drop.tsx +669 -0
  31. package/src/drawer.ts +674 -0
  32. package/src/dropzone.tsx +208 -0
  33. package/src/file-trigger.tsx +103 -0
  34. package/src/focus.ts +1370 -0
  35. package/src/form.tsx +94 -0
  36. package/src/gridlist.tsx +1354 -0
  37. package/src/group.tsx +146 -0
  38. package/src/i18n.ts +823 -0
  39. package/src/index.ts +1672 -0
  40. package/src/interactions/description.ts +73 -0
  41. package/src/interactions/flags.ts +35 -0
  42. package/src/interactions/focus-events.ts +226 -0
  43. package/src/interactions/focusable.ts +86 -0
  44. package/src/interactions/hover.ts +213 -0
  45. package/src/interactions/interact-outside.ts +123 -0
  46. package/src/interactions/keyboard.ts +298 -0
  47. package/src/interactions/listeners.ts +78 -0
  48. package/src/interactions/long-press.ts +160 -0
  49. package/src/interactions/modality.ts +428 -0
  50. package/src/interactions/move.ts +257 -0
  51. package/src/interactions/open-link.ts +226 -0
  52. package/src/interactions/press.ts +915 -0
  53. package/src/interactions/prevent-focus.ts +102 -0
  54. package/src/interactions/scroll-wheel.ts +43 -0
  55. package/src/interactions/text-selection.ts +77 -0
  56. package/src/interactions.ts +81 -0
  57. package/src/label.ts +140 -0
  58. package/src/link.tsx +477 -0
  59. package/src/listbox.tsx +1247 -0
  60. package/src/live.ts +208 -0
  61. package/src/menu.tsx +2383 -0
  62. package/src/menubar.tsx +579 -0
  63. package/src/navigation-tree.tsx +357 -0
  64. package/src/navigation.ts +735 -0
  65. package/src/numberfield.tsx +942 -0
  66. package/src/otp.ts +430 -0
  67. package/src/overlay-arrow.tsx +157 -0
  68. package/src/overlays.ts +1549 -0
  69. package/src/platform.ts +75 -0
  70. package/src/presence.ts +164 -0
  71. package/src/preview-trigger.tsx +582 -0
  72. package/src/progress-bar-context.ts +15 -0
  73. package/src/questionnaire.tsx +1717 -0
  74. package/src/radio.tsx +641 -0
  75. package/src/register-dom.ts +12 -0
  76. package/src/scroller.ts +1165 -0
  77. package/src/select.tsx +1745 -0
  78. package/src/selection-indicator.tsx +74 -0
  79. package/src/selection.ts +1472 -0
  80. package/src/shared-element-transition.tsx +316 -0
  81. package/src/sheet.tsx +1461 -0
  82. package/src/slider.tsx +1137 -0
  83. package/src/splitter.ts +1152 -0
  84. package/src/static-collection.ts +211 -0
  85. package/src/switch.tsx +212 -0
  86. package/src/table.tsx +2218 -0
  87. package/src/tabs.tsx +1127 -0
  88. package/src/tag.tsx +637 -0
  89. package/src/textarea.tsx +186 -0
  90. package/src/textfield.tsx +483 -0
  91. package/src/toast-components.tsx +589 -0
  92. package/src/toast.ts +658 -0
  93. package/src/toggle-button-group.tsx +175 -0
  94. package/src/toggle-input.tsx +86 -0
  95. package/src/toggle.ts +384 -0
  96. package/src/togglegroup.ts +365 -0
  97. package/src/tokenfield.tsx +1673 -0
  98. package/src/toolbar.tsx +184 -0
  99. package/src/tooltip.tsx +674 -0
  100. package/src/tree.tsx +2075 -0
  101. package/src/utils.ts +787 -0
  102. package/src/validation.ts +504 -0
  103. package/src/virtualizer.tsx +837 -0
package/src/radio.tsx ADDED
@@ -0,0 +1,641 @@
1
+ /**
2
+ * A radio group: one of several, mutually exclusive.
3
+ *
4
+ * The keyboard behaviour is what separates a radio group from a list of
5
+ * checkboxes, and it is not what the name suggests. The whole group is ONE Tab
6
+ * stop, and the arrow keys move between the members AND select as they go —
7
+ * there is no "focus without selecting". Tab therefore lands on the selected
8
+ * radio, or on the first one when nothing is selected.
9
+ *
10
+ * The platform gives all of that to a set of `<input type="radio">` sharing a
11
+ * `name`, which is why these are real radios. What is added is the arrow
12
+ * navigation, which the platform only provides inside a single form, and the
13
+ * roving `tabindex` that makes Tab skip the group once entered.
14
+ */
15
+
16
+ import {
17
+ cells,
18
+ type Accessor,
19
+ type Child,
20
+ type Incoming,
21
+ createContext,
22
+ createSignal,
23
+ getContext,
24
+ getOwner,
25
+ install,
26
+ } from "@barqjs/core";
27
+ import { ref as makeRef, type RefTarget } from "@barqjs/primitives/refs";
28
+ import { ownerWindow, targetElement } from "./dom.ts";
29
+ import { focusableWalker } from "./focus.ts";
30
+ import { focusRing } from "./focus.ts";
31
+ import { hover } from "./interactions/hover.ts";
32
+ import { focusable } from "./interactions/focusable.ts";
33
+ import { press, type ElementRef, type PressEvent } from "./interactions/press.ts";
34
+ import { useLocale } from "./i18n.ts";
35
+ import { field, type FieldOptions } from "./label.ts";
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";
40
+ import {
41
+ fieldValidation,
42
+ formValidation,
43
+ type FormValidationState,
44
+ type ValidateFunction,
45
+ type ValidationBehavior,
46
+ } from "./validation.ts";
47
+ import {
48
+ access,
49
+ callback,
50
+ controllable,
51
+ type DOMProps,
52
+ filterDOMProps,
53
+ fromProps,
54
+ id,
55
+ type MaybeAccessor,
56
+ mergeProps,
57
+ type StyleProps,
58
+ styleProps,
59
+ } from "./utils.ts";
60
+
61
+ export interface RadioGroupStateOptions {
62
+ value?: MaybeAccessor<string | null | undefined>;
63
+ defaultValue?: MaybeAccessor<string | null | undefined>;
64
+ isDisabled?: MaybeAccessor<boolean | undefined>;
65
+ isReadOnly?: MaybeAccessor<boolean | undefined>;
66
+ isRequired?: MaybeAccessor<boolean | undefined>;
67
+ isInvalid?: MaybeAccessor<boolean | undefined>;
68
+ /** What the page thinks of the choice, checked as it changes. */
69
+ validate?: ValidateFunction<string | null>;
70
+ /** @default "aria" */
71
+ validationBehavior?: MaybeAccessor<ValidationBehavior | undefined>;
72
+ /** The caller's own message, which wins over anything validation found. */
73
+ errorMessage?: MaybeAccessor<unknown>;
74
+ /** The form field name, for matching a server error to this group. */
75
+ name?: MaybeAccessor<string | undefined>;
76
+ onChange?: (value: string) => void;
77
+ }
78
+
79
+ export interface RadioGroupState {
80
+ selectedValue: Accessor<string | null>;
81
+ /** `null` clears the choice, which only a form reset does. */
82
+ setSelectedValue(value: string | null): void;
83
+ /**
84
+ * The radio Tab last landed on.
85
+ *
86
+ * With nothing selected, Tab must enter at the first radio; once the user
87
+ * has been inside, it must return to where they were.
88
+ */
89
+ lastFocusedValue: Accessor<string | null>;
90
+ setLastFocusedValue(value: string | null): void;
91
+ defaultValue: Accessor<string | null>;
92
+ isDisabled: Accessor<boolean>;
93
+ isReadOnly: Accessor<boolean>;
94
+ isRequired: Accessor<boolean>;
95
+ isInvalid: Accessor<boolean>;
96
+ /** The group's own behaviour, or the form's, which every radio follows. */
97
+ validationBehavior: Accessor<ValidationBehavior | undefined>;
98
+ /**
99
+ * On the STATE rather than on `radioGroup`, because each radio reads it:
100
+ * an invalid group marks every button in it, and a radio has no way back to
101
+ * the hook.
102
+ */
103
+ validation: FormValidationState;
104
+ errors: Accessor<string[]>;
105
+ /** For `field`, so `aria-describedby` points at whatever is being shown. */
106
+ errorMessage: Accessor<unknown>;
107
+ }
108
+
109
+ export function radioGroupState(options: RadioGroupStateOptions): RadioGroupState {
110
+ const [value, setValue] = controllable<string | null>(
111
+ () => access(options.value),
112
+ () => access(options.defaultValue) ?? null,
113
+ (next) => options.onChange?.(next as string),
114
+ );
115
+
116
+ const [lastFocused, setLastFocused] = createSignal<string | null>(null);
117
+ const isReadOnly = (): boolean => access(options.isReadOnly) === true;
118
+ const isDisabled = (): boolean => access(options.isDisabled) === true;
119
+
120
+ const {
121
+ state: validation,
122
+ behavior,
123
+ isInvalid,
124
+ errors,
125
+ errorMessage,
126
+ } = fieldValidation<string | null>({
127
+ value,
128
+ validate: options.validate,
129
+ validationBehavior: options.validationBehavior,
130
+ isInvalid: options.isInvalid,
131
+ errorMessage: options.errorMessage,
132
+ name: options.name,
133
+ });
134
+
135
+ return {
136
+ selectedValue: value,
137
+ setSelectedValue(next) {
138
+ if (isReadOnly() || isDisabled()) return;
139
+ setValue(next);
140
+ validation.commitValidation();
141
+ },
142
+ lastFocusedValue: lastFocused,
143
+ setLastFocusedValue: (next) => setLastFocused(next),
144
+ defaultValue: () => access(options.defaultValue) ?? null,
145
+ isDisabled,
146
+ isReadOnly,
147
+ isRequired: () => access(options.isRequired) === true,
148
+ isInvalid,
149
+ validationBehavior: behavior,
150
+ validation,
151
+ errors,
152
+ errorMessage,
153
+ };
154
+ }
155
+
156
+ export interface RadioGroupOptions extends FieldOptions {
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>;
160
+ /** @default "vertical" */
161
+ orientation?: MaybeAccessor<"horizontal" | "vertical" | undefined>;
162
+ isDisabled?: MaybeAccessor<boolean | undefined>;
163
+ isReadOnly?: MaybeAccessor<boolean | undefined>;
164
+ isRequired?: MaybeAccessor<boolean | undefined>;
165
+ }
166
+
167
+ export interface RadioGroupResult {
168
+ radioGroupProps: DOMProps;
169
+ labelProps: DOMProps;
170
+ descriptionProps: DOMProps;
171
+ errorMessageProps: DOMProps;
172
+ /** The shared `name`, which is what makes them one group to the platform. */
173
+ name: Accessor<string>;
174
+ /** For each radio's `aria-describedby`: the group's help text and error. */
175
+ descriptionIds: Accessor<string | undefined>;
176
+ }
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
+
186
+ export function radioGroup(options: RadioGroupOptions, state: RadioGroupState): RadioGroupResult {
187
+ const locale = useLocale();
188
+ const groupName = id(options.name);
189
+
190
+ const { labelProps, fieldProps, descriptionProps, errorMessageProps, descriptionIds } = field({
191
+ ...options,
192
+ // A radiogroup is not a labelable element.
193
+ labelElementType: "span",
194
+ isInvalid: state.isInvalid,
195
+ errorMessage: state.errorMessage,
196
+ });
197
+ radioGroupData.set(state, { form: () => access(options.form), descriptionIds });
198
+
199
+ const orientation = (): "horizontal" | "vertical" => access(options.orientation) ?? "vertical";
200
+
201
+ const move = (direction: "next" | "previous", event: KeyboardEvent): boolean => {
202
+ const container = event.currentTarget as Element | null;
203
+ if (container === null) return false;
204
+
205
+ const from = targetElement(event) ?? undefined;
206
+ const walker = focusableWalker(container, {
207
+ from,
208
+ accept: (node) => node instanceof ownerWindow(node).HTMLInputElement && node.type === "radio",
209
+ });
210
+
211
+ let next = direction === "next" ? walker.nextNode() : walker.previousNode();
212
+ if (next === null) {
213
+ // Wrap: the group is a ring, which is what the authoring practices say.
214
+ walker.currentNode = container;
215
+ next = direction === "next" ? walker.nextNode() : walker.last();
216
+ }
217
+ if (next === null) return false;
218
+
219
+ (next as HTMLInputElement).focus();
220
+ state.setSelectedValue((next as HTMLInputElement).value);
221
+ return true;
222
+ };
223
+
224
+ const onKeyDown = (event: KeyboardEvent): void => {
225
+ const rtl = locale().direction === "rtl";
226
+ const horizontal = orientation() !== "vertical";
227
+ let handled = false;
228
+
229
+ switch (event.key) {
230
+ case "ArrowRight":
231
+ handled = move(rtl && horizontal ? "previous" : "next", event);
232
+ break;
233
+ case "ArrowLeft":
234
+ handled = move(rtl && horizontal ? "next" : "previous", event);
235
+ break;
236
+ case "ArrowDown":
237
+ handled = move("next", event);
238
+ break;
239
+ case "ArrowUp":
240
+ handled = move("previous", event);
241
+ break;
242
+ default:
243
+ return;
244
+ }
245
+
246
+ if (handled) {
247
+ event.preventDefault();
248
+ event.stopPropagation();
249
+ }
250
+ };
251
+
252
+ return {
253
+ name: groupName,
254
+ descriptionIds,
255
+ radioGroupProps: mergeProps(filterDOMProps(options, { labelable: true }), fieldProps, {
256
+ role: "radiogroup",
257
+ "aria-orientation": orientation,
258
+ "aria-invalid": () => state.isInvalid() || undefined,
259
+ "aria-readonly": () => state.isReadOnly() || undefined,
260
+ "aria-required": () => state.isRequired() || undefined,
261
+ "aria-disabled": () => state.isDisabled() || undefined,
262
+ onKeyDown,
263
+ // With nothing selected, Tab must enter at the FIRST radio again next
264
+ // time; leaving the group is when that is decided.
265
+ onFocusOut: (event: FocusEvent) => {
266
+ const container = event.currentTarget as Element | null;
267
+ const related = event.relatedTarget as Element | null;
268
+ if (container !== null && related !== null && container.contains(related)) return;
269
+ if (state.selectedValue() === null) state.setLastFocusedValue(null);
270
+ },
271
+ }),
272
+ labelProps,
273
+ descriptionProps,
274
+ errorMessageProps,
275
+ };
276
+ }
277
+
278
+ export interface RadioOptions {
279
+ value: MaybeAccessor<string>;
280
+ isDisabled?: MaybeAccessor<boolean | undefined>;
281
+ /** Help text for this one radio, which describes it when there is any. */
282
+ description?: MaybeAccessor<unknown>;
283
+ autoFocus?: MaybeAccessor<boolean | undefined>;
284
+ children?: MaybeAccessor<unknown>;
285
+ "aria-label"?: MaybeAccessor<string | undefined>;
286
+ "aria-labelledby"?: MaybeAccessor<string | undefined>;
287
+ "aria-describedby"?: MaybeAccessor<string | undefined>;
288
+ onPress?: (event: PressEvent) => void;
289
+ onFocus?: (event: FocusEvent) => void;
290
+ onBlur?: (event: FocusEvent) => void;
291
+ }
292
+
293
+ export interface RadioResult {
294
+ labelProps: DOMProps;
295
+ inputProps: DOMProps;
296
+ /** For this radio's help text, outside its label. */
297
+ descriptionProps: DOMProps;
298
+ isSelected: Accessor<boolean>;
299
+ isPressed: Accessor<boolean>;
300
+ isDisabled: Accessor<boolean>;
301
+ }
302
+
303
+ export function radio(
304
+ options: RadioOptions,
305
+ state: RadioGroupState,
306
+ ref: ElementRef<HTMLInputElement>,
307
+ name: Accessor<string>,
308
+ ): RadioResult {
309
+ const value = (): string => access(options.value);
310
+ const isDisabled = (): boolean => access(options.isDisabled) === true || state.isDisabled();
311
+ const isSelected = (): boolean => state.selectedValue() === value();
312
+
313
+ const { pressProps, isPressed } = press({ isDisabled, onPress: options.onPress });
314
+
315
+ const [labelPressed, setLabelPressed] = createSignal(false);
316
+ const { pressProps: labelPressProps } = press({
317
+ isDisabled: () => isDisabled() || state.isReadOnly(),
318
+ onPressStart: (event) => {
319
+ if (event.pointerType === "keyboard" || event.pointerType === "virtual") {
320
+ event.continuePropagation();
321
+ return;
322
+ }
323
+ setLabelPressed(true);
324
+ },
325
+ onPressEnd: (event) => {
326
+ if (event.pointerType === "keyboard" || event.pointerType === "virtual") {
327
+ event.continuePropagation();
328
+ return;
329
+ }
330
+ setLabelPressed(false);
331
+ },
332
+ onPress: (event) => {
333
+ if (event.pointerType === "keyboard" || event.pointerType === "virtual") {
334
+ event.continuePropagation();
335
+ return;
336
+ }
337
+ options.onPress?.(event);
338
+ focusInput(ref);
339
+ },
340
+ });
341
+
342
+ const { focusableProps } = focusable(
343
+ {
344
+ ...options,
345
+ onFocus: (event) => {
346
+ state.setLastFocusedValue(value());
347
+ options.onFocus?.(event);
348
+ },
349
+ },
350
+ ref,
351
+ );
352
+
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;
364
+
365
+ /**
366
+ * The roving tab index: exactly one radio in the group is tabbable.
367
+ *
368
+ * The selected one when there is a selection; otherwise the one focus last
369
+ * left, or the first. Every other radio is reachable only with the arrows,
370
+ * which is what makes the group a single Tab stop.
371
+ */
372
+ const tabIndex = (): number | undefined => {
373
+ if (isDisabled()) return undefined;
374
+ const selected = state.selectedValue();
375
+ if (selected !== null) return selected === value() ? 0 : -1;
376
+ const lastFocused = state.lastFocusedValue();
377
+ return lastFocused === value() || lastFocused === null ? 0 : -1;
378
+ };
379
+
380
+ return {
381
+ labelProps: mergeProps(labelPressProps, {
382
+ onMouseDown: (event: MouseEvent) => event.preventDefault(),
383
+ }),
384
+
385
+ inputProps: mergeProps(
386
+ filterDOMProps(options, { labelable: true }),
387
+ pressProps,
388
+ focusableProps,
389
+ {
390
+ type: "radio",
391
+ name,
392
+ tabIndex,
393
+ checked: isSelected,
394
+ value,
395
+ form: () => group?.form(),
396
+ disabled: isDisabled,
397
+ // The browser refuses the submit for `required`, so only when the page
398
+ // asked for that.
399
+ required: () => state.isRequired() && isNative(),
400
+ // On each radio as well as on the group: a screen reader moving
401
+ // through the radios reads theirs, not the group's.
402
+ "aria-invalid": () => state.isInvalid() || undefined,
403
+ "aria-describedby": describedBy,
404
+ onChange: (event: Event) => {
405
+ event.stopPropagation();
406
+ state.setSelectedValue(value());
407
+ // The platform already checked this radio and unchecked its
408
+ // siblings; if the state declined, put the DOM back.
409
+ (event.target as HTMLInputElement).checked = isSelected();
410
+ },
411
+ },
412
+ ),
413
+
414
+ descriptionProps: { id: descriptionId },
415
+ isSelected,
416
+ isPressed: () => isPressed() || labelPressed(),
417
+ isDisabled,
418
+ };
419
+ }
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
+
438
+ // ---------------------------------------------------------------------------
439
+ // Components
440
+ // ---------------------------------------------------------------------------
441
+
442
+ interface RadioGroupContextValue {
443
+ state: RadioGroupState;
444
+ name: Accessor<string>;
445
+ }
446
+
447
+ const RadioGroupContext = createContext<RadioGroupContextValue | null>(null);
448
+
449
+ /** The enclosing radio group. Throws outside one, which is a coding error. */
450
+ export function useRadioGroup(): RadioGroupContextValue {
451
+ const value = getContext(RadioGroupContext);
452
+ if (value === null || value === undefined) {
453
+ throw new Error("A Radio must be rendered inside a RadioGroup.");
454
+ }
455
+ return value;
456
+ }
457
+
458
+ export interface RadioGroupComponentProps extends StyleProps {
459
+ children?: Child;
460
+ label?: Child;
461
+ description?: Child;
462
+ errorMessage?: Child;
463
+ value?: string | null;
464
+ defaultValue?: string | null;
465
+ name?: string;
466
+ /** The id of the `<form>` the radios belong to, when they are not inside it. */
467
+ form?: string;
468
+ orientation?: "horizontal" | "vertical";
469
+ isDisabled?: boolean;
470
+ isReadOnly?: boolean;
471
+ isRequired?: boolean;
472
+ isInvalid?: boolean;
473
+ /** What the page thinks of the choice, checked as it changes. */
474
+ validate?: ValidateFunction<string | null>;
475
+ /** @default "aria" */
476
+ validationBehavior?: ValidationBehavior;
477
+ "aria-label"?: string;
478
+ "aria-labelledby"?: string;
479
+ onChange?: (value: string) => void;
480
+ }
481
+
482
+ /**
483
+ * ```tsx
484
+ * <RadioGroup label="Size" defaultValue="m">
485
+ * <Radio value="s">Small</Radio>
486
+ * <Radio value="m">Medium</Radio>
487
+ * </RadioGroup>
488
+ * ```
489
+ */
490
+ export function RadioGroup(props: RadioGroupComponentProps) {
491
+ const options = fromProps(cells(props));
492
+ const state = radioGroupState({
493
+ ...(options as RadioGroupStateOptions),
494
+ validate: callback(cells(props).validate),
495
+ validationBehavior: () => props.validationBehavior,
496
+ });
497
+ const { radioGroupProps, labelProps, descriptionProps, errorMessageProps, name } = radioGroup(
498
+ options,
499
+ state,
500
+ );
501
+
502
+ const owner = getOwner();
503
+ if (owner !== null) {
504
+ const value: RadioGroupContextValue = { state, name };
505
+ install(owner, RadioGroupContext, () => value);
506
+ }
507
+
508
+ const outerProps = mergeProps(
509
+ radioGroupProps,
510
+ filterDOMProps(options, { global: true }),
511
+ styleProps(cells(props)),
512
+ {
513
+ "data-disabled": state.isDisabled,
514
+ "data-readonly": state.isReadOnly,
515
+ "data-invalid": state.isInvalid,
516
+ "data-required": state.isRequired,
517
+ "data-orientation": () => props.orientation ?? "vertical",
518
+ "data-testid": () => props["data-testid"],
519
+ },
520
+ );
521
+
522
+ return (
523
+ <div {...outerProps}>
524
+ <span {...labelProps}>{props.label}</span>
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>
528
+ <span {...descriptionProps}>{props.description}</span>
529
+ <span {...errorMessageProps}>
530
+ {() => (state.isInvalid() ? (state.errorMessage() as Child) : null)}
531
+ </span>
532
+ </div>
533
+ );
534
+ }
535
+
536
+ export interface RadioComponentProps extends StyleProps {
537
+ value: string;
538
+ children?: Child;
539
+ /**
540
+ * `"label"` when the children are the radio's name, `"span"` when they are
541
+ * its drawing and the name comes from somewhere else. Defaults to a label
542
+ * with children and a span without.
543
+ */
544
+ elementType?: ToggleElementType;
545
+ isDisabled?: boolean;
546
+ /** Help text for this radio, rendered after it and announced with it. */
547
+ description?: Child;
548
+ autoFocus?: boolean;
549
+ "aria-label"?: string;
550
+ "aria-labelledby"?: string;
551
+ "aria-describedby"?: string;
552
+ ref?: RefTarget<HTMLInputElement>;
553
+ onFocus?: (event: FocusEvent) => void;
554
+ onBlur?: (event: FocusEvent) => void;
555
+ onPress?: (event: PressEvent) => void;
556
+ }
557
+
558
+ /** What a part drawn inside a radio reads. */
559
+ export interface RadioContextValue {
560
+ isSelected: Accessor<boolean>;
561
+ isDisabled: Accessor<boolean>;
562
+ }
563
+
564
+ const RadioContext = createContext<RadioContextValue | null>(null);
565
+
566
+ /**
567
+ * The radio a part is drawn inside: the dot of a design system's circle, which
568
+ * appears with a state it does not own.
569
+ */
570
+ export function useRadio(): RadioContextValue {
571
+ const value = getContext(RadioContext);
572
+ if (value === null || value === undefined) {
573
+ throw new Error("This must be rendered inside a Radio.");
574
+ }
575
+ return value;
576
+ }
577
+
578
+ export function Radio(props: Incoming<RadioComponentProps>) {
579
+ const inputRef = makeRef<HTMLInputElement>();
580
+ const options = fromProps(props);
581
+ const group = useRadioGroup();
582
+
583
+ const { labelProps, inputProps, descriptionProps, isSelected, isPressed, isDisabled } = radio(
584
+ options as RadioOptions,
585
+ group.state,
586
+ inputRef,
587
+ group.name,
588
+ );
589
+ const { hoverProps, isHovered } = hover({ isDisabled });
590
+ const { focusProps, isFocused, isFocusVisible } = focusRing();
591
+
592
+ const owner = getOwner();
593
+ if (owner !== null) {
594
+ const value: RadioContextValue = { isSelected, isDisabled };
595
+ install(owner, RadioContext, () => value);
596
+ }
597
+
598
+ const outerProps = mergeProps(
599
+ labelProps,
600
+ hoverProps,
601
+ // NOT `id`: this is a `<label>`, and `id` belongs on the input it wraps.
602
+ // A `<label for>` elsewhere on the page can only reach a LABELABLE element,
603
+ // so an `id` here answered `getElementById` first — the label came earlier
604
+ // in the document — and clicking the text toggled nothing. Measured on the
605
+ // site's checkbox page.
606
+ filterDOMProps(options, { global: true, id: false }),
607
+ styleProps(props),
608
+ {
609
+ "data-selected": isSelected,
610
+ "data-state": () => (isSelected() ? "checked" : "unchecked"),
611
+ "data-pressed": isPressed,
612
+ "data-hovered": isHovered,
613
+ "data-focused": isFocused,
614
+ "data-focus-visible": isFocusVisible,
615
+ "data-disabled": isDisabled,
616
+ "data-readonly": group.state.isReadOnly,
617
+ "data-invalid": group.state.isInvalid,
618
+ "data-required": group.state.isRequired,
619
+ "data-testid": () => props["data-testid"]?.(),
620
+ },
621
+ );
622
+
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
+ );
641
+ }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * happy-dom's globals, installed as this module's BODY.
3
+ *
4
+ * It cannot be a statement in `test-setup.ts`: every import a module declares
5
+ * is evaluated before the first line of its own body runs, so a registration
6
+ * written there happens after `@barqjs/core` has already been evaluated, and
7
+ * `env.ts` reads `typeof document` once at module scope. The whole suite would
8
+ * then run with `isServer === true` against a live DOM.
9
+ */
10
+ import { GlobalRegistrator } from "@happy-dom/global-registrator";
11
+
12
+ GlobalRegistrator.register();