@bigpixelrocket/uiok 0.5.0 → 0.6.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 (83) hide show
  1. package/CHANGELOG.md +36 -1
  2. package/dist/button/TextLink.d.ts +9 -2
  3. package/dist/button/TextLink.js +4 -3
  4. package/dist/button/button.css +7 -0
  5. package/dist/dialog/SectionedDialog.d.ts +37 -0
  6. package/dist/dialog/SectionedDialog.js +157 -0
  7. package/dist/dialog/dialog.css +102 -0
  8. package/dist/dialog/index.d.ts +4 -2
  9. package/dist/dialog/index.js +4 -2
  10. package/dist/frame/AppRoot.d.ts +1 -1
  11. package/dist/frame/AppRoot.js +2 -1
  12. package/dist/frame/Dock.d.ts +1 -1
  13. package/dist/frame/Dock.js +2 -1
  14. package/dist/frame/FilterChips.d.ts +5 -2
  15. package/dist/frame/FilterChips.js +35 -2
  16. package/dist/frame/FilterChips.logic.d.ts +24 -0
  17. package/dist/frame/FilterChips.logic.js +34 -0
  18. package/dist/frame/PageParts.d.ts +4 -3
  19. package/dist/frame/PageParts.js +10 -3
  20. package/dist/frame/SideBySide.d.ts +32 -0
  21. package/dist/frame/SideBySide.js +50 -0
  22. package/dist/frame/Tree.d.ts +1 -1
  23. package/dist/frame/Tree.js +3 -2
  24. package/dist/frame/WindowFrame.d.ts +1 -1
  25. package/dist/frame/WindowFrame.js +2 -1
  26. package/dist/frame/frame.css +48 -1
  27. package/dist/frame/index.d.ts +4 -2
  28. package/dist/frame/index.js +4 -2
  29. package/dist/grid/grid.css +1 -0
  30. package/dist/keyboard/Keyboard.logic.js +20 -3
  31. package/dist/keychord/KeyChord.d.ts +17 -1
  32. package/dist/keychord/KeyChord.js +16 -1
  33. package/dist/keychord/ShortcutIndex.d.ts +49 -0
  34. package/dist/keychord/ShortcutIndex.js +28 -0
  35. package/dist/keychord/index.d.ts +8 -2
  36. package/dist/keychord/index.js +8 -2
  37. package/dist/keychord/keychord.css +101 -0
  38. package/dist/labels/dialog.d.ts +2 -0
  39. package/dist/labels/dialog.js +2 -1
  40. package/dist/labels/keys.d.ts +5 -0
  41. package/dist/labels/keys.js +2 -1
  42. package/dist/listing/Listing.d.ts +36 -4
  43. package/dist/listing/Listing.js +21 -6
  44. package/dist/listing/PaneMessage.d.ts +7 -4
  45. package/dist/listing/PaneMessage.js +11 -4
  46. package/dist/listing/index.d.ts +1 -1
  47. package/dist/listing/listing.css +84 -4
  48. package/dist/menu/Menu.d.ts +52 -8
  49. package/dist/menu/Menu.js +288 -80
  50. package/dist/menu/Menu.logic.d.ts +39 -12
  51. package/dist/menu/Menu.logic.js +36 -8
  52. package/dist/menu/index.d.ts +4 -3
  53. package/dist/menu/index.js +2 -1
  54. package/dist/menu/menu.css +6 -0
  55. package/dist/palette/PaletteRows.d.ts +11 -2
  56. package/dist/palette/PaletteRows.js +6 -4
  57. package/dist/pane/Pane.d.ts +42 -6
  58. package/dist/pane/Pane.js +92 -5
  59. package/dist/pane/PaneKeys.d.ts +5 -1
  60. package/dist/pane/PaneKeys.js +28 -3
  61. package/dist/pane/PaneKeys.logic.d.ts +25 -0
  62. package/dist/pane/PaneKeys.logic.js +30 -0
  63. package/dist/pane/index.d.ts +1 -1
  64. package/dist/pane/index.js +1 -1
  65. package/dist/pane/usePaneKeys.d.ts +8 -1
  66. package/dist/pane/usePaneKeys.js +32 -12
  67. package/dist/styles.css +17 -3
  68. package/dist/surface/FactList.d.ts +54 -0
  69. package/dist/surface/FactList.js +33 -0
  70. package/dist/surface/index.d.ts +4 -1
  71. package/dist/surface/index.js +4 -1
  72. package/dist/surface/surface.css +75 -2
  73. package/dist/terminal/ghostty/core.d.ts +12 -4
  74. package/dist/terminal/ghostty/core.js +12 -4
  75. package/dist/terminal/index.d.ts +4 -2
  76. package/dist/terminal/index.js +4 -2
  77. package/dist/text/Code.d.ts +15 -0
  78. package/dist/text/Code.js +12 -0
  79. package/dist/text/index.d.ts +6 -1
  80. package/dist/text/index.js +6 -1
  81. package/dist/text/text.css +21 -0
  82. package/dist/themes/tokens.js +3 -1
  83. package/package.json +1 -1
package/dist/menu/Menu.js CHANGED
@@ -1,4 +1,4 @@
1
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
1
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
2
  import { useEffect, useId, useLayoutEffect, useRef, useState } from 'react';
3
3
  import { createPortal } from 'react-dom';
4
4
  import { ICON_MD } from '../icon/sizes.js';
@@ -13,53 +13,51 @@ const revealItem = (itemId, position) => {
13
13
  return;
14
14
  document.getElementById(`${itemId}-${position}`)?.scrollIntoView?.({ block: 'nearest' });
15
15
  };
16
+ const isRow = (entry) => entry.kind === undefined || entry.kind === 'item' || entry.kind === 'submenu';
16
17
  const isItem = (entry) => entry.kind === undefined || entry.kind === 'item';
18
+ const pickableRows = (rows) => rows.map((row) => ({ key: row.id, taken: row.disabled === true }));
19
+ /** Whether an element reads right to left, where a submenu stands on the left. */
20
+ const isRtl = (element) => getComputedStyle(element).direction === 'rtl';
21
+ const NO_BOX = { left: 0, top: 0, right: 0, bottom: 0 };
17
22
  /**
18
- * A menu of verbs or places, opened at a point (a secondary click's) or
19
- * dropped from an element (a crumb's ⌄, an Actions button): each item its
20
- * glyph, its words, and its chord as key caps; a destructive one in the
21
- * danger tone; a setting with its check; the current place with its dot;
22
- * separators and group headings between them. It is the menu look
23
- * (`.folder-menu`) drawn through a portal on the document's body (or the
24
- * element shown full screen), so no pane or dialog clips it, placed inside
25
- * the window (`placeMenu`); React still carries its events to its owner.
26
- *
27
- * It takes the focus and holds the keys while open (`menuKeyAction`: ↑ ↓,
28
- * Home, End, Return and Space, Escape and Tab), which travel no further, so
29
- * neither a dialog nor the page hears them; `data-native-keyboard` keeps
30
- * the browser-key guard and a page's own key handlers out
31
- * (`isNativeKeyboardTarget`), and `data-overlay` marks it an open overlay
32
- * while it shows. A press on it never takes the focus from it; a press or
33
- * a focus anywhere else closes it. A menu taller than the window less its
34
- * margins scrolls, and the keys keep the cursor's item in view.
23
+ * How long the pointer may cross other items on its way into an open
24
+ * submenu, in milliseconds, before the item it stays on takes the cursor
25
+ * and the submenu gives way.
35
26
  */
36
- export function Menu({ label, entries, at, anchor = null, initial = null, compact = false, onPick, onClose }) {
37
- const rootRef = useRef(null);
38
- const itemId = useId();
39
- const [pointed, setPointed] = useState(initial);
40
- const items = entries.filter(isItem);
41
- const rows = items.map((item) => ({ key: item.id, taken: item.disabled === true }));
42
- const cursor = pickableCursor(rows, pointed);
43
- const cursorItem = cursor === null ? null : (items[cursor] ?? null);
44
- const onCloseRef = useRef(onClose);
27
+ const SUBMENU_GRACE_MS = 250;
28
+ /**
29
+ * The surface a menu or its submenu is drawn on, through a portal on the
30
+ * document's body (or the element shown full screen), placed after layout,
31
+ * when its size is known, and placed again when the window resizes, the
32
+ * cursor's item kept in view.
33
+ */
34
+ function MenuSurface({ surfaceRef, id, label, labelledBy, entries, cursor, itemId, expanded, compact, at, anchor, besideId, onKeyDown, onBlur, onMouseMove, onMouseLeave, dir, onRowClick, onRowMove }) {
45
35
  const cursorRef = useRef(cursor);
46
36
  useLayoutEffect(() => {
47
- onCloseRef.current = onClose;
48
37
  cursorRef.current = cursor;
49
38
  });
50
- // Placed after layout, when its own size is known, and given the keys;
51
- // placed again when the window resizes, so it never stands outside it,
52
- // with the cursor's item kept in view and the focus left where it is.
39
+ // Placed again when its menu moves or turns direction: a submenu is given
40
+ // its menu's point and anchor too, so it follows the item it stands beside,
41
+ // and turns sides with the direction, which places a menu itself nowhere new.
42
+ const turn = besideId === undefined ? undefined : dir;
53
43
  useLayoutEffect(() => {
54
- const menu = rootRef.current;
44
+ const menu = surfaceRef.current;
55
45
  if (menu === null)
56
46
  return;
47
+ const from = () => {
48
+ if (besideId !== undefined) {
49
+ const item = document.getElementById(besideId);
50
+ return {
51
+ beside: item?.getBoundingClientRect() ?? NO_BOX,
52
+ rtl: item !== null && isRtl(item)
53
+ };
54
+ }
55
+ return at !== undefined ? { point: at } : { box: anchor?.getBoundingClientRect() ?? NO_BOX };
56
+ };
57
57
  const place = () => {
58
58
  const size = { width: menu.offsetWidth, height: menu.offsetHeight };
59
59
  const viewport = { width: window.innerWidth, height: window.innerHeight };
60
- const spot = placeMenu(at !== undefined
61
- ? { point: at }
62
- : { box: anchor?.getBoundingClientRect() ?? { left: 0, top: 0, right: 0, bottom: 0 } }, size, viewport);
60
+ const spot = placeMenu(from(), size, viewport);
63
61
  menu.style.left = `${spot.left}px`;
64
62
  menu.style.top = `${spot.top}px`;
65
63
  };
@@ -69,75 +67,285 @@ export function Menu({ label, entries, at, anchor = null, initial = null, compac
69
67
  revealItem(itemId, cursorRef.current);
70
68
  };
71
69
  place();
72
- menu.focus({ preventScroll: true });
73
70
  // A menu taller than the window scrolls; it opens with its cursor in view.
74
71
  revealItem(itemId, cursorRef.current);
75
72
  window.addEventListener('resize', replace);
76
73
  return () => window.removeEventListener('resize', replace);
77
- }, [at, anchor, itemId]);
74
+ }, [at, anchor, besideId, turn, itemId, surfaceRef]);
75
+ let index = -1;
76
+ return createPortal(_jsx("div", { "aria-activedescendant": cursor === null ? undefined : `${itemId}-${cursor}`, "aria-label": label, "aria-labelledby": labelledBy, className: compact ? 'folder-menu folder-menu-compact menu-popup' : 'folder-menu menu-popup', "data-native-keyboard": true, "data-overlay": true, dir: dir, id: id, onBlur: onBlur, onContextMenu: (event) => event.preventDefault(), onKeyDown: onKeyDown,
77
+ // A press on an item must not take the focus from the menu, which holds the keys.
78
+ onMouseDown: (event) => event.preventDefault(), onMouseLeave: onMouseLeave, onMouseMove: onMouseMove, ref: surfaceRef, role: "menu", tabIndex: -1, children: entries.map((entry, entryIndex) => {
79
+ if (entry.kind === 'separator') {
80
+ return (_jsx("div", { className: "folder-menu-separator", role: "separator" }, `separator-${entryIndex}`));
81
+ }
82
+ if (entry.kind === 'heading') {
83
+ return (_jsx("div", { className: "entry-menu-heading", role: "presentation", children: entry.label }, `heading-${entryIndex}`));
84
+ }
85
+ index += 1;
86
+ const position = index;
87
+ const Icon = entry.icon;
88
+ const submenu = entry.kind === 'submenu';
89
+ const open = submenu && expanded?.id === entry.id;
90
+ const classes = [
91
+ 'folder-menu-option',
92
+ position === cursor ? 'cursor' : '',
93
+ !submenu && entry.danger === true ? 'danger' : ''
94
+ ]
95
+ .filter(Boolean)
96
+ .join(' ');
97
+ return (_jsxs("div", { "aria-checked": submenu ? undefined : entry.checked, "aria-controls": open ? expanded.menu : undefined, "aria-disabled": entry.disabled === true || undefined, "aria-expanded": submenu ? open : undefined, "aria-haspopup": submenu ? 'menu' : undefined, className: classes, id: `${itemId}-${position}`, onClick: () => onRowClick(entry), onMouseMove: () => onRowMove(entry), role: submenu || entry.checked === undefined ? 'menuitem' : 'menuitemcheckbox', ...describedTooltip(entry.tip), children: [_jsx("span", { "aria-hidden": true, className: "row-icon", children: Icon === undefined ? entry.mark : _jsx(Icon, { size: ICON_MD }) }), _jsxs("span", { className: "menu-label", children: [_jsx("span", { className: "menu-label-text", children: entry.label }), submenu || entry.current === undefined || entry.current === null ? null : (_jsx(CurrentDot, { label: entry.current }))] }), submenu || entry.chord === undefined ? null : _jsx(KeyChord, { chord: entry.chord })] }, entry.id));
98
+ }) }), document.fullscreenElement ?? document.body);
99
+ }
100
+ /**
101
+ * A menu of verbs or places, opened at a point (a secondary click's) or
102
+ * dropped from an element (a crumb's ⌄, an Actions button): each item its
103
+ * glyph, its words, and its chord as key caps; a destructive one in the
104
+ * danger tone; a setting with its check; the current place with its dot;
105
+ * separators and group headings between them; and items that open a
106
+ * submenu beside them, one level deep. It is the menu look
107
+ * (`.folder-menu`) drawn through a portal on the document's body (or the
108
+ * element shown full screen), so no pane or dialog clips it, placed inside
109
+ * the window (`placeMenu`); React still carries its events to its owner.
110
+ *
111
+ * It takes the focus and holds the keys while open (`menuKeyAction`: ↑ ↓,
112
+ * Home, End, Return and Space, → and ←, Escape and Tab), which travel no
113
+ * further, so neither a dialog nor the page hears them; `data-native-keyboard`
114
+ * keeps the browser-key guard and a page's own key handlers out
115
+ * (`isNativeKeyboardTarget`), and `data-overlay` marks it an open overlay
116
+ * while it shows. A press on it never takes the focus from it; a press or
117
+ * a focus anywhere else closes it. A menu taller than the window less its
118
+ * margins scrolls, and the keys keep the cursor's item in view.
119
+ *
120
+ * A submenu opens beside its item (on the inline-end side, or the other
121
+ * when the window has no room there) when the pointer rests on the item or
122
+ * presses it, showing without the keys, or with the keys and its cursor on
123
+ * its first item that can run on → (← in a right-to-left page), Return,
124
+ * or Space. ← goes back to its item, and Escape closes the submenu alone;
125
+ * Tab closes the whole menu. The pointer crossing other items on its way
126
+ * into the submenu leaves it open for a moment, and moving over the
127
+ * submenu gives it the keys.
128
+ */
129
+ export function Menu({ label, entries, at, anchor = null, initial = null, compact = false, dir: dirGiven, onPick, onClose }) {
130
+ const rootRef = useRef(null);
131
+ const originRef = useRef(null);
132
+ const [dirFound, setDirFound] = useState(undefined);
133
+ const dir = dirGiven ?? dirFound;
134
+ const subRef = useRef(null);
135
+ const itemId = useId();
136
+ const subItemId = useId();
137
+ const [pointed, setPointed] = useState(initial);
138
+ const [subState, setSub] = useState(null);
139
+ const grace = useRef(null);
140
+ const rows = entries.filter(isRow);
141
+ const pickable = pickableRows(rows);
142
+ const cursor = pickableCursor(pickable, pointed);
143
+ const cursorRow = cursor === null ? null : (rows[cursor] ?? null);
144
+ const subPlace = subState === null
145
+ ? -1
146
+ : rows.findIndex((row) => row.kind === 'submenu' && row.id === subState.id && !row.disabled);
147
+ const subEntry = subPlace === -1 ? null : rows[subPlace];
148
+ // The submenu shown: none once its item is disabled or gone from `entries`.
149
+ const sub = subEntry === null ? null : subState;
150
+ const subRows = subEntry === null ? [] : subEntry.entries.filter(isItem);
151
+ const subPickable = pickableRows(subRows);
152
+ const subCursor = sub === null || sub.pointed === null ? null : pickableCursor(subPickable, sub.pointed);
153
+ const subCursorRow = subCursor === null ? null : (subRows[subCursor] ?? null);
154
+ const keysInSub = subEntry !== null && sub?.keys === true;
155
+ const onCloseRef = useRef(onClose);
156
+ const subCursorRef = useRef(subCursor);
157
+ useLayoutEffect(() => {
158
+ onCloseRef.current = onClose;
159
+ subCursorRef.current = subCursor;
160
+ });
161
+ // Drawn on the body, it reads in the direction of what opened it, read
162
+ // again when the anchor changes or a given direction is taken away (the
163
+ // marker the fallback reads mounts only then).
164
+ useLayoutEffect(() => {
165
+ const opener = anchor ?? originRef.current;
166
+ if (opener !== null)
167
+ setDirFound(isRtl(opener) ? 'rtl' : 'ltr');
168
+ }, [anchor, dirGiven]);
169
+ // An item disabled or gone takes its submenu with it.
170
+ useEffect(() => {
171
+ if (subState !== null && subEntry === null)
172
+ setSub(null);
173
+ }, [subState, subEntry]);
174
+ // The keys stay where the menu holds them, the menu or its submenu, and
175
+ // come back to it when it moves (the anchor may have taken them). Keys
176
+ // taking over a submenu the pointer opened bring its cursor into view.
177
+ useLayoutEffect(() => {
178
+ (keysInSub ? subRef : rootRef).current?.focus({ preventScroll: true });
179
+ if (keysInSub)
180
+ revealItem(subItemId, subCursorRef.current);
181
+ }, [keysInSub, at, anchor, subItemId]);
78
182
  // A press outside closes it, but one on its anchor, which toggles it.
79
183
  useEffect(() => {
80
184
  const listener = (event) => {
81
185
  const target = event.target;
82
186
  if (!(target instanceof Node))
83
187
  return;
84
- if (rootRef.current?.contains(target) || anchor?.contains(target))
188
+ if (rootRef.current?.contains(target) ||
189
+ subRef.current?.contains(target) ||
190
+ anchor?.contains(target))
85
191
  return;
86
192
  onCloseRef.current('pointer');
87
193
  };
88
194
  document.addEventListener('mousedown', listener, true);
89
195
  return () => document.removeEventListener('mousedown', listener, true);
90
196
  }, [anchor]);
197
+ useEffect(() => () => {
198
+ if (grace.current !== null)
199
+ window.clearTimeout(grace.current.timer);
200
+ }, []);
201
+ const clearGrace = () => {
202
+ if (grace.current === null)
203
+ return;
204
+ window.clearTimeout(grace.current.timer);
205
+ grace.current = null;
206
+ };
91
207
  const run = (item) => {
92
- if (item !== null && item.disabled !== true)
208
+ if (item !== null && item.kind !== 'submenu' && item.disabled !== true)
93
209
  onPick(item.id);
94
210
  };
95
- let index = -1;
96
- return createPortal(_jsx("div", { "aria-activedescendant": cursor === null ? undefined : `${itemId}-${cursor}`, "aria-label": label, className: compact ? 'folder-menu folder-menu-compact menu-popup' : 'folder-menu menu-popup', "data-native-keyboard": true, "data-overlay": true, onBlur: (event) => {
97
- const to = event.relatedTarget;
98
- if (to instanceof Node && (event.currentTarget.contains(to) || anchor?.contains(to)))
99
- return;
211
+ // The keys go back to the menu before the submenu goes, so its unmount never drops the focus.
212
+ const closeSub = () => {
213
+ clearGrace();
214
+ if (subRef.current?.contains(document.activeElement)) {
215
+ rootRef.current?.focus({ preventScroll: true });
216
+ }
217
+ setSub(null);
218
+ };
219
+ // With the keys, its cursor starts on its first item that can run; for the pointer, on none.
220
+ const openSub = (item, keys) => {
221
+ clearGrace();
222
+ const items = item.entries.filter(isItem);
223
+ const first = keys ? pickableCursor(pickableRows(items), null) : null;
224
+ setPointed(item.id);
225
+ setSub({ id: item.id, keys, pointed: first === null ? null : (items[first]?.id ?? null) });
226
+ };
227
+ // The pointer rests on a row of the menu: it takes the cursor, and a submenu's opens.
228
+ const pointAt = (row) => {
229
+ clearGrace();
230
+ if (sub?.id === row.id)
231
+ return;
232
+ if (sub !== null)
233
+ closeSub();
234
+ if (row.disabled === true)
235
+ return;
236
+ if (row.kind === 'submenu')
237
+ openSub(row, false);
238
+ else
239
+ setPointed(row.id);
240
+ };
241
+ const within = (to) => to instanceof Node &&
242
+ (rootRef.current?.contains(to) === true ||
243
+ subRef.current?.contains(to) === true ||
244
+ anchor?.contains(to) === true);
245
+ const onBlur = (event) => {
246
+ if (!within(event.relatedTarget))
100
247
  onClose('pointer');
101
- }, onContextMenu: (event) => event.preventDefault(), onKeyDown: (event) => {
102
- const action = menuKeyAction(event);
103
- if (action === null)
248
+ };
249
+ const take = (event) => {
250
+ const action = menuKeyAction(event, isRtl(event.currentTarget));
251
+ if (action === null)
252
+ return null;
253
+ event.preventDefault();
254
+ event.stopPropagation();
255
+ return action;
256
+ };
257
+ const onMenuKey = (event) => {
258
+ const action = take(event);
259
+ if (action === null)
260
+ return;
261
+ switch (action.kind) {
262
+ case 'close':
263
+ // Escape closes a submenu the pointer opened before the menu.
264
+ if (action.all || sub === null)
265
+ onClose('keyboard');
266
+ else
267
+ closeSub();
104
268
  return;
105
- event.preventDefault();
106
- event.stopPropagation();
107
- if (action.kind === 'close')
108
- onClose('keyboard');
109
- else if (action.kind === 'run')
110
- run(cursorItem);
111
- else {
269
+ case 'back':
270
+ if (sub !== null)
271
+ closeSub();
272
+ return;
273
+ case 'open':
274
+ case 'run':
275
+ if (cursorRow?.kind === 'submenu')
276
+ openSub(cursorRow, true);
277
+ else if (action.kind === 'run')
278
+ run(cursorRow);
279
+ return;
280
+ default: {
112
281
  const next = action.kind === 'move'
113
- ? movedPickableCursor(rows, cursor, action.delta)
114
- : movedPickableCursor(rows, null, action.to === 'first' ? 1 : -1);
115
- setPointed(next === null ? null : (items[next]?.id ?? null));
282
+ ? movedPickableCursor(pickable, cursor, action.delta)
283
+ : movedPickableCursor(pickable, null, action.to === 'first' ? 1 : -1);
284
+ if (sub !== null)
285
+ closeSub();
286
+ setPointed(next === null ? null : (rows[next]?.id ?? null));
116
287
  // Only the keys scroll: an item under the pointer is already in view.
117
288
  revealItem(itemId, next);
118
289
  }
119
- },
120
- // A press on an item must not take the focus from the menu, which holds the keys.
121
- onMouseDown: (event) => event.preventDefault(), ref: rootRef, role: "menu", tabIndex: -1, children: entries.map((entry, entryIndex) => {
122
- if (entry.kind === 'separator') {
123
- return (_jsx("div", { className: "folder-menu-separator", role: "separator" }, `separator-${entryIndex}`));
124
- }
125
- if (entry.kind === 'heading') {
126
- return (_jsx("div", { className: "entry-menu-heading", role: "presentation", children: entry.label }, `heading-${entryIndex}`));
290
+ }
291
+ };
292
+ const onSubKey = (event) => {
293
+ const action = take(event);
294
+ if (action === null || sub === null)
295
+ return;
296
+ switch (action.kind) {
297
+ case 'close':
298
+ if (action.all)
299
+ onClose('keyboard');
300
+ else
301
+ closeSub();
302
+ return;
303
+ case 'back':
304
+ closeSub();
305
+ return;
306
+ case 'open':
307
+ return;
308
+ case 'run':
309
+ run(subCursorRow);
310
+ return;
311
+ default: {
312
+ const next = action.kind === 'move'
313
+ ? movedPickableCursor(subPickable, subCursor, action.delta)
314
+ : movedPickableCursor(subPickable, null, action.to === 'first' ? 1 : -1);
315
+ setSub({ ...sub, pointed: next === null ? null : (subRows[next]?.id ?? null) });
316
+ revealItem(subItemId, next);
127
317
  }
128
- index += 1;
129
- const position = index;
130
- const Icon = entry.icon;
131
- const classes = [
132
- 'folder-menu-option',
133
- position === cursor ? 'cursor' : '',
134
- entry.danger === true ? 'danger' : ''
135
- ]
136
- .filter(Boolean)
137
- .join(' ');
138
- return (_jsxs("div", { "aria-checked": entry.checked, "aria-disabled": entry.disabled === true || undefined, className: classes, id: `${itemId}-${position}`, onClick: () => run(entry), onMouseMove: () => {
139
- if (entry.disabled !== true)
140
- setPointed(entry.id);
141
- }, role: entry.checked === undefined ? 'menuitem' : 'menuitemcheckbox', ...describedTooltip(entry.tip), children: [_jsx("span", { "aria-hidden": true, className: "row-icon", children: Icon === undefined ? entry.mark : _jsx(Icon, { size: ICON_MD }) }), _jsxs("span", { className: "menu-label", children: [_jsx("span", { className: "menu-label-text", children: entry.label }), entry.current === undefined || entry.current === null ? null : (_jsx(CurrentDot, { label: entry.current }))] }), entry.chord === undefined ? null : _jsx(KeyChord, { chord: entry.chord })] }, entry.id));
142
- }) }), document.fullscreenElement ?? document.body);
318
+ }
319
+ };
320
+ const subMenuId = `${subItemId}-menu`;
321
+ return (_jsxs(_Fragment, { children: [dirGiven === undefined && anchor === null ? _jsx("span", { hidden: true, ref: originRef }) : null, _jsx(MenuSurface, { anchor: anchor, at: at, compact: compact, cursor: cursor, dir: dir, entries: entries, expanded: subEntry === null ? null : { id: subEntry.id, menu: subMenuId }, itemId: itemId, label: label, onBlur: onBlur, onKeyDown: onMenuKey, onMouseLeave: clearGrace, onRowClick: (row) => {
322
+ if (row.kind === 'submenu')
323
+ pointAt(row);
324
+ else
325
+ run(row);
326
+ }, onRowMove: (row) => {
327
+ if (sub === null || row.id === sub.id) {
328
+ pointAt(row);
329
+ return;
330
+ }
331
+ // On its way into the submenu the pointer may cross other items:
332
+ // the one it stays on takes the cursor once the moment passes.
333
+ if (grace.current !== null) {
334
+ grace.current.to = row;
335
+ return;
336
+ }
337
+ grace.current = {
338
+ to: row,
339
+ timer: window.setTimeout(() => {
340
+ const to = grace.current?.to;
341
+ grace.current = null;
342
+ if (to !== undefined)
343
+ pointAt(to);
344
+ }, SUBMENU_GRACE_MS)
345
+ };
346
+ }, surfaceRef: rootRef }), subEntry === null ? null : (_jsx(MenuSurface, { anchor: anchor, at: at, besideId: `${itemId}-${subPlace}`, compact: compact, cursor: subCursor, dir: dir, entries: subEntry.entries, expanded: null, id: subMenuId, itemId: subItemId, labelledBy: `${itemId}-${subPlace}`, onBlur: onBlur, onKeyDown: onSubKey, onMouseLeave: clearGrace, onMouseMove: clearGrace, onRowClick: run, onRowMove: (row) => {
347
+ clearGrace();
348
+ if (row.disabled !== true && sub !== null)
349
+ setSub({ ...sub, pointed: row.id, keys: true });
350
+ }, surfaceRef: subRef }))] }));
143
351
  }
@@ -17,25 +17,44 @@ export interface MenuSize {
17
17
  }
18
18
  /** The room a menu keeps from the window's edges, and the gap it leaves under what it drops from, in pixels. */
19
19
  export declare const MENU_MARGIN = 4;
20
+ /**
21
+ * Where a menu opens from: a point (a secondary click's), a box it drops
22
+ * from (a crumb, a button), or the box of the item whose submenu it is,
23
+ * which it stands beside (on the item's inline-end side, the right unless
24
+ * `rtl`).
25
+ */
26
+ export type MenuFrom = {
27
+ readonly point: MenuPoint;
28
+ } | {
29
+ readonly box: MenuBox;
30
+ } | {
31
+ readonly beside: MenuBox;
32
+ readonly rtl?: boolean;
33
+ };
20
34
  /**
21
35
  * Where a menu stands, its top-left corner in window coordinates. Opened at
22
36
  * a point, it hangs down and right from it, moved in to stay inside the
23
37
  * window, and opens upward from the point when there is no room below.
24
38
  * Dropped from a box, it starts under the box's leading edge, a margin
25
39
  * below, and stands over the box instead when there is room above and none
26
- * below; either way it stays inside the window. A menu its stylesheet
27
- * caps at the window's height less both margins (`.menu-popup`) measures
28
- * at that cap, so it starts at the top margin.
40
+ * below. A submenu stands beside its item, on the item's inline-end side,
41
+ * its first item level with it (a margin above the item's top, the menu's
42
+ * own padding), and on the other side when the window has no room on that
43
+ * one; when neither side has room it keeps to the side with more. Every
44
+ * way it stays inside the window. A menu its stylesheet caps at the
45
+ * window's height less both margins (`.menu-popup`) measures at that cap,
46
+ * so it starts at the top margin.
29
47
  */
30
- export declare function placeMenu(from: {
31
- readonly point: MenuPoint;
32
- } | {
33
- readonly box: MenuBox;
34
- }, size: MenuSize, viewport: MenuSize): {
48
+ export declare function placeMenu(from: MenuFrom, size: MenuSize, viewport: MenuSize): {
35
49
  readonly left: number;
36
50
  readonly top: number;
37
51
  };
38
- /** What a key pressed in an open menu does. */
52
+ /**
53
+ * What a key pressed in an open menu does. `open` and `back` are the arrows
54
+ * along the line: into the cursor's submenu, and out of a submenu to its
55
+ * item. `close` closes one level (Escape: a submenu, else the menu), or
56
+ * with `all` the whole menu (Tab).
57
+ */
39
58
  export type MenuKeyAction = {
40
59
  readonly kind: 'move';
41
60
  readonly delta: number;
@@ -44,13 +63,21 @@ export type MenuKeyAction = {
44
63
  readonly to: 'first' | 'last';
45
64
  } | {
46
65
  readonly kind: 'run';
66
+ } | {
67
+ readonly kind: 'open';
68
+ } | {
69
+ readonly kind: 'back';
47
70
  } | {
48
71
  readonly kind: 'close';
72
+ readonly all: boolean;
49
73
  };
50
74
  /**
51
75
  * The menu's keys, as a desktop menu's: ↑ and ↓ move the cursor over the
52
76
  * items that can run, Home and End to the first and the last, Return and
53
- * Space run the cursor's item, Escape and Tab close it. A chord with a
54
- * modifier is not the menu's (null), and neither is any other key.
77
+ * Space run the cursor's item (or open its submenu), → opens the cursor's
78
+ * submenu and ← goes back out of one (the other way round when `rtl`, so
79
+ * the arrow points where the submenu stands), Escape closes one level and
80
+ * Tab the whole menu. A chord with a modifier is not the menu's (null),
81
+ * and neither is any other key.
55
82
  */
56
- export declare function menuKeyAction(event: Pick<KeyboardEvent, 'key' | 'metaKey' | 'ctrlKey' | 'altKey' | 'shiftKey'>): MenuKeyAction | null;
83
+ export declare function menuKeyAction(event: Pick<KeyboardEvent, 'key' | 'metaKey' | 'ctrlKey' | 'altKey' | 'shiftKey'>, rtl?: boolean): MenuKeyAction | null;
@@ -8,12 +8,33 @@ const clamp = (start, extent, room) => Math.max(MENU_MARGIN, Math.min(start, roo
8
8
  * window, and opens upward from the point when there is no room below.
9
9
  * Dropped from a box, it starts under the box's leading edge, a margin
10
10
  * below, and stands over the box instead when there is room above and none
11
- * below; either way it stays inside the window. A menu its stylesheet
12
- * caps at the window's height less both margins (`.menu-popup`) measures
13
- * at that cap, so it starts at the top margin.
11
+ * below. A submenu stands beside its item, on the item's inline-end side,
12
+ * its first item level with it (a margin above the item's top, the menu's
13
+ * own padding), and on the other side when the window has no room on that
14
+ * one; when neither side has room it keeps to the side with more. Every
15
+ * way it stays inside the window. A menu its stylesheet caps at the
16
+ * window's height less both margins (`.menu-popup`) measures at that cap,
17
+ * so it starts at the top margin.
14
18
  */
15
19
  export function placeMenu(from, size, viewport) {
16
20
  const room = (top) => top + size.height <= viewport.height - MENU_MARGIN;
21
+ if ('beside' in from) {
22
+ const { beside: item, rtl = false } = from;
23
+ const after = item.right;
24
+ const before = item.left - size.width;
25
+ const fitsAfter = after + size.width <= viewport.width - MENU_MARGIN;
26
+ const fitsBefore = before >= MENU_MARGIN;
27
+ const roomAfter = viewport.width - item.right;
28
+ const roomBefore = item.left;
29
+ // Its own side while it fits there; the other while that fits; else the roomier one.
30
+ const startsAfter = rtl
31
+ ? !fitsBefore && (fitsAfter || roomAfter > roomBefore)
32
+ : fitsAfter || (!fitsBefore && roomAfter >= roomBefore);
33
+ return {
34
+ left: clamp(startsAfter ? after : before, size.width, viewport.width),
35
+ top: clamp(item.top - MENU_MARGIN, size.height, viewport.height)
36
+ };
37
+ }
17
38
  if ('point' in from) {
18
39
  const { x, y } = from.point;
19
40
  const top = room(y) || y - size.height < MENU_MARGIN ? y : y - size.height;
@@ -34,14 +55,17 @@ export function placeMenu(from, size, viewport) {
34
55
  /**
35
56
  * The menu's keys, as a desktop menu's: ↑ and ↓ move the cursor over the
36
57
  * items that can run, Home and End to the first and the last, Return and
37
- * Space run the cursor's item, Escape and Tab close it. A chord with a
38
- * modifier is not the menu's (null), and neither is any other key.
58
+ * Space run the cursor's item (or open its submenu), → opens the cursor's
59
+ * submenu and ← goes back out of one (the other way round when `rtl`, so
60
+ * the arrow points where the submenu stands), Escape closes one level and
61
+ * Tab the whole menu. A chord with a modifier is not the menu's (null),
62
+ * and neither is any other key.
39
63
  */
40
- export function menuKeyAction(event) {
64
+ export function menuKeyAction(event, rtl = false) {
41
65
  if (event.metaKey || event.ctrlKey || event.altKey)
42
66
  return null;
43
67
  if (event.key === 'Tab')
44
- return { kind: 'close' };
68
+ return { kind: 'close', all: true };
45
69
  if (event.shiftKey)
46
70
  return null;
47
71
  switch (event.key) {
@@ -49,6 +73,10 @@ export function menuKeyAction(event) {
49
73
  return { kind: 'move', delta: -1 };
50
74
  case 'ArrowDown':
51
75
  return { kind: 'move', delta: 1 };
76
+ case 'ArrowRight':
77
+ return { kind: rtl ? 'back' : 'open' };
78
+ case 'ArrowLeft':
79
+ return { kind: rtl ? 'open' : 'back' };
52
80
  case 'Home':
53
81
  return { kind: 'edge', to: 'first' };
54
82
  case 'End':
@@ -57,7 +85,7 @@ export function menuKeyAction(event) {
57
85
  case ' ':
58
86
  return { kind: 'run' };
59
87
  case 'Escape':
60
- return { kind: 'close' };
88
+ return { kind: 'close', all: false };
61
89
  default:
62
90
  return null;
63
91
  }
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * Menus: `Menu`, a menu of verbs or places opened at a point or dropped
3
- * from an element, with `placeMenu` and `menuKeyAction` under it; and for
3
+ * from an element, its items able to open one level of submenus, with
4
+ * `placeMenu` and `menuKeyAction` under it; and for
4
5
  * filtered menus: the fuzzy matcher (`matchQuery`), the rules every filtered
5
6
  * menu and palette shares (a refused keystroke, the cursor's moves, the trail
6
7
  * of levels opened in place), the visual bell's beat, and the menu look in
@@ -9,5 +10,5 @@
9
10
  export { hasNegativeTerms, hasQueryTerms, matchQuery, type FuzzyMatch } from './fuzzyMatch.js';
10
11
  export { enteredLevel, leftLevel, menuQueryRefused, movedPickableCursor, moveMenuCursor, pickableCursor, returnedCursor, trailLevel, type LevelReturn, type LevelStep, type PickableRow } from './MenuQuery.logic.js';
11
12
  export { useBellBeat } from './useBellBeat.js';
12
- export { Menu, type MenuEntry, type MenuHeadingEntry, type MenuItemEntry, type MenuProps, type MenuSeparatorEntry } from './Menu.js';
13
- export { MENU_MARGIN, menuKeyAction, placeMenu, type MenuBox, type MenuKeyAction, type MenuPoint, type MenuSize } from './Menu.logic.js';
13
+ export { Menu, type MenuEntry, type MenuHeadingEntry, type MenuItemEntry, type MenuLeafEntry, type MenuProps, type MenuSeparatorEntry, type MenuSubmenuEntry } from './Menu.js';
14
+ export { MENU_MARGIN, menuKeyAction, placeMenu, type MenuBox, type MenuFrom, type MenuKeyAction, type MenuPoint, type MenuSize } from './Menu.logic.js';
@@ -1,6 +1,7 @@
1
1
  /**
2
2
  * Menus: `Menu`, a menu of verbs or places opened at a point or dropped
3
- * from an element, with `placeMenu` and `menuKeyAction` under it; and for
3
+ * from an element, its items able to open one level of submenus, with
4
+ * `placeMenu` and `menuKeyAction` under it; and for
4
5
  * filtered menus: the fuzzy matcher (`matchQuery`), the rules every filtered
5
6
  * menu and palette shares (a refused keystroke, the cursor's moves, the trail
6
7
  * of levels opened in place), the visual bell's beat, and the menu look in
@@ -250,6 +250,12 @@
250
250
  background: var(--text-on-surface);
251
251
  }
252
252
 
253
+ /* Right to left, a submenu opens on the left, so the chevron points there.
254
+ :dir() follows the nearest direction, as the menu's own placement does. */
255
+ .folder-menu-option[aria-haspopup='menu']:dir(rtl)::after {
256
+ transform: scaleX(-1);
257
+ }
258
+
253
259
  /* A plain separator between groups of rows that need no name: a hairline
254
260
  in the border colour with a small space above and below, as a desktop
255
261
  menu draws one. */