@dxos/react-ui-menu 0.8.4-main.c4373fc → 0.8.4-main.d05673bc65
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/dist/lib/browser/index.mjs +547 -404
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +547 -404
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/builder.d.ts +7 -1
- package/dist/types/src/builder.d.ts.map +1 -1
- package/dist/types/src/components/ActionLabel.d.ts +3 -3
- package/dist/types/src/components/ActionLabel.d.ts.map +1 -1
- package/dist/types/src/components/DropdownMenu.d.ts +1 -1
- package/dist/types/src/components/DropdownMenu.d.ts.map +1 -1
- package/dist/types/src/components/Menu.d.ts +46 -0
- package/dist/types/src/components/Menu.d.ts.map +1 -0
- package/dist/types/src/components/Menu.test.d.ts +2 -0
- package/dist/types/src/components/Menu.test.d.ts.map +1 -0
- package/dist/types/src/components/MenuItems.stories.d.ts +21 -0
- package/dist/types/src/components/MenuItems.stories.d.ts.map +1 -0
- package/dist/types/src/components/ToolbarMenu.d.ts +4 -3
- package/dist/types/src/components/ToolbarMenu.d.ts.map +1 -1
- package/dist/types/src/components/ToolbarMenu.stories.d.ts +2 -2
- package/dist/types/src/components/ToolbarMenu.stories.d.ts.map +1 -1
- package/dist/types/src/components/index.d.ts +1 -3
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useMenuActions.d.ts +8 -7
- package/dist/types/src/hooks/useMenuActions.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +0 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/testing/generator.d.ts +15 -13
- package/dist/types/src/testing/generator.d.ts.map +1 -1
- package/dist/types/src/translations.d.ts +2 -2
- package/dist/types/src/translations.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +36 -26
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/src/util.d.ts +14 -5
- package/dist/types/src/util.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +35 -29
- package/src/builder.ts +36 -9
- package/src/components/ActionLabel.tsx +5 -5
- package/src/components/DropdownMenu.tsx +12 -5
- package/src/components/Menu.test.tsx +448 -0
- package/src/components/Menu.tsx +349 -0
- package/src/components/MenuItems.stories.tsx +265 -0
- package/src/components/ToolbarMenu.stories.tsx +30 -23
- package/src/components/ToolbarMenu.tsx +43 -18
- package/src/components/index.ts +1 -3
- package/src/hooks/useMenuActions.ts +29 -54
- package/src/index.ts +0 -2
- package/src/testing/generator.ts +67 -62
- package/src/translations.ts +1 -1
- package/src/types.ts +38 -27
- package/src/util.ts +34 -15
- package/dist/types/src/components/MenuContext.d.ts +0 -13
- package/dist/types/src/components/MenuContext.d.ts.map +0 -1
- package/src/components/MenuContext.tsx +0 -50
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Atom, RegistryContext, useAtomValue } from '@effect-atom/atom-react';
|
|
6
|
+
import { type Scope, createContextScope } from '@radix-ui/react-context';
|
|
7
|
+
import { useControllableState } from '@radix-ui/react-use-controllable-state';
|
|
8
|
+
import React, { type MouseEvent, type PropsWithChildren, useCallback, useContext, useMemo } from 'react';
|
|
9
|
+
|
|
10
|
+
import { log } from '@dxos/log';
|
|
11
|
+
import { type DropdownMenuRootProps, Icon, DropdownMenu as NaturalDropdownMenu } from '@dxos/react-ui';
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
type AddMenuItemsProps,
|
|
15
|
+
type MenuAction,
|
|
16
|
+
type MenuContextValue,
|
|
17
|
+
type MenuItem,
|
|
18
|
+
type MenuItemGroup,
|
|
19
|
+
type MenuItems,
|
|
20
|
+
type MenuItemsAccessor,
|
|
21
|
+
type MenuItemsMap,
|
|
22
|
+
} from '../types';
|
|
23
|
+
import { executeMenuAction } from '../util';
|
|
24
|
+
|
|
25
|
+
import { ActionLabel } from './ActionLabel';
|
|
26
|
+
import { ToolbarMenu } from './ToolbarMenu';
|
|
27
|
+
|
|
28
|
+
//
|
|
29
|
+
// Scoped context.
|
|
30
|
+
//
|
|
31
|
+
|
|
32
|
+
type MenuScopedProps<P> = P & { __menuScope?: Scope };
|
|
33
|
+
|
|
34
|
+
const MENU_NAME = 'Menu';
|
|
35
|
+
|
|
36
|
+
const [createMenuContext, createMenuScope] = createContextScope(MENU_NAME, []);
|
|
37
|
+
|
|
38
|
+
const nullItemsAtom = Atom.make<MenuItem[] | null>(null);
|
|
39
|
+
const defaultItemsAccessor: MenuItemsAccessor = () => nullItemsAtom;
|
|
40
|
+
|
|
41
|
+
const menuContextDefaults: MenuContextValue = {
|
|
42
|
+
iconSize: 5,
|
|
43
|
+
items: defaultItemsAccessor,
|
|
44
|
+
onAction: undefined,
|
|
45
|
+
menuItemsAtom: Atom.make<MenuItemsMap>(new Map()),
|
|
46
|
+
addMenuItems: () => {},
|
|
47
|
+
removeMenuItems: () => {},
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const [MenuContextProvider, useMenuScoped] = createMenuContext<MenuContextValue>(MENU_NAME, menuContextDefaults);
|
|
51
|
+
|
|
52
|
+
const useMenuScope = createMenuScope();
|
|
53
|
+
|
|
54
|
+
//
|
|
55
|
+
// Dropdown context (internal) — allows Menu.Content to close the parent dropdown.
|
|
56
|
+
//
|
|
57
|
+
|
|
58
|
+
type MenuDropdownContextValue = {
|
|
59
|
+
closeMenu: () => void;
|
|
60
|
+
caller?: string;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const MenuDropdownContext = React.createContext<MenuDropdownContextValue>({
|
|
64
|
+
closeMenu: () => {},
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
//
|
|
68
|
+
// MenuProvider (internal) — the context provider used by Menu.Root.
|
|
69
|
+
//
|
|
70
|
+
|
|
71
|
+
const DEFAULT_PRIORITY = 100;
|
|
72
|
+
|
|
73
|
+
const sortMenuItems = (items: MenuItems[]) =>
|
|
74
|
+
[...items].sort((a, b) => {
|
|
75
|
+
if (a.priority !== b.priority) {
|
|
76
|
+
return a.priority - b.priority;
|
|
77
|
+
}
|
|
78
|
+
return a.id.localeCompare(b.id);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
type MenuProviderProps = PropsWithChildren<Partial<MenuContextValue>>;
|
|
82
|
+
|
|
83
|
+
const MenuProvider = ({
|
|
84
|
+
children,
|
|
85
|
+
items = menuContextDefaults.items,
|
|
86
|
+
iconSize = menuContextDefaults.iconSize,
|
|
87
|
+
attendableId,
|
|
88
|
+
alwaysActive,
|
|
89
|
+
onAction,
|
|
90
|
+
}: MenuProviderProps) => {
|
|
91
|
+
const { scope } = useMenuScope(undefined);
|
|
92
|
+
const registry = useContext(RegistryContext);
|
|
93
|
+
const menuItemsAtom = useMemo(() => Atom.make<MenuItemsMap>(new Map()).pipe(Atom.keepAlive), []);
|
|
94
|
+
|
|
95
|
+
const addMenuItems = useCallback(
|
|
96
|
+
(props: AddMenuItemsProps) => {
|
|
97
|
+
const priority = props.priority ?? DEFAULT_PRIORITY;
|
|
98
|
+
const prev = registry.get(menuItemsAtom);
|
|
99
|
+
const next = new Map(prev);
|
|
100
|
+
next.set(props.id, { ...props, priority });
|
|
101
|
+
registry.set(menuItemsAtom, next);
|
|
102
|
+
},
|
|
103
|
+
[registry, menuItemsAtom],
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
const removeMenuItems = useCallback(
|
|
107
|
+
(id: string) => {
|
|
108
|
+
const prev = registry.get(menuItemsAtom);
|
|
109
|
+
const next = new Map(prev);
|
|
110
|
+
next.delete(id);
|
|
111
|
+
registry.set(menuItemsAtom, next);
|
|
112
|
+
},
|
|
113
|
+
[registry, menuItemsAtom],
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
return (
|
|
117
|
+
<MenuContextProvider
|
|
118
|
+
items={items}
|
|
119
|
+
iconSize={iconSize}
|
|
120
|
+
attendableId={attendableId}
|
|
121
|
+
alwaysActive={alwaysActive}
|
|
122
|
+
menuItemsAtom={menuItemsAtom}
|
|
123
|
+
addMenuItems={addMenuItems}
|
|
124
|
+
removeMenuItems={removeMenuItems}
|
|
125
|
+
onAction={onAction}
|
|
126
|
+
scope={scope}
|
|
127
|
+
>
|
|
128
|
+
{children}
|
|
129
|
+
</MenuContextProvider>
|
|
130
|
+
);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
//
|
|
134
|
+
// Item resolution.
|
|
135
|
+
//
|
|
136
|
+
|
|
137
|
+
const resolveItems = (
|
|
138
|
+
baseItems: MenuItem[] | null,
|
|
139
|
+
group: MenuItemGroup | undefined,
|
|
140
|
+
entries: ReadonlyMap<string, MenuItems>,
|
|
141
|
+
): MenuItem[] | null => {
|
|
142
|
+
const applicable = [...entries.values()].filter((entry) => !entry.groupFilter || entry.groupFilter(group));
|
|
143
|
+
if (applicable.length === 0) {
|
|
144
|
+
return baseItems;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const sorted = sortMenuItems(applicable);
|
|
148
|
+
|
|
149
|
+
const replacements = sorted.filter((entry) => entry.mode === 'replacement');
|
|
150
|
+
if (replacements.length > 0) {
|
|
151
|
+
if (replacements.length > 1) {
|
|
152
|
+
log.warn('multiple replacement entries found', {
|
|
153
|
+
ids: replacements.map((r) => r.id).join(', '),
|
|
154
|
+
using: replacements[0].id,
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
return replacements[0].items;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const additive = sorted.filter((entry) => entry.mode === 'additive');
|
|
161
|
+
const additiveItems = additive.flatMap((entry) => entry.items);
|
|
162
|
+
|
|
163
|
+
if (!baseItems || baseItems.length === 0) {
|
|
164
|
+
return additiveItems.length > 0 ? additiveItems : null;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return [...baseItems, ...additiveItems];
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
//
|
|
171
|
+
// Public hooks.
|
|
172
|
+
//
|
|
173
|
+
|
|
174
|
+
const useMenuItems = (
|
|
175
|
+
group?: MenuItemGroup,
|
|
176
|
+
propsItems?: MenuItem[],
|
|
177
|
+
consumerName: string = 'useMenuItemConsumer',
|
|
178
|
+
__menuScope?: Scope,
|
|
179
|
+
) => {
|
|
180
|
+
const { items, menuItemsAtom } = useMenuScoped(consumerName, __menuScope);
|
|
181
|
+
const groupItems = useAtomValue(items(group));
|
|
182
|
+
const entries = useAtomValue(menuItemsAtom) ?? new Map();
|
|
183
|
+
|
|
184
|
+
const baseItems = useMemo(() => propsItems ?? groupItems ?? null, [propsItems, groupItems]);
|
|
185
|
+
|
|
186
|
+
const resolved = useMemo(
|
|
187
|
+
() => resolveItems(baseItems, group, entries as ReadonlyMap<string, MenuItems>),
|
|
188
|
+
[baseItems, group, entries],
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
return resolved ?? undefined;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
/** Returns the menu context without Radix scope. */
|
|
195
|
+
const useMenu = (consumerName: string): MenuContextValue => {
|
|
196
|
+
return useMenuScoped(consumerName, undefined);
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
//
|
|
200
|
+
// Menu.Root
|
|
201
|
+
//
|
|
202
|
+
|
|
203
|
+
type MenuRootProps = PropsWithChildren<
|
|
204
|
+
Omit<MenuProviderProps, 'children'> &
|
|
205
|
+
Pick<DropdownMenuRootProps, 'open' | 'defaultOpen' | 'onOpenChange'> & {
|
|
206
|
+
/** Identifies the component that owns this menu (passed to action handlers). */
|
|
207
|
+
caller?: string;
|
|
208
|
+
}
|
|
209
|
+
>;
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Menu context boundary.
|
|
213
|
+
*
|
|
214
|
+
* Provides the menu context (action dispatch, contribution registry, icon size, etc.)
|
|
215
|
+
* and an optional dropdown root for use with `Menu.Trigger` + `Menu.Content`.
|
|
216
|
+
*/
|
|
217
|
+
const MenuRoot = ({ children, open, defaultOpen, onOpenChange, caller, ...props }: MenuRootProps) => {
|
|
218
|
+
const [menuOpen, setMenuOpen] = useControllableState({
|
|
219
|
+
prop: open,
|
|
220
|
+
defaultProp: defaultOpen,
|
|
221
|
+
onChange: onOpenChange,
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
const closeMenu = useCallback(() => setMenuOpen(false), [setMenuOpen]);
|
|
225
|
+
|
|
226
|
+
return (
|
|
227
|
+
<MenuProvider {...props}>
|
|
228
|
+
<MenuDropdownContext.Provider value={{ closeMenu, caller }}>
|
|
229
|
+
<NaturalDropdownMenu.Root open={menuOpen} onOpenChange={setMenuOpen}>
|
|
230
|
+
{children}
|
|
231
|
+
</NaturalDropdownMenu.Root>
|
|
232
|
+
</MenuDropdownContext.Provider>
|
|
233
|
+
</MenuProvider>
|
|
234
|
+
);
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
//
|
|
238
|
+
// Menu.Content
|
|
239
|
+
//
|
|
240
|
+
|
|
241
|
+
type MenuContentProps = {
|
|
242
|
+
group?: MenuItemGroup;
|
|
243
|
+
items?: MenuItem[];
|
|
244
|
+
caller?: string;
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
const MenuContentItem = ({
|
|
248
|
+
item,
|
|
249
|
+
onClick,
|
|
250
|
+
__menuScope,
|
|
251
|
+
}: MenuScopedProps<{
|
|
252
|
+
item: MenuItem;
|
|
253
|
+
onClick: (action: MenuAction, event: MouseEvent) => void;
|
|
254
|
+
}>) => {
|
|
255
|
+
const action = item as MenuAction;
|
|
256
|
+
const handleClick = useCallback((event: MouseEvent) => onClick(action, event), [action, onClick]);
|
|
257
|
+
const { iconSize } = useMenuScoped('MenuContentItem', __menuScope);
|
|
258
|
+
return (
|
|
259
|
+
<NaturalDropdownMenu.Item
|
|
260
|
+
onClick={handleClick}
|
|
261
|
+
classNames='gap-2'
|
|
262
|
+
disabled={action.properties?.disabled}
|
|
263
|
+
{...(action.properties?.testId && { 'data-testid': action.properties.testId })}
|
|
264
|
+
>
|
|
265
|
+
{action.properties?.icon && <Icon icon={action.properties!.icon} size={iconSize} />}
|
|
266
|
+
<ActionLabel action={action} />
|
|
267
|
+
</NaturalDropdownMenu.Item>
|
|
268
|
+
);
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Renders the dropdown menu portal, content, and graph-backed items.
|
|
273
|
+
*
|
|
274
|
+
* Must be a descendant of `Menu.Root`. Reads items via `useMenuItems` from the
|
|
275
|
+
* nearest menu context, with optional `group`/`items` prop overrides.
|
|
276
|
+
*/
|
|
277
|
+
const MenuContent = ({
|
|
278
|
+
group,
|
|
279
|
+
items: propsItems,
|
|
280
|
+
caller: callerOverride,
|
|
281
|
+
__menuScope,
|
|
282
|
+
}: MenuScopedProps<MenuContentProps>) => {
|
|
283
|
+
const { closeMenu, caller: contextCaller } = useContext(MenuDropdownContext);
|
|
284
|
+
const caller = callerOverride ?? contextCaller;
|
|
285
|
+
const { onAction } = useMenuScoped('MenuContent', __menuScope);
|
|
286
|
+
const resolvedItems = useMenuItems(group, propsItems, 'MenuContent', __menuScope);
|
|
287
|
+
|
|
288
|
+
const handleActionClick = useCallback(
|
|
289
|
+
(action: MenuAction, event: MouseEvent) => {
|
|
290
|
+
if (action.properties?.disabled) {
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
event.stopPropagation();
|
|
294
|
+
closeMenu();
|
|
295
|
+
if (onAction) {
|
|
296
|
+
onAction(action, { parent: group, caller });
|
|
297
|
+
} else {
|
|
298
|
+
void executeMenuAction(action, { parent: group, caller });
|
|
299
|
+
}
|
|
300
|
+
},
|
|
301
|
+
[group, caller, onAction, closeMenu],
|
|
302
|
+
);
|
|
303
|
+
|
|
304
|
+
return (
|
|
305
|
+
<NaturalDropdownMenu.Portal>
|
|
306
|
+
<NaturalDropdownMenu.Content>
|
|
307
|
+
<NaturalDropdownMenu.Viewport>
|
|
308
|
+
{resolvedItems?.map((item) => (
|
|
309
|
+
<MenuContentItem key={item.id} item={item} onClick={handleActionClick} />
|
|
310
|
+
))}
|
|
311
|
+
</NaturalDropdownMenu.Viewport>
|
|
312
|
+
<NaturalDropdownMenu.Arrow />
|
|
313
|
+
</NaturalDropdownMenu.Content>
|
|
314
|
+
</NaturalDropdownMenu.Portal>
|
|
315
|
+
);
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
//
|
|
319
|
+
// Namespace.
|
|
320
|
+
//
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Primary namespace export for the menu system.
|
|
324
|
+
*
|
|
325
|
+
* - `Menu.Root` — context boundary (replaces `MenuProvider`); also provides a dropdown root.
|
|
326
|
+
* - `Menu.Trigger` / `Menu.VirtualTrigger` — dropdown trigger (use with `Menu.Content`).
|
|
327
|
+
* - `Menu.Content` — renders graph-backed dropdown items inside a portal.
|
|
328
|
+
* - `Menu.Toolbar` — flat toolbar component with graph-backed item rendering.
|
|
329
|
+
*/
|
|
330
|
+
const Menu = {
|
|
331
|
+
Root: MenuRoot,
|
|
332
|
+
Trigger: NaturalDropdownMenu.Trigger,
|
|
333
|
+
Content: MenuContent,
|
|
334
|
+
VirtualTrigger: NaturalDropdownMenu.VirtualTrigger,
|
|
335
|
+
Toolbar: ToolbarMenu,
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
export { Menu, menuContextDefaults, useMenu, useMenuItems, useMenuScoped };
|
|
339
|
+
|
|
340
|
+
export type { MenuContentProps, MenuRootProps, MenuScopedProps };
|
|
341
|
+
|
|
342
|
+
export type {
|
|
343
|
+
ToolbarMenuProps,
|
|
344
|
+
ToolbarMenuActionGroupProperties,
|
|
345
|
+
ToolbarMenuActionGroupProps,
|
|
346
|
+
ToolbarMenuActionProps,
|
|
347
|
+
ToolbarMenuDropdownMenuActionGroup,
|
|
348
|
+
ToolbarMenuToggleGroupActionGroup,
|
|
349
|
+
} from './ToolbarMenu';
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Atom } from '@effect-atom/atom-react';
|
|
6
|
+
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
7
|
+
import React, { useEffect, useMemo, useState } from 'react';
|
|
8
|
+
import { expect, userEvent, within } from 'storybook/test';
|
|
9
|
+
|
|
10
|
+
import { Button, IconButton } from '@dxos/react-ui';
|
|
11
|
+
import { withTheme } from '@dxos/react-ui/testing';
|
|
12
|
+
import { withRegistry } from '@dxos/storybook-utils';
|
|
13
|
+
|
|
14
|
+
import { Menu, useMenu } from '../components';
|
|
15
|
+
import { type ActionGraphProps, useMenuActions } from '../hooks';
|
|
16
|
+
import { createActions } from '../testing';
|
|
17
|
+
import { translations } from '../translations';
|
|
18
|
+
import { type MenuItem } from '../types';
|
|
19
|
+
import { createMenuAction } from '../util';
|
|
20
|
+
|
|
21
|
+
const STORY_NAME = 'StoryMenuItems';
|
|
22
|
+
|
|
23
|
+
const meta = {
|
|
24
|
+
title: 'ui/react-ui-menu/MenuItems',
|
|
25
|
+
component: Menu.Root,
|
|
26
|
+
decorators: [withTheme(), withRegistry],
|
|
27
|
+
parameters: {
|
|
28
|
+
translations,
|
|
29
|
+
},
|
|
30
|
+
} satisfies Meta<typeof Menu.Root>;
|
|
31
|
+
|
|
32
|
+
export default meta;
|
|
33
|
+
|
|
34
|
+
type Story = StoryObj<typeof meta>;
|
|
35
|
+
|
|
36
|
+
const createBaseActionsAtom = (count = 3) => {
|
|
37
|
+
const actions = createActions({ count });
|
|
38
|
+
return Atom.make<ActionGraphProps>({
|
|
39
|
+
nodes: actions,
|
|
40
|
+
edges: actions.map((action) => ({ source: 'root', target: action.id, relation: 'child' })),
|
|
41
|
+
}).pipe(Atom.keepAlive);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const openDropdown = async (canvasElement: HTMLElement) => {
|
|
45
|
+
const canvas = within(canvasElement);
|
|
46
|
+
const trigger = canvas.getByRole('button', { name: /options/i });
|
|
47
|
+
await userEvent.click(trigger);
|
|
48
|
+
const body = within(document.body);
|
|
49
|
+
return body;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const StaticItemsProvider = () => {
|
|
53
|
+
const menu = useMenu(STORY_NAME);
|
|
54
|
+
const items: MenuItem[] = useMemo(
|
|
55
|
+
() => [
|
|
56
|
+
createMenuAction('static-1', () => {}, {
|
|
57
|
+
label: 'Static Action 1',
|
|
58
|
+
icon: 'ph--star--regular',
|
|
59
|
+
}),
|
|
60
|
+
createMenuAction('static-2', () => {}, {
|
|
61
|
+
label: 'Static Action 2',
|
|
62
|
+
icon: 'ph--heart--regular',
|
|
63
|
+
}),
|
|
64
|
+
],
|
|
65
|
+
[],
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
useEffect(() => {
|
|
69
|
+
menu.addMenuItems({ id: 'static-items', mode: 'additive', items });
|
|
70
|
+
return () => menu.removeMenuItems('static-items');
|
|
71
|
+
}, [menu, items]);
|
|
72
|
+
|
|
73
|
+
return null;
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export const StaticItems: Story = {
|
|
77
|
+
render: () => {
|
|
78
|
+
const actionsAtom = useMemo(() => createBaseActionsAtom(2), []);
|
|
79
|
+
const menuActions = useMenuActions(actionsAtom);
|
|
80
|
+
|
|
81
|
+
return (
|
|
82
|
+
<Menu.Root {...menuActions}>
|
|
83
|
+
<StaticItemsProvider />
|
|
84
|
+
<Menu.Trigger asChild>
|
|
85
|
+
<IconButton icon='ph--list-checks--regular' label='Options' />
|
|
86
|
+
</Menu.Trigger>
|
|
87
|
+
<Menu.Content />
|
|
88
|
+
</Menu.Root>
|
|
89
|
+
);
|
|
90
|
+
},
|
|
91
|
+
play: async ({ canvasElement }) => {
|
|
92
|
+
const body = await openDropdown(canvasElement);
|
|
93
|
+
const items = await body.findAllByRole('menuitem');
|
|
94
|
+
const labels = items.map((el) => el.textContent);
|
|
95
|
+
await expect(labels).toContain('Static Action 1');
|
|
96
|
+
await expect(labels).toContain('Static Action 2');
|
|
97
|
+
},
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const ReactiveItemsProvider = ({ items }: { items: MenuItem[] }) => {
|
|
101
|
+
const menu = useMenu(STORY_NAME);
|
|
102
|
+
|
|
103
|
+
useEffect(() => {
|
|
104
|
+
menu.addMenuItems({ id: 'reactive-items', mode: 'additive', priority: 50, items });
|
|
105
|
+
return () => menu.removeMenuItems('reactive-items');
|
|
106
|
+
}, [menu, items]);
|
|
107
|
+
|
|
108
|
+
return null;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export const ReactiveItems: Story = {
|
|
112
|
+
render: () => {
|
|
113
|
+
const actionsAtom = useMemo(() => createBaseActionsAtom(2), []);
|
|
114
|
+
const [count, setCount] = useState(1);
|
|
115
|
+
const reactiveItems: MenuItem[] = useMemo(
|
|
116
|
+
() => [
|
|
117
|
+
createMenuAction('reactive-1', () => {}, {
|
|
118
|
+
label: `Reactive Action (${count})`,
|
|
119
|
+
icon: 'ph--lightning--regular',
|
|
120
|
+
}),
|
|
121
|
+
],
|
|
122
|
+
[count],
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
const menuActions = useMenuActions(actionsAtom);
|
|
126
|
+
|
|
127
|
+
return (
|
|
128
|
+
<div className='flex flex-col gap-4'>
|
|
129
|
+
<Menu.Root {...menuActions}>
|
|
130
|
+
<ReactiveItemsProvider items={reactiveItems} />
|
|
131
|
+
<Menu.Trigger asChild>
|
|
132
|
+
<IconButton icon='ph--list-checks--regular' label='Options' />
|
|
133
|
+
</Menu.Trigger>
|
|
134
|
+
<Menu.Content />
|
|
135
|
+
</Menu.Root>
|
|
136
|
+
<Button data-testid='update-button' onClick={() => setCount((prev) => prev + 1)}>
|
|
137
|
+
Update Reactive Item ({count})
|
|
138
|
+
</Button>
|
|
139
|
+
</div>
|
|
140
|
+
);
|
|
141
|
+
},
|
|
142
|
+
play: async ({ canvasElement }) => {
|
|
143
|
+
const canvas = within(canvasElement);
|
|
144
|
+
|
|
145
|
+
const body = await openDropdown(canvasElement);
|
|
146
|
+
const items = await body.findAllByRole('menuitem');
|
|
147
|
+
await expect(items.map((el) => el.textContent)).toContain('Reactive Action (1)');
|
|
148
|
+
|
|
149
|
+
await userEvent.keyboard('{Escape}');
|
|
150
|
+
const updateButton = canvas.getByTestId('update-button');
|
|
151
|
+
await userEvent.click(updateButton);
|
|
152
|
+
|
|
153
|
+
const body2 = await openDropdown(canvasElement);
|
|
154
|
+
const updatedItems = await body2.findAllByRole('menuitem');
|
|
155
|
+
await expect(updatedItems.map((el) => el.textContent)).toContain('Reactive Action (2)');
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const ReplacementItemsProvider = () => {
|
|
160
|
+
const menu = useMenu(STORY_NAME);
|
|
161
|
+
const items: MenuItem[] = useMemo(
|
|
162
|
+
() => [
|
|
163
|
+
createMenuAction('replacement-1', () => {}, {
|
|
164
|
+
label: 'Replacement Only',
|
|
165
|
+
icon: 'ph--swap--regular',
|
|
166
|
+
}),
|
|
167
|
+
],
|
|
168
|
+
[],
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
useEffect(() => {
|
|
172
|
+
menu.addMenuItems({ id: 'replacement-items', mode: 'replacement', items });
|
|
173
|
+
return () => menu.removeMenuItems('replacement-items');
|
|
174
|
+
}, [menu, items]);
|
|
175
|
+
|
|
176
|
+
return null;
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
export const ReplacementMode: Story = {
|
|
180
|
+
render: () => {
|
|
181
|
+
const actionsAtom = useMemo(() => createBaseActionsAtom(5), []);
|
|
182
|
+
const menuActions = useMenuActions(actionsAtom);
|
|
183
|
+
|
|
184
|
+
return (
|
|
185
|
+
<Menu.Root {...menuActions}>
|
|
186
|
+
<ReplacementItemsProvider />
|
|
187
|
+
<Menu.Trigger asChild>
|
|
188
|
+
<IconButton icon='ph--list-checks--regular' label='Options (replaced)' />
|
|
189
|
+
</Menu.Trigger>
|
|
190
|
+
<Menu.Content />
|
|
191
|
+
</Menu.Root>
|
|
192
|
+
);
|
|
193
|
+
},
|
|
194
|
+
play: async ({ canvasElement }) => {
|
|
195
|
+
const body = await openDropdown(canvasElement);
|
|
196
|
+
const items = await body.findAllByRole('menuitem');
|
|
197
|
+
await expect(items).toHaveLength(1);
|
|
198
|
+
await expect(items[0].textContent).toBe('Replacement Only');
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const LowPriorityItemsProvider = () => {
|
|
203
|
+
const menu = useMenu(STORY_NAME);
|
|
204
|
+
const items: MenuItem[] = useMemo(
|
|
205
|
+
() => [
|
|
206
|
+
createMenuAction('low-priority', () => {}, {
|
|
207
|
+
label: 'Low Priority (150)',
|
|
208
|
+
icon: 'ph--arrow-down--regular',
|
|
209
|
+
}),
|
|
210
|
+
],
|
|
211
|
+
[],
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
useEffect(() => {
|
|
215
|
+
menu.addMenuItems({ id: 'low-priority-items', mode: 'additive', items, priority: 150 });
|
|
216
|
+
return () => menu.removeMenuItems('low-priority-items');
|
|
217
|
+
}, [menu, items]);
|
|
218
|
+
|
|
219
|
+
return null;
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
const HighPriorityItemsProvider = () => {
|
|
223
|
+
const menu = useMenu(STORY_NAME);
|
|
224
|
+
const items: MenuItem[] = useMemo(
|
|
225
|
+
() => [
|
|
226
|
+
createMenuAction('high-priority', () => {}, {
|
|
227
|
+
label: 'High Priority (50)',
|
|
228
|
+
icon: 'ph--arrow-up--regular',
|
|
229
|
+
}),
|
|
230
|
+
],
|
|
231
|
+
[],
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
useEffect(() => {
|
|
235
|
+
menu.addMenuItems({ id: 'high-priority-items', mode: 'additive', items, priority: 50 });
|
|
236
|
+
return () => menu.removeMenuItems('high-priority-items');
|
|
237
|
+
}, [menu, items]);
|
|
238
|
+
|
|
239
|
+
return null;
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
export const PriorityOrdering: Story = {
|
|
243
|
+
render: () => {
|
|
244
|
+
const actionsAtom = useMemo(() => createBaseActionsAtom(0), []);
|
|
245
|
+
const menuActions = useMenuActions(actionsAtom);
|
|
246
|
+
|
|
247
|
+
return (
|
|
248
|
+
<Menu.Root {...menuActions}>
|
|
249
|
+
<LowPriorityItemsProvider />
|
|
250
|
+
<HighPriorityItemsProvider />
|
|
251
|
+
<Menu.Trigger asChild>
|
|
252
|
+
<IconButton icon='ph--list-checks--regular' label='Options (priority ordered)' />
|
|
253
|
+
</Menu.Trigger>
|
|
254
|
+
<Menu.Content />
|
|
255
|
+
</Menu.Root>
|
|
256
|
+
);
|
|
257
|
+
},
|
|
258
|
+
play: async ({ canvasElement }) => {
|
|
259
|
+
const body = await openDropdown(canvasElement);
|
|
260
|
+
const items = await body.findAllByRole('menuitem');
|
|
261
|
+
await expect(items).toHaveLength(2);
|
|
262
|
+
await expect(items[0].textContent).toBe('High Priority (50)');
|
|
263
|
+
await expect(items[1].textContent).toBe('Low Priority (150)');
|
|
264
|
+
},
|
|
265
|
+
};
|