@fluixi-ui/menubar 0.2.0-alpha.17 → 0.2.0-alpha.19
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/index.js +2 -12
- package/dist/menubar.d.ts.map +1 -1
- package/dist/menubar.js +622 -468
- package/dist/tsconfig.lib.tsbuildinfo +1 -1
- package/package.json +5 -5
package/dist/menubar.js
CHANGED
|
@@ -1,490 +1,644 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { createComponent, createNativeElement, delegateEvents, handleRef, insert, mergeProps, splitProps, spread } from "@fluixi/dom";
|
|
2
|
+
import { createEffect, createMemo, createRenderEffect, createSignal, onCleanup } from "@fluixi/reactive/signal";
|
|
3
3
|
import { fxProps } from "@fluixi-ui/fx";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
4
|
+
import { Show, onMount, useRef } from "@fluixi/core";
|
|
5
|
+
import { composeEventHandlers, composeId, createControllableSignal, createId, createSafeContext, dataAttr, focusWithoutScrolling } from "@fluixi-ui/kit";
|
|
6
|
+
import { Portal, createCollection, createDismissableLayer, createFloatingPosition, createFocusScope, createTypeahead, getNextEnabledIndex } from "@fluixi-ui/primitives";
|
|
7
|
+
//#region src/menubar.tsx
|
|
7
8
|
delegateEvents(["keydown", "click"]);
|
|
8
|
-
|
|
9
|
+
var [MenubarProvider, useMenubarContext] = createSafeContext("Menubar");
|
|
10
|
+
/**
|
|
11
|
+
* Root of a menubar — a horizontal row of menus (like a desktop app's
|
|
12
|
+
* File/Edit/View bar). Left/Right arrows rove between triggers, and once one
|
|
13
|
+
* menu is open moving to another opens it; each menu navigates like a
|
|
14
|
+
* {@link DropdownMenu}.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```tsx
|
|
18
|
+
* import {
|
|
19
|
+
* Menubar, MenubarMenu, MenubarTrigger, MenubarContent,
|
|
20
|
+
* MenubarItem, MenubarSeparator,
|
|
21
|
+
* } from '@fluixi-ui/menubar';
|
|
22
|
+
*
|
|
23
|
+
* <Menubar>
|
|
24
|
+
* <MenubarMenu>
|
|
25
|
+
* <MenubarTrigger>File</MenubarTrigger>
|
|
26
|
+
* <MenubarContent>
|
|
27
|
+
* <MenubarItem onSelect={newFile}>New</MenubarItem>
|
|
28
|
+
* <MenubarSeparator />
|
|
29
|
+
* <MenubarItem onSelect={save}>Save</MenubarItem>
|
|
30
|
+
* </MenubarContent>
|
|
31
|
+
* </MenubarMenu>
|
|
32
|
+
* <MenubarMenu>
|
|
33
|
+
* <MenubarTrigger>Edit</MenubarTrigger>
|
|
34
|
+
* <MenubarContent>
|
|
35
|
+
* <MenubarItem onSelect={undo}>Undo</MenubarItem>
|
|
36
|
+
* </MenubarContent>
|
|
37
|
+
* </MenubarMenu>
|
|
38
|
+
* </Menubar>
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
9
41
|
function Menubar(props) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
42
|
+
const [local, others] = splitProps(props, [
|
|
43
|
+
"value",
|
|
44
|
+
"defaultValue",
|
|
45
|
+
"onValueChange",
|
|
46
|
+
"children",
|
|
47
|
+
"fx",
|
|
48
|
+
"style"
|
|
49
|
+
]);
|
|
50
|
+
const [value, setValue] = createControllableSignal({
|
|
51
|
+
value: () => local.value,
|
|
52
|
+
defaultValue: () => local.defaultValue,
|
|
53
|
+
onChange: (next) => local.onValueChange?.(next)
|
|
54
|
+
});
|
|
55
|
+
const triggers = createCollection();
|
|
56
|
+
const adjacent = (from, dir) => {
|
|
57
|
+
const items = triggers.items();
|
|
58
|
+
const index = items.findIndex((item) => item.value === from);
|
|
59
|
+
if (index === -1) return void 0;
|
|
60
|
+
return items[(index + (dir === "next" ? 1 : -1) + items.length) % items.length];
|
|
61
|
+
};
|
|
62
|
+
const context = {
|
|
63
|
+
value,
|
|
64
|
+
setValue: (next) => setValue(next),
|
|
65
|
+
triggers,
|
|
66
|
+
moveTrigger: (from, dir) => {
|
|
67
|
+
const next = adjacent(from, dir);
|
|
68
|
+
if (!next) return;
|
|
69
|
+
focusWithoutScrolling(next.ref);
|
|
70
|
+
if (value() !== void 0 && next.value !== void 0) setValue(next.value);
|
|
71
|
+
},
|
|
72
|
+
switchTo: (from, dir) => {
|
|
73
|
+
const next = adjacent(from, dir);
|
|
74
|
+
if (next?.value !== void 0) setValue(next.value);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
return createMemo(() => createComponent(MenubarProvider, {
|
|
78
|
+
get value() {
|
|
79
|
+
return context;
|
|
80
|
+
},
|
|
81
|
+
get children() {
|
|
82
|
+
return (() => {
|
|
83
|
+
const _el$ = createNativeElement("div");
|
|
84
|
+
spread({
|
|
85
|
+
element: _el$,
|
|
86
|
+
props: mergeProps(fxProps(() => local.fx, () => local.style), others, { role: "menubar" })
|
|
87
|
+
});
|
|
88
|
+
insert(_el$, local.children, null);
|
|
89
|
+
return _el$;
|
|
90
|
+
})();
|
|
91
|
+
}
|
|
92
|
+
}));
|
|
52
93
|
}
|
|
53
|
-
|
|
94
|
+
var [MenuProvider, useMenuContext] = createSafeContext("MenubarMenu");
|
|
95
|
+
/** One menu within a {@link Menubar}, pairing a trigger with its content. */
|
|
54
96
|
function MenubarMenu(props) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
97
|
+
const menubar = useMenubarContext();
|
|
98
|
+
const baseId = createId("menubar-menu");
|
|
99
|
+
const [triggerRef, setTriggerRef] = createSignal(void 0);
|
|
100
|
+
const itemCollection = createCollection();
|
|
101
|
+
const [focusedIndex, setFocusedIndex] = createSignal(-1);
|
|
102
|
+
let activeSubClose;
|
|
103
|
+
const focusIndex = (index) => {
|
|
104
|
+
const item = itemCollection.items()[index];
|
|
105
|
+
if (!item) return;
|
|
106
|
+
setFocusedIndex(index);
|
|
107
|
+
focusWithoutScrolling(item.ref);
|
|
108
|
+
};
|
|
109
|
+
const move = (action) => {
|
|
110
|
+
const next = getNextEnabledIndex(itemCollection.items(), focusedIndex(), action, true);
|
|
111
|
+
if (next !== -1) focusIndex(next);
|
|
112
|
+
};
|
|
113
|
+
const focusFirst = () => {
|
|
114
|
+
const i = itemCollection.items().findIndex((item) => !item.disabled);
|
|
115
|
+
if (i !== -1) focusIndex(i);
|
|
116
|
+
};
|
|
117
|
+
const focusLast = () => {
|
|
118
|
+
const items = itemCollection.items();
|
|
119
|
+
for (let i = items.length - 1; i >= 0; i--) if (!items[i].disabled) return focusIndex(i);
|
|
120
|
+
};
|
|
121
|
+
const typeahead = createTypeahead({
|
|
122
|
+
getItems: () => itemCollection.items().map((item) => ({
|
|
123
|
+
textValue: item.ref.textContent ?? "",
|
|
124
|
+
disabled: item.disabled
|
|
125
|
+
})),
|
|
126
|
+
getActiveIndex: () => focusedIndex(),
|
|
127
|
+
onMatch: (index) => focusIndex(index)
|
|
128
|
+
});
|
|
129
|
+
const onContentKeyDown = (event) => {
|
|
130
|
+
switch (event.key) {
|
|
131
|
+
case "ArrowDown":
|
|
132
|
+
event.preventDefault();
|
|
133
|
+
move("next");
|
|
134
|
+
break;
|
|
135
|
+
case "ArrowUp":
|
|
136
|
+
event.preventDefault();
|
|
137
|
+
move("previous");
|
|
138
|
+
break;
|
|
139
|
+
case "ArrowRight":
|
|
140
|
+
event.preventDefault();
|
|
141
|
+
menubar.switchTo(props.value, "next");
|
|
142
|
+
break;
|
|
143
|
+
case "ArrowLeft":
|
|
144
|
+
event.preventDefault();
|
|
145
|
+
menubar.switchTo(props.value, "previous");
|
|
146
|
+
break;
|
|
147
|
+
case "Home":
|
|
148
|
+
event.preventDefault();
|
|
149
|
+
focusFirst();
|
|
150
|
+
break;
|
|
151
|
+
case "End":
|
|
152
|
+
event.preventDefault();
|
|
153
|
+
focusLast();
|
|
154
|
+
break;
|
|
155
|
+
default: typeahead.onKeyDown(event);
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
const context = {
|
|
159
|
+
value: props.value,
|
|
160
|
+
isOpen: () => menubar.value() !== void 0 && menubar.value() === props.value,
|
|
161
|
+
ids: {
|
|
162
|
+
trigger: composeId(baseId, "trigger"),
|
|
163
|
+
content: composeId(baseId, "content")
|
|
164
|
+
},
|
|
165
|
+
triggerRef,
|
|
166
|
+
setTriggerRef,
|
|
167
|
+
items: itemCollection,
|
|
168
|
+
onContentKeyDown,
|
|
169
|
+
focusFirst,
|
|
170
|
+
select: () => {
|
|
171
|
+
activeSubClose?.();
|
|
172
|
+
activeSubClose = void 0;
|
|
173
|
+
menubar.setValue(void 0);
|
|
174
|
+
},
|
|
175
|
+
openSub: (closeFn) => {
|
|
176
|
+
activeSubClose?.();
|
|
177
|
+
activeSubClose = closeFn;
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
return createMemo(() => createComponent(MenuProvider, {
|
|
181
|
+
get value() {
|
|
182
|
+
return context;
|
|
183
|
+
},
|
|
184
|
+
get children() {
|
|
185
|
+
return props.children;
|
|
186
|
+
}
|
|
187
|
+
}));
|
|
140
188
|
}
|
|
189
|
+
/** The top-level button that opens its {@link MenubarMenu}. */
|
|
141
190
|
function MenubarTrigger(props) {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
191
|
+
const menubar = useMenubarContext();
|
|
192
|
+
const menu = useMenuContext();
|
|
193
|
+
const [local, others] = splitProps(props, [
|
|
194
|
+
"onClick",
|
|
195
|
+
"onKeyDown",
|
|
196
|
+
"ref",
|
|
197
|
+
"children",
|
|
198
|
+
"fx",
|
|
199
|
+
"style"
|
|
200
|
+
]);
|
|
201
|
+
const setRef = (node) => {
|
|
202
|
+
menu.setTriggerRef(node);
|
|
203
|
+
const remove = menubar.triggers.register({
|
|
204
|
+
ref: node,
|
|
205
|
+
value: menu.value
|
|
206
|
+
});
|
|
207
|
+
onCleanup(remove);
|
|
208
|
+
handleRef(local.ref, node);
|
|
209
|
+
};
|
|
210
|
+
return (() => {
|
|
211
|
+
const _el$ = createNativeElement("button");
|
|
212
|
+
spread({
|
|
213
|
+
element: _el$,
|
|
214
|
+
props: mergeProps(fxProps(() => local.fx, () => local.style), others, {
|
|
215
|
+
ref: setRef,
|
|
216
|
+
type: "button",
|
|
217
|
+
id: () => menu.ids.trigger,
|
|
218
|
+
role: "menuitem",
|
|
219
|
+
"aria-haspopup": "menu",
|
|
220
|
+
"aria-expanded": () => menu.isOpen() ? "true" : "false",
|
|
221
|
+
"aria-controls": () => menu.ids.content,
|
|
222
|
+
tabindex: -1,
|
|
223
|
+
"data-state": () => menu.isOpen() ? "open" : "closed",
|
|
224
|
+
onClick: composeEventHandlers([local.onClick, () => menubar.setValue(menu.isOpen() ? void 0 : menu.value)]),
|
|
225
|
+
onKeyDown: composeEventHandlers([local.onKeyDown, (event) => {
|
|
226
|
+
switch (event.key) {
|
|
227
|
+
case "ArrowRight":
|
|
228
|
+
event.preventDefault();
|
|
229
|
+
menubar.moveTrigger(menu.value, "next");
|
|
230
|
+
break;
|
|
231
|
+
case "ArrowLeft":
|
|
232
|
+
event.preventDefault();
|
|
233
|
+
menubar.moveTrigger(menu.value, "previous");
|
|
234
|
+
break;
|
|
235
|
+
case "ArrowDown":
|
|
236
|
+
case "Enter":
|
|
237
|
+
case " ":
|
|
238
|
+
event.preventDefault();
|
|
239
|
+
menubar.setValue(menu.value);
|
|
240
|
+
}
|
|
241
|
+
}])
|
|
242
|
+
})
|
|
243
|
+
});
|
|
244
|
+
insert(_el$, local.children, null);
|
|
245
|
+
return _el$;
|
|
246
|
+
})();
|
|
183
247
|
}
|
|
248
|
+
/** The floating `role="menu"` surface for one {@link MenubarMenu}. */
|
|
184
249
|
function MenubarContent(props) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
250
|
+
const menu = useMenuContext();
|
|
251
|
+
const ref = useRef(null);
|
|
252
|
+
createEffect(() => {
|
|
253
|
+
if (!menu.isOpen() && ref.current) ref.current.remove();
|
|
254
|
+
});
|
|
255
|
+
return createMemo(() => createComponent(Show, {
|
|
256
|
+
get when() {
|
|
257
|
+
return menu.isOpen();
|
|
258
|
+
},
|
|
259
|
+
get children() {
|
|
260
|
+
return createMemo(() => createComponent(Portal, {
|
|
261
|
+
get ref() {
|
|
262
|
+
return ref;
|
|
263
|
+
},
|
|
264
|
+
get children() {
|
|
265
|
+
return createMemo(() => createComponent(MenubarContentImpl, mergeProps(props, {})));
|
|
266
|
+
}
|
|
267
|
+
}));
|
|
268
|
+
}
|
|
269
|
+
}));
|
|
201
270
|
}
|
|
202
271
|
function MenubarContentImpl(props) {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
272
|
+
const menu = useMenuContext();
|
|
273
|
+
const [local, others] = splitProps(props, [
|
|
274
|
+
"ref",
|
|
275
|
+
"children",
|
|
276
|
+
"style",
|
|
277
|
+
"fx",
|
|
278
|
+
"style"
|
|
279
|
+
]);
|
|
280
|
+
const [element, setElement] = createSignal(void 0);
|
|
281
|
+
const position = createFloatingPosition(menu.triggerRef, element, {
|
|
282
|
+
placement: "bottom-start",
|
|
283
|
+
offset: 4
|
|
284
|
+
});
|
|
285
|
+
createDismissableLayer(element, {
|
|
286
|
+
shouldExcludeTarget: (target) => {
|
|
287
|
+
const trigger = menu.triggerRef();
|
|
288
|
+
if (trigger && (trigger === target || trigger.contains(target))) return true;
|
|
289
|
+
return !!(target instanceof Element ? target : target.parentElement)?.closest(`[data-menu-owner="${menu.ids.content}"]`);
|
|
290
|
+
},
|
|
291
|
+
onDismiss: () => menu.select()
|
|
292
|
+
});
|
|
293
|
+
createFocusScope(element, {
|
|
294
|
+
trap: false,
|
|
295
|
+
autoFocus: false,
|
|
296
|
+
restoreFocus: true
|
|
297
|
+
});
|
|
298
|
+
createRenderEffect(() => {
|
|
299
|
+
const el = element();
|
|
300
|
+
if (!el) return;
|
|
301
|
+
el.style.position = "absolute";
|
|
302
|
+
el.style.left = `${Math.round(position.x())}px`;
|
|
303
|
+
el.style.top = `${Math.round(position.y())}px`;
|
|
304
|
+
});
|
|
305
|
+
onMount(() => queueMicrotask(() => menu.focusFirst()));
|
|
306
|
+
const setRef = (node) => {
|
|
307
|
+
setElement(node);
|
|
308
|
+
handleRef(local.ref, node);
|
|
309
|
+
};
|
|
310
|
+
return (() => {
|
|
311
|
+
const _el$ = createNativeElement("div");
|
|
312
|
+
spread({
|
|
313
|
+
element: _el$,
|
|
314
|
+
props: mergeProps(fxProps(() => local.fx, () => local.style), others, {
|
|
315
|
+
ref: setRef,
|
|
316
|
+
role: "menu",
|
|
317
|
+
id: () => menu.ids.content,
|
|
318
|
+
"aria-labelledby": () => menu.ids.trigger,
|
|
319
|
+
"data-state": "open",
|
|
320
|
+
tabindex: -1,
|
|
321
|
+
onKeyDown: menu.onContentKeyDown
|
|
322
|
+
})
|
|
323
|
+
});
|
|
324
|
+
insert(_el$, local.children, null);
|
|
325
|
+
return _el$;
|
|
326
|
+
})();
|
|
239
327
|
}
|
|
328
|
+
/** A menu action; runs `onSelect` and closes the menu when chosen. */
|
|
240
329
|
function MenubarItem(props) {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
330
|
+
const menu = useMenuContext();
|
|
331
|
+
const [local, others] = splitProps(props, [
|
|
332
|
+
"disabled",
|
|
333
|
+
"onSelect",
|
|
334
|
+
"onClick",
|
|
335
|
+
"onPointerEnter",
|
|
336
|
+
"ref",
|
|
337
|
+
"children",
|
|
338
|
+
"fx",
|
|
339
|
+
"style"
|
|
340
|
+
]);
|
|
341
|
+
const setRef = (node) => {
|
|
342
|
+
const remove = menu.items.register({
|
|
343
|
+
ref: node,
|
|
344
|
+
disabled: local.disabled
|
|
345
|
+
});
|
|
346
|
+
onCleanup(remove);
|
|
347
|
+
handleRef(local.ref, node);
|
|
348
|
+
};
|
|
349
|
+
const select = () => {
|
|
350
|
+
if (local.disabled) return;
|
|
351
|
+
local.onSelect?.();
|
|
352
|
+
menu.select();
|
|
353
|
+
};
|
|
354
|
+
return (() => {
|
|
355
|
+
const _el$ = createNativeElement("div");
|
|
356
|
+
spread({
|
|
357
|
+
element: _el$,
|
|
358
|
+
props: mergeProps(fxProps(() => local.fx, () => local.style), others, {
|
|
359
|
+
ref: setRef,
|
|
360
|
+
role: "menuitem",
|
|
361
|
+
tabindex: -1,
|
|
362
|
+
"aria-disabled": () => local.disabled ? "true" : void 0,
|
|
363
|
+
"data-disabled": () => dataAttr(Boolean(local.disabled)),
|
|
364
|
+
onClick: composeEventHandlers([local.onClick, () => select()]),
|
|
365
|
+
onKeyDown: (event) => {
|
|
366
|
+
if (event.key === "Enter" || event.key === " ") {
|
|
367
|
+
event.preventDefault();
|
|
368
|
+
select();
|
|
369
|
+
}
|
|
370
|
+
},
|
|
371
|
+
onPointerEnter: composeEventHandlers([local.onPointerEnter, (e) => {
|
|
372
|
+
if (local.disabled) return;
|
|
373
|
+
e.currentTarget.focus();
|
|
374
|
+
}])
|
|
375
|
+
})
|
|
376
|
+
});
|
|
377
|
+
insert(_el$, local.children, null);
|
|
378
|
+
return _el$;
|
|
379
|
+
})();
|
|
276
380
|
}
|
|
381
|
+
/** A visual divider between groups of menu items. */
|
|
277
382
|
function MenubarSeparator(props) {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
383
|
+
return (() => {
|
|
384
|
+
const _el$ = createNativeElement("div");
|
|
385
|
+
spread({
|
|
386
|
+
element: _el$,
|
|
387
|
+
props: mergeProps(props, {
|
|
388
|
+
role: "separator",
|
|
389
|
+
"aria-orientation": "horizontal"
|
|
390
|
+
})
|
|
391
|
+
});
|
|
392
|
+
return _el$;
|
|
393
|
+
})();
|
|
283
394
|
}
|
|
284
|
-
|
|
395
|
+
var [SubMenuProvider, useSubMenuContext] = createSafeContext("MenubarSub");
|
|
396
|
+
/** Wraps a sub-trigger + sub-content pair to create a nested flyout inside a menubar menu. */
|
|
285
397
|
function MenubarSub(props) {
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
398
|
+
const menuCtx = useMenuContext();
|
|
399
|
+
const baseId = createId("menubar-sub");
|
|
400
|
+
const [isOpen, setIsOpen] = createSignal(false);
|
|
401
|
+
const [triggerRef, setTriggerRef] = createSignal(void 0);
|
|
402
|
+
const subCollection = createCollection();
|
|
403
|
+
const [focusedIndex, setFocusedIndex] = createSignal(-1);
|
|
404
|
+
let openTimer;
|
|
405
|
+
let closeTimer;
|
|
406
|
+
onCleanup(() => {
|
|
407
|
+
clearTimeout(openTimer);
|
|
408
|
+
clearTimeout(closeTimer);
|
|
409
|
+
});
|
|
410
|
+
const focusIndex = (index) => {
|
|
411
|
+
const item = subCollection.items()[index];
|
|
412
|
+
if (!item) return;
|
|
413
|
+
setFocusedIndex(index);
|
|
414
|
+
focusWithoutScrolling(item.ref);
|
|
415
|
+
};
|
|
416
|
+
const focusFirst = () => {
|
|
417
|
+
const i = subCollection.items().findIndex((item) => !item.disabled);
|
|
418
|
+
if (i !== -1) focusIndex(i);
|
|
419
|
+
};
|
|
420
|
+
const openSub = () => {
|
|
421
|
+
menuCtx.openSub(() => setIsOpen(false));
|
|
422
|
+
setIsOpen(true);
|
|
423
|
+
};
|
|
424
|
+
createEffect(() => {
|
|
425
|
+
if (!menuCtx.isOpen()) setIsOpen(false);
|
|
426
|
+
});
|
|
427
|
+
const context = {
|
|
428
|
+
isOpen,
|
|
429
|
+
setOpen: (open) => {
|
|
430
|
+
if (open) openSub();
|
|
431
|
+
else setIsOpen(false);
|
|
432
|
+
},
|
|
433
|
+
ids: {
|
|
434
|
+
trigger: composeId(baseId, "trigger"),
|
|
435
|
+
content: composeId(baseId, "content")
|
|
436
|
+
},
|
|
437
|
+
triggerRef,
|
|
438
|
+
setTriggerRef,
|
|
439
|
+
collection: subCollection,
|
|
440
|
+
focusedIndex,
|
|
441
|
+
focusIndex,
|
|
442
|
+
focusFirst,
|
|
443
|
+
scheduleOpen: () => {
|
|
444
|
+
clearTimeout(closeTimer);
|
|
445
|
+
openTimer = setTimeout(openSub, 200);
|
|
446
|
+
},
|
|
447
|
+
scheduleClose: (movingTowardSub = false) => {
|
|
448
|
+
clearTimeout(openTimer);
|
|
449
|
+
closeTimer = setTimeout(() => setIsOpen(false), movingTowardSub ? 500 : 200);
|
|
450
|
+
},
|
|
451
|
+
cancelClose: () => clearTimeout(closeTimer),
|
|
452
|
+
closeRoot: () => menuCtx.select()
|
|
453
|
+
};
|
|
454
|
+
return createMemo(() => createComponent(SubMenuProvider, {
|
|
455
|
+
get value() {
|
|
456
|
+
return context;
|
|
457
|
+
},
|
|
458
|
+
get children() {
|
|
459
|
+
return props.children;
|
|
460
|
+
}
|
|
461
|
+
}));
|
|
344
462
|
}
|
|
463
|
+
/** A `menuitem` inside a menubar menu that opens a nested flyout on hover or `ArrowRight`. */
|
|
345
464
|
function MenubarSubTrigger(props) {
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
465
|
+
const menu = useMenuContext();
|
|
466
|
+
const sub = useSubMenuContext();
|
|
467
|
+
const [local, others] = splitProps(props, [
|
|
468
|
+
"disabled",
|
|
469
|
+
"onKeyDown",
|
|
470
|
+
"onPointerEnter",
|
|
471
|
+
"onPointerLeave",
|
|
472
|
+
"ref",
|
|
473
|
+
"children",
|
|
474
|
+
"fx",
|
|
475
|
+
"style"
|
|
476
|
+
]);
|
|
477
|
+
const setRef = (node) => {
|
|
478
|
+
sub.setTriggerRef(node);
|
|
479
|
+
const remove = menu.items.register({
|
|
480
|
+
ref: node,
|
|
481
|
+
disabled: local.disabled
|
|
482
|
+
});
|
|
483
|
+
onCleanup(remove);
|
|
484
|
+
handleRef(local.ref, node);
|
|
485
|
+
};
|
|
486
|
+
const open = () => {
|
|
487
|
+
sub.setOpen(true);
|
|
488
|
+
queueMicrotask(() => sub.focusFirst());
|
|
489
|
+
};
|
|
490
|
+
return (() => {
|
|
491
|
+
const _el$ = createNativeElement("div");
|
|
492
|
+
spread({
|
|
493
|
+
element: _el$,
|
|
494
|
+
props: mergeProps(fxProps(() => local.fx, () => local.style), others, {
|
|
495
|
+
ref: setRef,
|
|
496
|
+
role: "menuitem",
|
|
497
|
+
tabindex: -1,
|
|
498
|
+
"aria-haspopup": "menu",
|
|
499
|
+
"aria-expanded": () => sub.isOpen() ? "true" : "false",
|
|
500
|
+
"aria-controls": () => sub.ids.content,
|
|
501
|
+
"data-state": () => sub.isOpen() ? "open" : "closed",
|
|
502
|
+
"data-disabled": () => dataAttr(Boolean(local.disabled)),
|
|
503
|
+
"aria-disabled": () => local.disabled ? "true" : void 0,
|
|
504
|
+
onPointerEnter: composeEventHandlers([local.onPointerEnter, (e) => {
|
|
505
|
+
if (local.disabled) return;
|
|
506
|
+
e.currentTarget.focus();
|
|
507
|
+
sub.scheduleOpen();
|
|
508
|
+
}]),
|
|
509
|
+
onPointerLeave: composeEventHandlers([local.onPointerLeave, (e) => sub.scheduleClose(e.movementX > 0)]),
|
|
510
|
+
onKeyDown: composeEventHandlers([local.onKeyDown, (e) => {
|
|
511
|
+
if (local.disabled) return;
|
|
512
|
+
if (e.key === "ArrowRight" || e.key === "Enter" || e.key === " ") {
|
|
513
|
+
e.preventDefault();
|
|
514
|
+
e.stopPropagation();
|
|
515
|
+
open();
|
|
516
|
+
}
|
|
517
|
+
}])
|
|
518
|
+
})
|
|
519
|
+
});
|
|
520
|
+
insert(_el$, local.children, null);
|
|
521
|
+
return _el$;
|
|
522
|
+
})();
|
|
388
523
|
}
|
|
524
|
+
/** The floating `role="menu"` surface for a nested menubar submenu. Self-portals while open. */
|
|
389
525
|
function MenubarSubContent(props) {
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
526
|
+
const sub = useSubMenuContext();
|
|
527
|
+
const root = useMenuContext();
|
|
528
|
+
const [local, others] = splitProps(props, [
|
|
529
|
+
"ref",
|
|
530
|
+
"children",
|
|
531
|
+
"style",
|
|
532
|
+
"fx",
|
|
533
|
+
"style"
|
|
534
|
+
]);
|
|
535
|
+
const [element, setElement] = createSignal(void 0);
|
|
536
|
+
const portalRef = useRef(null);
|
|
537
|
+
createEffect(() => {
|
|
538
|
+
if (!sub.isOpen() && portalRef.current) portalRef.current.remove();
|
|
539
|
+
});
|
|
540
|
+
const position = createFloatingPosition(sub.triggerRef, element, {
|
|
541
|
+
placement: "right-start",
|
|
542
|
+
offset: -2
|
|
543
|
+
});
|
|
544
|
+
createRenderEffect(() => {
|
|
545
|
+
const el = element();
|
|
546
|
+
if (!el) return;
|
|
547
|
+
el.style.position = "absolute";
|
|
548
|
+
el.style.left = `${Math.round(position.x())}px`;
|
|
549
|
+
el.style.top = `${Math.round(position.y())}px`;
|
|
550
|
+
});
|
|
551
|
+
onMount(() => queueMicrotask(() => sub.focusFirst()));
|
|
552
|
+
const move = (action) => {
|
|
553
|
+
const next = getNextEnabledIndex(sub.collection.items(), sub.focusedIndex(), action, true);
|
|
554
|
+
if (next !== -1) sub.focusIndex(next);
|
|
555
|
+
};
|
|
556
|
+
const focusLast = () => {
|
|
557
|
+
const items = sub.collection.items();
|
|
558
|
+
for (let i = items.length - 1; i >= 0; i--) if (!items[i].disabled) {
|
|
559
|
+
sub.focusIndex(i);
|
|
560
|
+
return;
|
|
561
|
+
}
|
|
562
|
+
};
|
|
563
|
+
const typeahead = createTypeahead({
|
|
564
|
+
getItems: () => sub.collection.items().map((item) => ({
|
|
565
|
+
textValue: item.ref.textContent ?? "",
|
|
566
|
+
disabled: item.disabled
|
|
567
|
+
})),
|
|
568
|
+
getActiveIndex: () => sub.focusedIndex(),
|
|
569
|
+
onMatch: (index) => sub.focusIndex(index)
|
|
570
|
+
});
|
|
571
|
+
const onContentKeyDown = (event) => {
|
|
572
|
+
switch (event.key) {
|
|
573
|
+
case "ArrowDown":
|
|
574
|
+
event.preventDefault();
|
|
575
|
+
move("next");
|
|
576
|
+
break;
|
|
577
|
+
case "ArrowUp":
|
|
578
|
+
event.preventDefault();
|
|
579
|
+
move("previous");
|
|
580
|
+
break;
|
|
581
|
+
case "Home":
|
|
582
|
+
event.preventDefault();
|
|
583
|
+
sub.focusFirst();
|
|
584
|
+
break;
|
|
585
|
+
case "End":
|
|
586
|
+
event.preventDefault();
|
|
587
|
+
focusLast();
|
|
588
|
+
break;
|
|
589
|
+
case "ArrowLeft":
|
|
590
|
+
case "Escape":
|
|
591
|
+
event.preventDefault();
|
|
592
|
+
event.stopPropagation();
|
|
593
|
+
sub.setOpen(false);
|
|
594
|
+
focusWithoutScrolling(sub.triggerRef());
|
|
595
|
+
break;
|
|
596
|
+
case "Tab":
|
|
597
|
+
event.preventDefault();
|
|
598
|
+
sub.closeRoot();
|
|
599
|
+
break;
|
|
600
|
+
default: typeahead.onKeyDown(event);
|
|
601
|
+
}
|
|
602
|
+
};
|
|
603
|
+
const setRef = (node) => {
|
|
604
|
+
setElement(node);
|
|
605
|
+
handleRef(local.ref, node);
|
|
606
|
+
};
|
|
607
|
+
return createMemo(() => createComponent(Show, {
|
|
608
|
+
get when() {
|
|
609
|
+
return sub.isOpen();
|
|
610
|
+
},
|
|
611
|
+
get children() {
|
|
612
|
+
return createMemo(() => createComponent(Portal, {
|
|
613
|
+
get ref() {
|
|
614
|
+
return portalRef;
|
|
615
|
+
},
|
|
616
|
+
get children() {
|
|
617
|
+
return (() => {
|
|
618
|
+
const _el$ = createNativeElement("div");
|
|
619
|
+
spread({
|
|
620
|
+
element: _el$,
|
|
621
|
+
props: mergeProps(fxProps(() => local.fx, () => local.style), others, {
|
|
622
|
+
ref: setRef,
|
|
623
|
+
role: "menu",
|
|
624
|
+
id: () => sub.ids.content,
|
|
625
|
+
"data-menu-owner": () => root.ids.content,
|
|
626
|
+
"aria-labelledby": () => sub.ids.trigger,
|
|
627
|
+
"data-state": "open",
|
|
628
|
+
"data-placement": () => position.placement(),
|
|
629
|
+
tabindex: -1,
|
|
630
|
+
onKeyDown: onContentKeyDown,
|
|
631
|
+
onPointerEnter: () => sub.cancelClose(),
|
|
632
|
+
onPointerLeave: () => sub.scheduleClose()
|
|
633
|
+
})
|
|
634
|
+
});
|
|
635
|
+
insert(_el$, local.children, null);
|
|
636
|
+
return _el$;
|
|
637
|
+
})();
|
|
638
|
+
}
|
|
639
|
+
}));
|
|
640
|
+
}
|
|
641
|
+
}));
|
|
479
642
|
}
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
MenubarContent,
|
|
483
|
-
MenubarItem,
|
|
484
|
-
MenubarMenu,
|
|
485
|
-
MenubarSeparator,
|
|
486
|
-
MenubarSub,
|
|
487
|
-
MenubarSubContent,
|
|
488
|
-
MenubarSubTrigger,
|
|
489
|
-
MenubarTrigger
|
|
490
|
-
};
|
|
643
|
+
//#endregion
|
|
644
|
+
export { Menubar, MenubarContent, MenubarItem, MenubarMenu, MenubarSeparator, MenubarSub, MenubarSubContent, MenubarSubTrigger, MenubarTrigger };
|