@oxy.so/bloom 19.9.1 → 19.10.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 (37) hide show
  1. package/docs/adoption-matrix.mdx +4 -4
  2. package/docs/menubar.mdx +3 -3
  3. package/docs/task-catalog.json +1 -1
  4. package/docs/task-catalog.mdx +1 -1
  5. package/lib/commonjs/menubar/declared.js +171 -66
  6. package/lib/commonjs/menubar/declared.js.map +1 -1
  7. package/lib/commonjs/menubar/description.js +293 -0
  8. package/lib/commonjs/menubar/description.js.map +1 -0
  9. package/lib/commonjs/menubar/use-edit-action.js +1 -0
  10. package/lib/commonjs/menubar/use-edit-action.js.map +1 -1
  11. package/lib/module/menubar/declared.js +145 -43
  12. package/lib/module/menubar/declared.js.map +1 -1
  13. package/lib/module/menubar/description.js +286 -0
  14. package/lib/module/menubar/description.js.map +1 -0
  15. package/lib/module/menubar/use-edit-action.js +1 -1
  16. package/lib/module/menubar/use-edit-action.js.map +1 -1
  17. package/lib/typescript/commonjs/menubar/declared.d.ts +29 -20
  18. package/lib/typescript/commonjs/menubar/declared.d.ts.map +1 -1
  19. package/lib/typescript/commonjs/menubar/description.d.ts +90 -0
  20. package/lib/typescript/commonjs/menubar/description.d.ts.map +1 -0
  21. package/lib/typescript/commonjs/menubar/types.d.ts +7 -6
  22. package/lib/typescript/commonjs/menubar/types.d.ts.map +1 -1
  23. package/lib/typescript/commonjs/menubar/use-edit-action.d.ts +4 -1
  24. package/lib/typescript/commonjs/menubar/use-edit-action.d.ts.map +1 -1
  25. package/lib/typescript/module/menubar/declared.d.ts +29 -20
  26. package/lib/typescript/module/menubar/declared.d.ts.map +1 -1
  27. package/lib/typescript/module/menubar/description.d.ts +90 -0
  28. package/lib/typescript/module/menubar/description.d.ts.map +1 -0
  29. package/lib/typescript/module/menubar/types.d.ts +7 -6
  30. package/lib/typescript/module/menubar/types.d.ts.map +1 -1
  31. package/lib/typescript/module/menubar/use-edit-action.d.ts +4 -1
  32. package/lib/typescript/module/menubar/use-edit-action.d.ts.map +1 -1
  33. package/package.json +1 -1
  34. package/src/menubar/declared.tsx +166 -39
  35. package/src/menubar/description.ts +337 -0
  36. package/src/menubar/types.ts +7 -6
  37. package/src/menubar/use-edit-action.ts +2 -2
@@ -0,0 +1,337 @@
1
+ /**
2
+ * A closed menu's DESCRIPTION, read from its declaration's elements without
3
+ * mounting them (OxyOS ADR 0021).
4
+ *
5
+ * A closed menu's rows are known to its bar so that their shortcuts work while
6
+ * it is closed. Mounting them headless did that by rendering every row, which
7
+ * cost every bar render a render of every closed menu's rows (OxyOS's shell has
8
+ * a Window menu of some 27). A menu's content is a declaration: Bloom's own
9
+ * parts (an item, a check item, a radio group and its items, a label, a
10
+ * separator, a group, a submenu), with their props. So a closed menu's entries
11
+ * are read from those elements here, as a Mac's bar keeps a menu's items as
12
+ * data and builds its rows as it opens: the label, the shortcut, the state, and
13
+ * what choosing it runs, the element's own handler.
14
+ *
15
+ * POSITIONS are the ones `DeclaredChildren` gives the mounted rows (arrays and
16
+ * fragments flattened, every child, `null` included, taking a place), so the
17
+ * order a description sorts by is the same open or closed.
18
+ *
19
+ * What is not one of Bloom's parts (an application's own component that
20
+ * renders rows) cannot be read without rendering it: it is returned as
21
+ * `unread`, with its place, for the caller to mount headless as before.
22
+ */
23
+ import { Fragment, isValidElement, type ReactNode } from 'react';
24
+
25
+ import { openHref } from '../hooks/link-activation';
26
+ import { type Chord, parseChord } from '../kbd/chord';
27
+ import type { PlatformEditAction, PlatformMenuAction } from '../platform-surface/types';
28
+ import type { MenuEntryKind } from './registry';
29
+ import { actionShortcut, isMenuAction } from './use-edit-action';
30
+
31
+ /** Which of Bloom's menubar parts a component is (`createMenubarRows` marks each). */
32
+ export type MenubarPart =
33
+ | 'item'
34
+ | 'checkbox'
35
+ | 'radioGroup'
36
+ | 'radio'
37
+ | 'label'
38
+ | 'separator'
39
+ | 'group'
40
+ | 'sub'
41
+ | 'subTrigger'
42
+ | 'subContent';
43
+
44
+ const PART = Symbol.for('bloom.menubarPart');
45
+ const HINTS = new WeakSet<object>();
46
+
47
+ /** Marks `component` as one of Bloom's menubar parts, so a description can read its elements. */
48
+ export function markMenubarPart<T extends object>(component: T, part: MenubarPart): T {
49
+ (component as Record<symbol, MenubarPart>)[PART] = part;
50
+ return component;
51
+ }
52
+
53
+ /** Marks a row's shortcut hint, which a label's text leaves out. */
54
+ export function markMenubarHint(component: unknown): void {
55
+ if ((typeof component === 'function' || typeof component === 'object') && component !== null) {
56
+ HINTS.add(component);
57
+ }
58
+ }
59
+
60
+ function partOf(type: unknown): MenubarPart | undefined {
61
+ if ((typeof type !== 'function' && typeof type !== 'object') || type === null) return undefined;
62
+ return (type as Record<symbol, MenubarPart | undefined>)[PART];
63
+ }
64
+
65
+ const isMarkedHint = (type: unknown) =>
66
+ (typeof type === 'function' || typeof type === 'object') && type !== null && HINTS.has(type);
67
+
68
+ /**
69
+ * The text of a part's children, as the platform's bar shows it: every string
70
+ * and number under it, element children included, except a shortcut hint
71
+ * (`isHint`), which is not part of the name.
72
+ */
73
+ export function textOf(children: ReactNode, isHint: (type: unknown) => boolean): string {
74
+ const parts: string[] = [];
75
+ const visit = (node: ReactNode) => {
76
+ if (typeof node === 'string' || typeof node === 'number') parts.push(String(node));
77
+ else if (Array.isArray(node)) node.forEach(visit);
78
+ else if (isValidElement<{ children?: ReactNode }>(node) && !isHint(node.type)) {
79
+ visit(node.props.children);
80
+ }
81
+ };
82
+ visit(children);
83
+ return parts.join('').replace(/\s+/g, ' ').trim();
84
+ }
85
+
86
+ const labelOf = (children: ReactNode) => textOf(children, isMarkedHint);
87
+
88
+ /** A radio group's selection, as its items read it. */
89
+ export interface RadioSelection {
90
+ readonly value: string | undefined;
91
+ readonly onValueChange: (value: string) => void;
92
+ }
93
+
94
+ /** One entry of a closed menu, read from its element. */
95
+ export interface DescribedEntry {
96
+ /** Stable while the element keeps its place: its kind and its path. */
97
+ readonly key: string;
98
+ readonly kind: MenuEntryKind;
99
+ /** The key of the submenu it is in; null for the menu's own content. */
100
+ readonly parentKey: string | null;
101
+ /** Its position under the content's own path. */
102
+ readonly path: readonly number[];
103
+ readonly label: string;
104
+ readonly enabled: boolean;
105
+ readonly shortcut: Chord | null;
106
+ readonly checked?: boolean;
107
+ readonly keepOpen?: boolean;
108
+ readonly action?: PlatformMenuAction;
109
+ /** An editing action: its state and what choosing it does are the focused field's while one is. */
110
+ readonly editAction?: PlatformEditAction;
111
+ /** The element's own handler, as a press on its row would run it; undefined for none. */
112
+ readonly run?: () => void;
113
+ /** Whether choosing it runs something: every item does, a label and a submenu's trigger do not. */
114
+ readonly runs: boolean;
115
+ }
116
+
117
+ /** What a description could not read: an element to mount headless at its place. */
118
+ export interface UnreadElement {
119
+ readonly node: ReactNode;
120
+ readonly parentKey: string | null;
121
+ readonly path: readonly number[];
122
+ readonly radio: RadioSelection | null;
123
+ /**
124
+ * Set for an element placed directly in a submenu (beside its trigger and
125
+ * content): the submenu (`parentKey`) is its owner, and this is the key of
126
+ * the menu or submenu the submenu is in.
127
+ */
128
+ readonly owner: { readonly parentKey: string | null } | null;
129
+ }
130
+
131
+ export interface MenuDescription {
132
+ readonly entries: DescribedEntry[];
133
+ readonly unread: UnreadElement[];
134
+ }
135
+
136
+ interface ItemProps {
137
+ children?: ReactNode;
138
+ accessibilityLabel?: string;
139
+ disabled?: boolean;
140
+ keepOpen?: boolean;
141
+ shortcut?: string;
142
+ action?: PlatformEditAction | PlatformMenuAction;
143
+ onPress?: () => void;
144
+ href?: string;
145
+ checked?: boolean;
146
+ onCheckedChange?: (checked: boolean) => void;
147
+ value?: string;
148
+ onValueChange?: (value: string) => void;
149
+ }
150
+
151
+ /** The children of a container as `DeclaredChildren` places them: one per place. */
152
+ function places(children: ReactNode): ReactNode[] {
153
+ const out: ReactNode[] = [];
154
+ const visit = (node: ReactNode) => {
155
+ if (Array.isArray(node)) {
156
+ node.forEach(visit);
157
+ return;
158
+ }
159
+ if (isValidElement<{ children?: ReactNode }>(node) && node.type === Fragment) {
160
+ visit(node.props.children);
161
+ return;
162
+ }
163
+ out.push(node);
164
+ };
165
+ visit(children);
166
+ return out;
167
+ }
168
+
169
+ /** The chord a `shortcut` declares; null when it is none (the mounted row warns once). */
170
+ function chordOf(shortcut: string | undefined): Chord | null {
171
+ return shortcut === undefined ? null : parseChord(shortcut);
172
+ }
173
+
174
+ const keyOf = (path: readonly number[]) => `d${path.join('.')}`;
175
+
176
+ /**
177
+ * `children`'s description: its entries, each at `[...path, place]` under
178
+ * `parentKey`, and what it could not read.
179
+ */
180
+ export function describeMenu(children: ReactNode): MenuDescription {
181
+ const entries: DescribedEntry[] = [];
182
+ const unread: UnreadElement[] = [];
183
+
184
+ const visit = (
185
+ nodes: ReactNode,
186
+ path: readonly number[],
187
+ parentKey: string | null,
188
+ radio: RadioSelection | null,
189
+ ) => {
190
+ places(nodes).forEach((node, place) => {
191
+ const at = [...path, place];
192
+ if (node === null || node === undefined || typeof node === 'boolean') return;
193
+ if (!isValidElement<ItemProps>(node)) {
194
+ // Text placed in a menu draws nowhere in a description.
195
+ return;
196
+ }
197
+ const part = partOf(node.type);
198
+ const props = node.props;
199
+ const label = labelOf(props.children) || (props.accessibilityLabel ?? '');
200
+ switch (part) {
201
+ case 'item': {
202
+ const { action } = props;
203
+ const handler =
204
+ props.onPress ??
205
+ (props.href != null ? () => openHref(props.href as string) : undefined);
206
+ const menuAction = isMenuAction(action) ? action : undefined;
207
+ const editAction = isMenuAction(action) ? undefined : action;
208
+ entries.push({
209
+ key: keyOf(at),
210
+ kind: 'item',
211
+ parentKey,
212
+ path: at,
213
+ label,
214
+ // An item with an action needs its own handler when no field takes the action (an
215
+ // application's own command never is a field's); while a field takes an editing
216
+ // action, its state is the field's (`DescribedEntry.editAction`).
217
+ enabled: !props.disabled && (action === undefined || handler !== undefined),
218
+ shortcut: chordOf(props.shortcut ?? actionShortcut(action)),
219
+ keepOpen: props.keepOpen ?? false,
220
+ ...(menuAction ? { action: menuAction } : null),
221
+ ...(editAction ? { editAction } : null),
222
+ run: handler,
223
+ runs: true,
224
+ });
225
+ return;
226
+ }
227
+ case 'checkbox': {
228
+ const { checked = false, onCheckedChange } = props;
229
+ entries.push({
230
+ key: keyOf(at),
231
+ kind: 'item',
232
+ parentKey,
233
+ path: at,
234
+ label,
235
+ enabled: !props.disabled,
236
+ shortcut: chordOf(props.shortcut),
237
+ checked,
238
+ keepOpen: props.keepOpen ?? false,
239
+ run: onCheckedChange ? () => onCheckedChange(!checked) : undefined,
240
+ runs: true,
241
+ });
242
+ return;
243
+ }
244
+ case 'radioGroup': {
245
+ const { value, onValueChange } = props;
246
+ visit(props.children, at, parentKey, onValueChange ? { value, onValueChange } : null);
247
+ return;
248
+ }
249
+ case 'radio': {
250
+ const { value } = props;
251
+ entries.push({
252
+ key: keyOf(at),
253
+ kind: 'item',
254
+ parentKey,
255
+ path: at,
256
+ label,
257
+ enabled: !props.disabled,
258
+ shortcut: chordOf(props.shortcut),
259
+ checked: radio !== null && radio.value === value,
260
+ keepOpen: props.keepOpen ?? false,
261
+ run: radio && value !== undefined ? () => radio.onValueChange(value) : undefined,
262
+ runs: true,
263
+ });
264
+ return;
265
+ }
266
+ case 'label':
267
+ // A group's label is shown, never chosen: an item that is never enabled.
268
+ entries.push({
269
+ key: keyOf(at),
270
+ kind: 'item',
271
+ parentKey,
272
+ path: at,
273
+ label: labelOf(props.children),
274
+ enabled: false,
275
+ shortcut: null,
276
+ runs: false,
277
+ });
278
+ return;
279
+ case 'separator':
280
+ entries.push({
281
+ key: keyOf(at),
282
+ kind: 'separator',
283
+ parentKey,
284
+ path: at,
285
+ label: '',
286
+ enabled: true,
287
+ shortcut: null,
288
+ runs: false,
289
+ });
290
+ return;
291
+ case 'group':
292
+ visit(props.children, at, parentKey, radio);
293
+ return;
294
+ case 'sub': {
295
+ // A submenu: its trigger names it, its content is its entries, at the submenu's place.
296
+ const key = keyOf(at);
297
+ for (const child of places(props.children)) {
298
+ if (!isValidElement<ItemProps>(child)) continue;
299
+ const childPart = partOf(child.type);
300
+ if (childPart === 'subTrigger') {
301
+ entries.push({
302
+ key,
303
+ kind: 'submenu',
304
+ parentKey,
305
+ path: at,
306
+ label: labelOf(child.props.children) || (child.props.accessibilityLabel ?? ''),
307
+ enabled: !child.props.disabled,
308
+ shortcut: null,
309
+ runs: false,
310
+ });
311
+ } else if (childPart === 'subContent') {
312
+ visit(child.props.children, at, key, null);
313
+ } else {
314
+ unread.push({
315
+ node: child,
316
+ parentKey: key,
317
+ path: at,
318
+ radio: null,
319
+ owner: { parentKey },
320
+ });
321
+ }
322
+ }
323
+ return;
324
+ }
325
+ case 'subTrigger':
326
+ case 'subContent':
327
+ // Outside a submenu they declare nothing, mounted or not.
328
+ return;
329
+ default:
330
+ unread.push({ node, parentKey, path: at, radio, owner: null });
331
+ }
332
+ });
333
+ };
334
+
335
+ visit(children, [], null, null);
336
+ return { entries, unread };
337
+ }
@@ -45,12 +45,13 @@ export interface MenubarProps {
45
45
  /** The keyboard moved across the bar, or left it (`undefined`). */
46
46
  onFocusValueChange?: (value: string | undefined) => void;
47
47
  /**
48
- * Whether a closed menu's content is mounted headless, so its rows' `shortcut`s work while the
49
- * menu is closed (the default, `true`). A bar whose rows run no chords of their own (an OS
50
- * shell's, whose rows only show the chord another part runs) passes `false`: a closed menu then
51
- * mounts nothing but its title, as a Mac's menu bar draws titles and builds a menu as it opens,
52
- * and its rows render again only while it is open or the popup is changing from it. Ignored
53
- * where the platform draws the bar (`menu-bar`), which needs every menu declared.
48
+ * Whether a closed menu's rows are known to the bar, so their `shortcut`s work while the menu is
49
+ * closed (the default, `true`). Known, not mounted: Bloom reads them from the content's elements
50
+ * (a closed menu's rows never render) and mounts headless only an application's own component
51
+ * placed in it, which it cannot read without rendering. A bar whose rows run no chords of their
52
+ * own (an OS shell's, whose rows only show the chord another part runs) passes `false`: a closed
53
+ * menu then declares nothing, and its shortcuts are the open menu's only. Ignored where the
54
+ * platform draws the bar (`menu-bar`), which needs every menu declared.
54
55
  */
55
56
  declareClosedMenus?: boolean;
56
57
  /** Accessible name for the bar. */
@@ -68,9 +68,9 @@ export function actionShortcut(
68
68
  const subscribeNowhere = () => () => undefined;
69
69
 
70
70
  /** What a field can do with `action` now: 'none' with no field focused. */
71
- type Responder = 'none' | 'enabled' | 'disabled';
71
+ export type Responder = 'none' | 'enabled' | 'disabled';
72
72
 
73
- function responderOf(
73
+ export function responderOf(
74
74
  editing: PlatformEditing | null,
75
75
  action: PlatformEditAction | undefined,
76
76
  ): Responder {