athena-ui-js 2.5.1 → 2.6.0
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/README.md +32 -2
- package/docs/migration-2.0.md +99 -83
- package/docs/styling.md +124 -123
- package/package.json +5 -1
- package/src/app.js +85 -0
- package/src/athena-ui.js +4 -79
- package/src/libs/config/config.js +1 -1
- package/src/libs/core/i18n.js +354 -352
- package/src/libs/hephaestus/MenuGenerator/menuGenerator.js +278 -265
- package/src/libs/hephaestus/chartGenerator/chartGenerator.js +772 -722
- package/src/libs/hephaestus/feedbackGenerator/feedbackGenerator.js +171 -161
- package/src/libs/hephaestus/formGenerator/formBuilder.js +470 -460
- package/src/libs/hephaestus/tableGenerator/tableGenerator.js +837 -825
- package/src/res/themes/default/style.css +3128 -3037
- package/types/app.d.ts +58 -0
- package/types/athena-ui.d.ts +1 -55
- package/types/libs/config/config.d.ts +96 -96
- package/types/libs/hephaestus/MenuGenerator/menuGenerator.d.ts +5 -0
- package/types/libs/hephaestus/chartGenerator/chartGenerator.d.ts +123 -117
- package/types/libs/hephaestus/dialogGenerator/dialogGenerator.d.ts +122 -122
- package/types/libs/hephaestus/feedbackGenerator/feedbackGenerator.d.ts +3 -0
- package/types/libs/hephaestus/formGenerator/authForms.d.ts +130 -130
- package/types/libs/hephaestus/formGenerator/formBuilder.d.ts +128 -128
- package/types/libs/hephaestus/tableGenerator/tableGenerator.d.ts +133 -129
|
@@ -1,265 +1,278 @@
|
|
|
1
|
-
//menuGenerator for Athena UI
|
|
2
|
-
//Author: Csaba Tóth https://github.com/csabatibortoth/
|
|
3
|
-
//LICENCE: MIT
|
|
4
|
-
//Repository: https://github.com/csabatibortoth/athena-ui
|
|
5
|
-
|
|
6
|
-
import { asHandle, cx, el, mount, setContent, uid } from "../../core/dom.js";
|
|
7
|
-
import { translate } from "../../core/i18n.js";
|
|
8
|
-
import { isSafeUrl } from "../../core/sanitize.js";
|
|
9
|
-
import { initialsOf } from "../componentGenerator/componentGenerator.js";
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @typedef {object} MenuItem
|
|
13
|
-
* @property {string} id
|
|
14
|
-
* @property {string} label
|
|
15
|
-
* @property {string|Node} [icon]
|
|
16
|
-
* @property {string} [href]
|
|
17
|
-
* @property {boolean} [disabled]
|
|
18
|
-
* @property {"end"} [align]
|
|
19
|
-
* @property {number|string|{text: (number|string), tone?: "info"|"ok"|"warn"|"bad"}} [badge]
|
|
20
|
-
* @property {string} [badgeLabel]
|
|
21
|
-
* @property {{name: string, src?: string}} [avatar]
|
|
22
|
-
* @property {MenuItem[]} [children]
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
const ENABLED = "[role=menuitem]:not([aria-disabled=true]):not(:disabled)";
|
|
26
|
-
|
|
27
|
-
/** The text of a badge: numbers above 99 become "99+", 0 / null / false / "" mean no badge. */
|
|
28
|
-
export function badgeText(value) {
|
|
29
|
-
if (value == null || value === false || value === 0 || value === "") return "";
|
|
30
|
-
return typeof value === "number" && value > 99 ? "99+" : String(value);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Generates a navigation menu. Items are real links (`href`) or buttons, so the keyboard and screen readers work without help:
|
|
35
|
-
* Tab reaches every item, Enter and Space use it, the arrow keys move inside a group, Esc closes it.
|
|
36
|
-
*
|
|
37
|
-
* @param {object} options
|
|
38
|
-
* @param {string|Element} [options.parent]
|
|
39
|
-
* @param {MenuItem[]} options.items
|
|
40
|
-
* `icon` is shown before the label (text, or a Node such as an SVG; it is hidden from screen readers).
|
|
41
|
-
* `badge` is a small count or word after the label (0, null and "" show nothing, numbers above 99 show "99+").
|
|
42
|
-
* `badgeLabel` is what a screen reader hears for it ("3 unread"); without it the badge text is read after the label.
|
|
43
|
-
* `avatar` shows the picture (or the initials) of a person: for the user menu, usually with `align: "end"` and children.
|
|
44
|
-
* An item with `children` opens a group; a child can have children too (a group inside a group, any depth).
|
|
45
|
-
* @param {(item: MenuItem, event: Event) => void} [options.onNavigate] called when a PERSON uses an item (click, Enter, Space).
|
|
46
|
-
* It is never called by select() or while the menu is built.
|
|
47
|
-
* @param {string} [options.active] id of the item that is the current page at the start
|
|
48
|
-
* @param {boolean} [options.followHash=false] the item whose `href` equals location.hash is the active one, and follows hashchange
|
|
49
|
-
* @param {"bar"|"bottom-tabs"|"responsive"} [options.layout="bar"] "bottom-tabs": a bar of tabs at the bottom of the screen,
|
|
50
|
-
* groups open as sheets above it. "responsive": a bar on wide screens, bottom tabs on a tablet or phone (max 700px).
|
|
51
|
-
* @param {
|
|
52
|
-
* @param {string} [options.
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const
|
|
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
|
-
openGroups.
|
|
132
|
-
if (
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
toggle.setAttribute("aria-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
const
|
|
158
|
-
if (event.key
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
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
|
-
found.
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
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
|
-
|
|
1
|
+
//menuGenerator for Athena UI
|
|
2
|
+
//Author: Csaba Tóth https://github.com/csabatibortoth/
|
|
3
|
+
//LICENCE: MIT
|
|
4
|
+
//Repository: https://github.com/csabatibortoth/athena-ui
|
|
5
|
+
|
|
6
|
+
import { asHandle, cx, el, mount, setContent, uid } from "../../core/dom.js";
|
|
7
|
+
import { translate } from "../../core/i18n.js";
|
|
8
|
+
import { isSafeUrl } from "../../core/sanitize.js";
|
|
9
|
+
import { initialsOf } from "../componentGenerator/componentGenerator.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {object} MenuItem
|
|
13
|
+
* @property {string} id
|
|
14
|
+
* @property {string} label
|
|
15
|
+
* @property {string|Node} [icon]
|
|
16
|
+
* @property {string} [href]
|
|
17
|
+
* @property {boolean} [disabled]
|
|
18
|
+
* @property {"end"} [align]
|
|
19
|
+
* @property {number|string|{text: (number|string), tone?: "info"|"ok"|"warn"|"bad"}} [badge]
|
|
20
|
+
* @property {string} [badgeLabel]
|
|
21
|
+
* @property {{name: string, src?: string}} [avatar]
|
|
22
|
+
* @property {MenuItem[]} [children]
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const ENABLED = "[role=menuitem]:not([aria-disabled=true]):not(:disabled)";
|
|
26
|
+
|
|
27
|
+
/** The text of a badge: numbers above 99 become "99+", 0 / null / false / "" mean no badge. */
|
|
28
|
+
export function badgeText(value) {
|
|
29
|
+
if (value == null || value === false || value === 0 || value === "") return "";
|
|
30
|
+
return typeof value === "number" && value > 99 ? "99+" : String(value);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Generates a navigation menu. Items are real links (`href`) or buttons, so the keyboard and screen readers work without help:
|
|
35
|
+
* Tab reaches every item, Enter and Space use it, the arrow keys move inside a group, Esc closes it.
|
|
36
|
+
*
|
|
37
|
+
* @param {object} options
|
|
38
|
+
* @param {string|Element} [options.parent]
|
|
39
|
+
* @param {MenuItem[]} options.items
|
|
40
|
+
* `icon` is shown before the label (text, or a Node such as an SVG; it is hidden from screen readers).
|
|
41
|
+
* `badge` is a small count or word after the label (0, null and "" show nothing, numbers above 99 show "99+").
|
|
42
|
+
* `badgeLabel` is what a screen reader hears for it ("3 unread"); without it the badge text is read after the label.
|
|
43
|
+
* `avatar` shows the picture (or the initials) of a person: for the user menu, usually with `align: "end"` and children.
|
|
44
|
+
* An item with `children` opens a group; a child can have children too (a group inside a group, any depth).
|
|
45
|
+
* @param {(item: MenuItem, event: Event) => void} [options.onNavigate] called when a PERSON uses an item (click, Enter, Space).
|
|
46
|
+
* It is never called by select() or while the menu is built.
|
|
47
|
+
* @param {string} [options.active] id of the item that is the current page at the start
|
|
48
|
+
* @param {boolean} [options.followHash=false] the item whose `href` equals location.hash is the active one, and follows hashchange
|
|
49
|
+
* @param {"bar"|"bottom-tabs"|"responsive"} [options.layout="bar"] "bottom-tabs": a bar of tabs at the bottom of the screen,
|
|
50
|
+
* groups open as sheets above it. "responsive": a bar on wide screens, bottom tabs on a tablet or phone (max 700px).
|
|
51
|
+
* @param {number} [options.breakpoint=700] layout "responsive": the screen width in px up to which the menu is a bar of tabs at the bottom
|
|
52
|
+
* @param {string} [options.label] accessible name of the navigation
|
|
53
|
+
*
|
|
54
|
+
* Colours: on a coloured header set --athena-menu-bg, --athena-menu-fg, --athena-menu-hover and --athena-menu-active (the line under the active item)
|
|
55
|
+
* on the menu or on a parent (they default to a transparent bar, the colour of the text, --athena-hover and --athena-accent).
|
|
56
|
+
* @param {string} [options.id]
|
|
57
|
+
* @param {string} [options.class]
|
|
58
|
+
* @returns {{element: HTMLElement, select: (id: string|null) => void, getActive: () => string|null, setBadge: (id: string, badge) => void,
|
|
59
|
+
* close: Function, update: (changes?: {items?: object[]}) => object, destroy: Function}}
|
|
60
|
+
*/
|
|
61
|
+
export function generateMenu(options = {}) {
|
|
62
|
+
const layout = options.layout ?? "bar";
|
|
63
|
+
let items = options.items ?? [];
|
|
64
|
+
let active = null;
|
|
65
|
+
const nodes = new Map(); // id -> { item, control, li, parent (id) }
|
|
66
|
+
let openGroups = []; // from the outermost to the innermost: { toggle, sheet, depth }
|
|
67
|
+
|
|
68
|
+
const list = el("ul", { class: "athena-menu-list" });
|
|
69
|
+
const element = el("nav", {
|
|
70
|
+
id: options.id,
|
|
71
|
+
class: cx("athena-menu", `athena-menu-${layout}`, options.class),
|
|
72
|
+
attrs: { "aria-label": options.label ?? translate("menu") }
|
|
73
|
+
}, list);
|
|
74
|
+
const doc = element.ownerDocument;
|
|
75
|
+
const view = doc.defaultView;
|
|
76
|
+
// layout "responsive": tabs at the bottom up to the breakpoint (the class is-tabs carries the style)
|
|
77
|
+
let tabsQuery = null;
|
|
78
|
+
const tabsChanged = () => element.classList.toggle("is-tabs", tabsQuery.matches);
|
|
79
|
+
if (layout === "responsive" && view?.matchMedia) {
|
|
80
|
+
tabsQuery = view.matchMedia(`(max-width: ${Number(options.breakpoint) > 0 ? Number(options.breakpoint) : 700}px)`);
|
|
81
|
+
tabsQuery.addEventListener("change", tabsChanged);
|
|
82
|
+
tabsChanged();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const badgeNode = badge => {
|
|
86
|
+
const spec = badge != null && typeof badge === "object" ? badge : { text: badge };
|
|
87
|
+
const text = badgeText(spec.text);
|
|
88
|
+
return text ? el("span", { class: cx("menu-badge", spec.tone && `menu-badge-${spec.tone}`), text }) : null;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const content = item => {
|
|
92
|
+
const parts = [];
|
|
93
|
+
if (item.avatar) {
|
|
94
|
+
const avatar = el("span", { class: "menu-avatar", attrs: { "aria-hidden": "true" } });
|
|
95
|
+
if (item.avatar.src && isSafeUrl(item.avatar.src)) avatar.append(el("img", { attrs: { src: item.avatar.src, alt: "" } }));
|
|
96
|
+
else avatar.textContent = initialsOf(item.avatar.name);
|
|
97
|
+
parts.push(avatar);
|
|
98
|
+
}
|
|
99
|
+
if (item.icon) parts.push(setContent(el("span", { class: "menu-icon", attrs: { "aria-hidden": "true" } }), item.icon));
|
|
100
|
+
parts.push(el("span", { class: "menu-label", text: item.label }));
|
|
101
|
+
const badge = badgeNode(item.badge);
|
|
102
|
+
if (badge) parts.push(badge);
|
|
103
|
+
return parts;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
// A link when there is an address, a button otherwise.
|
|
107
|
+
const control = (item, inGroup) => {
|
|
108
|
+
const attrs = { "data-id": item.id, role: inGroup ? "menuitem" : null };
|
|
109
|
+
const node = item.href && !item.children?.length
|
|
110
|
+
? el("a", { class: cx("menu-control", inGroup && "menu-sub-control"), attrs: { ...attrs, href: item.href } }, ...content(item))
|
|
111
|
+
: el("button", { class: cx("menu-control", inGroup && "menu-sub-control"), attrs: { ...attrs, type: "button", disabled: item.disabled } }, ...content(item));
|
|
112
|
+
if (item.disabled && node.tagName === "A") {
|
|
113
|
+
node.setAttribute("aria-disabled", "true");
|
|
114
|
+
node.removeAttribute("href");
|
|
115
|
+
}
|
|
116
|
+
if (item.badgeLabel) node.setAttribute("aria-label", `${item.label}, ${item.badgeLabel}`);
|
|
117
|
+
if (inGroup) node.tabIndex = -1;
|
|
118
|
+
return node;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/* ------------------GROUPS (any depth)------------------- */
|
|
122
|
+
|
|
123
|
+
function closeFrom(depth, restoreFocus = false) {
|
|
124
|
+
const closing = openGroups.filter(group => group.depth >= depth);
|
|
125
|
+
if (!closing.length) return;
|
|
126
|
+
for (const { toggle, sheet } of closing.reverse()) {
|
|
127
|
+
sheet.hidden = true;
|
|
128
|
+
toggle.setAttribute("aria-expanded", "false");
|
|
129
|
+
}
|
|
130
|
+
const outermost = closing.at(-1);
|
|
131
|
+
openGroups = openGroups.filter(group => group.depth < depth);
|
|
132
|
+
if (restoreFocus) outermost.toggle.focus();
|
|
133
|
+
if (!openGroups.length) doc.removeEventListener("pointerdown", onOutside);
|
|
134
|
+
}
|
|
135
|
+
const closeAll = (restoreFocus = false) => closeFrom(0, restoreFocus);
|
|
136
|
+
const onOutside = event => { if (!element.contains(event.target)) closeAll(); };
|
|
137
|
+
|
|
138
|
+
function openSheet(toggle, sheet, depth, focusFirst) {
|
|
139
|
+
closeFrom(depth); // siblings and everything deeper than this group close first
|
|
140
|
+
sheet.hidden = false;
|
|
141
|
+
toggle.setAttribute("aria-expanded", "true");
|
|
142
|
+
if (!openGroups.length) doc.addEventListener("pointerdown", onOutside);
|
|
143
|
+
openGroups.push({ toggle, sheet, depth });
|
|
144
|
+
if (focusFirst) sheet.querySelector(`:scope > li > ${ENABLED}`)?.focus();
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Builds a group: the toggle (a button) and its sheet, which holds the children (which may be groups again).
|
|
148
|
+
function buildGroup(item, depth, toggle) {
|
|
149
|
+
const sheetId = uid("menu-sheet");
|
|
150
|
+
const sheet = el("ul", { id: sheetId, class: cx("menu-sheet", depth > 0 && "menu-sheet-nested"), attrs: { role: "menu", hidden: true, "aria-label": item.label } });
|
|
151
|
+
toggle.setAttribute("aria-haspopup", "menu");
|
|
152
|
+
toggle.setAttribute("aria-expanded", "false");
|
|
153
|
+
toggle.setAttribute("aria-controls", sheetId);
|
|
154
|
+
toggle.append(el("span", { class: "menu-caret", text: depth > 0 ? "▸" : "▾", attrs: { "aria-hidden": "true" } }));
|
|
155
|
+
toggle.addEventListener("click", () => (sheet.hidden ? openSheet(toggle, sheet, depth, depth > 0) : closeFrom(depth)));
|
|
156
|
+
toggle.addEventListener("keydown", event => {
|
|
157
|
+
const opens = depth === 0 ? ["ArrowDown", "ArrowUp"] : ["ArrowRight"];
|
|
158
|
+
if (opens.includes(event.key)) {
|
|
159
|
+
event.preventDefault();
|
|
160
|
+
event.stopPropagation();
|
|
161
|
+
openSheet(toggle, sheet, depth, true);
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
for (const child of item.children) sheet.append(buildItem(child, depth + 1, item.id, "menuitem"));
|
|
165
|
+
sheet.addEventListener("keydown", event => {
|
|
166
|
+
// Only the innermost sheet reacts: the event comes from an item that is a direct child of this sheet
|
|
167
|
+
if (event.target.closest(".menu-sheet") !== sheet) return;
|
|
168
|
+
const entries = [...sheet.querySelectorAll(`:scope > li > ${ENABLED}`)];
|
|
169
|
+
const index = entries.indexOf(doc.activeElement);
|
|
170
|
+
if (event.key === "ArrowDown") entries[(index + 1) % entries.length]?.focus();
|
|
171
|
+
else if (event.key === "ArrowUp") entries[(index - 1 + entries.length) % entries.length]?.focus();
|
|
172
|
+
else if (event.key === "Home") entries[0]?.focus();
|
|
173
|
+
else if (event.key === "End") entries.at(-1)?.focus();
|
|
174
|
+
else if (event.key === "Escape" || (event.key === "ArrowLeft" && depth > 0)) closeFrom(depth, true);
|
|
175
|
+
else if (event.key === "Tab") closeAll();
|
|
176
|
+
else return;
|
|
177
|
+
if (event.key !== "Tab") {
|
|
178
|
+
event.preventDefault();
|
|
179
|
+
event.stopPropagation();
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
return sheet;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function buildItem(item, depth, parentId, role) {
|
|
186
|
+
const li = el("li", { class: cx("menu-item", depth === 0 && item.align === "end" && "menu-item-end", item.children?.length && "has-submenu"), attrs: { role: role === "menuitem" ? "none" : null } });
|
|
187
|
+
const node = control(item, depth > 0);
|
|
188
|
+
nodes.set(item.id, { item, control: node, li, parent: parentId });
|
|
189
|
+
if (item.children?.length) {
|
|
190
|
+
li.append(node, buildGroup(item, depth, node));
|
|
191
|
+
} else {
|
|
192
|
+
node.addEventListener("click", event => {
|
|
193
|
+
if (item.disabled) return;
|
|
194
|
+
if (depth > 0) closeAll(true);
|
|
195
|
+
options.onNavigate?.(item, event);
|
|
196
|
+
});
|
|
197
|
+
li.append(node);
|
|
198
|
+
}
|
|
199
|
+
return li;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function build() {
|
|
203
|
+
closeAll();
|
|
204
|
+
list.replaceChildren();
|
|
205
|
+
nodes.clear();
|
|
206
|
+
for (const item of items) list.append(buildItem(item, 0, null, null));
|
|
207
|
+
// Arrow keys move along the top level, like a toolbar (Tab still reaches every item).
|
|
208
|
+
list.onkeydown = event => {
|
|
209
|
+
if (event.key !== "ArrowRight" && event.key !== "ArrowLeft") return;
|
|
210
|
+
if (event.target.closest(".menu-sheet")) return;
|
|
211
|
+
const tops = [...list.querySelectorAll(":scope > li > .menu-control:not(:disabled)")];
|
|
212
|
+
const index = tops.indexOf(event.target);
|
|
213
|
+
if (index < 0) return;
|
|
214
|
+
event.preventDefault();
|
|
215
|
+
tops[(index + (event.key === "ArrowRight" ? 1 : -1) + tops.length) % tops.length].focus();
|
|
216
|
+
};
|
|
217
|
+
select(active);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Marks the current page: class is-active and aria-current="page" on the item (and has-active on every group around it). Does not call onNavigate. */
|
|
221
|
+
function select(id) {
|
|
222
|
+
active = id ?? null;
|
|
223
|
+
for (const { control: node, li } of nodes.values()) {
|
|
224
|
+
node.classList.remove("is-active");
|
|
225
|
+
node.removeAttribute("aria-current");
|
|
226
|
+
li.classList.remove("has-active");
|
|
227
|
+
}
|
|
228
|
+
const found = nodes.get(active);
|
|
229
|
+
if (!found) return;
|
|
230
|
+
found.control.classList.add("is-active");
|
|
231
|
+
found.control.setAttribute("aria-current", "page");
|
|
232
|
+
for (let parent = found.parent; parent; parent = nodes.get(parent)?.parent) nodes.get(parent)?.li.classList.add("has-active");
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Changes the badge of an item (a number, text, { text, tone }, or 0 / null to remove it) without building the menu again. */
|
|
236
|
+
function setBadge(id, badge) {
|
|
237
|
+
const found = nodes.get(id);
|
|
238
|
+
if (!found) return;
|
|
239
|
+
found.item.badge = badge;
|
|
240
|
+
found.control.querySelector(".menu-badge")?.remove();
|
|
241
|
+
const node = badgeNode(badge);
|
|
242
|
+
if (node) found.control.querySelector(".menu-caret") ? found.control.querySelector(".menu-caret").before(node) : found.control.append(node);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const fromHash = () => {
|
|
246
|
+
const hash = view.location.hash;
|
|
247
|
+
const match = [...nodes.values()].find(({ item }) => item.href && item.href === hash);
|
|
248
|
+
if (match) select(match.item.id);
|
|
249
|
+
};
|
|
250
|
+
if (options.followHash) view.addEventListener("hashchange", fromHash);
|
|
251
|
+
|
|
252
|
+
active = options.active ?? null;
|
|
253
|
+
build();
|
|
254
|
+
if (options.followHash) fromHash();
|
|
255
|
+
mount(options.parent, element);
|
|
256
|
+
|
|
257
|
+
return asHandle({
|
|
258
|
+
element,
|
|
259
|
+
select,
|
|
260
|
+
setBadge,
|
|
261
|
+
getActive: () => active,
|
|
262
|
+
close: closeAll,
|
|
263
|
+
update(changes = {}) {
|
|
264
|
+
if (changes.items) {
|
|
265
|
+
items = changes.items;
|
|
266
|
+
build();
|
|
267
|
+
if (options.followHash) fromHash();
|
|
268
|
+
}
|
|
269
|
+
return this;
|
|
270
|
+
},
|
|
271
|
+
destroy() {
|
|
272
|
+
closeAll();
|
|
273
|
+
view.removeEventListener("hashchange", fromHash);
|
|
274
|
+
tabsQuery?.removeEventListener("change", tabsChanged);
|
|
275
|
+
element.remove();
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
}
|