@oxy.so/bloom 21.4.0 → 21.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 (114) hide show
  1. package/docs/adoption-matrix.mdx +6 -6
  2. package/docs/agent-task-index.mdx +1 -0
  3. package/docs/dropdown-menu.mdx +11 -0
  4. package/docs/menubar.mdx +21 -0
  5. package/docs/platform-surface.mdx +11 -2
  6. package/docs/task-catalog.json +1 -1
  7. package/docs/task-catalog.mdx +1 -1
  8. package/lib/commonjs/dropdown-menu/DropdownMenu.js +86 -6
  9. package/lib/commonjs/dropdown-menu/DropdownMenu.js.map +1 -1
  10. package/lib/commonjs/dropdown-menu/context.js.map +1 -1
  11. package/lib/commonjs/floating/PopupPanel.js +17 -1
  12. package/lib/commonjs/floating/PopupPanel.js.map +1 -1
  13. package/lib/commonjs/floating/TriggerSlot.js +4 -0
  14. package/lib/commonjs/floating/TriggerSlot.js.map +1 -1
  15. package/lib/commonjs/index.js.map +1 -1
  16. package/lib/commonjs/index.web.js.map +1 -1
  17. package/lib/commonjs/kbd/chord.js +53 -0
  18. package/lib/commonjs/kbd/chord.js.map +1 -1
  19. package/lib/commonjs/menubar/declared.js +134 -11
  20. package/lib/commonjs/menubar/declared.js.map +1 -1
  21. package/lib/commonjs/menubar/description.js +37 -1
  22. package/lib/commonjs/menubar/description.js.map +1 -1
  23. package/lib/commonjs/menubar/registry.js +4 -1
  24. package/lib/commonjs/menubar/registry.js.map +1 -1
  25. package/lib/commonjs/menubar/use-held-modifiers.js +88 -0
  26. package/lib/commonjs/menubar/use-held-modifiers.js.map +1 -0
  27. package/lib/module/dropdown-menu/DropdownMenu.js +88 -6
  28. package/lib/module/dropdown-menu/DropdownMenu.js.map +1 -1
  29. package/lib/module/dropdown-menu/context.js.map +1 -1
  30. package/lib/module/floating/PopupPanel.js +17 -1
  31. package/lib/module/floating/PopupPanel.js.map +1 -1
  32. package/lib/module/floating/TriggerSlot.js +4 -0
  33. package/lib/module/floating/TriggerSlot.js.map +1 -1
  34. package/lib/module/index.js.map +1 -1
  35. package/lib/module/index.web.js.map +1 -1
  36. package/lib/module/kbd/chord.js +50 -0
  37. package/lib/module/kbd/chord.js.map +1 -1
  38. package/lib/module/menubar/declared.js +137 -14
  39. package/lib/module/menubar/declared.js.map +1 -1
  40. package/lib/module/menubar/description.js +36 -3
  41. package/lib/module/menubar/description.js.map +1 -1
  42. package/lib/module/menubar/registry.js +4 -1
  43. package/lib/module/menubar/registry.js.map +1 -1
  44. package/lib/module/menubar/use-held-modifiers.js +81 -0
  45. package/lib/module/menubar/use-held-modifiers.js.map +1 -0
  46. package/lib/typescript/commonjs/dropdown-menu/DropdownMenu.d.ts +7 -1
  47. package/lib/typescript/commonjs/dropdown-menu/DropdownMenu.d.ts.map +1 -1
  48. package/lib/typescript/commonjs/dropdown-menu/context.d.ts +2 -0
  49. package/lib/typescript/commonjs/dropdown-menu/context.d.ts.map +1 -1
  50. package/lib/typescript/commonjs/floating/PopupPanel.d.ts +17 -4
  51. package/lib/typescript/commonjs/floating/PopupPanel.d.ts.map +1 -1
  52. package/lib/typescript/commonjs/floating/TriggerSlot.d.ts +7 -1
  53. package/lib/typescript/commonjs/floating/TriggerSlot.d.ts.map +1 -1
  54. package/lib/typescript/commonjs/index.d.ts +1 -1
  55. package/lib/typescript/commonjs/index.d.ts.map +1 -1
  56. package/lib/typescript/commonjs/index.web.d.ts +1 -1
  57. package/lib/typescript/commonjs/index.web.d.ts.map +1 -1
  58. package/lib/typescript/commonjs/kbd/chord.d.ts +19 -1
  59. package/lib/typescript/commonjs/kbd/chord.d.ts.map +1 -1
  60. package/lib/typescript/commonjs/menubar/declared.d.ts.map +1 -1
  61. package/lib/typescript/commonjs/menubar/description.d.ts +20 -0
  62. package/lib/typescript/commonjs/menubar/description.d.ts.map +1 -1
  63. package/lib/typescript/commonjs/menubar/registry.d.ts +5 -0
  64. package/lib/typescript/commonjs/menubar/registry.d.ts.map +1 -1
  65. package/lib/typescript/commonjs/menubar/types.d.ts +20 -0
  66. package/lib/typescript/commonjs/menubar/types.d.ts.map +1 -1
  67. package/lib/typescript/commonjs/menubar/use-held-modifiers.d.ts +7 -0
  68. package/lib/typescript/commonjs/menubar/use-held-modifiers.d.ts.map +1 -0
  69. package/lib/typescript/commonjs/platform-surface/index.d.ts +1 -1
  70. package/lib/typescript/commonjs/platform-surface/index.d.ts.map +1 -1
  71. package/lib/typescript/commonjs/platform-surface/types.d.ts +16 -5
  72. package/lib/typescript/commonjs/platform-surface/types.d.ts.map +1 -1
  73. package/lib/typescript/module/dropdown-menu/DropdownMenu.d.ts +7 -1
  74. package/lib/typescript/module/dropdown-menu/DropdownMenu.d.ts.map +1 -1
  75. package/lib/typescript/module/dropdown-menu/context.d.ts +2 -0
  76. package/lib/typescript/module/dropdown-menu/context.d.ts.map +1 -1
  77. package/lib/typescript/module/floating/PopupPanel.d.ts +17 -4
  78. package/lib/typescript/module/floating/PopupPanel.d.ts.map +1 -1
  79. package/lib/typescript/module/floating/TriggerSlot.d.ts +7 -1
  80. package/lib/typescript/module/floating/TriggerSlot.d.ts.map +1 -1
  81. package/lib/typescript/module/index.d.ts +1 -1
  82. package/lib/typescript/module/index.d.ts.map +1 -1
  83. package/lib/typescript/module/index.web.d.ts +1 -1
  84. package/lib/typescript/module/index.web.d.ts.map +1 -1
  85. package/lib/typescript/module/kbd/chord.d.ts +19 -1
  86. package/lib/typescript/module/kbd/chord.d.ts.map +1 -1
  87. package/lib/typescript/module/menubar/declared.d.ts.map +1 -1
  88. package/lib/typescript/module/menubar/description.d.ts +20 -0
  89. package/lib/typescript/module/menubar/description.d.ts.map +1 -1
  90. package/lib/typescript/module/menubar/registry.d.ts +5 -0
  91. package/lib/typescript/module/menubar/registry.d.ts.map +1 -1
  92. package/lib/typescript/module/menubar/types.d.ts +20 -0
  93. package/lib/typescript/module/menubar/types.d.ts.map +1 -1
  94. package/lib/typescript/module/menubar/use-held-modifiers.d.ts +7 -0
  95. package/lib/typescript/module/menubar/use-held-modifiers.d.ts.map +1 -0
  96. package/lib/typescript/module/platform-surface/index.d.ts +1 -1
  97. package/lib/typescript/module/platform-surface/index.d.ts.map +1 -1
  98. package/lib/typescript/module/platform-surface/types.d.ts +16 -5
  99. package/lib/typescript/module/platform-surface/types.d.ts.map +1 -1
  100. package/package.json +1 -1
  101. package/src/dropdown-menu/DropdownMenu.tsx +94 -6
  102. package/src/dropdown-menu/context.ts +2 -0
  103. package/src/floating/PopupPanel.tsx +41 -2
  104. package/src/floating/TriggerSlot.tsx +10 -0
  105. package/src/index.ts +1 -0
  106. package/src/index.web.ts +1 -0
  107. package/src/kbd/chord.ts +52 -1
  108. package/src/menubar/declared.tsx +143 -13
  109. package/src/menubar/description.ts +42 -3
  110. package/src/menubar/registry.ts +9 -0
  111. package/src/menubar/types.ts +20 -0
  112. package/src/menubar/use-held-modifiers.ts +90 -0
  113. package/src/platform-surface/index.ts +1 -0
  114. package/src/platform-surface/types.ts +17 -5
@@ -18,9 +18,15 @@ import { BloomScope } from '../appearance';
18
18
  *
19
19
  * The rows come from `floating/menu-rows`, the same set `ContextMenu` and
20
20
  * `Menubar` publish under their own prefixes.
21
+ *
22
+ * `activation="hover"` in a platform popup (a desktop with a pointer): the pointer resting on
23
+ * the trigger for `openDelay` opens the menu, as a long press does; leaving the trigger and the
24
+ * panel closes it after `closeDelay`, unless it is back on either by then or a key was used in the
25
+ * panel (the keyboard then has it, as on the web). The popup goes on tracking the pointer over
26
+ * the trigger while it holds its grab (`trackingRef`). The trigger's own press is unchanged.
21
27
  */
22
28
  import React, { useCallback, useMemo, useRef, type ComponentRef } from 'react';
23
- import type { View } from 'react-native';
29
+ import type { PointerEvent, View } from 'react-native';
24
30
 
25
31
  import { SheetShell } from '../dialog/SheetShell';
26
32
  import { PopupPanel } from '../floating/PopupPanel';
@@ -34,6 +40,8 @@ import { TriggerSlot } from '../floating/TriggerSlot';
34
40
  import { useMenuFocusIntent, useOpenFocus } from '../floating/menu-keyboard';
35
41
  import { useSheetOpenBridge } from '../floating/use-sheet-open-bridge';
36
42
  import { useControllableState } from '../hooks/use-controllable-state';
43
+ import { HOVER_CARD_CLOSE_DELAY, HOVER_CARD_OPEN_DELAY } from '../hover-card/constants';
44
+ import { useHoverIntent } from '../hover-card/use-hover-intent';
37
45
  import { DropdownMenuProvider, useDropdownMenu } from './context';
38
46
  import type {
39
47
  DropdownMenuContentProps,
@@ -41,6 +49,11 @@ import type {
41
49
  DropdownMenuTriggerProps,
42
50
  } from './types';
43
51
  import { useCommonMessages } from '../locale/common-messages';
52
+ import { CONTEXT_MENU_MESSAGES } from '../context-menu/messages';
53
+ import { useMessages } from '../locale/messages';
54
+
55
+ /** React Native's standard accessibility action for a long press (TalkBack's, a reader's). */
56
+ const LONG_PRESS_ACTION = 'longpress';
44
57
 
45
58
  export function DropdownMenu({
46
59
  children,
@@ -48,6 +61,8 @@ export function DropdownMenu({
48
61
  defaultOpen = false,
49
62
  onOpenChange,
50
63
  activation = 'click',
64
+ openDelay = HOVER_CARD_OPEN_DELAY,
65
+ closeDelay = HOVER_CARD_CLOSE_DELAY,
51
66
  openFocus,
52
67
  }: DropdownMenuProps) {
53
68
  const [isOpen, setOpen] = useControllableState<boolean>({
@@ -59,19 +74,51 @@ export function DropdownMenu({
59
74
  // An open from the owner's keys starts with a row active in a platform popup, as on a Mac.
60
75
  const focusIntent = useMenuFocusIntent();
61
76
  useOpenFocus(focusIntent, isOpen, openFocus);
77
+ const intent = useHoverIntent(isOpen, setOpen, openDelay, closeDelay);
78
+ // Opened by the pointer resting on the trigger, and none of its keys used since: the pointer
79
+ // leaving closes it. Opened any other way, only a choice, Escape or a press outside does.
80
+ const byHover = useRef(false);
81
+ const latestOpen = useRef(isOpen);
82
+ latestOpen.current = isOpen;
62
83
  const primaryOpen = useRef(false);
63
84
  const setMenuOpen = useCallback(
64
85
  (next: boolean) => {
65
- if (!next || !primaryOpen.current) setOpen(next);
86
+ if (next && primaryOpen.current) return;
87
+ intent.hold();
88
+ byHover.current = false;
89
+ setOpen(next);
66
90
  },
67
- [setOpen],
91
+ [intent, setOpen],
68
92
  );
69
93
  const onPrimaryOpenChange = useCallback(
70
94
  (next: boolean) => {
71
95
  primaryOpen.current = next;
72
- if (next) setOpen(false);
96
+ if (next) setMenuOpen(false);
73
97
  },
74
- [setOpen],
98
+ [setMenuOpen],
99
+ );
100
+ const hover = useMemo(
101
+ () => ({
102
+ enter: () => {
103
+ if (primaryOpen.current) return;
104
+ if (!latestOpen.current) byHover.current = true;
105
+ intent.show();
106
+ },
107
+ show: () => {
108
+ if (!primaryOpen.current) intent.show();
109
+ },
110
+ hide: () => {
111
+ // A pending open is dropped; an open menu closes only if hover opened it.
112
+ if (!latestOpen.current || byHover.current) intent.hide();
113
+ },
114
+ hold: intent.hold,
115
+ dismiss: () => setMenuOpen(false),
116
+ key: () => {
117
+ byHover.current = false;
118
+ intent.hold();
119
+ },
120
+ }),
121
+ [intent, setMenuOpen],
75
122
  );
76
123
  const value = useMemo(
77
124
  () => ({
@@ -80,9 +127,10 @@ export function DropdownMenu({
80
127
  anchorRef,
81
128
  focusIntent,
82
129
  activation,
130
+ ...(activation === 'hover' ? { hover } : null),
83
131
  onPrimaryOpenChange,
84
132
  }),
85
- [isOpen, setMenuOpen, focusIntent, activation, onPrimaryOpenChange],
133
+ [isOpen, setMenuOpen, focusIntent, activation, hover, onPrimaryOpenChange],
86
134
  );
87
135
 
88
136
  return <DropdownMenuProvider value={value}>{children}</DropdownMenuProvider>;
@@ -100,6 +148,20 @@ export function DropdownMenuTrigger({
100
148
  restoreFocus,
101
149
  }: DropdownMenuTriggerProps) {
102
150
  const menu = useDropdownMenu();
151
+ // Hover opens it only where it is a desktop's popup: never a phone's sheet.
152
+ const hover = usePopupSurface() ? menu.hover : undefined;
153
+ // A menu that opens on hover opens from the screen reader as a long press opens it, by the
154
+ // trigger's `longpress` action ("Show menu"), as `ContextMenu`'s does.
155
+ const { messages } = useMessages(CONTEXT_MENU_MESSAGES);
156
+ const showMenu =
157
+ menu.activation === 'hover' && !disabled
158
+ ? {
159
+ accessibilityActions: [{ name: LONG_PRESS_ACTION, label: messages.showMenu }],
160
+ onAccessibilityAction: (event: { nativeEvent: { actionName: string } }) => {
161
+ if (event.nativeEvent.actionName === LONG_PRESS_ACTION) menu.setOpen(true);
162
+ },
163
+ }
164
+ : null;
103
165
  const primaryAction =
104
166
  pressTrigger != null ||
105
167
  (React.isValidElement<{ onPress?: unknown; href?: string }>(children) &&
@@ -112,12 +174,27 @@ export function DropdownMenuTrigger({
112
174
  onPrimaryOpenChange={menu.onPrimaryOpenChange}
113
175
  anchorRef={menu.anchorRef}
114
176
  restoreFocus={restoreFocus}
177
+ onPointerEnter={
178
+ hover
179
+ ? (event: PointerEvent) => {
180
+ if (pointerTypeOf(event) !== 'touch') hover.enter();
181
+ }
182
+ : undefined
183
+ }
184
+ onPointerLeave={
185
+ hover
186
+ ? (event: PointerEvent) => {
187
+ if (pointerTypeOf(event) !== 'touch') hover.hide();
188
+ }
189
+ : undefined
190
+ }
115
191
  className={className}
116
192
  style={style}
117
193
  testID={testID}
118
194
  handle={{
119
195
  onPress: () => menu.setOpen(menu.activation !== 'hover' || !primaryAction),
120
196
  onLongPress: menu.activation === 'hover' ? () => menu.setOpen(true) : undefined,
197
+ ...showMenu,
121
198
  disabled,
122
199
  accessibilityLabel: label,
123
200
  accessibilityRole: menu.activation === 'hover' && asChild ? undefined : 'button',
@@ -174,6 +251,12 @@ export function DropdownMenuContent({
174
251
  focusIntent={menu.focusIntent}
175
252
  onDismiss={() => menu.setOpen(false)}
176
253
  onClose={() => menu.setOpen(false)}
254
+ // Hover: the pointer over the trigger goes on reaching it while the popup is open, and
255
+ // the pointer on the panel or a key in it keeps the menu open.
256
+ trackingRef={menu.hover ? menu.anchorRef : undefined}
257
+ onPointerEnter={menu.hover?.hold}
258
+ onPointerLeave={menu.hover?.hide}
259
+ onKey={menu.hover?.key}
177
260
  minWidth={minWidth}
178
261
  maxWidth={maxWidth}
179
262
  maxHeight={maxHeight}
@@ -208,6 +291,11 @@ export function DropdownMenuContent({
208
291
  );
209
292
  }
210
293
 
294
+ /** A pointer event's kind (`mouse`, `pen`, `touch`). */
295
+ function pointerTypeOf(event: PointerEvent): string | undefined {
296
+ return event.nativeEvent?.pointerType;
297
+ }
298
+
211
299
  const rows = createMenuRows('DropdownMenu', createInlineMenuSub);
212
300
 
213
301
  export const DropdownMenuItem = rows.Item;
@@ -20,6 +20,8 @@ export interface DropdownMenuContextValue extends OverlayShellContextValue {
20
20
  hide: () => void;
21
21
  hold: () => void;
22
22
  dismiss: () => void;
23
+ /** A key used in the open panel (native): a menu opened by hover then stays open. */
24
+ key?: () => void;
23
25
  };
24
26
  }
25
27
 
@@ -17,7 +17,13 @@
17
17
  import type React from 'react';
18
18
  import { createContext, useContext, useEffect, useRef, useState } from 'react';
19
19
  import type { ComponentRef, ComponentType, RefObject } from 'react';
20
- import { StyleSheet, type View, type StyleProp, type ViewStyle } from 'react-native';
20
+ import {
21
+ type PointerEvent,
22
+ StyleSheet,
23
+ type View,
24
+ type StyleProp,
25
+ type ViewStyle,
26
+ } from 'react-native';
21
27
  import { StyledView } from '../styles/styled-primitives';
22
28
  import { SurfacePaint } from '../surface/SurfacePaint';
23
29
  import type { PopupDismissReason, PopupSurfaceProps } from '../platform-surface/types';
@@ -71,12 +77,17 @@ export function PopupPanelSurface({
71
77
  style,
72
78
  panelRef,
73
79
  edge = POPUP_PANEL_BORDER,
80
+ onPointerEnter,
81
+ onPointerLeave,
74
82
  children,
75
83
  }: {
76
84
  className?: string;
77
85
  style?: StyleProp<ViewStyle>;
78
86
  panelRef?: React.Ref<ComponentRef<typeof View>>;
79
87
  edge?: number;
88
+ /** The pointer coming onto the panel and leaving it: a hover menu stays open while it is on it. */
89
+ onPointerEnter?: (event: PointerEvent) => void;
90
+ onPointerLeave?: (event: PointerEvent) => void;
80
91
  children?: React.ReactNode;
81
92
  }) {
82
93
  const palette = useMenuPalette();
@@ -86,6 +97,8 @@ export function PopupPanelSurface({
86
97
  <StyledView
87
98
  ref={panelRef}
88
99
  className={className}
100
+ onPointerEnter={onPointerEnter}
101
+ onPointerLeave={onPointerLeave}
89
102
  style={[
90
103
  styles.panel,
91
104
  { borderWidth: edge, boxShadow: palette.shadow },
@@ -120,6 +133,8 @@ export function PopupPanelChrome({
120
133
  className,
121
134
  style,
122
135
  panelRef,
136
+ onPointerEnter,
137
+ onPointerLeave,
123
138
  children,
124
139
  }: {
125
140
  minWidth?: number;
@@ -128,12 +143,16 @@ export function PopupPanelChrome({
128
143
  className?: string;
129
144
  style?: StyleProp<ViewStyle>;
130
145
  panelRef?: React.Ref<ComponentRef<typeof View>>;
146
+ onPointerEnter?: (event: PointerEvent) => void;
147
+ onPointerLeave?: (event: PointerEvent) => void;
131
148
  children?: React.ReactNode;
132
149
  }) {
133
150
  const density = useMenuDensity();
134
151
  return (
135
152
  <PopupPanelSurface
136
153
  panelRef={panelRef}
154
+ onPointerEnter={onPointerEnter}
155
+ onPointerLeave={onPointerLeave}
137
156
  className={className}
138
157
  style={[
139
158
  // `MENU_WIDTH`, or a desktop's floor: there a menu is as wide as its widest row.
@@ -168,6 +187,14 @@ export interface PopupPanelProps extends FloatingPositionProps {
168
187
  focusIntent?: MenuFocusIntent;
169
188
  /** Given the panel's keyboard navigation (a menu bar asks it which arrows it takes). */
170
189
  navigationRef?: React.MutableRefObject<PopupNavigation | null>;
190
+ /**
191
+ * The pointer coming onto the panel and leaving it, and a key pressed in it before its rows
192
+ * take it: a menu opened by hover stays open while the pointer is on it, and once its keys
193
+ * are used.
194
+ */
195
+ onPointerEnter?: (event: PointerEvent) => void;
196
+ onPointerLeave?: (event: PointerEvent) => void;
197
+ onKey?: (key: string) => void;
171
198
  minWidth?: number;
172
199
  maxWidth?: number;
173
200
  maxHeight?: number;
@@ -192,6 +219,9 @@ export function PopupPanel({
192
219
  onClose,
193
220
  focusIntent,
194
221
  navigationRef,
222
+ onPointerEnter,
223
+ onPointerLeave,
224
+ onKey,
195
225
  minWidth,
196
226
  maxWidth,
197
227
  maxHeight,
@@ -235,13 +265,22 @@ export function PopupPanel({
235
265
  role={role}
236
266
  label={label}
237
267
  onDismiss={onDismiss}
238
- onKey={navigation.onKey}
268
+ onKey={
269
+ onKey
270
+ ? (key) => {
271
+ onKey(key);
272
+ navigation.onKey(key);
273
+ }
274
+ : navigation.onKey
275
+ }
239
276
  testID={testID}
240
277
  >
241
278
  {/* The popup surface carries the content's role and label; labelling the panel too
242
279
  would announce the menu's name twice. */}
243
280
  <PopupPanelChrome
244
281
  panelRef={panelRef}
282
+ onPointerEnter={onPointerEnter}
283
+ onPointerLeave={onPointerLeave}
245
284
  minWidth={minWidth}
246
285
  maxWidth={maxWidth}
247
286
  maxHeight={maxHeight}
@@ -71,6 +71,12 @@ export interface TriggerSlotProps {
71
71
  * while another of the bar's menus is open.
72
72
  */
73
73
  onPointerMove?: (event: PointerEvent) => void;
74
+ /**
75
+ * The pointer coming onto the trigger and leaving it, heard on the wrapper: a menu that opens on
76
+ * hover (`DropdownMenu`'s `activation="hover"`) opens as the pointer rests on its trigger.
77
+ */
78
+ onPointerEnter?: (event: PointerEvent) => void;
79
+ onPointerLeave?: (event: PointerEvent) => void;
74
80
  /**
75
81
  * Whether the trigger's surface is open, when the trigger does not say so
76
82
  * itself through `aria-expanded`: a context menu's area is no disclosure, and
@@ -220,6 +226,8 @@ export function TriggerSlot({
220
226
  anchorRef,
221
227
  onPointerDown,
222
228
  onPointerMove,
229
+ onPointerEnter,
230
+ onPointerLeave,
223
231
  open,
224
232
  restoreFocus = true,
225
233
  className,
@@ -251,6 +259,8 @@ export function TriggerSlot({
251
259
  collapsable={false}
252
260
  onPointerDown={onPointerDown}
253
261
  onPointerMove={onPointerMove}
262
+ onPointerEnter={onPointerEnter}
263
+ onPointerLeave={onPointerLeave}
254
264
  className={className}
255
265
  style={[styles.wrap, style]}
256
266
  testID={testID}
package/src/index.ts CHANGED
@@ -107,6 +107,7 @@ export type {
107
107
  PlatformEditingState,
108
108
  PlatformKeyboard,
109
109
  PlatformKeyEvent,
110
+ PlatformModifiers,
110
111
  PlatformMenu,
111
112
  PlatformMenuBar,
112
113
  PlatformMenuAction,
package/src/index.web.ts CHANGED
@@ -112,6 +112,7 @@ export type {
112
112
  PlatformEditingState,
113
113
  PlatformKeyboard,
114
114
  PlatformKeyEvent,
115
+ PlatformModifiers,
115
116
  PlatformMenu,
116
117
  PlatformMenuBar,
117
118
  PlatformMenuAction,
package/src/kbd/chord.ts CHANGED
@@ -18,7 +18,7 @@
18
18
  */
19
19
  import { Platform } from 'react-native';
20
20
 
21
- import type { PlatformKeyEvent } from '../platform-surface/types';
21
+ import type { PlatformKeyEvent, PlatformModifiers } from '../platform-surface/types';
22
22
 
23
23
  export interface Chord {
24
24
  /** A lower-case character, or a `NAMED_KEYS` name. */
@@ -137,6 +137,57 @@ export function chordText(chord: Chord): string {
137
137
  return parts.join('+');
138
138
  }
139
139
 
140
+ /**
141
+ * Modifier keys alone, without a key: the ones an alternate menu item is shown
142
+ * with (`MenubarItemProps.alternate`), as a Mac's `keyEquivalentModifierMask`.
143
+ */
144
+ export type ChordModifiers = Omit<Chord, 'key'>;
145
+
146
+ /**
147
+ * Modifiers from their declared text (`Alt`, `Alt+Shift`, `Mod+Alt`: the
148
+ * names a chord's modifiers take), or null when the text is not one.
149
+ */
150
+ export function parseModifiers(text: string): ChordModifiers | null {
151
+ const modifiers = { mod: false, ctrl: false, alt: false, shift: false, meta: false };
152
+ const names = text.split('+').map((part) => part.trim().toLowerCase());
153
+ for (const name of names) {
154
+ const modifier = MODIFIERS[name as keyof typeof MODIFIERS];
155
+ if (!modifier) return null;
156
+ modifiers[modifier] = true;
157
+ }
158
+ return modifiers;
159
+ }
160
+
161
+ /** The modifiers in their one canonical spelling, as `chordText` spells a chord's. */
162
+ export function modifiersText(modifiers: ChordModifiers): string {
163
+ const parts: string[] = [];
164
+ if (modifiers.mod) parts.push('Mod');
165
+ if (modifiers.ctrl) parts.push('Ctrl');
166
+ if (modifiers.alt) parts.push('Alt');
167
+ if (modifiers.shift) parts.push('Shift');
168
+ if (modifiers.meta) parts.push('Meta');
169
+ return parts.join('+');
170
+ }
171
+
172
+ /**
173
+ * Whether exactly these modifiers are held, on this platform (`Mod` resolved):
174
+ * `Alt` is Option alone, not Option with Shift, as a Mac matches an alternate
175
+ * item's modifier mask.
176
+ */
177
+ export function modifiersHeld(
178
+ modifiers: ChordModifiers,
179
+ held: PlatformModifiers,
180
+ apple: boolean = isApplePlatform(),
181
+ ): boolean {
182
+ const wanted = modifiersOf({ ...modifiers, key: '' }, apple);
183
+ return (
184
+ held.ctrlKey === wanted.ctrl &&
185
+ held.altKey === wanted.alt &&
186
+ held.shiftKey === wanted.shift &&
187
+ held.metaKey === wanted.meta
188
+ );
189
+ }
190
+
140
191
  /** The modifiers the chord's key press carries on this platform: `Mod` resolved. */
141
192
  function modifiersOf(chord: Chord, apple: boolean): Modifiers {
142
193
  return {
@@ -28,7 +28,9 @@
28
28
  import {
29
29
  type ComponentType,
30
30
  Fragment,
31
+ cloneElement,
31
32
  isValidElement,
33
+ type ReactElement,
32
34
  type ReactNode,
33
35
  createContext,
34
36
  useContext,
@@ -43,7 +45,14 @@ import { openHref } from '../hooks/link-activation';
43
45
  import { MenuRadioGroupProvider, useMenuRadioGroup } from '../floating/context';
44
46
  import { useControllableState } from '../hooks/use-controllable-state';
45
47
  import type { MenuRowParts } from '../floating/types';
46
- import { type Chord, formatChord, parseChord } from '../kbd/chord';
48
+ import {
49
+ type Chord,
50
+ formatChord,
51
+ isApplePlatform,
52
+ modifiersHeld,
53
+ parseChord,
54
+ parseModifiers,
55
+ } from '../kbd/chord';
47
56
  import { usePlatformEditing } from '../platform-surface/context';
48
57
  import { useIsomorphicLayoutEffect } from '../theme/use-isomorphic-layout-effect';
49
58
  import {
@@ -53,14 +62,18 @@ import {
53
62
  useMenubarScope,
54
63
  } from './context';
55
64
  import {
65
+ alternateOf,
66
+ alternatePlaces,
56
67
  type DescribedEntry,
57
68
  describeMenu,
58
69
  markMenubarHint,
59
70
  markMenubarPart,
71
+ places,
60
72
  textOf,
61
73
  } from './description';
62
74
  import { type MenuEntry, nextMenuEntryId } from './registry';
63
75
  import { actionShortcut, isMenuAction, responderOf, useEditAction } from './use-edit-action';
76
+ import { useHeldModifiers } from './use-held-modifiers';
64
77
  import { useMenubarLeaving } from './viewport';
65
78
  import type {
66
79
  MenubarCheckboxItemProps,
@@ -136,6 +149,25 @@ export function useChord(shortcut: string | undefined): Chord | null {
136
149
  }, [shortcut]);
137
150
  }
138
151
 
152
+ const warnedAlternates = new Set<string>();
153
+
154
+ /** Warns once in development when an item's `alternate` names no modifiers. */
155
+ function useAlternateWarning(alternate: string | undefined): void {
156
+ if (
157
+ alternate === undefined ||
158
+ process.env.NODE_ENV === 'production' ||
159
+ parseModifiers(alternate) !== null ||
160
+ warnedAlternates.has(alternate)
161
+ ) {
162
+ return;
163
+ }
164
+ warnedAlternates.add(alternate);
165
+ console.warn(
166
+ `[Bloom] Menubar: "${alternate}" is not a set of modifiers, so the item is not an alternate. ` +
167
+ 'Write them out of Mod, Ctrl, Alt, Shift and Meta: "Alt", "Alt+Shift".',
168
+ );
169
+ }
170
+
139
171
  function Slot({
140
172
  scope,
141
173
  position,
@@ -160,7 +192,7 @@ function Slot({
160
192
  export function DeclaredChildren({ children }: { children?: ReactNode }) {
161
193
  const scope = useMenubarScope();
162
194
  if (!scope) return <>{children}</>;
163
- const slots: ReactNode[] = [];
195
+ const placed: { node: ReactNode; key: string }[] = [];
164
196
  const visit = (node: ReactNode, keyPrefix: string) => {
165
197
  if (Array.isArray(node)) {
166
198
  node.forEach((child, index) => {
@@ -172,19 +204,112 @@ export function DeclaredChildren({ children }: { children?: ReactNode }) {
172
204
  visit(node.props.children, `${keyPrefix}${node.key ?? ''}:`);
173
205
  return;
174
206
  }
175
- const position = slots.length;
207
+ const position = placed.length;
176
208
  const key =
177
209
  isValidElement(node) && node.key != null
178
210
  ? `${keyPrefix}$${node.key}`
179
211
  : `${keyPrefix}${position}`;
180
- slots.push(
181
- <Slot key={key} scope={scope} position={position}>
182
- {node}
183
- </Slot>,
184
- );
212
+ placed.push({ node, key });
185
213
  };
186
214
  visit(children, '');
187
- return <>{slots}</>;
215
+ const alternates = alternatePlaces(placed.map(({ node }) => node));
216
+ return (
217
+ <>
218
+ {placed.map(({ node, key }, position) => (
219
+ <Slot key={key} scope={scope} position={position}>
220
+ {alternates[position] ? <Alternate of={node}>{node}</Alternate> : node}
221
+ </Slot>
222
+ ))}
223
+ </>
224
+ );
225
+ }
226
+
227
+ /**
228
+ * The modifiers an item shows with in place of the item before it, when it is an alternate placed
229
+ * after one (`alternatePlaces`); undefined for every other item.
230
+ */
231
+ const AlternateContext = createContext<string | undefined>(undefined);
232
+
233
+ /** An alternate's item, known as one: it registers its modifiers. */
234
+ function Alternate({ of, children }: { of: ReactNode; children: ReactNode }) {
235
+ const props = isValidElement<{ alternate?: string }>(of) ? of.props : {};
236
+ return (
237
+ <AlternateContext.Provider value={alternateOf(props.alternate).alternate}>
238
+ {children}
239
+ </AlternateContext.Provider>
240
+ );
241
+ }
242
+
243
+ /**
244
+ * A drawn container's children with each item and its alternates as one row (`AlternateRow`); the
245
+ * children themselves when none of them is an alternate.
246
+ */
247
+ function withAlternates(children: ReactNode): ReactNode {
248
+ const placed = places(children);
249
+ const alternates = alternatePlaces(placed);
250
+ if (!alternates.includes(true)) return children;
251
+ const out: ReactNode[] = [];
252
+ placed.forEach((node, position) => {
253
+ if (alternates[position]) return;
254
+ const key = isValidElement(node) && node.key != null ? `$${node.key}` : `@${position}`;
255
+ let next = position + 1;
256
+ while (alternates[next]) next += 1;
257
+ if (next === position + 1 || !isValidElement(node)) {
258
+ out.push(<Fragment key={key}>{node}</Fragment>);
259
+ return;
260
+ }
261
+ out.push(
262
+ <AlternateRow
263
+ key={key}
264
+ item={node}
265
+ alternates={placed.slice(position + 1, next) as ReactElement<{ alternate?: string }>[]}
266
+ />,
267
+ );
268
+ });
269
+ return out;
270
+ }
271
+
272
+ /**
273
+ * An item and its alternates, drawn as ONE row of the open menu, as a Mac's menu draws an item
274
+ * and its `isAlternate` siblings: the first alternate whose modifiers are held now, else the item.
275
+ * The row keeps its place in the menu's keyboard navigation as it swaps (it is one element of one
276
+ * type, so React keeps it), and registers nothing itself: the item and each alternate are known to
277
+ * the bar headless beside it, each with its own entry and shortcut, whichever is drawn.
278
+ */
279
+ function AlternateRow({
280
+ item,
281
+ alternates,
282
+ }: {
283
+ item: ReactElement;
284
+ alternates: readonly ReactElement<{ alternate?: string }>[];
285
+ }) {
286
+ const scope = useMenubarScope();
287
+ const held = useHeldModifiers();
288
+ const apple = isApplePlatform();
289
+ const shown =
290
+ alternates.find((alternate) => {
291
+ const modifiers = parseModifiers(alternate.props.alternate ?? '');
292
+ return modifiers !== null && modifiersHeld(modifiers, held, apple);
293
+ }) ?? item;
294
+ const known = useMemo(() => scope && { ...scope, headless: true, owner: undefined }, [scope]);
295
+ return (
296
+ <>
297
+ {known ? (
298
+ <MenubarScopeProvider value={known}>
299
+ {item}
300
+ {alternates.map((alternate, index) => (
301
+ // biome-ignore lint/suspicious/noArrayIndexKey: an alternate's place after its item is its identity.
302
+ <Alternate key={index} of={alternate}>
303
+ {alternate}
304
+ </Alternate>
305
+ ))}
306
+ </MenubarScopeProvider>
307
+ ) : null}
308
+ <MenubarScopeProvider value={null}>
309
+ {cloneElement(shown, { key: 'shown' })}
310
+ </MenubarScopeProvider>
311
+ </>
312
+ );
188
313
  }
189
314
 
190
315
  const subscribeNowhere = () => () => undefined;
@@ -264,6 +389,7 @@ export function StaticDeclaration({ children }: { children?: ReactNode }) {
264
389
  ...(entry.checked === undefined ? null : { checked: entry.checked }),
265
390
  ...(entry.keepOpen === undefined ? null : { keepOpen: entry.keepOpen }),
266
391
  ...(entry.action ? { action: entry.action } : null),
392
+ ...(entry.alternate ? { alternate: entry.alternate } : null),
267
393
  run: entry.runs ? runFor(entry.key) : undefined,
268
394
  });
269
395
  }
@@ -333,7 +459,7 @@ export function createMenubarContent(
333
459
  {menu.open || scope?.declareClosed === false ? null : (
334
460
  <StaticDeclaration>{props.children}</StaticDeclaration>
335
461
  )}
336
- <Panel {...props} />
462
+ <Panel {...props}>{withAlternates(props.children)}</Panel>
337
463
  </>
338
464
  );
339
465
  }
@@ -367,9 +493,11 @@ export function createMenubarRows(rows: MenuRowParts): MenubarRowParts {
367
493
  const isHint = (type: unknown) => type === Shortcut;
368
494
  const hint = (chord: Chord | null) => (chord ? <Shortcut>{formatChord(chord)}</Shortcut> : null);
369
495
 
370
- function MenubarItem({ shortcut, action, ...props }: MenubarItemProps) {
496
+ function MenubarItem({ shortcut, action, alternate: declared, ...props }: MenubarItemProps) {
371
497
  const scope = useMenubarScope();
372
498
  const chord = useChord(shortcut ?? actionShortcut(action));
499
+ useAlternateWarning(declared);
500
+ const alternate = useContext(AlternateContext);
373
501
  const { onPress, href } = props;
374
502
  const handler = onPress ?? (href != null ? () => openHref(href) : undefined);
375
503
  // An editing action goes to the focused text field first (use-edit-action.ts); the
@@ -387,6 +515,7 @@ export function createMenubarRows(rows: MenuRowParts): MenubarRowParts {
387
515
  shortcut: chord,
388
516
  keepOpen: props.keepOpen ?? false,
389
517
  ...(menuAction ? { action: menuAction } : null),
518
+ ...(alternate ? { alternate } : null),
390
519
  },
391
520
  item.run,
392
521
  );
@@ -477,7 +606,8 @@ export function createMenubarRows(rows: MenuRowParts): MenubarRowParts {
477
606
 
478
607
  function MenubarGroup(props: MenubarGroupProps) {
479
608
  const scope = useMenubarScope();
480
- if (!scope?.headless) return <rows.Group {...props} />;
609
+ if (!scope?.headless)
610
+ return <rows.Group {...props}>{withAlternates(props.children)}</rows.Group>;
481
611
  return <DeclaredChildren>{props.children}</DeclaredChildren>;
482
612
  }
483
613
  MenubarGroup.displayName = 'MenubarGroup';
@@ -552,7 +682,7 @@ export function createMenubarRows(rows: MenuRowParts): MenubarRowParts {
552
682
  return (
553
683
  <>
554
684
  {open ? null : <StaticDeclaration>{props.children}</StaticDeclaration>}
555
- <rows.SubContent {...props} />
685
+ <rows.SubContent {...props}>{withAlternates(props.children)}</rows.SubContent>
556
686
  </>
557
687
  );
558
688
  }