@barqjs/aria 0.0.0-stage → 0.2.0-next.0

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 (76) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +293 -2
  3. package/package.json +347 -4
  4. package/src/breadcrumbs.tsx +296 -0
  5. package/src/button.tsx +342 -0
  6. package/src/calendar.tsx +1389 -0
  7. package/src/checkbox.tsx +570 -0
  8. package/src/collections.ts +976 -0
  9. package/src/color.ts +562 -0
  10. package/src/colorpicker.tsx +1478 -0
  11. package/src/combobox.tsx +2481 -0
  12. package/src/command.tsx +1184 -0
  13. package/src/date.ts +652 -0
  14. package/src/datefield.tsx +951 -0
  15. package/src/datepicker.tsx +556 -0
  16. package/src/dialog.tsx +890 -0
  17. package/src/disclosure.tsx +659 -0
  18. package/src/dom.ts +706 -0
  19. package/src/drawer.ts +674 -0
  20. package/src/focus.ts +1370 -0
  21. package/src/form.tsx +94 -0
  22. package/src/gridlist.tsx +563 -0
  23. package/src/i18n.ts +823 -0
  24. package/src/index.ts +1270 -0
  25. package/src/interactions/description.ts +73 -0
  26. package/src/interactions/flags.ts +35 -0
  27. package/src/interactions/focus-events.ts +218 -0
  28. package/src/interactions/focusable.ts +86 -0
  29. package/src/interactions/hover.ts +213 -0
  30. package/src/interactions/interact-outside.ts +123 -0
  31. package/src/interactions/keyboard.ts +105 -0
  32. package/src/interactions/listeners.ts +78 -0
  33. package/src/interactions/long-press.ts +160 -0
  34. package/src/interactions/modality.ts +416 -0
  35. package/src/interactions/move.ts +257 -0
  36. package/src/interactions/open-link.ts +193 -0
  37. package/src/interactions/press.ts +898 -0
  38. package/src/interactions/prevent-focus.ts +102 -0
  39. package/src/interactions/scroll-wheel.ts +43 -0
  40. package/src/interactions/text-selection.ts +77 -0
  41. package/src/interactions.ts +81 -0
  42. package/src/label.ts +128 -0
  43. package/src/link.tsx +381 -0
  44. package/src/listbox.tsx +721 -0
  45. package/src/live.ts +204 -0
  46. package/src/menu.tsx +2290 -0
  47. package/src/menubar.tsx +575 -0
  48. package/src/navigation.ts +735 -0
  49. package/src/numberfield.tsx +838 -0
  50. package/src/otp.ts +430 -0
  51. package/src/overlays.ts +1247 -0
  52. package/src/platform.ts +74 -0
  53. package/src/presence.ts +164 -0
  54. package/src/questionnaire.tsx +1712 -0
  55. package/src/radio.tsx +567 -0
  56. package/src/register-dom.ts +12 -0
  57. package/src/scroller.ts +1165 -0
  58. package/src/select.tsx +1665 -0
  59. package/src/selection.ts +1415 -0
  60. package/src/slider.tsx +1042 -0
  61. package/src/splitter.ts +1152 -0
  62. package/src/static-collection.ts +211 -0
  63. package/src/switch.tsx +186 -0
  64. package/src/table.tsx +0 -0
  65. package/src/tabs.tsx +1018 -0
  66. package/src/tag.tsx +452 -0
  67. package/src/textfield.tsx +417 -0
  68. package/src/toast.ts +658 -0
  69. package/src/toggle-input.tsx +48 -0
  70. package/src/toggle.ts +321 -0
  71. package/src/togglegroup.ts +365 -0
  72. package/src/toolbar.tsx +179 -0
  73. package/src/tooltip.tsx +657 -0
  74. package/src/utils.ts +758 -0
  75. package/src/validation.ts +447 -0
  76. package/src/virtualizer.tsx +674 -0
@@ -0,0 +1,659 @@
1
+ /**
2
+ * A disclosure: a button that shows and hides one panel, and an accordion of
3
+ * several.
4
+ *
5
+ * The panel is `hidden="until-found"` rather than removed. That is the whole
6
+ * reason to prefer it over a conditional: the browser's own find-in-page
7
+ * searches a `until-found` subtree, scrolls to a match and fires `beforematch`
8
+ * so the disclosure can open itself. Content the user cannot search for is
9
+ * content they cannot find, and Ctrl+F is how most people navigate a long
10
+ * page.
11
+ *
12
+ * `aria-expanded` on the button and `aria-controls` pointing at the panel are
13
+ * what make the pair a disclosure rather than a button beside a div. The panel
14
+ * points back with `aria-labelledby`, so a screen reader entering it knows
15
+ * what it is a panel of.
16
+ *
17
+ * An accordion is disclosures sharing one state. Whether more than one may be
18
+ * open at a time is the caller's decision and not ARIA's: `allowsMultiple`.
19
+ */
20
+
21
+ import {
22
+ type Accessor,
23
+ type Child,
24
+ For,
25
+ createContext,
26
+ createEffect,
27
+ getContext,
28
+ getOwner,
29
+ type Incoming,
30
+ install,
31
+ provide,
32
+ } from "@barqjs/core";
33
+ import { isServer } from "@barqjs/web";
34
+ import { ref as makeRef, mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
35
+ import { button, type ButtonOptions } from "./button.tsx";
36
+ import type { ItemAccessors, Key, Node } from "./collections.ts";
37
+ import { buildCollection, type ListCollection } from "./collections.ts";
38
+ import { exitDuration } from "./presence.ts";
39
+ import { focusRing } from "./focus.ts";
40
+ import { useLocale } from "./i18n.ts";
41
+ import { hover } from "./interactions/hover.ts";
42
+ import type { ElementRef, PressEvent } from "./interactions/press.ts";
43
+ import {
44
+ access,
45
+ controllable,
46
+ type DOMProps,
47
+ filterDOMProps,
48
+ fromProps,
49
+ id,
50
+ type MaybeAccessor,
51
+ mergeProps,
52
+ type StyleProps,
53
+ styleProps,
54
+ } from "./utils.ts";
55
+
56
+ // ---------------------------------------------------------------------------
57
+ // State
58
+ // ---------------------------------------------------------------------------
59
+
60
+ export interface DisclosureStateOptions {
61
+ isExpanded?: MaybeAccessor<boolean | undefined>;
62
+ defaultExpanded?: MaybeAccessor<boolean | undefined>;
63
+ onExpandedChange?: (isExpanded: boolean) => void;
64
+ }
65
+
66
+ export interface DisclosureState {
67
+ isExpanded: Accessor<boolean>;
68
+ setExpanded(isExpanded: boolean): void;
69
+ expand(): void;
70
+ collapse(): void;
71
+ toggle(): void;
72
+ }
73
+
74
+ export function disclosureState(options: DisclosureStateOptions = {}): DisclosureState {
75
+ const [isExpanded, setExpanded] = controllable<boolean>(
76
+ () => access(options.isExpanded),
77
+ () => access(options.defaultExpanded) ?? false,
78
+ options.onExpandedChange,
79
+ );
80
+
81
+ return {
82
+ isExpanded,
83
+ setExpanded,
84
+ expand: () => setExpanded(true),
85
+ collapse: () => setExpanded(false),
86
+ toggle: () => setExpanded(!isExpanded()),
87
+ };
88
+ }
89
+
90
+ export interface DisclosureGroupStateOptions {
91
+ expandedKeys?: MaybeAccessor<Iterable<Key> | undefined>;
92
+ defaultExpandedKeys?: MaybeAccessor<Iterable<Key> | undefined>;
93
+ /** Let more than one panel be open at once. @default false */
94
+ allowsMultiple?: MaybeAccessor<boolean | undefined>;
95
+ /**
96
+ * Whether the one open section may be shut, leaving none open. Read only
97
+ * without `allowsMultiple`, where every section closes on its own anyway.
98
+ * @default true
99
+ */
100
+ isCollapsible?: MaybeAccessor<boolean | undefined>;
101
+ isDisabled?: MaybeAccessor<boolean | undefined>;
102
+ onExpandedChange?: (keys: Set<Key>) => void;
103
+ }
104
+
105
+ export interface DisclosureGroupState {
106
+ expandedKeys: Accessor<Set<Key>>;
107
+ allowsMultiple: Accessor<boolean>;
108
+ /** Whether pressing this open section would do nothing. */
109
+ isLocked(key: Key): boolean;
110
+ isDisabled: Accessor<boolean>;
111
+ toggleKey(key: Key): void;
112
+ setExpandedKeys(keys: Set<Key>): void;
113
+ }
114
+
115
+ /** Several disclosures sharing which of them is open. */
116
+ export function disclosureGroupState(
117
+ options: DisclosureGroupStateOptions = {},
118
+ ): DisclosureGroupState {
119
+ const [expandedKeys, setExpandedKeys] = controllable<Set<Key>>(
120
+ () => {
121
+ const given = access(options.expandedKeys);
122
+ return given === undefined ? undefined : new Set(given);
123
+ },
124
+ () => new Set(access(options.defaultExpandedKeys) ?? []),
125
+ options.onExpandedChange,
126
+ );
127
+
128
+ const allowsMultiple = (): boolean => access(options.allowsMultiple) === true;
129
+ const isLocked = (key: Key): boolean =>
130
+ !allowsMultiple() && access(options.isCollapsible) === false && expandedKeys().has(key);
131
+
132
+ return {
133
+ expandedKeys,
134
+ allowsMultiple,
135
+ isLocked,
136
+ isDisabled: () => access(options.isDisabled) === true,
137
+ setExpandedKeys,
138
+ toggleKey: (key) => {
139
+ const open = expandedKeys();
140
+ if (open.has(key)) {
141
+ if (isLocked(key)) return;
142
+ const next = new Set(open);
143
+ next.delete(key);
144
+ setExpandedKeys(next);
145
+ return;
146
+ }
147
+ // Closing the others is the DEFAULT: an accordion whose panels all open
148
+ // at once is a list of disclosures, and the caller says so.
149
+ setExpandedKeys(allowsMultiple() ? new Set([...open, key]) : new Set([key]));
150
+ },
151
+ };
152
+ }
153
+
154
+ // ---------------------------------------------------------------------------
155
+ // Hooks
156
+ // ---------------------------------------------------------------------------
157
+
158
+ export interface DisclosureOptions {
159
+ panelRef: ElementRef;
160
+ isDisabled?: MaybeAccessor<boolean | undefined>;
161
+ }
162
+
163
+ export interface DisclosureResult {
164
+ /** Options for {@link button}, not props for an element. */
165
+ buttonProps: DOMProps;
166
+ panelProps: DOMProps;
167
+ }
168
+
169
+ export function disclosure(options: DisclosureOptions, state: DisclosureState): DisclosureResult {
170
+ const triggerId = id();
171
+ const panelId = id();
172
+ const isDisabled = (): boolean => access(options.isDisabled) === true;
173
+
174
+ if (!isServer) {
175
+ /**
176
+ * Find-in-page opens it.
177
+ *
178
+ * The browser strips `hidden` before firing `beforematch`, so the
179
+ * attribute is put back and the state told to open: without that the panel
180
+ * is visible while the state still says it is closed, and the next toggle
181
+ * closes nothing.
182
+ */
183
+ createEffect(
184
+ () => access(options.panelRef) as HTMLElement | null,
185
+ (panel) => {
186
+ if (panel === null) return undefined;
187
+ const onBeforeMatch = (): void => {
188
+ panel.setAttribute("hidden", "until-found");
189
+ state.expand();
190
+ };
191
+ panel.addEventListener("beforematch", onBeforeMatch);
192
+ return () => panel.removeEventListener("beforematch", onBeforeMatch);
193
+ },
194
+ );
195
+
196
+ /**
197
+ * `until-found`, not `true`: the panel stays searchable while it is shut.
198
+ *
199
+ * The attribute goes on only once the stylesheet has finished closing the
200
+ * panel. `hidden` stops the content being rendered AT ONCE, so setting it
201
+ * the moment the state flipped cancelled the collapse: a panel whose CSS
202
+ * transitions `grid-template-rows` opened over 200ms and then vanished in
203
+ * one frame, and the transition it was still running painted nothing.
204
+ *
205
+ * It is set synchronously in every other case — a panel that starts shut,
206
+ * one with no transition, and one under `prefers-reduced-motion` — so
207
+ * nothing flashes open on the first render.
208
+ */
209
+ let hasOpened = false;
210
+ createEffect(
211
+ () => [access(options.panelRef) as HTMLElement | null, state.isExpanded()] as const,
212
+ ([panel, expanded]) => {
213
+ if (panel === null) return undefined;
214
+
215
+ const hide = (): void => panel.setAttribute("hidden", "until-found");
216
+ if (expanded) {
217
+ hasOpened = true;
218
+ panel.removeAttribute("hidden");
219
+ return undefined;
220
+ }
221
+
222
+ const closing = hasOpened ? exitDuration(panel) : 0;
223
+ if (closing <= 0) {
224
+ hide();
225
+ return undefined;
226
+ }
227
+ const timer = setTimeout(hide, closing);
228
+ return () => clearTimeout(timer);
229
+ },
230
+ );
231
+ }
232
+
233
+ return {
234
+ buttonProps: {
235
+ id: triggerId,
236
+ isDisabled: options.isDisabled,
237
+ "aria-expanded": () => state.isExpanded(),
238
+ "aria-controls": panelId,
239
+ // On press for a pointer, on press START for a key: a keyboard toggle
240
+ // that waited for the key to come up would feel a frame late next to
241
+ // everything else the platform does on key down.
242
+ onPress: (event: PressEvent) => {
243
+ if (isDisabled() || event.pointerType === "keyboard") return;
244
+ state.toggle();
245
+ },
246
+ onPressStart: (event: PressEvent) => {
247
+ if (isDisabled() || event.pointerType !== "keyboard") return;
248
+ state.toggle();
249
+ },
250
+ },
251
+ panelProps: {
252
+ id: panelId,
253
+ role: "group",
254
+ "aria-labelledby": triggerId,
255
+ // On the SERVER the attribute is all there is: the effects above have
256
+ // not run, and a panel rendered without it would flash open.
257
+ hidden: () => (isServer ? (state.isExpanded() ? undefined : "until-found") : undefined),
258
+ },
259
+ };
260
+ }
261
+
262
+ // ---------------------------------------------------------------------------
263
+ // Components
264
+ // ---------------------------------------------------------------------------
265
+
266
+ interface DisclosureContextValue {
267
+ state: DisclosureState;
268
+ buttonProps: DOMProps;
269
+ panelProps: DOMProps;
270
+ panelRef: ReturnType<typeof makeRef<HTMLDivElement>>;
271
+ isDisabled: Accessor<boolean>;
272
+ /** Open, and not allowed to shut: the last open section of a group that may not be empty. */
273
+ isLocked?: Accessor<boolean>;
274
+ /** The group this section belongs to. Absent for a lone disclosure. */
275
+ group?: DisclosureGroupContextValue;
276
+ }
277
+
278
+ const DisclosureContext = createContext<DisclosureContextValue | null>(null);
279
+
280
+ export function useDisclosure(): DisclosureContextValue {
281
+ const value = getContext(DisclosureContext);
282
+ if (value === null || value === undefined) {
283
+ throw new Error("This must be rendered inside a Disclosure.");
284
+ }
285
+ return value;
286
+ }
287
+
288
+ /**
289
+ * NO `StyleProps`, and that is the whole of what this component is.
290
+ *
291
+ * `<Disclosure>` renders no element — it is a provider, like every grouping
292
+ * component here — so a `class`, an `id` or a `data-*` handed to it has nothing
293
+ * to land on and vanishes in silence. It used to declare them anyway, and
294
+ * `@barqjs/ui`'s accordion lost its dividers to exactly that on
295
+ * `<DisclosureGroupItem>`. A consumer that needs an element brings its own
296
+ * around this one, which is what `<Collapsible>` and `<AccordionItem>` do.
297
+ */
298
+ export interface DisclosureComponentProps {
299
+ children?: Child;
300
+ isExpanded?: boolean;
301
+ defaultExpanded?: boolean;
302
+ isDisabled?: boolean;
303
+ onExpandedChange?: (isExpanded: boolean) => void;
304
+ }
305
+
306
+ /**
307
+ * ```tsx
308
+ * <Disclosure>
309
+ * <DisclosureButton>Details</DisclosureButton>
310
+ * <DisclosurePanel>Everything else</DisclosurePanel>
311
+ * </Disclosure>
312
+ * ```
313
+ */
314
+ export function Disclosure(props: Incoming<DisclosureComponentProps>) {
315
+ const panelRef = makeRef<HTMLDivElement>();
316
+ const options = fromProps(props);
317
+ const state = disclosureState(options);
318
+
319
+ const { buttonProps, panelProps } = disclosure(
320
+ { panelRef, isDisabled: () => props.isDisabled?.() },
321
+ state,
322
+ );
323
+
324
+ const value: DisclosureContextValue = {
325
+ state,
326
+ buttonProps,
327
+ panelProps,
328
+ panelRef,
329
+ isDisabled: () => props.isDisabled?.() === true,
330
+ };
331
+ return provide(
332
+ DisclosureContext,
333
+ () => value,
334
+ () => props.children as unknown,
335
+ );
336
+ }
337
+
338
+ export interface DisclosureButtonComponentProps extends StyleProps {
339
+ children?: Child;
340
+ isDisabled?: boolean;
341
+ ref?: RefTarget<HTMLButtonElement>;
342
+ }
343
+
344
+ export function DisclosureButton(props: Incoming<DisclosureButtonComponentProps>) {
345
+ const domRef = makeRef<HTMLButtonElement>();
346
+ const value = useDisclosure();
347
+
348
+ const { buttonProps, isPressed } = button(
349
+ { ...(fromProps(props) as ButtonOptions), ...value.buttonProps },
350
+ domRef,
351
+ );
352
+ const { hoverProps, isHovered } = hover({ isDisabled: () => props.isDisabled?.() });
353
+ const { focusProps, isFocusVisible } = focusRing();
354
+ // `button()` delivers `onClick` itself; bound on the element as well, a
355
+ // caller's handler runs twice for every mouse click.
356
+ const { onClick: _, ...forwarded } = filterDOMProps(fromProps(props), { global: true });
357
+
358
+ const elementProps = mergeProps(
359
+ buttonProps,
360
+ { id: value.buttonProps.id },
361
+ hoverProps,
362
+ focusProps,
363
+ forwarded,
364
+ styleProps(props),
365
+ {
366
+ "aria-disabled": () => value.isLocked?.() === true || undefined,
367
+ "data-expanded": value.state.isExpanded,
368
+ "data-state": () => (value.state.isExpanded() ? "open" : "closed"),
369
+ "data-orientation": () => value.group?.orientation(),
370
+ "data-pressed": isPressed,
371
+ "data-hovered": isHovered,
372
+ "data-focus-visible": isFocusVisible,
373
+ "data-disabled": () => props.isDisabled?.() === true || value.isDisabled(),
374
+ "data-testid": () => props["data-testid"]?.(),
375
+ },
376
+ );
377
+
378
+ const group = value.group;
379
+ const enlist = (element: HTMLButtonElement | null): void => {
380
+ if (element !== null && group !== undefined) headerGroups.set(element, group);
381
+ };
382
+
383
+ return (
384
+ <button {...elementProps} ref={mergeRefs(domRef.set, enlist, props.ref?.())}>
385
+ {props.children}
386
+ </button>
387
+ );
388
+ }
389
+
390
+ export interface DisclosurePanelComponentProps extends StyleProps {
391
+ children?: Child;
392
+ ref?: RefTarget<HTMLDivElement>;
393
+ }
394
+
395
+ /**
396
+ * The panel. Still in the document while collapsed, so find-in-page can reach
397
+ * it and open the disclosure it belongs to.
398
+ */
399
+ export function DisclosurePanel(props: Incoming<DisclosurePanelComponentProps>) {
400
+ const value = useDisclosure();
401
+
402
+ const elementProps = mergeProps(
403
+ value.panelProps,
404
+ filterDOMProps(fromProps(props), { global: true, labelable: true }),
405
+ styleProps(props),
406
+ {
407
+ "data-expanded": value.state.isExpanded,
408
+ "data-state": () => (value.state.isExpanded() ? "open" : "closed"),
409
+ "data-orientation": () => value.group?.orientation(),
410
+ "data-disabled": value.isDisabled,
411
+ "data-testid": () => props["data-testid"]?.(),
412
+ },
413
+ );
414
+
415
+ return (
416
+ <div {...elementProps} ref={mergeRefs(value.panelRef.set, props.ref?.())}>
417
+ {props.children}
418
+ </div>
419
+ );
420
+ }
421
+
422
+ // ---------------------------------------------------------------------------
423
+ // A group of them
424
+ // ---------------------------------------------------------------------------
425
+
426
+ interface DisclosureGroupContextValue {
427
+ state: DisclosureGroupState;
428
+ orientation: Accessor<"vertical" | "horizontal">;
429
+ }
430
+
431
+ /**
432
+ * Which group each header button belongs to.
433
+ *
434
+ * By element rather than by a query, because an accordion inside another
435
+ * accordion's panel has headers that match any selector the outer one could
436
+ * write, and the arrow keys must not walk into them.
437
+ */
438
+ const headerGroups = new WeakMap<Element, DisclosureGroupContextValue>();
439
+
440
+ const DisclosureGroupContext = createContext<DisclosureGroupContextValue | null>(null);
441
+ const DisclosureNodeContext = createContext<Node<unknown> | null>(null);
442
+
443
+ export function useDisclosureGroup(): DisclosureGroupContextValue {
444
+ const value = getContext(DisclosureGroupContext);
445
+ if (value === null || value === undefined) {
446
+ throw new Error("This must be rendered inside a DisclosureGroup.");
447
+ }
448
+ return value;
449
+ }
450
+
451
+ /** The collection node the section being built is for. */
452
+ export function useDisclosureNode(): Node<unknown> {
453
+ const node = getContext(DisclosureNodeContext);
454
+ if (node === null || node === undefined) {
455
+ throw new Error("This must be rendered inside a DisclosureGroup's item callback.");
456
+ }
457
+ return node;
458
+ }
459
+
460
+ export interface DisclosureGroupComponentProps<T> extends StyleProps, ItemAccessors<T> {
461
+ /** The sections, in order. Leave it out to write the sections as children. */
462
+ items?: Iterable<T>;
463
+ /**
464
+ * How one section renders, returning a `<DisclosureGroupItem>`. Without
465
+ * `items`, the sections themselves, each with an `id`.
466
+ */
467
+ children?: ((item: T) => Child) | Child;
468
+ expandedKeys?: Iterable<Key>;
469
+ defaultExpandedKeys?: Iterable<Key>;
470
+ /** @default false */
471
+ allowsMultiple?: boolean;
472
+ /** Whether the one open section may be shut. @default true */
473
+ isCollapsible?: boolean;
474
+ /** Which arrow keys move between the headers. @default "vertical" */
475
+ orientation?: "vertical" | "horizontal";
476
+ /** Overrides the direction in scope for the left and right arrows. */
477
+ dir?: "ltr" | "rtl";
478
+ isDisabled?: boolean;
479
+ ref?: RefTarget<HTMLDivElement>;
480
+ onExpandedChange?: (keys: Set<Key>) => void;
481
+ }
482
+
483
+ /**
484
+ * An accordion.
485
+ *
486
+ * ```tsx
487
+ * <DisclosureGroup items={sections()}>
488
+ * {(section) => (
489
+ * <DisclosureGroupItem>
490
+ * <DisclosureButton>{section.name}</DisclosureButton>
491
+ * <DisclosurePanel>{section.body}</DisclosurePanel>
492
+ * </DisclosureGroupItem>
493
+ * )}
494
+ * </DisclosureGroup>
495
+ * ```
496
+ *
497
+ * Or written out, each section naming itself:
498
+ *
499
+ * ```tsx
500
+ * <DisclosureGroup>
501
+ * <DisclosureGroupItem id="billing">…</DisclosureGroupItem>
502
+ * <DisclosureGroupItem id="shipping">…</DisclosureGroupItem>
503
+ * </DisclosureGroup>
504
+ * ```
505
+ *
506
+ * The arrow keys, Home and End move between the headers. The authoring
507
+ * practices call that optional; it is here because a long accordion is
508
+ * otherwise a Tab stop per section with nothing faster.
509
+ */
510
+ export function DisclosureGroup<T>(props: Incoming<DisclosureGroupComponentProps<T>>) {
511
+ const options = fromProps(props as unknown as Incoming<Record<string, unknown>>);
512
+ const state = disclosureGroupState({
513
+ ...(options as DisclosureGroupStateOptions),
514
+ onExpandedChange: (keys) => props.onExpandedChange?.()?.(keys),
515
+ });
516
+
517
+ const items = props.items;
518
+ const collection = (): ListCollection<T> =>
519
+ buildCollection(items?.() ?? [], props as unknown as ItemAccessors<T>);
520
+
521
+ const locale = useLocale();
522
+ const orientation = (): "vertical" | "horizontal" => props.orientation?.() ?? "vertical";
523
+ const group: DisclosureGroupContextValue = { state, orientation };
524
+
525
+ const owner = getOwner();
526
+ if (owner !== null) install(owner, DisclosureGroupContext, () => group);
527
+
528
+ const onKeyDown = (event: KeyboardEvent): void => {
529
+ const root = event.currentTarget as Element | null;
530
+ const from = (event.target as Element | null)?.closest("button") ?? null;
531
+ if (root === null || from === null || headerGroups.get(from) !== group) return;
532
+
533
+ const rtl = (props.dir?.() ?? locale().direction) === "rtl";
534
+ const horizontal = orientation() === "horizontal";
535
+ const forward = horizontal ? (rtl ? "ArrowLeft" : "ArrowRight") : "ArrowDown";
536
+ const back = horizontal ? (rtl ? "ArrowRight" : "ArrowLeft") : "ArrowUp";
537
+ if (
538
+ event.key !== forward &&
539
+ event.key !== back &&
540
+ event.key !== "Home" &&
541
+ event.key !== "End"
542
+ ) {
543
+ return;
544
+ }
545
+
546
+ const headers = [...root.querySelectorAll<HTMLButtonElement>("button")].filter(
547
+ (candidate) => headerGroups.get(candidate) === group && !candidate.disabled,
548
+ );
549
+ const at = headers.indexOf(from);
550
+ if (at === -1) return;
551
+
552
+ let next = at;
553
+ if (event.key === "Home") next = 0;
554
+ else if (event.key === "End") next = headers.length - 1;
555
+ else if (event.key === forward) next = (at + 1) % headers.length;
556
+ else next = (at - 1 + headers.length) % headers.length;
557
+
558
+ event.preventDefault();
559
+ headers[next]?.focus();
560
+ };
561
+
562
+ const elementProps = mergeProps(
563
+ filterDOMProps(fromProps(props), { global: true }),
564
+ styleProps(props),
565
+ {
566
+ onKeyDown,
567
+ "data-orientation": orientation,
568
+ "data-disabled": state.isDisabled,
569
+ "data-testid": () => props["data-testid"]?.(),
570
+ },
571
+ );
572
+
573
+ if (items === undefined) {
574
+ // A written-out group inside a section of a collection-driven one must not
575
+ // key its sections by the outer section's node.
576
+ if (owner !== null) install(owner, DisclosureNodeContext, () => null);
577
+ return (
578
+ <div {...elementProps} ref={mergeRefs(props.ref?.())}>
579
+ {props.children}
580
+ </div>
581
+ );
582
+ }
583
+
584
+ const render = props.children as unknown as (item: T) => Child;
585
+
586
+ return (
587
+ <div {...elementProps} ref={mergeRefs(props.ref?.())}>
588
+ <For each={() => [...collection()]}>
589
+ {(node: Node<T>) => {
590
+ const rowOwner = getOwner();
591
+ if (rowOwner !== null) {
592
+ install(rowOwner, DisclosureNodeContext, () => node as Node<unknown>);
593
+ }
594
+ return render(node.value as T);
595
+ }}
596
+ </For>
597
+ </div>
598
+ );
599
+ }
600
+
601
+ /** No `StyleProps`, for the reason on {@link DisclosureComponentProps}. */
602
+ export interface DisclosureGroupItemComponentProps {
603
+ children?: Child;
604
+ isDisabled?: boolean;
605
+ /**
606
+ * The key this section answers to in `expandedKeys`. Read when the group was
607
+ * given no `items`; with them the key is the item's own.
608
+ */
609
+ id?: Key;
610
+ }
611
+
612
+ /**
613
+ * One section of a {@link DisclosureGroup}, sharing the group's state.
614
+ *
615
+ * The same `<DisclosureButton>` and `<DisclosurePanel>` go inside it as go
616
+ * inside a lone `<Disclosure>`: what changes is where "expanded" is kept.
617
+ */
618
+ export function DisclosureGroupItem(props: Incoming<DisclosureGroupItemComponentProps>) {
619
+ const panelRef = makeRef<HTMLDivElement>();
620
+ const group = useDisclosureGroup();
621
+ const node = getContext(DisclosureNodeContext) ?? null;
622
+ const generated = id();
623
+ const key = (): Key => node?.key ?? props.id?.() ?? generated();
624
+
625
+ const state: DisclosureState = {
626
+ isExpanded: () => group.state.expandedKeys().has(key()),
627
+ setExpanded: (expanded) => {
628
+ if (expanded === group.state.expandedKeys().has(key())) return;
629
+ group.state.toggleKey(key());
630
+ },
631
+ expand: () => {
632
+ if (!group.state.expandedKeys().has(key())) group.state.toggleKey(key());
633
+ },
634
+ collapse: () => {
635
+ if (group.state.expandedKeys().has(key())) group.state.toggleKey(key());
636
+ },
637
+ toggle: () => group.state.toggleKey(key()),
638
+ };
639
+
640
+ const isDisabled = (): boolean =>
641
+ props.isDisabled?.() === true || group.state.isDisabled() || node?.props?.isDisabled === true;
642
+
643
+ const { buttonProps, panelProps } = disclosure({ panelRef, isDisabled }, state);
644
+
645
+ const value: DisclosureContextValue = {
646
+ state,
647
+ buttonProps,
648
+ panelProps,
649
+ panelRef,
650
+ isDisabled,
651
+ isLocked: () => group.state.isLocked(key()),
652
+ group,
653
+ };
654
+ return provide(
655
+ DisclosureContext,
656
+ () => value,
657
+ () => props.children as unknown,
658
+ );
659
+ }