@dxos/react-ui-menu 0.8.4-main.d05673bc65 → 0.8.4-main.e00bdcdb52
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 +104 -134
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/translations.mjs +14 -0
- package/dist/lib/browser/translations.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +104 -134
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/translations.mjs +16 -0
- package/dist/lib/node-esm/translations.mjs.map +7 -0
- package/dist/types/src/builder.d.ts +23 -4
- 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.map +1 -1
- package/dist/types/src/components/DropdownMenu.d.ts +3 -1
- package/dist/types/src/components/DropdownMenu.d.ts.map +1 -1
- package/dist/types/src/components/Menu.d.ts +8 -4
- package/dist/types/src/components/Menu.d.ts.map +1 -1
- package/dist/types/src/components/{MenuItems.stories.d.ts → Menu.stories.d.ts} +1 -1
- package/dist/types/src/components/Menu.stories.d.ts.map +1 -0
- package/dist/types/src/components/ToolbarMenu.d.ts +4 -4
- package/dist/types/src/components/ToolbarMenu.d.ts.map +1 -1
- package/dist/types/src/components/ToolbarMenu.stories.d.ts +3 -1
- package/dist/types/src/components/ToolbarMenu.stories.d.ts.map +1 -1
- package/dist/types/src/hooks/useMenuActions.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +1 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/testing/generator.d.ts +8 -4
- package/dist/types/src/testing/generator.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +2 -1
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/src/util.d.ts +2 -2
- package/dist/types/src/util.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +27 -21
- package/src/builder.test.ts +224 -0
- package/src/builder.ts +41 -18
- package/src/components/ActionLabel.tsx +2 -1
- package/src/components/DropdownMenu.tsx +0 -1
- package/src/components/{MenuItems.stories.tsx → Menu.stories.tsx} +2 -1
- package/src/components/Menu.test.tsx +0 -1
- package/src/components/Menu.tsx +9 -10
- package/src/components/ToolbarMenu.stories.tsx +15 -12
- package/src/components/ToolbarMenu.tsx +111 -86
- package/src/hooks/useMenuActions.ts +6 -7
- package/src/index.ts +1 -1
- package/src/testing/generator.ts +15 -10
- package/src/types.ts +1 -0
- package/dist/types/src/components/MenuItems.stories.d.ts.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/react-ui-menu",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.e00bdcdb52",
|
|
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",
|
|
@@ -12,18 +12,24 @@
|
|
|
12
12
|
"author": "DXOS.org",
|
|
13
13
|
"sideEffects": false,
|
|
14
14
|
"type": "module",
|
|
15
|
+
"imports": {
|
|
16
|
+
"#translations": "./src/translations.ts"
|
|
17
|
+
},
|
|
15
18
|
"exports": {
|
|
16
19
|
".": {
|
|
17
20
|
"source": "./src/index.ts",
|
|
18
21
|
"types": "./dist/types/src/index.d.ts",
|
|
19
22
|
"browser": "./dist/lib/browser/index.mjs",
|
|
20
23
|
"node": "./dist/lib/node-esm/index.mjs"
|
|
24
|
+
},
|
|
25
|
+
"./translations": {
|
|
26
|
+
"source": "./src/translations.ts",
|
|
27
|
+
"types": "./dist/types/src/translations.d.ts",
|
|
28
|
+
"browser": "./dist/lib/browser/translations.mjs",
|
|
29
|
+
"node": "./dist/lib/node-esm/translations.mjs"
|
|
21
30
|
}
|
|
22
31
|
},
|
|
23
32
|
"types": "dist/types/src/index.d.ts",
|
|
24
|
-
"typesVersions": {
|
|
25
|
-
"*": {}
|
|
26
|
-
},
|
|
27
33
|
"files": [
|
|
28
34
|
"dist",
|
|
29
35
|
"src"
|
|
@@ -31,14 +37,14 @@
|
|
|
31
37
|
"dependencies": {
|
|
32
38
|
"@radix-ui/react-context": "1.1.1",
|
|
33
39
|
"@radix-ui/react-use-controllable-state": "1.1.0",
|
|
34
|
-
"@dxos/
|
|
35
|
-
"@dxos/invariant": "0.8.4-main.
|
|
36
|
-
"@dxos/
|
|
37
|
-
"@dxos/log": "0.8.4-main.
|
|
38
|
-
"@dxos/
|
|
39
|
-
"@dxos/ui-
|
|
40
|
-
"@dxos/util": "0.8.4-main.
|
|
41
|
-
"@dxos/
|
|
40
|
+
"@dxos/app-graph": "0.8.4-main.e00bdcdb52",
|
|
41
|
+
"@dxos/invariant": "0.8.4-main.e00bdcdb52",
|
|
42
|
+
"@dxos/effect": "0.8.4-main.e00bdcdb52",
|
|
43
|
+
"@dxos/log": "0.8.4-main.e00bdcdb52",
|
|
44
|
+
"@dxos/keyboard": "0.8.4-main.e00bdcdb52",
|
|
45
|
+
"@dxos/react-ui-attention": "0.8.4-main.e00bdcdb52",
|
|
46
|
+
"@dxos/util": "0.8.4-main.e00bdcdb52",
|
|
47
|
+
"@dxos/ui-types": "0.8.4-main.e00bdcdb52"
|
|
42
48
|
},
|
|
43
49
|
"devDependencies": {
|
|
44
50
|
"@effect-atom/atom-react": "^0.5.0",
|
|
@@ -47,23 +53,23 @@
|
|
|
47
53
|
"@testing-library/react": "^16.3.0",
|
|
48
54
|
"@types/react": "~19.2.7",
|
|
49
55
|
"@types/react-dom": "~19.2.3",
|
|
50
|
-
"effect": "3.
|
|
56
|
+
"effect": "3.20.0",
|
|
51
57
|
"react": "~19.2.3",
|
|
52
58
|
"react-dom": "~19.2.3",
|
|
53
|
-
"vite": "^
|
|
54
|
-
"@dxos/random": "0.8.4-main.
|
|
55
|
-
"@dxos/storybook-utils": "0.8.4-main.
|
|
56
|
-
"@dxos/ui-theme": "0.8.4-main.
|
|
57
|
-
"@dxos/react-ui": "0.8.4-main.
|
|
59
|
+
"vite": "^8.0.10",
|
|
60
|
+
"@dxos/random": "0.8.4-main.e00bdcdb52",
|
|
61
|
+
"@dxos/storybook-utils": "0.8.4-main.e00bdcdb52",
|
|
62
|
+
"@dxos/ui-theme": "0.8.4-main.e00bdcdb52",
|
|
63
|
+
"@dxos/react-ui": "0.8.4-main.e00bdcdb52"
|
|
58
64
|
},
|
|
59
65
|
"peerDependencies": {
|
|
60
66
|
"@effect-atom/atom-react": "^0.5.0",
|
|
61
67
|
"@effect/platform": "0.94.4",
|
|
62
|
-
"effect": "3.
|
|
68
|
+
"effect": "3.20.0",
|
|
63
69
|
"react": "~19.2.3",
|
|
64
70
|
"react-dom": "~19.2.3",
|
|
65
|
-
"@dxos/react-ui": "0.8.4-main.
|
|
66
|
-
"@dxos/ui-theme": "0.8.4-main.
|
|
71
|
+
"@dxos/react-ui": "0.8.4-main.e00bdcdb52",
|
|
72
|
+
"@dxos/ui-theme": "0.8.4-main.e00bdcdb52"
|
|
67
73
|
},
|
|
68
74
|
"publishConfig": {
|
|
69
75
|
"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
|
@@ -9,23 +9,43 @@ import type { ActionGraphProps } from './hooks';
|
|
|
9
9
|
import { MenuSeparatorType } from './types';
|
|
10
10
|
import { createMenuAction, createMenuItemGroup } from './util';
|
|
11
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
|
+
*/
|
|
12
29
|
export interface ActionGroupBuilder {
|
|
13
30
|
/** Add an action node as a child of the current group. */
|
|
14
31
|
action<P extends {} = {}>(id: string, props: P & MenuActionProperties, invoke: () => void): this;
|
|
32
|
+
|
|
15
33
|
/** Add a nested action group. */
|
|
16
|
-
group<P extends {} = {}>(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
/** Merge pre-built nodes and edges into this builder. */
|
|
22
|
-
subgraph(subgraph: ActionGraphProps): this;
|
|
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
|
+
|
|
23
39
|
/** Add a separator. */
|
|
24
|
-
separator(
|
|
40
|
+
separator(variant?: 'gap' | 'line'): this;
|
|
25
41
|
}
|
|
26
42
|
|
|
43
|
+
/** Top-level builder that creates the root group and produces the final action graph. */
|
|
27
44
|
export interface MenuBuilder extends ActionGroupBuilder {
|
|
45
|
+
/** Set properties on the root menu group. May only be called once. */
|
|
28
46
|
root(props: MenuItemGroupProperties): this;
|
|
47
|
+
|
|
48
|
+
/** Return the assembled action graph. */
|
|
29
49
|
build(): ActionGraphProps;
|
|
30
50
|
}
|
|
31
51
|
|
|
@@ -54,25 +74,28 @@ class MenuBuilderImpl implements MenuBuilder {
|
|
|
54
74
|
return this;
|
|
55
75
|
}
|
|
56
76
|
|
|
57
|
-
group<P extends {} = {}>(
|
|
58
|
-
id: string,
|
|
59
|
-
props: P & MenuItemGroupProperties,
|
|
60
|
-
cb: (builder: ActionGroupBuilder) => void,
|
|
61
|
-
): this {
|
|
77
|
+
group<P extends {} = {}>(id: string, props: P & MenuItemGroupProperties, cb: ActionGroupBuilderFn): this {
|
|
62
78
|
this._data.nodes.push(createMenuItemGroup(id, props));
|
|
63
79
|
this._data.edges.push({ source: this._rootId, target: id, relation: 'child' });
|
|
64
80
|
cb(new MenuBuilderImpl(this._data, id));
|
|
65
81
|
return this;
|
|
66
82
|
}
|
|
67
83
|
|
|
68
|
-
subgraph(
|
|
69
|
-
|
|
70
|
-
|
|
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
|
+
}
|
|
71
94
|
return this;
|
|
72
95
|
}
|
|
73
96
|
|
|
74
|
-
separator(
|
|
75
|
-
id
|
|
97
|
+
separator(variant: 'gap' | 'line' = 'gap'): this {
|
|
98
|
+
const id = `separator-${++this._separatorCount}`;
|
|
76
99
|
this._data.nodes.push({
|
|
77
100
|
id,
|
|
78
101
|
type: MenuSeparatorType,
|
|
@@ -10,7 +10,8 @@ import { type TFunction, toLocalizedString, useTranslation } from '@dxos/react-u
|
|
|
10
10
|
import { mx } from '@dxos/ui-theme';
|
|
11
11
|
import { type MenuActionProperties } from '@dxos/ui-types';
|
|
12
12
|
|
|
13
|
-
import { translationKey } from '
|
|
13
|
+
import { translationKey } from '#translations';
|
|
14
|
+
|
|
14
15
|
import { getShortcut } from '../util';
|
|
15
16
|
|
|
16
17
|
type Action = Node.Action<MenuActionProperties> | Node.ActionGroup<Omit<MenuActionProperties, 'variant'>>;
|
|
@@ -9,7 +9,6 @@ import { type DropdownMenuRootProps, Icon, DropdownMenu as NaturalDropdownMenu }
|
|
|
9
9
|
|
|
10
10
|
import { type MenuAction, type MenuItem, type MenuItemGroup } from '../types';
|
|
11
11
|
import { executeMenuAction } from '../util';
|
|
12
|
-
|
|
13
12
|
import { ActionLabel } from './ActionLabel';
|
|
14
13
|
import { type MenuScopedProps, useMenuItems, useMenuScoped } from './Menu';
|
|
15
14
|
|
|
@@ -11,10 +11,11 @@ import { Button, IconButton } from '@dxos/react-ui';
|
|
|
11
11
|
import { withTheme } from '@dxos/react-ui/testing';
|
|
12
12
|
import { withRegistry } from '@dxos/storybook-utils';
|
|
13
13
|
|
|
14
|
+
import { translations } from '#translations';
|
|
15
|
+
|
|
14
16
|
import { Menu, useMenu } from '../components';
|
|
15
17
|
import { type ActionGraphProps, useMenuActions } from '../hooks';
|
|
16
18
|
import { createActions } from '../testing';
|
|
17
|
-
import { translations } from '../translations';
|
|
18
19
|
import { type MenuItem } from '../types';
|
|
19
20
|
import { createMenuAction } from '../util';
|
|
20
21
|
|
|
@@ -9,7 +9,6 @@ import { afterEach, describe, test } from 'vitest';
|
|
|
9
9
|
|
|
10
10
|
import { type MenuItem, type MenuItemsAccessor } from '../types';
|
|
11
11
|
import { createMenuAction } from '../util';
|
|
12
|
-
|
|
13
12
|
import { Menu, useMenu, useMenuItems } from './Menu';
|
|
14
13
|
|
|
15
14
|
const TEST_CONTRIBUTOR = 'TestContributor';
|
package/src/components/Menu.tsx
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { Atom, RegistryContext, useAtomValue } from '@effect-atom/atom-react';
|
|
6
6
|
import { type Scope, createContextScope } from '@radix-ui/react-context';
|
|
7
7
|
import { useControllableState } from '@radix-ui/react-use-controllable-state';
|
|
8
|
-
import React, { type MouseEvent, type PropsWithChildren, useCallback, useContext, useMemo } from 'react';
|
|
8
|
+
import React, { createContext, type MouseEvent, type PropsWithChildren, useCallback, useContext, useMemo } from 'react';
|
|
9
9
|
|
|
10
10
|
import { log } from '@dxos/log';
|
|
11
11
|
import { type DropdownMenuRootProps, Icon, DropdownMenu as NaturalDropdownMenu } from '@dxos/react-ui';
|
|
@@ -21,7 +21,6 @@ import {
|
|
|
21
21
|
type MenuItemsMap,
|
|
22
22
|
} from '../types';
|
|
23
23
|
import { executeMenuAction } from '../util';
|
|
24
|
-
|
|
25
24
|
import { ActionLabel } from './ActionLabel';
|
|
26
25
|
import { ToolbarMenu } from './ToolbarMenu';
|
|
27
26
|
|
|
@@ -60,7 +59,7 @@ type MenuDropdownContextValue = {
|
|
|
60
59
|
caller?: string;
|
|
61
60
|
};
|
|
62
61
|
|
|
63
|
-
const MenuDropdownContext =
|
|
62
|
+
const MenuDropdownContext = createContext<MenuDropdownContextValue>({
|
|
64
63
|
closeMenu: () => {},
|
|
65
64
|
});
|
|
66
65
|
|
|
@@ -200,17 +199,17 @@ const useMenu = (consumerName: string): MenuContextValue => {
|
|
|
200
199
|
// Menu.Root
|
|
201
200
|
//
|
|
202
201
|
|
|
203
|
-
type MenuRootProps =
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
209
|
-
>;
|
|
202
|
+
type MenuRootProps = MenuProviderProps &
|
|
203
|
+
Pick<DropdownMenuRootProps, 'children' | 'open' | 'defaultOpen' | 'onOpenChange'> & {
|
|
204
|
+
/** Identifies the component that owns this menu (passed to action handlers). */
|
|
205
|
+
caller?: string;
|
|
206
|
+
};
|
|
210
207
|
|
|
211
208
|
/**
|
|
212
209
|
* Menu context boundary.
|
|
213
210
|
*
|
|
211
|
+
* NOTE: This component is headless since it's root div has `contents`.
|
|
212
|
+
*
|
|
214
213
|
* Provides the menu context (action dispatch, contribution registry, icon size, etc.)
|
|
215
214
|
* and an optional dropdown root for use with `Menu.Trigger` + `Menu.Content`.
|
|
216
215
|
*/
|
|
@@ -2,21 +2,22 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Atom } from '@effect-atom/atom-react';
|
|
5
|
+
import { Atom, RegistryContext } from '@effect-atom/atom-react';
|
|
6
6
|
import { type Meta, type StoryObj } from '@storybook/react-vite';
|
|
7
|
-
import React, { useMemo } from 'react';
|
|
7
|
+
import React, { useContext, useMemo } from 'react';
|
|
8
8
|
|
|
9
|
-
import {
|
|
9
|
+
import { random } from '@dxos/random';
|
|
10
10
|
import { IconButton } from '@dxos/react-ui';
|
|
11
11
|
import { withTheme } from '@dxos/react-ui/testing';
|
|
12
12
|
import { withRegistry } from '@dxos/storybook-utils';
|
|
13
13
|
|
|
14
|
+
import { translations } from '#translations';
|
|
15
|
+
|
|
14
16
|
import { Menu } from '../components';
|
|
15
17
|
import { type ActionGraphProps, useMenuActions } from '../hooks';
|
|
16
18
|
import { createActions, createNestedActions, createNestedActionsResolver, useMutateActions } from '../testing';
|
|
17
|
-
import { translations } from '../translations';
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
random.seed(1234);
|
|
20
21
|
|
|
21
22
|
const meta = {
|
|
22
23
|
title: 'ui/react-ui-menu/ToolbarMenu',
|
|
@@ -33,18 +34,19 @@ type Story = StoryObj<typeof meta>;
|
|
|
33
34
|
|
|
34
35
|
export const DropdownMenu: Story = {
|
|
35
36
|
render: () => {
|
|
36
|
-
const
|
|
37
|
+
const actions = useMemo(() => {
|
|
37
38
|
const actions = createActions();
|
|
38
39
|
return Atom.make<ActionGraphProps>({
|
|
39
40
|
nodes: actions,
|
|
40
41
|
edges: actions.map((action) => ({ source: 'root', target: action.id, relation: 'child' })),
|
|
41
42
|
}).pipe(Atom.keepAlive);
|
|
42
43
|
}, []);
|
|
43
|
-
|
|
44
|
-
|
|
44
|
+
|
|
45
|
+
useMutateActions(actions);
|
|
46
|
+
const menuActions = useMenuActions(actions);
|
|
45
47
|
|
|
46
48
|
return (
|
|
47
|
-
<Menu.Root {...
|
|
49
|
+
<Menu.Root {...menuActions}>
|
|
48
50
|
<Menu.Trigger asChild>
|
|
49
51
|
<IconButton icon='ph--list-checks--regular' label='Options' />
|
|
50
52
|
</Menu.Trigger>
|
|
@@ -56,7 +58,8 @@ export const DropdownMenu: Story = {
|
|
|
56
58
|
|
|
57
59
|
export const Toolbar: Story = {
|
|
58
60
|
render: () => {
|
|
59
|
-
const
|
|
61
|
+
const registry = useContext(RegistryContext);
|
|
62
|
+
const nestedMenuActions = useMemo(() => createNestedActionsResolver({ registry }), [registry]);
|
|
60
63
|
|
|
61
64
|
return (
|
|
62
65
|
<Menu.Root {...nestedMenuActions}>
|
|
@@ -69,10 +72,10 @@ export const Toolbar: Story = {
|
|
|
69
72
|
export const UseMenuActionsToolbar: Story = {
|
|
70
73
|
render: () => {
|
|
71
74
|
useMutateActions(createNestedActions);
|
|
72
|
-
const
|
|
75
|
+
const menuActions = useMenuActions(createNestedActions);
|
|
73
76
|
|
|
74
77
|
return (
|
|
75
|
-
<Menu.Root {...
|
|
78
|
+
<Menu.Root {...menuActions}>
|
|
76
79
|
<Menu.Toolbar />
|
|
77
80
|
</Menu.Root>
|
|
78
81
|
);
|