@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
@@ -0,0 +1,579 @@
1
+ /**
2
+ * A menubar: a row of menus that is ONE widget rather than several.
3
+ *
4
+ * What separates it from a row of menu buttons is all here:
5
+ *
6
+ * - **One Tab stop.** The triggers share a roving `tabindex`, so Tab enters the
7
+ * bar once and leaves it once, and the horizontal arrows, Home, End and a
8
+ * typed letter move between the triggers.
9
+ * - **One menu open, and the bar owns which.** Each menu's open state is a
10
+ * reading of the bar's `value`, so opening one is closing the other and
11
+ * there is never a frame with two.
12
+ * - **While a menu is open the horizontal arrows change WHICH menu is open.**
13
+ * From a trigger, and from any depth inside a menu: the arrow belongs to the
14
+ * bar unless the item under it has a submenu to open, or the submenu it is in
15
+ * is the thing the arrow closes.
16
+ * - **Pointing at a trigger opens its menu only while another is open.** A bar
17
+ * that opened on hover alone would open under a pointer on its way somewhere
18
+ * else.
19
+ * - **Closing gives focus back to the trigger, and says so.** The menu's own
20
+ * focus scope restores to whatever had focus when it opened, which after a
21
+ * switch between menus is an item of a menu that no longer exists.
22
+ *
23
+ * The menus inside are ordinary `<Menu>`s. A `<MenubarMenu>` is a menu trigger
24
+ * whose state is the bar's, and nothing below it knows the difference.
25
+ */
26
+
27
+ import {
28
+ cells,
29
+ type Accessor,
30
+ type Child,
31
+ createContext,
32
+ createEffect,
33
+ createSignal,
34
+ createUniqueId,
35
+ flush,
36
+ getContext,
37
+ type Incoming,
38
+ onCleanup,
39
+ onSettled,
40
+ provide,
41
+ untrack,
42
+ } from "@barqjs/core";
43
+ import { ref as makeRef, mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
44
+
45
+ import { button, type ButtonOptions } from "./button.tsx";
46
+ import type { FocusStrategy } from "./collections.ts";
47
+ import { focusWithoutScrolling, ownerDocument, targetElement } from "./dom.ts";
48
+ import { focusRing } from "./focus.ts";
49
+ import { useLocale } from "./i18n.ts";
50
+ import { hover } from "./interactions/hover.ts";
51
+ import {
52
+ menuTrigger,
53
+ menuTriggerState,
54
+ provideMenuTrigger,
55
+ type MenuTriggerValue,
56
+ type RootMenuTriggerState,
57
+ } from "./menu.tsx";
58
+ import {
59
+ access,
60
+ controllable,
61
+ type DOMProps,
62
+ filterDOMProps,
63
+ fromProps,
64
+ mergeProps,
65
+ type StyleProps,
66
+ styleProps,
67
+ } from "./utils.ts";
68
+
69
+ /** How long a typed prefix is remembered before the next key starts a new one. */
70
+ const TYPEAHEAD_RESET = 500;
71
+
72
+ interface MenubarEntry {
73
+ value: string;
74
+ state: RootMenuTriggerState;
75
+ trigger: Accessor<HTMLElement | null>;
76
+ isDisabled: Accessor<boolean>;
77
+ }
78
+
79
+ type Step = "next" | "previous" | "first" | "last";
80
+
81
+ interface MenubarValue {
82
+ /** The `value` of the open menu, or `""` when none is. */
83
+ value: Accessor<string>;
84
+ setValue: (value: string) => void;
85
+ /** The menu whose trigger is in the Tab order. */
86
+ tabStop: Accessor<string | null>;
87
+ setTabStop: (value: string) => void;
88
+ direction: Accessor<"ltr" | "rtl">;
89
+ register: (entry: MenubarEntry) => void;
90
+ /** The menu `step` away from `from`, skipping disabled ones. */
91
+ towards: (from: string, step: Step) => MenubarEntry | null;
92
+ /** The next menu after `from` whose trigger starts with what has been typed. */
93
+ matching: (from: string, character: string) => MenubarEntry | null;
94
+ /** Close whatever is open and open `value` in its place. */
95
+ switchTo: (value: string, focusStrategy: FocusStrategy | null) => void;
96
+ }
97
+
98
+ const MenubarContext = createContext<MenubarValue | null>(null);
99
+
100
+ function useMenubar(): MenubarValue {
101
+ const bar = getContext(MenubarContext);
102
+ if (bar === null || bar === undefined) {
103
+ throw new Error("A MenubarMenu must be rendered inside a Menubar.");
104
+ }
105
+ return bar;
106
+ }
107
+
108
+ interface MenubarMenuValue {
109
+ value: string;
110
+ trigger: MenuTriggerValue;
111
+ setDisabled: (isDisabled: boolean) => void;
112
+ }
113
+
114
+ const MenubarMenuContext = createContext<MenubarMenuValue | null>(null);
115
+
116
+ function byDocument(a: MenubarEntry, b: MenubarEntry): number {
117
+ const first = a.trigger();
118
+ const second = b.trigger();
119
+ if (first === null || second === null) return 0;
120
+ const relation = first.compareDocumentPosition(second);
121
+ if ((relation & Node.DOCUMENT_POSITION_FOLLOWING) !== 0) return -1;
122
+ if ((relation & Node.DOCUMENT_POSITION_PRECEDING) !== 0) return 1;
123
+ return 0;
124
+ }
125
+
126
+ export interface MenubarComponentProps extends StyleProps {
127
+ children?: Child;
128
+ /** The `value` of the open `<MenubarMenu>`, or `""` for none. */
129
+ value?: string;
130
+ defaultValue?: string;
131
+ onValueChange?: (value: string) => void;
132
+ /** Whether the arrows go round from the last trigger to the first. @default true */
133
+ loop?: boolean;
134
+ "aria-label"?: string;
135
+ "aria-labelledby"?: string;
136
+ ref?: RefTarget<HTMLDivElement>;
137
+ }
138
+
139
+ /**
140
+ * ```tsx
141
+ * <Menubar aria-label="Application">
142
+ * <MenubarMenu>
143
+ * <MenubarTrigger>File</MenubarTrigger>
144
+ * <Menu>
145
+ * <MenuItem onAction={() => open()}>Open</MenuItem>
146
+ * </Menu>
147
+ * </MenubarMenu>
148
+ * <MenubarMenu>
149
+ * <MenubarTrigger>Edit</MenubarTrigger>
150
+ * <Menu>
151
+ * <MenuItem onAction={() => undo()}>Undo</MenuItem>
152
+ * </Menu>
153
+ * </MenubarMenu>
154
+ * </Menubar>
155
+ * ```
156
+ */
157
+ export function Menubar(props: MenubarComponentProps) {
158
+ const locale = useLocale();
159
+ const entries = new Map<string, MenubarEntry>();
160
+ const [registered, setRegistered] = createSignal(0, { ownedWrite: true });
161
+ const [focused, setFocused] = createSignal<string | null>(null);
162
+
163
+ const [value, setValue] = controllable<string>(
164
+ () => props.value,
165
+ () => props.defaultValue ?? "",
166
+ (next) => props.onValueChange?.(next),
167
+ );
168
+
169
+ const ordered = (): MenubarEntry[] =>
170
+ [...entries.values()].toSorted(byDocument).filter((entry) => !entry.isDisabled());
171
+
172
+ let typed = "";
173
+ let forget: ReturnType<typeof setTimeout> | undefined;
174
+ onCleanup(() => clearTimeout(forget));
175
+
176
+ // A menu's focus scope restores to what had focus when it opened. After a
177
+ // switch that can be an item of the menu before, which is gone, so when the
178
+ // last menu closes its trigger is named outright. Only if focus fell to
179
+ // nothing: a press elsewhere that took focus keeps it.
180
+ let switching = false;
181
+ let wasOpen = "";
182
+ createEffect(value, (open) => {
183
+ const closed = wasOpen;
184
+ wasOpen = open;
185
+ if (open !== "" || closed === "" || switching) return;
186
+ const trigger = untrack(() => entries.get(closed)?.trigger() ?? null);
187
+ if (trigger === null) return;
188
+ requestAnimationFrame(() => {
189
+ if (value() !== "" || !trigger.isConnected) return;
190
+ const doc = ownerDocument(trigger);
191
+ const active = doc.activeElement;
192
+ if (active === null || active === doc.body || !active.isConnected) {
193
+ focusWithoutScrolling(trigger);
194
+ }
195
+ });
196
+ });
197
+
198
+ const bar: MenubarValue = {
199
+ value,
200
+ setValue,
201
+ direction: () => locale().direction,
202
+ setTabStop: setFocused,
203
+ tabStop: () => {
204
+ registered();
205
+ const held = focused();
206
+ if (held !== null && entries.get(held)?.isDisabled() === false) return held;
207
+ for (const entry of entries.values()) {
208
+ if (!entry.isDisabled()) return entry.value;
209
+ }
210
+ return null;
211
+ },
212
+ register: (entry) => {
213
+ entries.set(entry.value, entry);
214
+ setRegistered((count) => count + 1);
215
+ onCleanup(() => {
216
+ entries.delete(entry.value);
217
+ setRegistered((count) => count + 1);
218
+ });
219
+ },
220
+ towards: (from, step) => {
221
+ const all = ordered();
222
+ if (all.length === 0) return null;
223
+ if (step === "first") return all[0] ?? null;
224
+ if (step === "last") return all[all.length - 1] ?? null;
225
+ const at = all.findIndex((entry) => entry.value === from);
226
+ const to = at + (step === "next" ? 1 : -1);
227
+ if (to >= 0 && to < all.length) return all[to] ?? null;
228
+ if (props.loop === false) return null;
229
+ return all[(to + all.length) % all.length] ?? null;
230
+ },
231
+ matching: (from, character) => {
232
+ clearTimeout(forget);
233
+ forget = setTimeout(() => {
234
+ typed = "";
235
+ }, TYPEAHEAD_RESET);
236
+ // The same letter again steps on to the next trigger starting with it,
237
+ // rather than searching for the letter twice over.
238
+ const letter = character.toLowerCase();
239
+ typed = typed === "" || typed === letter ? letter : typed + letter;
240
+
241
+ const all = ordered();
242
+ const at = all.findIndex((entry) => entry.value === from);
243
+ // One letter typed again steps to the next match; a longer prefix may
244
+ // still be the trigger the user is already on.
245
+ const rotated = [...all.slice(at + 1), ...all.slice(0, at + 1)];
246
+ const candidates = typed.length === 1 ? rotated : [...rotated.slice(-1), ...rotated];
247
+ return (
248
+ candidates.find((entry) =>
249
+ (entry.trigger()?.textContent ?? "").trim().toLowerCase().startsWith(typed),
250
+ ) ?? null
251
+ );
252
+ },
253
+ switchTo: (to, focusStrategy) => {
254
+ const next = entries.get(to);
255
+ if (next === undefined || next.isDisabled()) return;
256
+ const current = entries.get(value());
257
+ if (current !== undefined && current !== next) {
258
+ switching = true;
259
+ current.state.close();
260
+ // The open menu holds focus inside itself until it is gone, and would
261
+ // take it straight back from the trigger focused below.
262
+ flush();
263
+ switching = false;
264
+ }
265
+ // Before the menu opens, so that is where its focus scope restores to.
266
+ const trigger = next.trigger();
267
+ if (trigger !== null) focusWithoutScrolling(trigger);
268
+ next.state.open(focusStrategy);
269
+ },
270
+ };
271
+
272
+ const elementProps = mergeProps(
273
+ filterDOMProps(fromProps(cells(props)), { global: true }),
274
+ styleProps(cells(props)),
275
+ {
276
+ role: "menubar",
277
+ "aria-label": () => props["aria-label"],
278
+ "aria-labelledby": () => props["aria-labelledby"],
279
+ "data-orientation": "horizontal",
280
+ "data-testid": () => props["data-testid"],
281
+ },
282
+ );
283
+
284
+ return (
285
+ <div {...elementProps} ref={mergeRefs(props.ref)}>
286
+ {provide(
287
+ MenubarContext,
288
+ () => bar,
289
+ () => props.children as unknown,
290
+ )}
291
+ </div>
292
+ );
293
+ }
294
+
295
+ export interface MenubarMenuComponentProps {
296
+ /** What the bar's `value` is while this menu is open. Generated when left out. */
297
+ value?: string;
298
+ /**
299
+ * Hold this one menu open or shut. The bar's `value` is the usual way to say
300
+ * which menu is open; this is for a caller that only cares about one of them.
301
+ */
302
+ isOpen?: boolean;
303
+ /** Start open, when the bar itself names no menu. */
304
+ defaultOpen?: boolean;
305
+ /**
306
+ * Told when this menu opens or closes, however that came about: its own
307
+ * trigger, a neighbour taking over, or the bar's `value` changing.
308
+ */
309
+ onOpenChange?: (isOpen: boolean) => void;
310
+ children?: Child;
311
+ }
312
+
313
+ /** One menu of the bar: a `<MenubarTrigger>` and the `<Menu>` it opens. */
314
+ export function MenubarMenu(props: Incoming<MenubarMenuComponentProps>) {
315
+ const bar = useMenubar();
316
+ const value = props.value?.() ?? createUniqueId();
317
+ const triggerRef = makeRef<HTMLElement>();
318
+ const [isDisabled, setDisabled] = createSignal(false, { ownedWrite: true });
319
+
320
+ const held = (): boolean | undefined => props.isOpen?.();
321
+ const isCurrent = (): boolean => bar.value() === value;
322
+
323
+ const state = menuTriggerState({
324
+ isOpen: () => held() ?? isCurrent(),
325
+ onOpenChange: (isOpen) => {
326
+ // A held menu only asks: whoever holds it answers by changing `isOpen`.
327
+ if (held() !== undefined) {
328
+ props.onOpenChange?.()?.(isOpen);
329
+ return;
330
+ }
331
+ if (isOpen) bar.setValue(value);
332
+ else if (bar.value() === value) bar.setValue("");
333
+ },
334
+ });
335
+
336
+ bar.register({ value, state, trigger: triggerRef, isDisabled });
337
+
338
+ // After the tree has settled, because a component body may not write state.
339
+ // Starting open is not a change, so it is not reported as one.
340
+ let isDefaulting = false;
341
+ if (untrack(() => props.defaultOpen?.()) === true) {
342
+ onSettled(() => {
343
+ if (untrack(bar.value) !== "") return;
344
+ isDefaulting = true;
345
+ bar.setValue(value);
346
+ });
347
+ }
348
+
349
+ // The bar is what the arrow keys and the neighbours read, so a menu held
350
+ // open from outside has to be the bar's open menu as well.
351
+ createEffect(held, (isHeld) => {
352
+ if (isHeld === true) bar.setValue(value);
353
+ else if (isHeld === false && untrack(bar.value) === value) bar.setValue("");
354
+ });
355
+
356
+ createEffect(isCurrent, (now, before) => {
357
+ if (before === undefined || before === now) return;
358
+ if (isDefaulting) {
359
+ isDefaulting = false;
360
+ return;
361
+ }
362
+ untrack(() => props.onOpenChange?.())?.(now);
363
+ });
364
+
365
+ // Another menu taking over closes this one from outside, without going
366
+ // through `close`, so the submenus it had open are forgotten here.
367
+ createEffect(
368
+ () => bar.value() !== value && state.expandedKeys().length > 0,
369
+ (taken) => {
370
+ if (taken) state.close();
371
+ },
372
+ );
373
+
374
+ const { menuTriggerProps, menuProps } = menuTrigger({ isDisabled }, state);
375
+
376
+ const nextKey = (): string => (bar.direction() === "rtl" ? "ArrowLeft" : "ArrowRight");
377
+
378
+ const trigger: MenuTriggerValue = {
379
+ state,
380
+ triggerRef,
381
+ menuTriggerProps,
382
+ menuProps,
383
+ anchorRect: () => null,
384
+ placement: "bottom start",
385
+ offset: 8,
386
+ onMenuKeyDown: (event, isSubmenu) => {
387
+ if (event.defaultPrevented) return;
388
+ if (event.key !== "ArrowLeft" && event.key !== "ArrowRight") return;
389
+ const forwards = event.key === nextKey();
390
+ const item = targetElement(event)?.closest('[role^="menuitem"]') ?? null;
391
+ // The arrow pointing at a submenu opens it, and the one pointing back at
392
+ // a submenu's trigger closes it. Neither is the bar's.
393
+ if (
394
+ forwards &&
395
+ item !== null &&
396
+ item.hasAttribute("aria-haspopup") &&
397
+ item.getAttribute("aria-disabled") !== "true"
398
+ ) {
399
+ return;
400
+ }
401
+ if (!forwards && isSubmenu) return;
402
+
403
+ const to = bar.towards(value, forwards ? "next" : "previous");
404
+ if (to === null || to.value === value) return;
405
+ event.preventDefault();
406
+ event.stopPropagation();
407
+ bar.switchTo(to.value, "first");
408
+ },
409
+ };
410
+
411
+ const menu: MenubarMenuValue = { value, trigger, setDisabled };
412
+
413
+ return provide(
414
+ MenubarMenuContext,
415
+ () => menu,
416
+ () => provideMenuTrigger(trigger, () => props.children),
417
+ );
418
+ }
419
+
420
+ export interface MenubarTriggerOptions extends ButtonOptions {}
421
+
422
+ export interface MenubarTriggerResult {
423
+ /** For the trigger's element: the ARIA, the roving `tabindex`, the handlers. */
424
+ triggerProps: DOMProps;
425
+ /**
426
+ * For the same element. The menu is anchored to it and returns focus to it.
427
+ *
428
+ * Method syntax for the reason `TriggerSlot.ref` gives: the ref behind it is
429
+ * an `HTMLElement`'s, and whoever takes this hands it their own element.
430
+ */
431
+ ref(this: void, element: Element | null): void;
432
+ isOpen: Accessor<boolean>;
433
+ }
434
+
435
+ /**
436
+ * The trigger of the enclosing {@link MenubarMenu}, for an element of the
437
+ * caller's own.
438
+ *
439
+ * A `menuitem` of the bar, which is what makes a screen reader announce the
440
+ * row as one menubar of N items rather than N unrelated buttons.
441
+ */
442
+ export function menubarTrigger(options: MenubarTriggerOptions = {}): MenubarTriggerResult {
443
+ const bar = useMenubar();
444
+ const menu = getContext(MenubarMenuContext);
445
+ if (menu === null || menu === undefined) {
446
+ throw new Error("A MenubarTrigger must be rendered inside a MenubarMenu.");
447
+ }
448
+ const { value, trigger } = menu;
449
+ const isDisabled = (): boolean => access(options.isDisabled) === true;
450
+
451
+ createEffect(isDisabled, (disabled) => {
452
+ menu.setDisabled(disabled);
453
+ });
454
+
455
+ /** With a menu open the arrows carry it along; with none they only move focus. */
456
+ const focusOrSwitch = (to: MenubarEntry): void => {
457
+ if (bar.value() !== "") {
458
+ bar.switchTo(to.value, null);
459
+ return;
460
+ }
461
+ const element = to.trigger();
462
+ if (element !== null) focusWithoutScrolling(element);
463
+ };
464
+
465
+ const moveTo = (step: Step): boolean => {
466
+ const to = bar.towards(value, step);
467
+ if (to === null) return false;
468
+ focusOrSwitch(to);
469
+ return true;
470
+ };
471
+
472
+ const onKeyDown = (event: KeyboardEvent): void => {
473
+ // Enter, Space and the vertical arrows are the menu trigger's, and it has
474
+ // already taken them.
475
+ if (event.defaultPrevented || event.altKey || event.ctrlKey || event.metaKey) return;
476
+ const rtl = bar.direction() === "rtl";
477
+ let handled = false;
478
+
479
+ switch (event.key) {
480
+ case "ArrowRight":
481
+ handled = moveTo(rtl ? "previous" : "next");
482
+ break;
483
+ case "ArrowLeft":
484
+ handled = moveTo(rtl ? "next" : "previous");
485
+ break;
486
+ case "Home":
487
+ handled = moveTo("first");
488
+ break;
489
+ case "End":
490
+ handled = moveTo("last");
491
+ break;
492
+ case "Escape":
493
+ if (trigger.state.isOpen()) {
494
+ trigger.state.close();
495
+ handled = true;
496
+ }
497
+ break;
498
+ default:
499
+ if (event.key.length === 1 && event.key !== " ") {
500
+ const to = bar.matching(value, event.key);
501
+ if (to !== null) {
502
+ focusOrSwitch(to);
503
+ handled = true;
504
+ }
505
+ }
506
+ break;
507
+ }
508
+
509
+ if (handled) {
510
+ event.preventDefault();
511
+ event.stopPropagation();
512
+ }
513
+ };
514
+
515
+ const buttonOptions: ButtonOptions = mergeProps({ ...options }, trigger.menuTriggerProps, {
516
+ onKeyDown,
517
+ });
518
+ const { buttonProps, isPressed } = button(buttonOptions, trigger.triggerRef);
519
+
520
+ const { hoverProps, isHovered } = hover({
521
+ isDisabled,
522
+ onHoverStart: () => {
523
+ const open = bar.value();
524
+ if (open !== "" && open !== value) bar.switchTo(value, null);
525
+ },
526
+ });
527
+ const { focusProps, isFocused, isFocusVisible: showsRing } = focusRing();
528
+
529
+ return {
530
+ ref: trigger.triggerRef.set,
531
+ isOpen: trigger.state.isOpen,
532
+ triggerProps: mergeProps(
533
+ buttonProps,
534
+ { id: trigger.menuTriggerProps.id },
535
+ hoverProps,
536
+ focusProps,
537
+ {
538
+ role: "menuitem",
539
+ // Roving: the bar is one Tab stop, and this is it or it is not.
540
+ tabIndex: () => (bar.tabStop() === value ? 0 : -1),
541
+ onFocus: () => bar.setTabStop(value),
542
+ "data-pressed": isPressed,
543
+ "data-hovered": isHovered,
544
+ "data-focused": isFocused,
545
+ "data-highlighted": isFocused,
546
+ "data-focus-visible": showsRing,
547
+ "data-disabled": isDisabled,
548
+ "data-open": () => trigger.state.isOpen(),
549
+ "data-state": () => (trigger.state.isOpen() ? "open" : "closed"),
550
+ },
551
+ ),
552
+ };
553
+ }
554
+
555
+ export interface MenubarTriggerComponentProps extends StyleProps {
556
+ children?: Child;
557
+ isDisabled?: boolean;
558
+ "aria-label"?: string;
559
+ ref?: RefTarget<HTMLButtonElement>;
560
+ }
561
+
562
+ /** The button in the bar that opens the enclosing {@link MenubarMenu}. */
563
+ export function MenubarTrigger(props: MenubarTriggerComponentProps) {
564
+ const options = fromProps(cells(props));
565
+ const { triggerProps, ref } = menubarTrigger(options);
566
+
567
+ const elementProps = mergeProps(
568
+ triggerProps,
569
+ filterDOMProps(options, { global: true }),
570
+ styleProps(cells(props)),
571
+ { "data-testid": () => props["data-testid"] },
572
+ );
573
+
574
+ return (
575
+ <button {...elementProps} ref={mergeRefs(ref, props.ref)}>
576
+ {props.children}
577
+ </button>
578
+ );
579
+ }