@dxos/react-ui-menu 0.8.4-main.ead640a → 0.8.4-main.ef1bc66f44
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 +298 -355
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +298 -355
- 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 +1 -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.map +1 -1
- package/dist/types/src/components/MenuContext.d.ts +3 -1
- package/dist/types/src/components/MenuContext.d.ts.map +1 -1
- package/dist/types/src/components/ToolbarMenu.d.ts +3 -2
- 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/hooks/useMenuActions.d.ts +5 -5
- 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 +13 -11
- 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 +13 -24
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/src/util.d.ts +12 -5
- package/dist/types/src/util.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +31 -28
- package/src/builder.ts +1 -1
- package/src/components/ActionLabel.tsx +4 -4
- package/src/components/DropdownMenu.tsx +10 -3
- package/src/components/MenuContext.tsx +17 -3
- package/src/components/ToolbarMenu.stories.tsx +17 -9
- package/src/components/ToolbarMenu.tsx +38 -13
- package/src/hooks/useMenuActions.ts +20 -47
- package/src/index.ts +0 -2
- package/src/testing/generator.ts +63 -58
- package/src/translations.ts +1 -1
- package/src/types.ts +14 -26
- package/src/util.ts +32 -14
package/package.json
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/react-ui-menu",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.ef1bc66f44",
|
|
4
4
|
"description": "Components for facilitating reactive and consistent menus.",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
7
11
|
"license": "MIT",
|
|
8
12
|
"author": "DXOS.org",
|
|
9
|
-
"sideEffects":
|
|
13
|
+
"sideEffects": false,
|
|
10
14
|
"type": "module",
|
|
11
15
|
"exports": {
|
|
12
16
|
".": {
|
|
@@ -25,39 +29,38 @@
|
|
|
25
29
|
"src"
|
|
26
30
|
],
|
|
27
31
|
"dependencies": {
|
|
28
|
-
"@preact-signals/safe-react": "^0.9.0",
|
|
29
|
-
"@preact/signals-react": "^3.3.0",
|
|
30
32
|
"@radix-ui/react-context": "1.1.1",
|
|
31
33
|
"@radix-ui/react-use-controllable-state": "1.1.0",
|
|
32
|
-
"@dxos/app-graph": "0.8.4-main.
|
|
33
|
-
"@dxos/
|
|
34
|
-
"@dxos/
|
|
35
|
-
"@dxos/
|
|
36
|
-
"@dxos/
|
|
34
|
+
"@dxos/app-graph": "0.8.4-main.ef1bc66f44",
|
|
35
|
+
"@dxos/invariant": "0.8.4-main.ef1bc66f44",
|
|
36
|
+
"@dxos/keyboard": "0.8.4-main.ef1bc66f44",
|
|
37
|
+
"@dxos/ui-types": "0.8.4-main.ef1bc66f44",
|
|
38
|
+
"@dxos/react-ui-attention": "0.8.4-main.ef1bc66f44",
|
|
39
|
+
"@dxos/util": "0.8.4-main.ef1bc66f44",
|
|
40
|
+
"@dxos/effect": "0.8.4-main.ef1bc66f44"
|
|
37
41
|
},
|
|
38
42
|
"devDependencies": {
|
|
39
|
-
"@effect-
|
|
40
|
-
"@effect/platform": "0.
|
|
41
|
-
"@types/react": "~19.2.
|
|
42
|
-
"@types/react-dom": "~19.2.
|
|
43
|
-
"effect": "3.
|
|
44
|
-
"react": "~19.2.
|
|
45
|
-
"react-dom": "~19.2.
|
|
43
|
+
"@effect-atom/atom-react": "^0.5.0",
|
|
44
|
+
"@effect/platform": "0.94.4",
|
|
45
|
+
"@types/react": "~19.2.7",
|
|
46
|
+
"@types/react-dom": "~19.2.3",
|
|
47
|
+
"effect": "3.19.16",
|
|
48
|
+
"react": "~19.2.3",
|
|
49
|
+
"react-dom": "~19.2.3",
|
|
46
50
|
"vite": "7.1.9",
|
|
47
|
-
"@dxos/random": "0.8.4-main.
|
|
48
|
-
"@dxos/
|
|
49
|
-
"@dxos/
|
|
50
|
-
"@dxos/
|
|
51
|
-
"@dxos/storybook-utils": "0.8.4-main.ead640a"
|
|
51
|
+
"@dxos/random": "0.8.4-main.ef1bc66f44",
|
|
52
|
+
"@dxos/react-ui": "0.8.4-main.ef1bc66f44",
|
|
53
|
+
"@dxos/ui-theme": "0.8.4-main.ef1bc66f44",
|
|
54
|
+
"@dxos/storybook-utils": "0.8.4-main.ef1bc66f44"
|
|
52
55
|
},
|
|
53
56
|
"peerDependencies": {
|
|
54
|
-
"@effect-
|
|
55
|
-
"@effect/platform": "
|
|
56
|
-
"effect": "
|
|
57
|
-
"react": "
|
|
58
|
-
"react-dom": "
|
|
59
|
-
"@dxos/react-ui": "0.8.4-main.
|
|
60
|
-
"@dxos/
|
|
57
|
+
"@effect-atom/atom-react": "^0.5.0",
|
|
58
|
+
"@effect/platform": "0.94.4",
|
|
59
|
+
"effect": "3.19.16",
|
|
60
|
+
"react": "~19.2.3",
|
|
61
|
+
"react-dom": "~19.2.3",
|
|
62
|
+
"@dxos/react-ui": "0.8.4-main.ef1bc66f44",
|
|
63
|
+
"@dxos/ui-theme": "0.8.4-main.ef1bc66f44"
|
|
61
64
|
},
|
|
62
65
|
"publishConfig": {
|
|
63
66
|
"access": "public"
|
package/src/builder.ts
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { invariant } from '@dxos/invariant';
|
|
6
|
+
import type { MenuActionProperties, MenuItemGroupProperties } from '@dxos/ui-types';
|
|
6
7
|
|
|
7
8
|
import type { ActionGraphProps } from './hooks';
|
|
8
|
-
import type { MenuActionProperties, MenuItemGroupProperties } from './types';
|
|
9
9
|
import { createMenuAction, createMenuItemGroup } from './util';
|
|
10
10
|
|
|
11
11
|
export interface ActionGroupBuilder {
|
|
@@ -4,16 +4,16 @@
|
|
|
4
4
|
|
|
5
5
|
import React from 'react';
|
|
6
6
|
|
|
7
|
-
import { type
|
|
7
|
+
import { type Node } from '@dxos/app-graph';
|
|
8
8
|
import { keySymbols } from '@dxos/keyboard';
|
|
9
9
|
import { type TFunction, toLocalizedString, useTranslation } from '@dxos/react-ui';
|
|
10
|
-
import { descriptionText, mx } from '@dxos/
|
|
10
|
+
import { descriptionText, mx } from '@dxos/ui-theme';
|
|
11
|
+
import { type MenuActionProperties } from '@dxos/ui-types';
|
|
11
12
|
|
|
12
13
|
import { translationKey } from '../translations';
|
|
13
|
-
import { type MenuActionProperties } from '../types';
|
|
14
14
|
import { getShortcut } from '../util';
|
|
15
15
|
|
|
16
|
-
type Action =
|
|
16
|
+
type Action = Node.Action<MenuActionProperties> | Node.ActionGroup<Omit<MenuActionProperties, 'variant'>>;
|
|
17
17
|
|
|
18
18
|
export const ActionLabel = ({ action }: { action: Action }) => {
|
|
19
19
|
const { t } = useTranslation(translationKey);
|
|
@@ -8,6 +8,7 @@ import React, { type MouseEvent, useCallback } from 'react';
|
|
|
8
8
|
import { type DropdownMenuRootProps, Icon, DropdownMenu as NaturalDropdownMenu } from '@dxos/react-ui';
|
|
9
9
|
|
|
10
10
|
import { type MenuAction, type MenuItem, type MenuItemGroup } from '../types';
|
|
11
|
+
import { executeMenuAction } from '../util';
|
|
11
12
|
|
|
12
13
|
import { ActionLabel } from './ActionLabel';
|
|
13
14
|
import { type MenuScopedProps, useMenu, useMenuItems } from './MenuContext';
|
|
@@ -60,17 +61,23 @@ const DropdownMenuRoot = ({
|
|
|
60
61
|
onChange: onOpenChange,
|
|
61
62
|
});
|
|
62
63
|
|
|
64
|
+
const { onAction } = useMenu('DropdownMenuRoot', __menuScope);
|
|
65
|
+
|
|
63
66
|
const handleActionClick = useCallback(
|
|
64
67
|
(action: MenuAction, event: MouseEvent) => {
|
|
65
68
|
if (action.properties?.disabled) {
|
|
66
69
|
return;
|
|
67
70
|
}
|
|
68
71
|
event.stopPropagation();
|
|
69
|
-
// TODO(thure): Why does Dialog
|
|
72
|
+
// TODO(thure): Why does Dialog's modal-ness cause issues if we don't explicitly close the menu here?
|
|
70
73
|
setOptionsMenuOpen(false);
|
|
71
|
-
|
|
74
|
+
if (onAction) {
|
|
75
|
+
onAction(action, { parent: group, caller });
|
|
76
|
+
} else {
|
|
77
|
+
void executeMenuAction(action, { parent: group, caller });
|
|
78
|
+
}
|
|
72
79
|
},
|
|
73
|
-
[group, caller],
|
|
80
|
+
[group, caller, onAction],
|
|
74
81
|
);
|
|
75
82
|
|
|
76
83
|
const items = useMenuItems(group, propsItems);
|
|
@@ -9,7 +9,7 @@ import { type MenuContextValue, type MenuItem, type MenuItemGroup } from '../typ
|
|
|
9
9
|
|
|
10
10
|
export type MenuScopedProps<P> = P & { __menuScope?: Scope };
|
|
11
11
|
|
|
12
|
-
const MENU_NAME = '
|
|
12
|
+
const MENU_NAME = 'Menu';
|
|
13
13
|
|
|
14
14
|
const [createMenuContext, createMenuScope] = createContextScope(MENU_NAME, []);
|
|
15
15
|
|
|
@@ -18,19 +18,31 @@ const [MenuContextProvider, useMenu] = createMenuContext<MenuContextValue>(MENU_
|
|
|
18
18
|
export const menuContextDefaults: MenuContextValue = {
|
|
19
19
|
iconSize: 5,
|
|
20
20
|
useGroupItems: () => null,
|
|
21
|
+
onAction: undefined,
|
|
21
22
|
};
|
|
22
23
|
|
|
23
24
|
const useMenuScope = createMenuScope();
|
|
24
25
|
|
|
26
|
+
type MenuProviderProps = PropsWithChildren<Partial<MenuContextValue>>;
|
|
27
|
+
|
|
25
28
|
const MenuProvider = ({
|
|
26
29
|
children,
|
|
27
30
|
useGroupItems = menuContextDefaults.useGroupItems,
|
|
28
31
|
iconSize = menuContextDefaults.iconSize,
|
|
29
32
|
attendableId,
|
|
30
|
-
|
|
33
|
+
alwaysActive,
|
|
34
|
+
onAction,
|
|
35
|
+
}: MenuProviderProps) => {
|
|
31
36
|
const { scope } = useMenuScope(undefined);
|
|
32
37
|
return (
|
|
33
|
-
<MenuContextProvider
|
|
38
|
+
<MenuContextProvider
|
|
39
|
+
useGroupItems={useGroupItems}
|
|
40
|
+
iconSize={iconSize}
|
|
41
|
+
attendableId={attendableId}
|
|
42
|
+
alwaysActive={alwaysActive}
|
|
43
|
+
onAction={onAction}
|
|
44
|
+
scope={scope}
|
|
45
|
+
>
|
|
34
46
|
{children}
|
|
35
47
|
</MenuContextProvider>
|
|
36
48
|
);
|
|
@@ -48,3 +60,5 @@ export const useMenuItems = (
|
|
|
48
60
|
};
|
|
49
61
|
|
|
50
62
|
export { useMenu, createMenuScope, MenuProvider };
|
|
63
|
+
|
|
64
|
+
export type { MenuProviderProps };
|
|
@@ -2,25 +2,26 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import { Atom } from '@effect-atom/atom-react';
|
|
5
6
|
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
6
|
-
import React, {
|
|
7
|
+
import React, { useMemo } from 'react';
|
|
7
8
|
|
|
8
9
|
import { faker } from '@dxos/random';
|
|
9
10
|
import { IconButton } from '@dxos/react-ui';
|
|
10
11
|
import { withTheme } from '@dxos/react-ui/testing';
|
|
12
|
+
import { withRegistry } from '@dxos/storybook-utils';
|
|
11
13
|
|
|
12
14
|
import { MenuProvider, DropdownMenu as NaturalDropdownMenu, ToolbarMenu } from '../components';
|
|
13
|
-
import { useMenuActions } from '../hooks';
|
|
15
|
+
import { type ActionGraphProps, useMenuActions } from '../hooks';
|
|
14
16
|
import { createActions, createNestedActions, createNestedActionsResolver, useMutateActions } from '../testing';
|
|
15
17
|
import { translations } from '../translations';
|
|
16
|
-
import { type MenuAction } from '../types';
|
|
17
18
|
|
|
18
19
|
faker.seed(1234);
|
|
19
20
|
|
|
20
21
|
const meta = {
|
|
21
22
|
title: 'ui/react-ui-menu/ToolbarMenu',
|
|
22
23
|
component: ToolbarMenu,
|
|
23
|
-
decorators: [withTheme],
|
|
24
|
+
decorators: [withTheme(), withRegistry],
|
|
24
25
|
parameters: {
|
|
25
26
|
translations,
|
|
26
27
|
},
|
|
@@ -32,15 +33,21 @@ type Story = StoryObj<typeof meta>;
|
|
|
32
33
|
|
|
33
34
|
export const DropdownMenu: Story = {
|
|
34
35
|
render: () => {
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
const actionsAtom = useMemo(() => {
|
|
37
|
+
const actions = createActions();
|
|
38
|
+
return Atom.make<ActionGraphProps>({
|
|
39
|
+
nodes: actions,
|
|
40
|
+
edges: actions.map((action) => ({ source: 'root', target: action.id })),
|
|
41
|
+
}).pipe(Atom.keepAlive);
|
|
42
|
+
}, []);
|
|
43
|
+
useMutateActions(actionsAtom);
|
|
44
|
+
const menu = useMenuActions(actionsAtom);
|
|
38
45
|
|
|
39
46
|
return (
|
|
40
|
-
<MenuProvider
|
|
47
|
+
<MenuProvider {...menu}>
|
|
41
48
|
<NaturalDropdownMenu.Root>
|
|
42
49
|
<NaturalDropdownMenu.Trigger asChild>
|
|
43
|
-
<IconButton icon='ph--list-checks--regular'
|
|
50
|
+
<IconButton icon='ph--list-checks--regular' label='Options' />
|
|
44
51
|
</NaturalDropdownMenu.Trigger>
|
|
45
52
|
</NaturalDropdownMenu.Root>
|
|
46
53
|
</MenuProvider>
|
|
@@ -62,6 +69,7 @@ export const Toolbar: Story = {
|
|
|
62
69
|
|
|
63
70
|
export const UseMenuActionsToolbar: Story = {
|
|
64
71
|
render: () => {
|
|
72
|
+
useMutateActions(createNestedActions);
|
|
65
73
|
const menu = useMenuActions(createNestedActions);
|
|
66
74
|
|
|
67
75
|
return (
|
|
@@ -6,12 +6,12 @@ import React, { Fragment, useCallback } from 'react';
|
|
|
6
6
|
|
|
7
7
|
import { Icon, Toolbar as NaturalToolbar, type ToolbarRootProps, useTranslation } from '@dxos/react-ui';
|
|
8
8
|
import { useAttention } from '@dxos/react-ui-attention';
|
|
9
|
-
import { mx, textBlockWidth, toolbarLayout } from '@dxos/
|
|
9
|
+
import { mx, textBlockWidth, toolbarLayout } from '@dxos/ui-theme';
|
|
10
|
+
import { type MenuActionProperties } from '@dxos/ui-types';
|
|
10
11
|
|
|
11
12
|
import { translationKey } from '../translations';
|
|
12
13
|
import {
|
|
13
14
|
type MenuAction,
|
|
14
|
-
type MenuActionProperties,
|
|
15
15
|
type MenuItem,
|
|
16
16
|
type MenuItemGroup,
|
|
17
17
|
type MenuMultipleSelectActionGroup,
|
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
isMenuGroup,
|
|
20
20
|
isSeparator,
|
|
21
21
|
} from '../types';
|
|
22
|
+
import { executeMenuAction } from '../util';
|
|
22
23
|
|
|
23
24
|
import { ActionLabel, actionLabel } from './ActionLabel';
|
|
24
25
|
import { DropdownMenu } from './DropdownMenu';
|
|
@@ -53,15 +54,23 @@ export type ToolbarMenuActionProps = {
|
|
|
53
54
|
};
|
|
54
55
|
|
|
55
56
|
const ActionToolbarItem = ({ action, __menuScope }: MenuScopedProps<{ action: MenuAction }>) => {
|
|
56
|
-
const { iconSize } = useMenu('ActionToolbarItem', __menuScope);
|
|
57
|
+
const { iconSize, onAction } = useMenu('ActionToolbarItem', __menuScope);
|
|
57
58
|
const { t } = useTranslation(translationKey);
|
|
58
|
-
|
|
59
|
+
|
|
59
60
|
const { icon, iconOnly = true, disabled, testId, hidden, classNames } = action.properties;
|
|
60
61
|
const Root = icon ? NaturalToolbar.IconButton : NaturalToolbar.Button;
|
|
61
62
|
const rootProps = icon
|
|
62
63
|
? { icon, size: iconSize, iconOnly, label: actionLabel(action, t) }
|
|
63
64
|
: { children: <ActionLabel action={action} /> };
|
|
64
65
|
|
|
66
|
+
const handleClick = useCallback(() => {
|
|
67
|
+
if (onAction) {
|
|
68
|
+
onAction(action, {});
|
|
69
|
+
} else {
|
|
70
|
+
void executeMenuAction(action);
|
|
71
|
+
}
|
|
72
|
+
}, [action, onAction]);
|
|
73
|
+
|
|
65
74
|
return hidden ? null : (
|
|
66
75
|
<Root
|
|
67
76
|
key={action.id}
|
|
@@ -93,7 +102,13 @@ const DropdownMenuToolbarItem = ({
|
|
|
93
102
|
const Root = icon ? NaturalToolbar.IconButton : NaturalToolbar.Button;
|
|
94
103
|
const labelAction = (group.properties as any).applyActive && activeItem ? (activeItem as MenuAction) : group;
|
|
95
104
|
const rootProps = icon
|
|
96
|
-
? {
|
|
105
|
+
? {
|
|
106
|
+
icon,
|
|
107
|
+
size: iconSize,
|
|
108
|
+
iconOnly,
|
|
109
|
+
label: actionLabel(labelAction, t),
|
|
110
|
+
caretDown: true,
|
|
111
|
+
}
|
|
97
112
|
: {
|
|
98
113
|
children: (
|
|
99
114
|
<>
|
|
@@ -113,10 +128,16 @@ const DropdownMenuToolbarItem = ({
|
|
|
113
128
|
};
|
|
114
129
|
|
|
115
130
|
const ToggleGroupItem = ({ group, action, __menuScope }: MenuScopedProps<ToolbarMenuActionProps>) => {
|
|
116
|
-
const { iconSize } = useMenu('ToggleGroupItem', __menuScope);
|
|
131
|
+
const { iconSize, onAction } = useMenu('ToggleGroupItem', __menuScope);
|
|
117
132
|
const { t } = useTranslation(translationKey);
|
|
118
133
|
const { icon, iconOnly = true, disabled, testId, hidden, classNames } = action.properties;
|
|
119
|
-
const handleClick = useCallback(() =>
|
|
134
|
+
const handleClick = useCallback(() => {
|
|
135
|
+
if (onAction) {
|
|
136
|
+
onAction(action, { parent: group });
|
|
137
|
+
} else {
|
|
138
|
+
void executeMenuAction(action, { parent: group });
|
|
139
|
+
}
|
|
140
|
+
}, [action, group, onAction]);
|
|
120
141
|
const Root = icon ? NaturalToolbar.ToggleGroupIconItem : NaturalToolbar.ToggleGroupItem;
|
|
121
142
|
const rootProps = icon
|
|
122
143
|
? { icon, size: iconSize, iconOnly, label: actionLabel(action, t) }
|
|
@@ -158,23 +179,27 @@ const ToggleGroupToolbarItem = ({
|
|
|
158
179
|
);
|
|
159
180
|
};
|
|
160
181
|
|
|
182
|
+
// TODO(burdon): Reconcile with react-ui/Toolbar (incl. textBlockWidth)
|
|
161
183
|
export const ToolbarMenu = ({
|
|
162
184
|
__menuScope,
|
|
163
185
|
classNames,
|
|
164
|
-
textBlockWidth:
|
|
186
|
+
textBlockWidth: textBlockWidthProp,
|
|
165
187
|
...props
|
|
166
188
|
}: MenuScopedProps<ToolbarMenuProps>) => {
|
|
167
189
|
const items = useMenuItems(undefined, undefined, 'ToolbarMenu', __menuScope);
|
|
168
|
-
const { attendableId } = useMenu('ToolbarMenu', __menuScope);
|
|
190
|
+
const { attendableId, alwaysActive } = useMenu('ToolbarMenu', __menuScope);
|
|
169
191
|
const { hasAttention } = useAttention(attendableId);
|
|
170
|
-
const InnerRoot =
|
|
171
|
-
const innerRootProps =
|
|
192
|
+
const InnerRoot = textBlockWidthProp ? 'div' : Fragment;
|
|
193
|
+
const innerRootProps = textBlockWidthProp
|
|
194
|
+
? { role: 'none', className: mx(textBlockWidth, toolbarLayout, 'bs-full') }
|
|
195
|
+
: {};
|
|
172
196
|
|
|
173
197
|
return (
|
|
174
198
|
<NaturalToolbar.Root
|
|
175
199
|
{...props}
|
|
176
|
-
|
|
177
|
-
|
|
200
|
+
classNames={[attendableId, classNames]}
|
|
201
|
+
disabled={!alwaysActive && !hasAttention}
|
|
202
|
+
layoutManaged={textBlockWidthProp}
|
|
178
203
|
>
|
|
179
204
|
<InnerRoot {...innerRootProps}>
|
|
180
205
|
{items?.map((item: MenuItem, i: number) => (
|
|
@@ -2,69 +2,42 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import { useCallback, useContext,
|
|
5
|
+
import { type Atom, RegistryContext, useAtomValue } from '@effect-atom/atom-react';
|
|
6
|
+
import { useCallback, useContext, useRef } from 'react';
|
|
7
7
|
|
|
8
|
-
import {
|
|
8
|
+
import { Graph, Node } from '@dxos/app-graph';
|
|
9
9
|
|
|
10
10
|
import { type MenuItem, type MenuItemGroup } from '../types';
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
Object.entries(
|
|
15
|
-
edges.reduce((acc: Record<string, { inbound: string[]; outbound: string[] }>, { source, target }) => {
|
|
16
|
-
if (!acc[source]) {
|
|
17
|
-
acc[source] = { inbound: [], outbound: [] };
|
|
18
|
-
}
|
|
19
|
-
if (!acc[target]) {
|
|
20
|
-
acc[target] = { inbound: [], outbound: [] };
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const sourceEdges = acc[source];
|
|
24
|
-
if (!sourceEdges.outbound.includes(target)) {
|
|
25
|
-
sourceEdges.outbound.push(target);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const targetEdges = acc[target];
|
|
29
|
-
if (!targetEdges.inbound.includes(source)) {
|
|
30
|
-
targetEdges.inbound.push(source);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return acc;
|
|
34
|
-
}, {}),
|
|
35
|
-
),
|
|
36
|
-
);
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export type ActionGraphNodes = NodeArg<any>[];
|
|
40
|
-
export type ActionGraphEdges = Edge[];
|
|
12
|
+
export type ActionGraphNodes = Node.NodeArg<any>[];
|
|
13
|
+
export type ActionGraphEdges = Graph.Edge[];
|
|
41
14
|
export type ActionGraphProps = {
|
|
42
15
|
nodes: ActionGraphNodes;
|
|
43
16
|
edges: ActionGraphEdges;
|
|
44
17
|
};
|
|
45
18
|
|
|
46
|
-
export type MenuActions = {
|
|
19
|
+
export type MenuActions = {
|
|
20
|
+
useGroupItems: (sourceNode?: MenuItemGroup) => MenuItem[];
|
|
21
|
+
};
|
|
47
22
|
|
|
48
|
-
export const useMenuActions = (props:
|
|
23
|
+
export const useMenuActions = (props: Atom.Atom<ActionGraphProps>): MenuActions => {
|
|
49
24
|
const registry = useContext(RegistryContext);
|
|
50
|
-
const menuGraphProps =
|
|
25
|
+
const menuGraphProps = useAtomValue(props);
|
|
51
26
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
27
|
+
// Create a new graph whenever props change to preserve correct order.
|
|
28
|
+
// (Graph.addEdges appends rather than replaces, which breaks ordering on updates.)
|
|
29
|
+
const graphRef = useRef<{ graph: Graph.WritableGraph; props: ActionGraphProps } | null>(null);
|
|
30
|
+
if (!graphRef.current || graphRef.current.props !== menuGraphProps) {
|
|
31
|
+
const newGraph = Graph.make({ registry });
|
|
32
|
+
newGraph.pipe(Graph.addNodes(menuGraphProps.nodes as Node.NodeArg<any>[]), Graph.addEdges(menuGraphProps.edges));
|
|
33
|
+
graphRef.current = { graph: newGraph, props: menuGraphProps };
|
|
34
|
+
}
|
|
59
35
|
|
|
60
|
-
|
|
61
|
-
graph.addNodes(menuGraphProps.nodes);
|
|
62
|
-
graph.addEdges(menuGraphProps.edges);
|
|
63
|
-
}, [menuGraphProps]);
|
|
36
|
+
const graph = graphRef.current.graph;
|
|
64
37
|
|
|
65
38
|
const useGroupItems = useCallback(
|
|
66
39
|
(sourceNode?: MenuItemGroup) => {
|
|
67
|
-
const items =
|
|
40
|
+
const items = useAtomValue(graph.connections(sourceNode?.id || Node.RootId)) as MenuItem[];
|
|
68
41
|
return items;
|
|
69
42
|
},
|
|
70
43
|
[graph],
|
package/src/index.ts
CHANGED
package/src/testing/generator.ts
CHANGED
|
@@ -2,27 +2,17 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import { useEffect } from 'react';
|
|
5
|
+
import { Atom, RegistryContext } from '@effect-atom/atom-react';
|
|
6
|
+
import { useContext, useEffect } from 'react';
|
|
7
7
|
|
|
8
|
-
import {
|
|
9
|
-
ACTION_GROUP_TYPE,
|
|
10
|
-
ACTION_TYPE,
|
|
11
|
-
type Action,
|
|
12
|
-
Graph,
|
|
13
|
-
type NodeArg,
|
|
14
|
-
ROOT_ID,
|
|
15
|
-
actionGroupSymbol,
|
|
16
|
-
} from '@dxos/app-graph';
|
|
17
|
-
import { live } from '@dxos/live-object';
|
|
8
|
+
import { Graph, Node } from '@dxos/app-graph';
|
|
18
9
|
import { faker } from '@dxos/random';
|
|
19
|
-
import { type DeepWriteable } from '@dxos/util';
|
|
20
10
|
|
|
21
11
|
import { type ActionGraphProps } from '../hooks/useMenuActions';
|
|
22
|
-
import { type
|
|
12
|
+
import { type MenuItem, type MenuItemGroup } from '../types';
|
|
23
13
|
|
|
24
|
-
export type
|
|
25
|
-
type?: typeof
|
|
14
|
+
export type CreateActionsProps = Partial<{
|
|
15
|
+
type?: typeof Node.ActionType | typeof Node.ActionGroupType;
|
|
26
16
|
callback: () => void;
|
|
27
17
|
count: number;
|
|
28
18
|
}>;
|
|
@@ -46,29 +36,27 @@ const icons = {
|
|
|
46
36
|
],
|
|
47
37
|
};
|
|
48
38
|
|
|
49
|
-
export const createActions = (params?:
|
|
50
|
-
|
|
51
|
-
const { callback = () => console.log('invoke'), count = 12, type = ACTION_TYPE } = params ?? {};
|
|
39
|
+
export const createActions = (params?: CreateActionsProps) => {
|
|
40
|
+
const { callback = () => console.log('invoke'), count = 12, type = Node.ActionType } = params ?? {};
|
|
52
41
|
return faker.helpers.multiple(
|
|
53
|
-
() =>
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}),
|
|
42
|
+
() => ({
|
|
43
|
+
id: faker.string.uuid(),
|
|
44
|
+
type,
|
|
45
|
+
data: type === Node.ActionGroupType ? Node.actionGroupSymbol : callback,
|
|
46
|
+
properties: {
|
|
47
|
+
label: faker.lorem.words(2),
|
|
48
|
+
icon: faker.helpers.arrayElement(icons[faker.helpers.arrayElement(Object.keys(icons)) as keyof typeof icons]),
|
|
49
|
+
disabled: faker.helpers.arrayElement([true, false]),
|
|
50
|
+
...(type === Node.ActionGroupType && { variant: 'dropdownMenu' }),
|
|
51
|
+
},
|
|
52
|
+
}),
|
|
65
53
|
{ count },
|
|
66
54
|
);
|
|
67
55
|
};
|
|
68
56
|
|
|
69
|
-
|
|
57
|
+
const buildNestedActions = (): ActionGraphProps => {
|
|
70
58
|
const result: ActionGraphProps = { edges: [], nodes: [] };
|
|
71
|
-
const actionGroups = createActions({ type:
|
|
59
|
+
const actionGroups = createActions({ type: Node.ActionGroupType });
|
|
72
60
|
actionGroups.forEach((group) => {
|
|
73
61
|
const actions = createActions();
|
|
74
62
|
result.nodes.push(group, ...actions);
|
|
@@ -78,39 +66,56 @@ export const createNestedActions = Rx.make(() => {
|
|
|
78
66
|
);
|
|
79
67
|
});
|
|
80
68
|
return result;
|
|
81
|
-
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export const createNestedActions = Atom.make(buildNestedActions()).pipe(Atom.keepAlive);
|
|
82
72
|
|
|
83
|
-
export const createNestedActionsResolver = (groupParams?:
|
|
84
|
-
const graph =
|
|
85
|
-
const actionGroups = createActions({ type:
|
|
73
|
+
export const createNestedActionsResolver = (groupParams?: CreateActionsProps, params?: CreateActionsProps) => {
|
|
74
|
+
const graph = Graph.make();
|
|
75
|
+
const actionGroups = createActions({ type: Node.ActionGroupType, ...groupParams });
|
|
86
76
|
actionGroups.forEach((group) => {
|
|
87
77
|
const actions = createActions(params);
|
|
88
|
-
graph.
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
78
|
+
graph.pipe(
|
|
79
|
+
Graph.addNodes([group as Node.NodeArg<any>, ...(actions as Node.NodeArg<any>[])]),
|
|
80
|
+
Graph.addEdges([
|
|
81
|
+
{ source: 'root', target: group.id },
|
|
82
|
+
...actions.map((action) => ({ source: group.id, target: action.id })),
|
|
83
|
+
]),
|
|
84
|
+
Graph.expand(group.id),
|
|
85
|
+
);
|
|
94
86
|
});
|
|
95
87
|
const resolveGroupItems = (groupNode?: MenuItemGroup) =>
|
|
96
|
-
(
|
|
88
|
+
(Graph.getActions(graph, groupNode?.id ?? Node.RootId) || null) as MenuItem[] | null;
|
|
97
89
|
return { resolveGroupItems };
|
|
98
90
|
};
|
|
99
91
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
action.properties.icon.endsWith('regular') ? icons.fill : icons.regular,
|
|
106
|
-
);
|
|
107
|
-
action.properties.disabled = !action.properties.disabled;
|
|
108
|
-
cursor = (cursor + actions.length + 1) % actions.length;
|
|
109
|
-
}, 1_000);
|
|
110
|
-
};
|
|
92
|
+
/**
|
|
93
|
+
* Hook to mutate actions in an atom on an interval for testing reactivity.
|
|
94
|
+
*/
|
|
95
|
+
export const useMutateActions = (actionsAtom: Atom.Writable<ActionGraphProps>) => {
|
|
96
|
+
const registry = useContext(RegistryContext);
|
|
111
97
|
|
|
112
|
-
export const useMutateActions = (actions: MenuAction[]) =>
|
|
113
98
|
useEffect(() => {
|
|
114
|
-
|
|
99
|
+
let cursor = 0;
|
|
100
|
+
const interval = setInterval(() => {
|
|
101
|
+
const current = registry.get(actionsAtom);
|
|
102
|
+
const nodes = current.nodes.map((node, index) => {
|
|
103
|
+
if (index !== cursor) {
|
|
104
|
+
return node;
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
...node,
|
|
108
|
+
properties: {
|
|
109
|
+
...node.properties,
|
|
110
|
+
icon: faker.helpers.arrayElement(node.properties?.icon?.endsWith('regular') ? icons.fill : icons.regular),
|
|
111
|
+
disabled: !node.properties?.disabled,
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
});
|
|
115
|
+
registry.set(actionsAtom, { ...current, nodes });
|
|
116
|
+
cursor = (cursor + 1) % current.nodes.length;
|
|
117
|
+
}, 1_000);
|
|
118
|
+
|
|
115
119
|
return () => clearInterval(interval);
|
|
116
|
-
}, []);
|
|
120
|
+
}, [actionsAtom, registry]);
|
|
121
|
+
};
|