@uniflowed/ui 0.0.0-alpha.13 → 0.0.0-alpha.14

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.
package/menubar.js ADDED
@@ -0,0 +1,285 @@
1
+ // @flow
2
+ //
3
+ // A row of menus that behaves as one control: File, Edit, View.
4
+ //
5
+ // It is not a row of `Menu.Root`s, and the reason is that the *set* has a
6
+ // keyboard map of its own — one that only exists because the menus are next to
7
+ // each other:
8
+ //
9
+ // * The whole bar takes **one** stop in the page's tab order, so `Tab` past an
10
+ // application menu is one press rather than six.
11
+ // * `ArrowLeft` / `ArrowRight` move between the top-level menus, mirrored in a
12
+ // right-to-left page because they are the inline axis.
13
+ // * `ArrowDown` opens the menu under the cursor and lands on its first item;
14
+ // `ArrowUp` opens it onto its last, which is the same argument `Menu.Trigger`
15
+ // makes about the destructive command at the bottom of a long menu.
16
+ // * `Home` / `End` go to the first and last menu.
17
+ // * And the part that is always missing: `ArrowLeft` / `ArrowRight` **while a
18
+ // menu is open** close it and open the adjacent one, so a reader can walk
19
+ // File → Edit → View without pressing `Escape` between them. A menubar
20
+ // without it makes the arrow keys mean two different things depending on
21
+ // whether a menu happens to be showing.
22
+ // * `Escape` closes the open menu and leaves focus on its trigger, in the bar,
23
+ // which `Menu.Body` already does — a menubar trigger is the menu's trigger.
24
+ //
25
+ // Everything inside a menu is `menu.js`: the arrow keys within it, typeahead,
26
+ // submenus, the checkable items and the roving tab stop of the menu itself.
27
+ // `Menubar.Body` is `Menu.Body` itself rather than a wrapper around it, because
28
+ // a bar's menu *is* a root menu — it hangs off a button and drops from it — and
29
+ // `Menu.Body` already places a root menu on the bottom, aligned to the start. A
30
+ // wrapper would have been a second component with the same defaults written out
31
+ // again, and a second place for them to drift.
32
+ //
33
+ // # How the bar finds its own triggers
34
+ //
35
+ // `internal/roving-focus.js`'s `itemsOf(container, item, owner)` takes the owner
36
+ // selector as a parameter for exactly this: a set says what owns it. A menu
37
+ // passes `[role="menu"]`, and a menubar has to pass **both** — an open menu is a
38
+ // DOM descendant of the bar, and its items are `role="menuitem"` too, so a bar
39
+ // that only asked "menu items inside me" would step into the open menu's items
40
+ // with `ArrowRight`. Naming the two owners makes `closest` stop at the menu for
41
+ // an item inside one and at the bar for a trigger, which is the distinction, and
42
+ // it is settled by the roles the two containers already carry.
43
+ //
44
+ // Turning a trigger *element* back into the menu it opens is the one thing the
45
+ // roles cannot say, and `data-uf-menubar-value` is that and nothing more. The
46
+ // alternative is a registry of refs, which `roving-focus.js` explains at length
47
+ // is a second opinion about document order.
48
+ //
49
+ // # Which menu is open is the bar's state
50
+ //
51
+ // One value, `string | null`, rather than a boolean per menu. Two menus open at
52
+ // once is the state this component exists to prevent, and a per-menu boolean is
53
+ // a set of booleans somebody has to keep exclusive; `accordion.js` makes the
54
+ // same argument about `single`. It is also what makes "close this one and open
55
+ // the next" a single assignment rather than a pair of them that render twice.
56
+
57
+ "use client";
58
+
59
+ import * as React from "@uniflowed/react";
60
+ import {
61
+ createContext,
62
+ useCallback,
63
+ useContext,
64
+ useMemo,
65
+ useRef,
66
+ useState,
67
+ } from "@uniflowed/react";
68
+
69
+ import type { Rest } from "./internal/merge-props.js";
70
+ import { composeHandlers, composeRefs, withoutComposed } from "./internal/merge-props.js";
71
+ import type { RovingSet } from "./internal/roving-focus.js";
72
+ import {
73
+ directionOf,
74
+ indexOfActive,
75
+ itemsOf,
76
+ movementFor,
77
+ moveTo,
78
+ useFirstItem,
79
+ } from "./internal/roving-focus.js";
80
+ import { MenuLevel, useMenu } from "./internal/menu-tree.js";
81
+
82
+ /**
83
+ * The bar's own items, and the two things that may own one.
84
+ *
85
+ * See the module header: an open menu is inside the bar and its items wear the
86
+ * same role, so the owner selector names both and `closest` settles it.
87
+ */
88
+ const TRIGGERS: RovingSet = {
89
+ item: '[role="menuitem"]',
90
+ owner: '[role="menu"], [role="menubar"]',
91
+ orientation: "horizontal",
92
+ wrap: true,
93
+ skipDisabled: true,
94
+ };
95
+
96
+ type MenubarState = {|
97
+ /** Which menu is showing, by the `value` its `Menubar.Menu` was given. */
98
+ readonly open: string | null,
99
+ readonly setOpen: (value: string | null) => void,
100
+ /** Which trigger holds the bar's single tab stop, or null for "the first". */
101
+ readonly active: string | null,
102
+ readonly setActive: (value: string) => void,
103
+ readonly firstId: string | null,
104
+ |};
105
+
106
+ const MenubarContext: React.Context<MenubarState | null> = createContext(null);
107
+
108
+ /** The `value` of the `Menubar.Menu` a trigger belongs to. */
109
+ const MenubarMenuContext: React.Context<string | null> = createContext(null);
110
+
111
+ hook useMenubar(part: string): MenubarState {
112
+ const state = useContext(MenubarContext);
113
+ if (state == null) {
114
+ throw new Error(`${part} must be rendered inside a Menubar.Root`);
115
+ }
116
+ return state;
117
+ }
118
+
119
+ /**
120
+ * The bar: `role="menubar"`, one tab stop, and the arrows between the menus.
121
+ *
122
+ * `aria-label` is the caller's and matters more here than on most containers —
123
+ * a page with an application menubar and a formatting toolbar has two, and
124
+ * "menu bar" twice tells a reader nothing about which is which.
125
+ */
126
+ export component MenubarRoot(children: renders* MenubarMenu, ...rest: Rest) {
127
+ const barRef = useRef<HTMLElement | null>(null);
128
+ const [open, setOpenValue] = useState<string | null>(null);
129
+ const [active, setActive] = useState<string | null>(null);
130
+ // Only while nothing has been focused or opened. Once a trigger holds the tab
131
+ // stop, asking the document which one comes first is work with no reader.
132
+ const firstId = useFirstItem(barRef, TRIGGERS, active == null);
133
+
134
+ const setOpen = useCallback((value: string | null) => {
135
+ setOpenValue(value);
136
+ if (value != null) {
137
+ setActive(value);
138
+ }
139
+ }, []);
140
+
141
+ const state = useMemo(
142
+ () => ({ open, setOpen, active, setActive, firstId }),
143
+ [open, setOpen, active, firstId],
144
+ );
145
+ const passed = withoutComposed(rest, ["onKeyDown", "ref"]);
146
+
147
+ return (
148
+ <MenubarContext.Provider value={state}>
149
+ <div
150
+ {...passed}
151
+ aria-orientation="horizontal"
152
+ onKeyDown={composeHandlers(rest.onKeyDown, (event) => {
153
+ const bar: $FlowFixMe = event.currentTarget;
154
+ const movement = movementFor(event.key, "horizontal", directionOf(bar));
155
+ if (movement == null) {
156
+ return;
157
+ }
158
+ const triggers = itemsOf(bar, TRIGGERS.item, TRIGGERS.owner);
159
+ // With a menu open, focus is on one of *its* items rather than on a
160
+ // trigger, so "where am I in the bar" is the expanded trigger. This
161
+ // is the whole of walking File → Edit → View without pressing Escape.
162
+ const focused = indexOfActive(triggers, bar.ownerDocument?.activeElement);
163
+ const at =
164
+ focused >= 0
165
+ ? focused
166
+ : triggers.findIndex((each) => each.getAttribute("aria-expanded") === "true");
167
+ const next = moveTo(triggers, at, movement, TRIGGERS.wrap, TRIGGERS.skipDisabled);
168
+ if (next == null) {
169
+ return;
170
+ }
171
+ // Claimed before focus moves, or the browser scrolls the page under
172
+ // the trigger that has just taken it; `moveOnKey` says the same.
173
+ event.preventDefault();
174
+ event.stopPropagation();
175
+ const value = next.getAttribute("data-uf-menubar-value");
176
+ if (open != null && value != null) {
177
+ // Swap which menu is showing. Focus lands on the new menu's first
178
+ // item through `Menu.Body`'s own opening effect, so nothing here
179
+ // moves it: focusing the trigger as well would be two focus moves
180
+ // in one commit and the reader would see the second.
181
+ setOpen(value);
182
+ return;
183
+ }
184
+ next.focus();
185
+ if (value != null) {
186
+ setActive(value);
187
+ }
188
+ })}
189
+ ref={composeRefs(rest.ref, (element) => {
190
+ barRef.current = element;
191
+ })}
192
+ role="menubar"
193
+ >
194
+ {children}
195
+ </div>
196
+ </MenubarContext.Provider>
197
+ );
198
+ }
199
+
200
+ /**
201
+ * One menu of the bar, named by the `value` the bar opens and closes it with.
202
+ *
203
+ * Renders no element of its own, for the reason `Menu.Root` gives: a trigger and
204
+ * its body are siblings in whatever layout the caller wrote.
205
+ */
206
+ export component MenubarMenu(children: React.Node, value: string) {
207
+ const bar = useMenubar("Menubar.Menu");
208
+ const setOpen = bar.setOpen;
209
+ const onOpenChange = useCallback(
210
+ (next: boolean) => setOpen(next ? value : null),
211
+ [setOpen, value],
212
+ );
213
+
214
+ return (
215
+ <MenubarMenuContext.Provider value={value}>
216
+ <MenuLevel
217
+ defaultOpen={false}
218
+ onOpenChange={onOpenChange}
219
+ open={bar.open === value}
220
+ parent={null}
221
+ >
222
+ {children}
223
+ </MenuLevel>
224
+ </MenubarMenuContext.Provider>
225
+ );
226
+ }
227
+
228
+ /**
229
+ * The button that opens one of the bar's menus.
230
+ *
231
+ * `role="menuitem"` rather than a plain button, because it *is* an item of the
232
+ * menubar — a reader is told "File, menu item, has popup, 1 of 3" — and it is
233
+ * what makes the bar's arrow keys agree with what they were told is in it.
234
+ */
235
+ export component MenubarTrigger(children: React.Node, ...rest: Rest) {
236
+ const bar = useMenubar("Menubar.Trigger");
237
+ const menu = useMenu("Menubar.Trigger");
238
+ const value = useContext(MenubarMenuContext);
239
+ if (value == null) {
240
+ throw new Error("Menubar.Trigger must be rendered inside a Menubar.Menu");
241
+ }
242
+ const passed = withoutComposed(rest, ["onClick", "onFocus", "onKeyDown", "ref"]);
243
+ const id = `${menu.base}-trigger`;
244
+ // The bar's single tab stop. Before anything has been focused or opened it
245
+ // belongs to the first trigger, which is a fact about the document rather
246
+ // than about this component — `useFirstItem` reads it in the bar.
247
+ const stop = bar.active == null ? bar.firstId === id : bar.active === value;
248
+
249
+ return (
250
+ <button
251
+ {...passed}
252
+ aria-controls={menu.open ? `${menu.base}-body` : undefined}
253
+ aria-expanded={menu.open ? "true" : "false"}
254
+ aria-haspopup="menu"
255
+ // How the bar's keyboard turns a trigger element back into the menu it
256
+ // opens; the module header says why this is an attribute and the rest of
257
+ // the bar's arithmetic is not.
258
+ data-uf-menubar-value={value}
259
+ id={id}
260
+ onClick={composeHandlers(rest.onClick, () => bar.setOpen(menu.open ? null : value))}
261
+ onFocus={composeHandlers(rest.onFocus, () => bar.setActive(value))}
262
+ onKeyDown={composeHandlers(rest.onKeyDown, (event) => {
263
+ const end = match (event.key) {
264
+ "ArrowDown" => "first",
265
+ "ArrowUp" => "last",
266
+ _ => null,
267
+ };
268
+ if (end == null) {
269
+ return;
270
+ }
271
+ event.preventDefault();
272
+ menu.pendingFocus.current = end;
273
+ bar.setOpen(value);
274
+ })}
275
+ ref={composeRefs(rest.ref, (element) => {
276
+ menu.triggerRef.current = element;
277
+ })}
278
+ role="menuitem"
279
+ tabIndex={stop ? 0 : -1}
280
+ type="button"
281
+ >
282
+ {children}
283
+ </button>
284
+ );
285
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniflowed/ui",
3
- "version": "0.0.0-alpha.13",
3
+ "version": "0.0.0-alpha.14",
4
4
  "description": "Headless, accessible React components whose composition Flow checks, part of the Unified Toolchain for Flow.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -19,6 +19,7 @@
19
19
  "./checkbox": "./checkbox.js",
20
20
  "./collapsible": "./collapsible.js",
21
21
  "./combobox": "./combobox.js",
22
+ "./context-menu": "./context-menu.js",
22
23
  "./date-picker": "./date-picker.js",
23
24
  "./dialog": "./dialog.js",
24
25
  "./drawer": "./drawer.js",
@@ -26,6 +27,7 @@
26
27
  "./hover-card": "./hover-card.js",
27
28
  "./input-otp": "./input-otp.js",
28
29
  "./menu": "./menu.js",
30
+ "./menubar": "./menubar.js",
29
31
  "./navigation-menu": "./navigation-menu.js",
30
32
  "./pagination": "./pagination.js",
31
33
  "./popover": "./popover.js",
@@ -50,9 +52,9 @@
50
52
  "internal"
51
53
  ],
52
54
  "dependencies": {
53
- "@uniflowed/core": "0.0.0-alpha.13",
54
- "@uniflowed/hooks": "0.0.0-alpha.13",
55
- "@uniflowed/react": "0.0.0-alpha.13"
55
+ "@uniflowed/core": "0.0.0-alpha.14",
56
+ "@uniflowed/hooks": "0.0.0-alpha.14",
57
+ "@uniflowed/react": "0.0.0-alpha.14"
56
58
  },
57
59
  "peerDependencies": {
58
60
  "react": ">=19"
package/switch.js CHANGED
@@ -19,9 +19,11 @@
19
19
  //
20
20
  // The keyboard follows from the same distinction. `Space` toggles both. `Enter`
21
21
  // toggles a *switch*, because a switch is an operation and pressing Enter on
22
- // something that operates is what a reader expects — while `checkbox.js`
23
- // deliberately leaves `Enter` alone so that a checkbox inside a form still
24
- // submits it. That is the whole reason these are not one file with a flag.
22
+ // something that operates is what a reader expects — while `checkbox.js` turns
23
+ // `Enter` into the submission of the form the checkbox is in, which is what a
24
+ // native `<input type="checkbox">` does with the key and what a reader
25
+ // answering a question on their way to a submit button is asking for. That is
26
+ // the whole reason these are not one file with a flag.
25
27
 
26
28
  "use client";
27
29
 
package/toggle.js CHANGED
@@ -28,8 +28,9 @@
28
28
  //
29
29
  // Because a toggle button is a button, and a button activates on both. That is
30
30
  // the same reasoning `switch.js` gives for `Enter`, and the opposite of
31
- // `checkbox.js`, which leaves `Enter` alone because a checkbox is something a
32
- // reader answers on their way to submitting a form.
31
+ // `checkbox.js`, where `Enter` submits the form rather than touching the
32
+ // control — because a checkbox is something a reader answers on their way to
33
+ // submitting a form, and that is what the native one does with the key.
33
34
  //
34
35
  // # It is `disabled`, not `aria-disabled`
35
36
  //