@dxos/react-ui-menu 0.8.4-main.f5c0578 → 0.8.4-main.fcfe5033a5
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 +556 -391
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +556 -391
- 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 +28 -3
- package/dist/types/src/builder.d.ts.map +1 -1
- package/dist/types/src/builder.test.d.ts +2 -0
- package/dist/types/src/builder.test.d.ts.map +1 -0
- 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 +4 -2
- package/dist/types/src/components/DropdownMenu.d.ts.map +1 -1
- package/dist/types/src/components/Menu.d.ts +50 -0
- package/dist/types/src/components/Menu.d.ts.map +1 -0
- package/dist/types/src/components/Menu.stories.d.ts +21 -0
- package/dist/types/src/components/Menu.stories.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/ToolbarMenu.d.ts +7 -6
- package/dist/types/src/components/ToolbarMenu.d.ts.map +1 -1
- package/dist/types/src/components/ToolbarMenu.stories.d.ts +22 -0
- package/dist/types/src/components/ToolbarMenu.stories.d.ts.map +1 -0
- 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 +1 -2
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/testing/generator.d.ts +19 -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 +37 -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.test.ts +224 -0
- package/src/builder.ts +76 -26
- package/src/components/ActionLabel.tsx +5 -5
- package/src/components/DropdownMenu.tsx +12 -6
- package/src/components/Menu.stories.tsx +265 -0
- package/src/components/Menu.test.tsx +447 -0
- package/src/components/Menu.tsx +348 -0
- package/src/components/ToolbarMenu.stories.tsx +82 -0
- package/src/components/ToolbarMenu.tsx +153 -95
- package/src/components/index.ts +1 -3
- package/src/hooks/useMenuActions.ts +30 -53
- package/src/index.ts +1 -3
- package/src/testing/generator.ts +74 -64
- package/src/translations.ts +1 -1
- package/src/types.ts +39 -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/dist/types/src/components/Menus.stories.d.ts +0 -25
- package/dist/types/src/components/Menus.stories.d.ts.map +0 -1
- package/src/components/MenuContext.tsx +0 -50
- package/src/components/Menus.stories.tsx +0 -69
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.fcfe5033a5",
|
|
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,41 @@
|
|
|
25
29
|
"src"
|
|
26
30
|
],
|
|
27
31
|
"dependencies": {
|
|
28
|
-
"@preact-signals/safe-react": "^0.9.0",
|
|
29
|
-
"@preact/signals-react": "^3.2.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/react-ui-attention": "0.8.4-main.
|
|
34
|
+
"@dxos/app-graph": "0.8.4-main.fcfe5033a5",
|
|
35
|
+
"@dxos/effect": "0.8.4-main.fcfe5033a5",
|
|
36
|
+
"@dxos/invariant": "0.8.4-main.fcfe5033a5",
|
|
37
|
+
"@dxos/keyboard": "0.8.4-main.fcfe5033a5",
|
|
38
|
+
"@dxos/react-ui-attention": "0.8.4-main.fcfe5033a5",
|
|
39
|
+
"@dxos/ui-types": "0.8.4-main.fcfe5033a5",
|
|
40
|
+
"@dxos/util": "0.8.4-main.fcfe5033a5",
|
|
41
|
+
"@dxos/log": "0.8.4-main.fcfe5033a5"
|
|
37
42
|
},
|
|
38
43
|
"devDependencies": {
|
|
39
|
-
"@effect-
|
|
40
|
-
"@effect/platform": "0.
|
|
41
|
-
"@
|
|
42
|
-
"@
|
|
43
|
-
"
|
|
44
|
-
"react": "~
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"@dxos/
|
|
50
|
-
"@dxos/react-ui
|
|
51
|
-
"@dxos/storybook-utils": "0.8.4-main.
|
|
44
|
+
"@effect-atom/atom-react": "^0.5.0",
|
|
45
|
+
"@effect/platform": "0.94.4",
|
|
46
|
+
"@testing-library/jest-dom": "^6.9.1",
|
|
47
|
+
"@testing-library/react": "^16.3.0",
|
|
48
|
+
"@types/react": "~19.2.7",
|
|
49
|
+
"@types/react-dom": "~19.2.3",
|
|
50
|
+
"effect": "3.20.0",
|
|
51
|
+
"react": "~19.2.3",
|
|
52
|
+
"react-dom": "~19.2.3",
|
|
53
|
+
"vite": "^7.1.11",
|
|
54
|
+
"@dxos/random": "0.8.4-main.fcfe5033a5",
|
|
55
|
+
"@dxos/react-ui": "0.8.4-main.fcfe5033a5",
|
|
56
|
+
"@dxos/storybook-utils": "0.8.4-main.fcfe5033a5",
|
|
57
|
+
"@dxos/ui-theme": "0.8.4-main.fcfe5033a5"
|
|
52
58
|
},
|
|
53
59
|
"peerDependencies": {
|
|
54
|
-
"@effect-
|
|
55
|
-
"@effect/platform": "
|
|
56
|
-
"effect": "
|
|
57
|
-
"react": "~
|
|
58
|
-
"react-dom": "~
|
|
59
|
-
"@dxos/react-ui": "0.8.4-main.
|
|
60
|
-
"@dxos/
|
|
60
|
+
"@effect-atom/atom-react": "^0.5.0",
|
|
61
|
+
"@effect/platform": "0.94.4",
|
|
62
|
+
"effect": "3.20.0",
|
|
63
|
+
"react": "~19.2.3",
|
|
64
|
+
"react-dom": "~19.2.3",
|
|
65
|
+
"@dxos/react-ui": "0.8.4-main.fcfe5033a5",
|
|
66
|
+
"@dxos/ui-theme": "0.8.4-main.fcfe5033a5"
|
|
61
67
|
},
|
|
62
68
|
"publishConfig": {
|
|
63
69
|
"access": "public"
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { describe, test } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { Node } from '@dxos/app-graph';
|
|
8
|
+
|
|
9
|
+
import { MenuBuilder } from './builder';
|
|
10
|
+
import { MenuSeparatorType } from './types';
|
|
11
|
+
|
|
12
|
+
describe('MenuBuilder', () => {
|
|
13
|
+
test('builds a flat menu with actions', ({ expect }) => {
|
|
14
|
+
const graph = MenuBuilder.make()
|
|
15
|
+
.root({ label: 'toolbar' })
|
|
16
|
+
.action('bold', { label: 'Bold', icon: 'ph--text-b--regular' }, () => {})
|
|
17
|
+
.action('italic', { label: 'Italic', icon: 'ph--text-italic--regular' }, () => {})
|
|
18
|
+
.build();
|
|
19
|
+
|
|
20
|
+
expect(graph.nodes).toHaveLength(3);
|
|
21
|
+
expect(graph.edges).toHaveLength(2);
|
|
22
|
+
expect(graph.edges).toEqual([
|
|
23
|
+
{ source: 'root', target: 'bold', relation: 'child' },
|
|
24
|
+
{ source: 'root', target: 'italic', relation: 'child' },
|
|
25
|
+
]);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('builds nested groups', ({ expect }) => {
|
|
29
|
+
const graph = MenuBuilder.make()
|
|
30
|
+
.root({ label: 'toolbar' })
|
|
31
|
+
.group('formatting', { label: 'Formatting' }, (group) => {
|
|
32
|
+
group
|
|
33
|
+
.action('bold', { label: 'Bold', icon: 'ph--text-b--regular' }, () => {})
|
|
34
|
+
.action('italic', { label: 'Italic', icon: 'ph--text-italic--regular' }, () => {});
|
|
35
|
+
})
|
|
36
|
+
.build();
|
|
37
|
+
|
|
38
|
+
expect(graph.nodes).toHaveLength(4);
|
|
39
|
+
expect(graph.edges).toEqual([
|
|
40
|
+
{ source: 'root', target: 'formatting', relation: 'child' },
|
|
41
|
+
{ source: 'formatting', target: 'bold', relation: 'child' },
|
|
42
|
+
{ source: 'formatting', target: 'italic', relation: 'child' },
|
|
43
|
+
]);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test('builds deeply nested hierarchy', ({ expect }) => {
|
|
47
|
+
const graph = MenuBuilder.make()
|
|
48
|
+
.root({ label: 'toolbar' })
|
|
49
|
+
.group('text', { label: 'Text' }, (text) => {
|
|
50
|
+
text
|
|
51
|
+
.group('headings', { label: 'Headings' }, (headings) => {
|
|
52
|
+
headings
|
|
53
|
+
.action('h1', { label: 'Heading 1', icon: 'ph--text-h-one--regular' }, () => {})
|
|
54
|
+
.action('h2', { label: 'Heading 2', icon: 'ph--text-h-two--regular' }, () => {});
|
|
55
|
+
})
|
|
56
|
+
.group('formatting', { label: 'Formatting' }, (formatting) => {
|
|
57
|
+
formatting.action('bold', { label: 'Bold', icon: 'ph--text-b--regular' }, () => {});
|
|
58
|
+
});
|
|
59
|
+
})
|
|
60
|
+
.build();
|
|
61
|
+
|
|
62
|
+
// root + text + headings + h1 + h2 + formatting + bold = 7 nodes.
|
|
63
|
+
expect(graph.nodes).toHaveLength(7);
|
|
64
|
+
expect(graph.edges).toEqual([
|
|
65
|
+
{ source: 'root', target: 'text', relation: 'child' },
|
|
66
|
+
{ source: 'text', target: 'headings', relation: 'child' },
|
|
67
|
+
{ source: 'headings', target: 'h1', relation: 'child' },
|
|
68
|
+
{ source: 'headings', target: 'h2', relation: 'child' },
|
|
69
|
+
{ source: 'text', target: 'formatting', relation: 'child' },
|
|
70
|
+
{ source: 'formatting', target: 'bold', relation: 'child' },
|
|
71
|
+
]);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('merges pre-built subgraph', ({ expect }) => {
|
|
75
|
+
const headings = MenuBuilder.make()
|
|
76
|
+
.root({ label: 'headings' })
|
|
77
|
+
.group('headings', { label: 'Headings', variant: 'dropdownMenu' }, (group) => {
|
|
78
|
+
group
|
|
79
|
+
.action('h1', { label: 'H1', icon: 'ph--text-h-one--regular' }, () => {})
|
|
80
|
+
.action('h2', { label: 'H2', icon: 'ph--text-h-two--regular' }, () => {});
|
|
81
|
+
})
|
|
82
|
+
.build();
|
|
83
|
+
|
|
84
|
+
const graph = MenuBuilder.make()
|
|
85
|
+
.root({ label: 'toolbar' })
|
|
86
|
+
.action('search', { label: 'Search', icon: 'ph--magnifying-glass--regular' }, () => {})
|
|
87
|
+
.subgraph(headings)
|
|
88
|
+
.build();
|
|
89
|
+
|
|
90
|
+
// root (toolbar) + search + root (headings) + headings group + h1 + h2 = 6 nodes.
|
|
91
|
+
expect(graph.nodes).toHaveLength(6);
|
|
92
|
+
// search → root, headings root→headings group, h1→headings, h2→headings.
|
|
93
|
+
const headingsEdge = graph.edges.find((edge) => edge.target === 'headings');
|
|
94
|
+
expect(headingsEdge).toEqual({ source: 'root', target: 'headings', relation: 'child' });
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test('separators are inserted between groups', ({ expect }) => {
|
|
98
|
+
const graph = MenuBuilder.make()
|
|
99
|
+
.root({ label: 'toolbar' })
|
|
100
|
+
.action('bold', { label: 'Bold', icon: 'ph--text-b--regular' }, () => {})
|
|
101
|
+
.separator()
|
|
102
|
+
.action('search', { label: 'Search', icon: 'ph--magnifying-glass--regular' }, () => {})
|
|
103
|
+
.build();
|
|
104
|
+
|
|
105
|
+
const separatorNode = graph.nodes.find((node) => node.type === MenuSeparatorType);
|
|
106
|
+
expect(separatorNode).toBeDefined();
|
|
107
|
+
expect(separatorNode!.properties).toEqual({ variant: 'gap' });
|
|
108
|
+
|
|
109
|
+
const separatorEdge = graph.edges.find((edge) => edge.target === separatorNode!.id);
|
|
110
|
+
expect(separatorEdge).toEqual({ source: 'root', target: separatorNode!.id, relation: 'child' });
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test('subgraph callback adds children to current group', ({ expect }) => {
|
|
114
|
+
const graph = MenuBuilder.make()
|
|
115
|
+
.root({ label: 'toolbar' })
|
|
116
|
+
.subgraph((sub) => {
|
|
117
|
+
sub
|
|
118
|
+
.group('headings', { label: 'Headings', variant: 'dropdownMenu' }, (group) => {
|
|
119
|
+
group
|
|
120
|
+
.action('h1', { label: 'H1', icon: 'ph--text-h-one--regular' }, () => {})
|
|
121
|
+
.action('h2', { label: 'H2', icon: 'ph--text-h-two--regular' }, () => {});
|
|
122
|
+
})
|
|
123
|
+
.group('formatting', { label: 'Formatting' }, (group) => {
|
|
124
|
+
group.action('bold', { label: 'Bold', icon: 'ph--text-b--regular' }, () => {});
|
|
125
|
+
});
|
|
126
|
+
})
|
|
127
|
+
.build();
|
|
128
|
+
|
|
129
|
+
// root + headings + h1 + h2 + formatting + bold = 6 nodes.
|
|
130
|
+
expect(graph.nodes).toHaveLength(6);
|
|
131
|
+
expect(graph.edges).toEqual([
|
|
132
|
+
{ source: 'root', target: 'headings', relation: 'child' },
|
|
133
|
+
{ source: 'headings', target: 'h1', relation: 'child' },
|
|
134
|
+
{ source: 'headings', target: 'h2', relation: 'child' },
|
|
135
|
+
{ source: 'root', target: 'formatting', relation: 'child' },
|
|
136
|
+
{ source: 'formatting', target: 'bold', relation: 'child' },
|
|
137
|
+
]);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test('subgraph callback scoped to nested group', ({ expect }) => {
|
|
141
|
+
const graph = MenuBuilder.make()
|
|
142
|
+
.root({ label: 'toolbar' })
|
|
143
|
+
.group('text', { label: 'Text' }, (text) => {
|
|
144
|
+
text.subgraph((sub) => {
|
|
145
|
+
sub.action('bold', { label: 'Bold', icon: 'ph--text-b--regular' }, () => {});
|
|
146
|
+
sub.action('italic', { label: 'Italic', icon: 'ph--text-italic--regular' }, () => {});
|
|
147
|
+
});
|
|
148
|
+
})
|
|
149
|
+
.build();
|
|
150
|
+
|
|
151
|
+
// root + text + bold + italic = 4 nodes.
|
|
152
|
+
expect(graph.nodes).toHaveLength(4);
|
|
153
|
+
expect(graph.edges).toEqual([
|
|
154
|
+
{ source: 'root', target: 'text', relation: 'child' },
|
|
155
|
+
{ source: 'text', target: 'bold', relation: 'child' },
|
|
156
|
+
{ source: 'text', target: 'italic', relation: 'child' },
|
|
157
|
+
]);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test('composable curried subgraph functions', ({ expect }) => {
|
|
161
|
+
// Simulates the curried pattern: addX(args) returns (builder) => void.
|
|
162
|
+
const addHeadingsSection = () => (builder: import('./builder').ActionGroupBuilder) => {
|
|
163
|
+
builder.group('headings', { label: 'Headings', variant: 'dropdownMenu' }, (group) => {
|
|
164
|
+
group
|
|
165
|
+
.action('h1', { label: 'H1', icon: 'ph--text-h-one--regular' }, () => {})
|
|
166
|
+
.action('h2', { label: 'H2', icon: 'ph--text-h-two--regular' }, () => {});
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const addFormattingSection = () => (builder: import('./builder').ActionGroupBuilder) => {
|
|
171
|
+
builder.group('formatting', { label: 'Formatting' }, (group) => {
|
|
172
|
+
group.action('bold', { label: 'Bold', icon: 'ph--text-b--regular' }, () => {});
|
|
173
|
+
});
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const graph = MenuBuilder.make()
|
|
177
|
+
.root({ label: 'toolbar' })
|
|
178
|
+
.subgraph(addHeadingsSection())
|
|
179
|
+
.separator()
|
|
180
|
+
.subgraph(addFormattingSection())
|
|
181
|
+
.build();
|
|
182
|
+
|
|
183
|
+
expect(graph.nodes).toHaveLength(7); // root + headings + h1 + h2 + separator + formatting + bold.
|
|
184
|
+
expect(graph.edges[0]).toEqual({ source: 'root', target: 'headings', relation: 'child' });
|
|
185
|
+
expect(graph.edges[3]).toEqual({ source: 'root', target: 'separator-1', relation: 'child' });
|
|
186
|
+
expect(graph.edges[4]).toEqual({ source: 'root', target: 'formatting', relation: 'child' });
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
test('subgraph skips falsy values', ({ expect }) => {
|
|
190
|
+
const showHeadings = false;
|
|
191
|
+
const graph = MenuBuilder.make()
|
|
192
|
+
.root({ label: 'toolbar' })
|
|
193
|
+
.subgraph(
|
|
194
|
+
showHeadings &&
|
|
195
|
+
((builder) => {
|
|
196
|
+
builder.group('headings', { label: 'Headings' }, () => {});
|
|
197
|
+
}),
|
|
198
|
+
)
|
|
199
|
+
.action('bold', { label: 'Bold', icon: 'ph--text-b--regular' }, () => {})
|
|
200
|
+
.subgraph(undefined)
|
|
201
|
+
.subgraph(null)
|
|
202
|
+
.build();
|
|
203
|
+
|
|
204
|
+
// root + bold = 2 nodes; headings skipped.
|
|
205
|
+
expect(graph.nodes).toHaveLength(2);
|
|
206
|
+
expect(graph.edges).toHaveLength(1);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test('nodes have correct types', ({ expect }) => {
|
|
210
|
+
const graph = MenuBuilder.make()
|
|
211
|
+
.root({ label: 'toolbar' })
|
|
212
|
+
.action('bold', { label: 'Bold', icon: 'ph--text-b--regular' }, () => {})
|
|
213
|
+
.group('headings', { label: 'Headings' }, () => {})
|
|
214
|
+
.build();
|
|
215
|
+
|
|
216
|
+
const rootNode = graph.nodes.find((node) => node.id === 'root');
|
|
217
|
+
const actionNode = graph.nodes.find((node) => node.id === 'bold');
|
|
218
|
+
const groupNode = graph.nodes.find((node) => node.id === 'headings');
|
|
219
|
+
|
|
220
|
+
expect(rootNode!.type).toBe(Node.ActionGroupType);
|
|
221
|
+
expect(actionNode!.type).toBe(Node.ActionType);
|
|
222
|
+
expect(groupNode!.type).toBe(Node.ActionGroupType);
|
|
223
|
+
});
|
|
224
|
+
});
|
package/src/builder.ts
CHANGED
|
@@ -3,41 +3,60 @@
|
|
|
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
|
|
9
|
+
import { MenuSeparatorType } from './types';
|
|
9
10
|
import { createMenuAction, createMenuItemGroup } from './util';
|
|
10
11
|
|
|
12
|
+
/** Callback that populates an action group builder. */
|
|
13
|
+
export type ActionGroupBuilderFn = (builder: ActionGroupBuilder) => void;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Fluent builder for composing menu action graphs.
|
|
17
|
+
* Subgraph functions return a callback that receives the builder, enabling composition via partial application:
|
|
18
|
+
* ```ts
|
|
19
|
+
* const addHeadings = (state): ActionGroupBuilderFn => (builder) => {
|
|
20
|
+
* builder.group('headings', { ... }, (group) => { ... });
|
|
21
|
+
* };
|
|
22
|
+
*
|
|
23
|
+
* MenuBuilder.make()
|
|
24
|
+
* .subgraph(showHeadings && addHeadings(state))
|
|
25
|
+
* .subgraph(showFormatting && addFormatting(state))
|
|
26
|
+
* .build();
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
11
29
|
export interface ActionGroupBuilder {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
30
|
+
/** Add an action node as a child of the current group. */
|
|
31
|
+
action<P extends {} = {}>(id: string, props: P & MenuActionProperties, invoke: () => void): this;
|
|
32
|
+
|
|
33
|
+
/** Add a nested action group. */
|
|
34
|
+
group<P extends {} = {}>(id: string, props: P & MenuItemGroupProperties, cb: ActionGroupBuilderFn): this;
|
|
35
|
+
|
|
36
|
+
/** Merge pre-built nodes and edges, or build a subgraph via callback scoped to the current group. Falsy values are ignored. */
|
|
37
|
+
subgraph(subgraphOrCb: ActionGraphProps | ActionGroupBuilderFn | false | null | undefined): this;
|
|
38
|
+
|
|
39
|
+
/** Add a separator. */
|
|
40
|
+
separator(variant?: 'gap' | 'line'): this;
|
|
18
41
|
}
|
|
19
42
|
|
|
43
|
+
/** Top-level builder that creates the root group and produces the final action graph. */
|
|
20
44
|
export interface MenuBuilder extends ActionGroupBuilder {
|
|
45
|
+
/** Set properties on the root menu group. May only be called once. */
|
|
21
46
|
root(props: MenuItemGroupProperties): this;
|
|
47
|
+
|
|
48
|
+
/** Return the assembled action graph. */
|
|
22
49
|
build(): ActionGraphProps;
|
|
23
50
|
}
|
|
24
51
|
|
|
25
|
-
export const MenuBuilder = Object.freeze({
|
|
26
|
-
make: (): MenuBuilder => new MenuBuilderImpl({ nodes: [], edges: [] }, 'root'),
|
|
27
|
-
});
|
|
28
|
-
|
|
29
52
|
class MenuBuilderImpl implements MenuBuilder {
|
|
53
|
+
private _separatorCount = 0;
|
|
54
|
+
|
|
30
55
|
constructor(
|
|
31
56
|
private readonly _data: ActionGraphProps,
|
|
32
57
|
private readonly _rootId: string,
|
|
33
58
|
) {}
|
|
34
59
|
|
|
35
|
-
action<P extends {} = {}>(id: string, invoke: () => void, properties: P & MenuActionProperties): this {
|
|
36
|
-
this._data.nodes.push(createMenuAction(id, invoke, properties));
|
|
37
|
-
this._data.edges.push({ source: this._rootId, target: id });
|
|
38
|
-
return this;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
60
|
root(props: MenuItemGroupProperties): this {
|
|
42
61
|
invariant(this._rootId === 'root', 'Root group can only be at the top level');
|
|
43
62
|
invariant(this._data.nodes.find((node) => node.id === 'root') === undefined, 'Root group can only be created once');
|
|
@@ -45,18 +64,49 @@ class MenuBuilderImpl implements MenuBuilder {
|
|
|
45
64
|
return this;
|
|
46
65
|
}
|
|
47
66
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
): this {
|
|
53
|
-
this._data.nodes.push(
|
|
54
|
-
this._data.edges.push({ source: this._rootId, target: id });
|
|
67
|
+
build(): ActionGraphProps {
|
|
68
|
+
return this._data;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
action<P extends {} = {}>(id: string, props: P & MenuActionProperties, invoke: () => void): this {
|
|
72
|
+
this._data.nodes.push(createMenuAction(id, invoke, props));
|
|
73
|
+
this._data.edges.push({ source: this._rootId, target: id, relation: 'child' });
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
group<P extends {} = {}>(id: string, props: P & MenuItemGroupProperties, cb: ActionGroupBuilderFn): this {
|
|
78
|
+
this._data.nodes.push(createMenuItemGroup(id, props));
|
|
79
|
+
this._data.edges.push({ source: this._rootId, target: id, relation: 'child' });
|
|
55
80
|
cb(new MenuBuilderImpl(this._data, id));
|
|
56
81
|
return this;
|
|
57
82
|
}
|
|
58
83
|
|
|
59
|
-
|
|
60
|
-
|
|
84
|
+
subgraph(subgraphOrCb: ActionGraphProps | ActionGroupBuilderFn | false | null | undefined): this {
|
|
85
|
+
if (!subgraphOrCb) {
|
|
86
|
+
return this;
|
|
87
|
+
}
|
|
88
|
+
if (typeof subgraphOrCb === 'function') {
|
|
89
|
+
subgraphOrCb(new MenuBuilderImpl(this._data, this._rootId));
|
|
90
|
+
} else {
|
|
91
|
+
this._data.nodes.push(...subgraphOrCb.nodes);
|
|
92
|
+
this._data.edges.push(...subgraphOrCb.edges);
|
|
93
|
+
}
|
|
94
|
+
return this;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
separator(variant: 'gap' | 'line' = 'gap'): this {
|
|
98
|
+
const id = `separator-${++this._separatorCount}`;
|
|
99
|
+
this._data.nodes.push({
|
|
100
|
+
id,
|
|
101
|
+
type: MenuSeparatorType,
|
|
102
|
+
properties: { variant },
|
|
103
|
+
data: undefined as never,
|
|
104
|
+
});
|
|
105
|
+
this._data.edges.push({ source: this._rootId, target: id, relation: 'child' });
|
|
106
|
+
return this;
|
|
61
107
|
}
|
|
62
108
|
}
|
|
109
|
+
|
|
110
|
+
export const MenuBuilder = Object.freeze({
|
|
111
|
+
make: (): MenuBuilder => new MenuBuilderImpl({ nodes: [], edges: [] }, 'root'),
|
|
112
|
+
});
|
|
@@ -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 {
|
|
10
|
+
import { 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);
|
|
@@ -21,7 +21,7 @@ export const ActionLabel = ({ action }: { action: Action }) => {
|
|
|
21
21
|
return (
|
|
22
22
|
<>
|
|
23
23
|
<span className='grow truncate'>{toLocalizedString(action.properties!.label, t)}</span>
|
|
24
|
-
{shortcut && <span className={mx('shrink-0',
|
|
24
|
+
{shortcut && <span className={mx('shrink-0', 'text-description')}>{keySymbols(shortcut).join('')}</span>}
|
|
25
25
|
</>
|
|
26
26
|
);
|
|
27
27
|
};
|
|
@@ -8,9 +8,9 @@ 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
|
-
|
|
11
|
+
import { executeMenuAction } from '../util';
|
|
12
12
|
import { ActionLabel } from './ActionLabel';
|
|
13
|
-
import { type MenuScopedProps,
|
|
13
|
+
import { type MenuScopedProps, useMenuItems, useMenuScoped } from './Menu';
|
|
14
14
|
|
|
15
15
|
export type DropdownMenuProps = DropdownMenuRootProps & {
|
|
16
16
|
group?: MenuItemGroup;
|
|
@@ -29,7 +29,7 @@ const DropdownMenuItem = ({
|
|
|
29
29
|
// TODO(thure): handle other items.
|
|
30
30
|
const action = item as MenuAction;
|
|
31
31
|
const handleClick = useCallback((event: MouseEvent) => onClick(action, event), [action, onClick]);
|
|
32
|
-
const { iconSize } =
|
|
32
|
+
const { iconSize } = useMenuScoped('DropdownMenuItem', __menuScope);
|
|
33
33
|
return (
|
|
34
34
|
<NaturalDropdownMenu.Item
|
|
35
35
|
onClick={handleClick}
|
|
@@ -60,17 +60,23 @@ const DropdownMenuRoot = ({
|
|
|
60
60
|
onChange: onOpenChange,
|
|
61
61
|
});
|
|
62
62
|
|
|
63
|
+
const { onAction } = useMenuScoped('DropdownMenuRoot', __menuScope);
|
|
64
|
+
|
|
63
65
|
const handleActionClick = useCallback(
|
|
64
66
|
(action: MenuAction, event: MouseEvent) => {
|
|
65
67
|
if (action.properties?.disabled) {
|
|
66
68
|
return;
|
|
67
69
|
}
|
|
68
70
|
event.stopPropagation();
|
|
69
|
-
// TODO(thure): Why does Dialog
|
|
71
|
+
// TODO(thure): Why does Dialog's modal-ness cause issues if we don't explicitly close the menu here?
|
|
70
72
|
setOptionsMenuOpen(false);
|
|
71
|
-
|
|
73
|
+
if (onAction) {
|
|
74
|
+
onAction(action, { parent: group, caller });
|
|
75
|
+
} else {
|
|
76
|
+
void executeMenuAction(action, { parent: group, caller });
|
|
77
|
+
}
|
|
72
78
|
},
|
|
73
|
-
[group, caller],
|
|
79
|
+
[group, caller, onAction],
|
|
74
80
|
);
|
|
75
81
|
|
|
76
82
|
const items = useMenuItems(group, propsItems);
|