@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,184 @@
1
+ /**
2
+ * A toolbar: a row of controls that Tab enters ONCE.
3
+ *
4
+ * Without one, a formatting bar of twelve buttons is twelve Tab stops between
5
+ * the user and the next field. `role="toolbar"` says the arrows move inside it
6
+ * and Tab leaves it, which is the same bargain a listbox or a tab list makes,
7
+ * and it is the reason the role exists.
8
+ *
9
+ * Two things make it work rather than merely claim to:
10
+ *
11
+ * - **Tab leaves from the END.** Focus is moved to the last control before the
12
+ * browser handles the key, so its own Tab continues past the whole toolbar
13
+ * rather than into the middle of it. Shift+Tab moves to the first.
14
+ * - **Coming back lands where you left.** The control that had focus when the
15
+ * toolbar was left is remembered and refocused, so a toolbar is not a place
16
+ * you have to arrow across again every time.
17
+ *
18
+ * A toolbar inside a toolbar takes no arrow keys of its own: the outer one is
19
+ * already handling them, and two handlers on one keypress move focus twice.
20
+ */
21
+
22
+ import { cells, type Child, createEffect, createSignal } from "@barqjs/core";
23
+ import { isServer } from "@barqjs/web";
24
+ import { ref as makeRef, mergeRefs, type RefTarget } from "@barqjs/primitives/refs";
25
+ import { contains, targetElement } from "./dom.ts";
26
+ import { createFocusManager } from "./focus.ts";
27
+ import { useLocale } from "./i18n.ts";
28
+ import { getInteractionModality } from "./interactions/modality.ts";
29
+ import type { ElementRef } from "./interactions/press.ts";
30
+ import type { Orientation } from "./selection.ts";
31
+ import {
32
+ access,
33
+ type DOMProps,
34
+ filterDOMProps,
35
+ fromProps,
36
+ type MaybeAccessor,
37
+ mergeProps,
38
+ type StyleProps,
39
+ styleProps,
40
+ } from "./utils.ts";
41
+
42
+ export interface ToolbarOptions {
43
+ ref: ElementRef;
44
+ /** @default "horizontal" */
45
+ orientation?: MaybeAccessor<Orientation | undefined>;
46
+ "aria-label"?: MaybeAccessor<string | undefined>;
47
+ "aria-labelledby"?: MaybeAccessor<string | undefined>;
48
+ }
49
+
50
+ export interface ToolbarResult {
51
+ toolbarProps: DOMProps;
52
+ }
53
+
54
+ export function toolbar(options: ToolbarOptions): ToolbarResult {
55
+ const locale = useLocale();
56
+ const manager = createFocusManager(options.ref);
57
+ const [nested, setNested] = createSignal(false);
58
+
59
+ const orientation = (): Orientation => access(options.orientation) ?? "horizontal";
60
+ // Left and right follow the WRITING direction; up and down never do.
61
+ const flipped = (): boolean => locale().direction === "rtl" && orientation() === "horizontal";
62
+
63
+ if (!isServer) {
64
+ createEffect(
65
+ () => access(options.ref) as Element | null,
66
+ (element) => {
67
+ const outer = element?.parentElement?.closest('[role="toolbar"]');
68
+ setNested(outer !== null && outer !== undefined);
69
+ },
70
+ );
71
+ }
72
+
73
+ let lastFocused: HTMLElement | null = null;
74
+
75
+ const onKeyDown = (event: KeyboardEvent): void => {
76
+ // A key from a portalled subtree — a menu the toolbar opened — is not the
77
+ // toolbar's to act on, whatever the DOM says about where it bubbled.
78
+ const target = targetElement(event);
79
+ if (!contains(event.currentTarget as Element, target)) return;
80
+ if (nested()) return;
81
+
82
+ const forwards = orientation() === "horizontal" ? "ArrowRight" : "ArrowDown";
83
+ const backwards = orientation() === "horizontal" ? "ArrowLeft" : "ArrowUp";
84
+
85
+ if (event.key === "Tab") {
86
+ // Move to the end the browser is about to leave from, and let it.
87
+ lastFocused = (target as HTMLElement | null) ?? null;
88
+ if (event.shiftKey) manager.focusFirst();
89
+ else manager.focusLast();
90
+ return;
91
+ }
92
+
93
+ if (event.key === forwards) {
94
+ if (flipped()) manager.focusPrevious();
95
+ else manager.focusNext();
96
+ } else if (event.key === backwards) {
97
+ if (flipped()) manager.focusNext();
98
+ else manager.focusPrevious();
99
+ } else {
100
+ return;
101
+ }
102
+
103
+ // A nested action group must not move focus a second time for one key.
104
+ event.stopPropagation();
105
+ event.preventDefault();
106
+ };
107
+
108
+ const onBlur = (event: FocusEvent): void => {
109
+ const current = event.currentTarget as Element;
110
+ if (contains(current, event.relatedTarget as Element | null)) return;
111
+ if (lastFocused !== null) return;
112
+ lastFocused = targetElement(event) as HTMLElement | null;
113
+ };
114
+
115
+ const onFocus = (event: FocusEvent): void => {
116
+ const current = event.currentTarget as Element;
117
+ if (lastFocused === null) return;
118
+ // Only when focus came from OUTSIDE: moving between two controls inside
119
+ // is the roving focus doing its job.
120
+ if (contains(current, event.relatedTarget as Element | null)) return;
121
+ if (!contains(current, targetElement(event))) return;
122
+ // Only for the keyboard: a pointer landed on the control it meant, and
123
+ // moving focus off it would take the press with it. Gone from the document
124
+ // since: whatever the browser landed on stands.
125
+ if (getInteractionModality() === "keyboard" && lastFocused.isConnected) lastFocused.focus();
126
+ lastFocused = null;
127
+ };
128
+
129
+ return {
130
+ toolbarProps: mergeProps(filterDOMProps(options, { labelable: true }), {
131
+ // A toolbar inside another is a group of the outer one's controls: the
132
+ // outer toolbar is the one stop in the Tab order.
133
+ role: () => (nested() ? "group" : "toolbar"),
134
+ "aria-orientation": orientation,
135
+ onKeyDown,
136
+ onFocusIn: onFocus,
137
+ onFocusOut: onBlur,
138
+ }),
139
+ };
140
+ }
141
+
142
+ // ---------------------------------------------------------------------------
143
+ // Component
144
+ // ---------------------------------------------------------------------------
145
+
146
+ export interface ToolbarComponentProps extends StyleProps {
147
+ children?: Child;
148
+ /** @default "horizontal" */
149
+ orientation?: Orientation;
150
+ "aria-label"?: string;
151
+ "aria-labelledby"?: string;
152
+ ref?: RefTarget<HTMLDivElement>;
153
+ }
154
+
155
+ /**
156
+ * ```tsx
157
+ * <Toolbar aria-label="Formatting">
158
+ * <ToggleButton>Bold</ToggleButton>
159
+ * <ToggleButton>Italic</ToggleButton>
160
+ * </Toolbar>
161
+ * ```
162
+ */
163
+ export function Toolbar(props: ToolbarComponentProps) {
164
+ const domRef = makeRef<HTMLDivElement>();
165
+ const options = fromProps(cells(props));
166
+
167
+ const { toolbarProps } = toolbar({ ...(options as unknown as ToolbarOptions), ref: domRef });
168
+
169
+ const elementProps = mergeProps(
170
+ toolbarProps,
171
+ filterDOMProps(options, { global: true }),
172
+ styleProps(cells(props)),
173
+ {
174
+ "data-orientation": () => props.orientation ?? "horizontal",
175
+ "data-testid": () => props["data-testid"],
176
+ },
177
+ );
178
+
179
+ return (
180
+ <div {...elementProps} ref={mergeRefs(domRef.set, props.ref)}>
181
+ {props.children}
182
+ </div>
183
+ );
184
+ }