material 3.15.3 → 3.16.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 +2 -2
- package/dist/components/drawer/api.js +14 -0
- package/dist/components/drawer/config.js +5 -0
- package/dist/components/drawer/constants.d.ts +10 -0
- package/dist/components/drawer/constants.js +10 -0
- package/dist/components/drawer/drawer.js +10 -3
- package/dist/components/drawer/features/items.js +252 -109
- package/dist/components/drawer/index.d.ts +31 -2
- package/dist/elements/css/drawer.css +1 -1
- package/dist/elements/drawer.js +76 -18
- package/dist/elements/index.d.ts +34 -1
- package/dist/elements/preupgrade/badge.css +1 -1
- package/dist/elements/preupgrade/bottom-app-bar.css +1 -1
- package/dist/elements/preupgrade/bottom-sheet.css +1 -1
- package/dist/elements/preupgrade/button-group.css +1 -1
- package/dist/elements/preupgrade/button.css +1 -1
- package/dist/elements/preupgrade/card.css +1 -1
- package/dist/elements/preupgrade/carousel.css +1 -1
- package/dist/elements/preupgrade/checkbox.css +1 -1
- package/dist/elements/preupgrade/chips.css +1 -1
- package/dist/elements/preupgrade/datepicker.css +1 -1
- package/dist/elements/preupgrade/dialog.css +1 -1
- package/dist/elements/preupgrade/divider.css +1 -1
- package/dist/elements/preupgrade/drawer.css +2 -2
- package/dist/elements/preupgrade/extended-fab.css +1 -1
- package/dist/elements/preupgrade/fab-menu.css +1 -1
- package/dist/elements/preupgrade/fab.css +1 -1
- package/dist/elements/preupgrade/icon-button.css +1 -1
- package/dist/elements/preupgrade/list.css +1 -1
- package/dist/elements/preupgrade/loading-indicator.css +1 -1
- package/dist/elements/preupgrade/menu.css +1 -1
- package/dist/elements/preupgrade/navigation-bar.css +1 -1
- package/dist/elements/preupgrade/navigation-rail.css +1 -1
- package/dist/elements/preupgrade/progress.css +1 -1
- package/dist/elements/preupgrade/radios.css +1 -1
- package/dist/elements/preupgrade/search.css +1 -1
- package/dist/elements/preupgrade/select.css +1 -1
- package/dist/elements/preupgrade/side-sheet.css +1 -1
- package/dist/elements/preupgrade/slider.css +1 -1
- package/dist/elements/preupgrade/snackbar.css +1 -1
- package/dist/elements/preupgrade/split-button.css +1 -1
- package/dist/elements/preupgrade/switch.css +1 -1
- package/dist/elements/preupgrade/tabs.css +1 -1
- package/dist/elements/preupgrade/text-field.css +1 -1
- package/dist/elements/preupgrade/timepicker.css +1 -1
- package/dist/elements/preupgrade/toolbar.css +1 -1
- package/dist/elements/preupgrade/tooltip.css +1 -1
- package/dist/elements/preupgrade/top-app-bar.css +1 -1
- package/dist/elements/preupgrade.css +2 -2
- package/dist/elements/styles.js +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/solid/jsx.d.ts +9 -2
- package/dist/styles/badge.css +1 -1
- package/dist/styles/base-elements.css +1 -1
- package/dist/styles/base-lite.css +1 -1
- package/dist/styles/base.css +1 -1
- package/dist/styles/bottom-app-bar-toolbar.css +1 -1
- package/dist/styles/bottom-app-bar.css +1 -1
- package/dist/styles/bottom-sheet.css +1 -1
- package/dist/styles/button-filled-small-round.css +1 -1
- package/dist/styles/button-group.css +1 -1
- package/dist/styles/button.css +1 -1
- package/dist/styles/card.css +1 -1
- package/dist/styles/carousel.css +1 -1
- package/dist/styles/checkbox.css +1 -1
- package/dist/styles/chips.css +1 -1
- package/dist/styles/contrast.css +1 -1
- package/dist/styles/datepicker.css +1 -1
- package/dist/styles/dialog.css +1 -1
- package/dist/styles/divider.css +1 -1
- package/dist/styles/drawer.css +2 -2
- package/dist/styles/extended-fab.css +1 -1
- package/dist/styles/fab-menu.css +1 -1
- package/dist/styles/fab.css +1 -1
- package/dist/styles/icon-button.css +1 -1
- package/dist/styles/list.css +1 -1
- package/dist/styles/listbox.css +1 -1
- package/dist/styles/loading-indicator.css +1 -1
- package/dist/styles/menu.css +1 -1
- package/dist/styles/navigation-bar.css +1 -1
- package/dist/styles/navigation-rail.css +1 -1
- package/dist/styles/progress.css +1 -1
- package/dist/styles/radios.css +1 -1
- package/dist/styles/search.css +1 -1
- package/dist/styles/select.css +1 -1
- package/dist/styles/side-sheet.css +1 -1
- package/dist/styles/slider.css +1 -1
- package/dist/styles/snackbar.css +1 -1
- package/dist/styles/split-button.css +1 -1
- package/dist/styles/switch.css +1 -1
- package/dist/styles/tabs.css +1 -1
- package/dist/styles/text-field.css +1 -1
- package/dist/styles/timepicker.css +1 -1
- package/dist/styles/toolbar.css +1 -1
- package/dist/styles/tooltip.css +1 -1
- package/dist/styles/top-app-bar.css +1 -1
- package/dist/styles/typography.css +1 -1
- package/dist/styles/utilities.css +1 -1
- package/dist/styles.css +2 -2
- package/dist/themes/autumn-contrast.css +1 -1
- package/dist/themes/autumn.css +1 -1
- package/dist/themes/baseline-contrast.css +1 -1
- package/dist/themes/baseline.css +1 -1
- package/dist/themes/brownbeige-contrast.css +1 -1
- package/dist/themes/brownbeige.css +1 -1
- package/dist/themes/content-contrast.css +1 -1
- package/dist/themes/content.css +1 -1
- package/dist/themes/desert-contrast.css +1 -1
- package/dist/themes/desert.css +1 -1
- package/dist/themes/expressive-contrast.css +1 -1
- package/dist/themes/expressive.css +1 -1
- package/dist/themes/fidelity-contrast.css +1 -1
- package/dist/themes/fidelity.css +1 -1
- package/dist/themes/forest-contrast.css +1 -1
- package/dist/themes/forest.css +1 -1
- package/dist/themes/fruit-salad-contrast.css +1 -1
- package/dist/themes/fruit-salad.css +1 -1
- package/dist/themes/highcontrast-contrast.css +1 -1
- package/dist/themes/highcontrast.css +1 -1
- package/dist/themes/monochrome-contrast.css +1 -1
- package/dist/themes/monochrome.css +1 -1
- package/dist/themes/neutral-contrast.css +1 -1
- package/dist/themes/neutral.css +1 -1
- package/dist/themes/ocean-contrast.css +1 -1
- package/dist/themes/ocean.css +1 -1
- package/dist/themes/rainbow-contrast.css +1 -1
- package/dist/themes/rainbow.css +1 -1
- package/dist/themes/sageivory-contrast.css +1 -1
- package/dist/themes/sageivory.css +1 -1
- package/dist/themes/spring-contrast.css +1 -1
- package/dist/themes/spring.css +1 -1
- package/dist/themes/summer-contrast.css +1 -1
- package/dist/themes/summer.css +1 -1
- package/dist/themes/sunset-contrast.css +1 -1
- package/dist/themes/sunset.css +1 -1
- package/dist/themes/tealcaramel-contrast.css +1 -1
- package/dist/themes/tealcaramel.css +1 -1
- package/dist/themes/vibrant-contrast.css +1 -1
- package/dist/themes/vibrant.css +1 -1
- package/dist/tokens/catalogue.json +12 -3
- package/package.json +1 -1
- package/src/styles/components/_drawer.scss +75 -7
- package/src/styles/elements/_drawer.scss +1 -1
package/README.md
CHANGED
|
@@ -138,11 +138,11 @@ What a bundler keeps, minified and gzipped (kB is 1,000 bytes), measured from th
|
|
|
138
138
|
| `createButton` from `material`, with code splitting: the initial chunks (the progress indicator loads on demand) | 7.5 kB |
|
|
139
139
|
| `createButton`, without code splitting (the progress indicator included) | 13.9 kB |
|
|
140
140
|
| `createButton`, `createTextField` and `createCheckbox`, without code splitting | 20.3 kB |
|
|
141
|
-
| Everything the root exports (`export * from 'material'`), without code splitting |
|
|
141
|
+
| Everything the root exports (`export * from 'material'`), without code splitting | 133.5 kB |
|
|
142
142
|
| `material/styles/base` | 3.1 kB |
|
|
143
143
|
| `material/styles/base-lite` | 1.7 kB |
|
|
144
144
|
| `material/styles/base` and `material/styles/button` | 5.4 kB |
|
|
145
|
-
| `material/styles`, the full stylesheet | 60.
|
|
145
|
+
| `material/styles`, the full stylesheet | 60.4 kB |
|
|
146
146
|
<!-- /sizes -->
|
|
147
147
|
|
|
148
148
|
`material` publishes ESM only, so bundlers drop what you do not import. Each component's page on [md3.io/components](https://md3.io/components/) gives its size.
|
|
@@ -61,6 +61,20 @@ setBadge(id, badge) {
|
|
|
61
61
|
items.setBadge(id, badge);
|
|
62
62
|
return drawerComponent;
|
|
63
63
|
},
|
|
64
|
+
expandSection(id) {
|
|
65
|
+
items.expandSection(id);
|
|
66
|
+
return drawerComponent;
|
|
67
|
+
},
|
|
68
|
+
collapseSection(id) {
|
|
69
|
+
items.collapseSection(id);
|
|
70
|
+
return drawerComponent;
|
|
71
|
+
},
|
|
72
|
+
toggleSection(id) {
|
|
73
|
+
items.toggleSection(id);
|
|
74
|
+
return drawerComponent;
|
|
75
|
+
},
|
|
76
|
+
isSectionExpanded: items.isSectionExpanded,
|
|
77
|
+
getExpandedSections: items.getExpandedSections,
|
|
64
78
|
setHeadline(text) {
|
|
65
79
|
headline.setHeadline(text);
|
|
66
80
|
return drawerComponent;
|
|
@@ -55,6 +55,11 @@ getActive: () => comp.drawerItems.getActive(),
|
|
|
55
55
|
setItems: (items) => comp.drawerItems.setItems(items || []),
|
|
56
56
|
getItems: () => comp.drawerItems.getItems(),
|
|
57
57
|
setBadge: (id, badge) => comp.drawerItems.setBadge(id, badge),
|
|
58
|
+
expandSection: comp.drawerItems.expandSection,
|
|
59
|
+
collapseSection: comp.drawerItems.collapseSection,
|
|
60
|
+
toggleSection: comp.drawerItems.toggleSection,
|
|
61
|
+
isSectionExpanded: comp.drawerItems.isSectionExpanded,
|
|
62
|
+
getExpandedSections: comp.drawerItems.getExpandedSections,
|
|
58
63
|
},
|
|
59
64
|
headline: {
|
|
60
65
|
setHeadline: (text) => comp.drawerHeadline.setHeadline(text),
|
|
@@ -29,6 +29,8 @@ export declare const DRAWER_EVENTS: {
|
|
|
29
29
|
readonly CLOSE: "close";
|
|
30
30
|
/** Fired when a navigation item is selected */
|
|
31
31
|
readonly SELECT: "select";
|
|
32
|
+
readonly EXPAND: "expand";
|
|
33
|
+
readonly COLLAPSE: "collapse";
|
|
32
34
|
};
|
|
33
35
|
/** CSS classes used by the drawer component */
|
|
34
36
|
export declare const DRAWER_CLASSES: {
|
|
@@ -50,6 +52,14 @@ export declare const DRAWER_CLASSES: {
|
|
|
50
52
|
readonly ITEM_LABEL: "drawer__item-label";
|
|
51
53
|
/** Item badge */
|
|
52
54
|
readonly ITEM_BADGE: "drawer__item-badge";
|
|
55
|
+
readonly GROUP: "drawer__group";
|
|
56
|
+
readonly GROUP_ITEMS: "drawer__group-items";
|
|
57
|
+
readonly GROUP_ITEMS_INNER: "drawer__group-items-inner";
|
|
58
|
+
readonly ITEM_EXPANDABLE: "drawer__item--expandable";
|
|
59
|
+
readonly ITEM_EXPANDED: "drawer__item--expanded";
|
|
60
|
+
readonly ITEM_CHILD: "drawer__item--child";
|
|
61
|
+
readonly ITEM_HAS_ACTIVE: "drawer__item--has-active";
|
|
62
|
+
readonly ITEM_CHEVRON: "drawer__item-chevron";
|
|
53
63
|
/** Active indicator shape */
|
|
54
64
|
readonly ACTIVE_INDICATOR: "drawer__active-indicator";
|
|
55
65
|
/** Dense/compact variant */
|
|
@@ -15,6 +15,8 @@ export const DRAWER_EVENTS = {
|
|
|
15
15
|
OPEN: "open",
|
|
16
16
|
CLOSE: "close",
|
|
17
17
|
SELECT: "select",
|
|
18
|
+
EXPAND: "expand",
|
|
19
|
+
COLLAPSE: "collapse",
|
|
18
20
|
};
|
|
19
21
|
export const DRAWER_CLASSES = {
|
|
20
22
|
ROOT: "drawer",
|
|
@@ -26,6 +28,14 @@ ITEM: "drawer__item",
|
|
|
26
28
|
ITEM_ICON: "drawer__item-icon",
|
|
27
29
|
ITEM_LABEL: "drawer__item-label",
|
|
28
30
|
ITEM_BADGE: "drawer__item-badge",
|
|
31
|
+
GROUP: "drawer__group",
|
|
32
|
+
GROUP_ITEMS: "drawer__group-items",
|
|
33
|
+
GROUP_ITEMS_INNER: "drawer__group-items-inner",
|
|
34
|
+
ITEM_EXPANDABLE: "drawer__item--expandable",
|
|
35
|
+
ITEM_EXPANDED: "drawer__item--expanded",
|
|
36
|
+
ITEM_CHILD: "drawer__item--child",
|
|
37
|
+
ITEM_HAS_ACTIVE: "drawer__item--has-active",
|
|
38
|
+
ITEM_CHEVRON: "drawer__item-chevron",
|
|
29
39
|
ACTIVE_INDICATOR: "drawer__active-indicator",
|
|
30
40
|
DENSE: "drawer--dense",
|
|
31
41
|
DIVIDER: "drawer__divider",
|
|
@@ -5,10 +5,14 @@ import { withItems, withState, withHeadline } from "./features/index.js";
|
|
|
5
5
|
import { withAPI } from "./api.js";
|
|
6
6
|
import { createBaseConfig, getElementConfig, getApiConfig } from "./config.js";
|
|
7
7
|
import { DRAWER_DEFAULTS } from "./constants.js";
|
|
8
|
-
|
|
8
|
+
export default function createDrawer(config = {}) {
|
|
9
|
+
return buildDrawer(config, false);
|
|
10
|
+
}
|
|
11
|
+
export const createShadowDrawer = (config) => buildDrawer(config, true);
|
|
12
|
+
const buildDrawer = (config, shadow) => {
|
|
9
13
|
const baseConfig = createBaseConfig(config);
|
|
10
14
|
try {
|
|
11
|
-
const drawer = pipe(createBase, withEvents(), withElement(getElementConfig(baseConfig)), withVariant(baseConfig), withHeadline(baseConfig), withItems(baseConfig), withState(baseConfig), withLifecycle(), (component) => {
|
|
15
|
+
const drawer = pipe(createBase, withEvents(), withElement(getElementConfig(baseConfig)), withVariant(baseConfig), withHeadline(baseConfig), withItems(baseConfig, shadow), withState(baseConfig), withLifecycle(), (component) => {
|
|
12
16
|
const sheetEl = document.createElement("div");
|
|
13
17
|
sheetEl.className = component.getClass("drawer__sheet");
|
|
14
18
|
const width = baseConfig.width;
|
|
@@ -38,6 +42,10 @@ if (baseConfig.onOpen)
|
|
|
38
42
|
drawer.on("open", baseConfig.onOpen);
|
|
39
43
|
if (baseConfig.onClose)
|
|
40
44
|
drawer.on("close", baseConfig.onClose);
|
|
45
|
+
if (baseConfig.onExpand)
|
|
46
|
+
drawer.on("expand", baseConfig.onExpand);
|
|
47
|
+
if (baseConfig.onCollapse)
|
|
48
|
+
drawer.on("collapse", baseConfig.onCollapse);
|
|
41
49
|
return drawer;
|
|
42
50
|
}
|
|
43
51
|
catch (error) {
|
|
@@ -45,4 +53,3 @@ console.error("Drawer creation error:", error);
|
|
|
45
53
|
throw new Error(`Failed to create drawer: ${error.message}`);
|
|
46
54
|
}
|
|
47
55
|
};
|
|
48
|
-
export default createDrawer;
|
|
@@ -1,18 +1,39 @@
|
|
|
1
|
-
import { DRAWER_EVENTS } from "../constants.js";
|
|
1
|
+
import { DRAWER_CLASSES, DRAWER_EVENTS } from "../constants.js";
|
|
2
2
|
import { createRipple, } from "../../../core/compose/features/ripple.js";
|
|
3
3
|
import { setHTML } from "../../../core/dom/html.js";
|
|
4
4
|
import { activeElementOf } from "../../../core/dom/focus.js";
|
|
5
|
+
import { getIcon } from "../../../core/icons/index.js";
|
|
6
|
+
import { safeUrl } from "../../../core/utils/url.js";
|
|
7
|
+
let drawerCount = 0;
|
|
8
|
+
const DEFAULT_CHEVRON = '<svg width="24" height="24" viewBox="0 0 24 24" aria-hidden="true"><path fill="currentColor" d="M16.59 8.59 12 13.17 7.41 8.59 6 10l6 6 6-6z"/></svg>';
|
|
9
|
+
const isDestination = (item) => item.type !== "divider" && item.type !== "section";
|
|
10
|
+
const isSection = (item) => item.type !== "divider" && !!item.items?.length;
|
|
11
|
+
const hasModifiers = (event) => event.ctrlKey || event.metaKey || event.shiftKey || event.altKey;
|
|
12
|
+
const enabled = (element) => !element.matches('[disabled], [aria-disabled="true"]');
|
|
13
|
+
const visible = (element) => enabled(element) && !element.closest("[inert], [hidden]");
|
|
14
|
+
const initialActive = (items) => items.flatMap(item => isSection(item) ? item.items : [item])
|
|
15
|
+
.find(item => isDestination(item) && item.active && !item.disabled)?.id || null;
|
|
5
16
|
const hasBadge = (badge) => !!badge && badge !== "0";
|
|
6
17
|
const createItemElement = (item, getClass) => {
|
|
7
|
-
const
|
|
8
|
-
el
|
|
9
|
-
el.
|
|
18
|
+
const { href } = item;
|
|
19
|
+
const el = document.createElement(href === undefined ? "button" : "a");
|
|
20
|
+
el.className = getClass(DRAWER_CLASSES.ITEM);
|
|
21
|
+
if (href !== undefined) {
|
|
22
|
+
if (item.disabled)
|
|
23
|
+
el.setAttribute("role", "link");
|
|
24
|
+
else
|
|
25
|
+
el.setAttribute("href", safeUrl(href) || (href ? "about:blank" : ""));
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
el.setAttribute("type", "button");
|
|
29
|
+
if (item.disabled)
|
|
30
|
+
el.setAttribute("disabled", "");
|
|
31
|
+
}
|
|
10
32
|
el.setAttribute("tabindex", item.disabled ? "-1" : "0");
|
|
11
33
|
if (item.id) {
|
|
12
34
|
el.dataset.id = item.id;
|
|
13
35
|
}
|
|
14
36
|
if (item.disabled) {
|
|
15
|
-
el.disabled = true;
|
|
16
37
|
el.setAttribute("aria-disabled", "true");
|
|
17
38
|
}
|
|
18
39
|
const indicator = document.createElement("span");
|
|
@@ -55,157 +76,262 @@ el.className = getClass("drawer__section-label");
|
|
|
55
76
|
el.textContent = item.label || item.sectionLabel || "";
|
|
56
77
|
return el;
|
|
57
78
|
};
|
|
58
|
-
export const withItems = (config) => (component) => {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
79
|
+
export const withItems = (config, shadow) => (component) => {
|
|
80
|
+
const cls = component.getClass;
|
|
81
|
+
const itemsContainer = document.createElement("div");
|
|
82
|
+
itemsContainer.className = cls(DRAWER_CLASSES.ITEMS);
|
|
83
|
+
let currentItems = config.items ?? [];
|
|
84
|
+
let activeId = initialActive(currentItems);
|
|
85
|
+
let destinations = [];
|
|
86
|
+
let instance;
|
|
87
|
+
const sections = new Map();
|
|
88
|
+
const chevronMarkup = getIcon("expand-more", DEFAULT_CHEVRON);
|
|
62
89
|
const ripple = config.ripple !== false ? createRipple() : null;
|
|
63
90
|
let rippled = [];
|
|
64
91
|
const unmountRipples = () => {
|
|
65
|
-
rippled.forEach(
|
|
92
|
+
rippled.forEach(element => ripple?.unmount(element));
|
|
66
93
|
rippled = [];
|
|
67
94
|
};
|
|
68
|
-
const
|
|
69
|
-
if (
|
|
70
|
-
|
|
95
|
+
const mountRipple = (element, disabled) => {
|
|
96
|
+
if (!ripple || disabled)
|
|
97
|
+
return;
|
|
98
|
+
ripple.mount(element);
|
|
99
|
+
rippled.push(element);
|
|
100
|
+
};
|
|
101
|
+
const paintSection = (section, expanded) => {
|
|
102
|
+
section.expanded = expanded;
|
|
103
|
+
section.button.setAttribute("aria-expanded", String(expanded));
|
|
104
|
+
section.button.classList.toggle(cls(DRAWER_CLASSES.ITEM_EXPANDED), expanded);
|
|
105
|
+
section.group.classList.toggle(cls("drawer__group--expanded"), expanded);
|
|
106
|
+
section.panel.toggleAttribute("inert", !expanded);
|
|
107
|
+
};
|
|
108
|
+
const paintParent = (section, active) => {
|
|
109
|
+
section.button.classList.toggle(cls(DRAWER_CLASSES.ITEM_HAS_ACTIVE), active);
|
|
110
|
+
};
|
|
111
|
+
const setExpanded = (section, expanded, event) => {
|
|
112
|
+
if (!section || section.expanded === expanded)
|
|
113
|
+
return;
|
|
114
|
+
if (!expanded && section.panel.contains(activeElementOf(itemsContainer))) {
|
|
115
|
+
(enabled(section.button) ? section.button : component.element).focus();
|
|
71
116
|
}
|
|
72
|
-
|
|
73
|
-
|
|
117
|
+
paintSection(section, expanded);
|
|
118
|
+
const detail = {
|
|
119
|
+
id: section.id, value: section.id, label: section.label, expanded,
|
|
120
|
+
...(event && { originalEvent: event }),
|
|
121
|
+
};
|
|
122
|
+
component.emit(expanded ? DRAWER_EVENTS.EXPAND : DRAWER_EVENTS.COLLAPSE, detail);
|
|
123
|
+
};
|
|
124
|
+
const mapItems = (map) => {
|
|
125
|
+
currentItems = currentItems.map(item => {
|
|
126
|
+
const next = map(item);
|
|
127
|
+
return isSection(item) ? { ...next, items: item.items.map(map) } : next;
|
|
128
|
+
});
|
|
129
|
+
};
|
|
130
|
+
const setActive = (id) => {
|
|
131
|
+
const target = id ? destinations.find(({ item }) => item.id === id && !item.disabled) : undefined;
|
|
132
|
+
if (id && !target)
|
|
74
133
|
return;
|
|
134
|
+
activeId = target ? id : null;
|
|
135
|
+
for (const row of destinations) {
|
|
136
|
+
const active = row === target;
|
|
137
|
+
row.element.classList.toggle(cls("drawer__item--active"), active);
|
|
138
|
+
if (active)
|
|
139
|
+
row.element.setAttribute("aria-current", "page");
|
|
140
|
+
else
|
|
141
|
+
row.element.removeAttribute("aria-current");
|
|
142
|
+
const icon = row.element.querySelector(`.${cls(DRAWER_CLASSES.ITEM_ICON)}`);
|
|
143
|
+
if (icon)
|
|
144
|
+
setHTML(icon, active && row.item.activeIcon ? row.item.activeIcon : row.item.icon);
|
|
145
|
+
}
|
|
146
|
+
for (const section of sections.values())
|
|
147
|
+
paintParent(section, section === target?.parent);
|
|
148
|
+
if (target?.parent && !target.parent.expanded)
|
|
149
|
+
paintSection(target.parent, true);
|
|
150
|
+
mapItems(item => ({ ...item, active: item.id === id }));
|
|
151
|
+
};
|
|
152
|
+
const renderItems = () => {
|
|
75
153
|
unmountRipples();
|
|
76
154
|
itemsContainer.replaceChildren();
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
155
|
+
destinations = [];
|
|
156
|
+
sections.clear();
|
|
157
|
+
const usedDomIds = new Set();
|
|
158
|
+
const namedSections = new Set(currentItems.filter(isSection).map(item => item.id));
|
|
159
|
+
let sectionIndex = 0;
|
|
160
|
+
const renderItem = (item, container, parent) => {
|
|
161
|
+
if (item.type === "divider") {
|
|
162
|
+
container.append(createDividerElement(cls));
|
|
82
163
|
return;
|
|
83
164
|
}
|
|
84
|
-
if (type === "section") {
|
|
85
|
-
|
|
165
|
+
if (item.type === "section") {
|
|
166
|
+
container.append(createSectionElement(item, cls));
|
|
86
167
|
return;
|
|
87
168
|
}
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
id: item.id,
|
|
96
|
-
value: item.id,
|
|
97
|
-
label: item.label || "",
|
|
98
|
-
index: parseInt(itemEl.dataset.navIndex || "0", 10),
|
|
99
|
-
originalEvent: e,
|
|
100
|
-
};
|
|
101
|
-
component.emit(DRAWER_EVENTS.SELECT, selectEvent);
|
|
169
|
+
const active = !!activeId && item.id === activeId && !item.disabled;
|
|
170
|
+
const element = createItemElement({ ...item, active }, cls);
|
|
171
|
+
const index = destinations.length;
|
|
172
|
+
element.dataset.navIndex = String(index);
|
|
173
|
+
if (parent) {
|
|
174
|
+
element.classList.add(cls(DRAWER_CLASSES.ITEM_CHILD));
|
|
175
|
+
element.dataset.parentId = parent.id;
|
|
102
176
|
}
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
177
|
+
destinations.push({ item, element, parent });
|
|
178
|
+
element.addEventListener("click", (event) => {
|
|
179
|
+
if (item.disabled) {
|
|
180
|
+
event.preventDefault();
|
|
181
|
+
return;
|
|
107
182
|
}
|
|
108
|
-
|
|
109
|
-
});
|
|
110
|
-
};
|
|
111
|
-
const swapIcon = (element, active) => {
|
|
112
|
-
const item = currentItems.find((entry) => entry.id === element.dataset.id);
|
|
113
|
-
const iconEl = element.querySelector(`.${component.getClass("drawer__item-icon")}`);
|
|
114
|
-
if (!item || !iconEl)
|
|
183
|
+
if (item.href !== undefined && (hasModifiers(event) || event.button !== 0))
|
|
115
184
|
return;
|
|
116
|
-
|
|
117
|
-
};
|
|
118
|
-
const setActive = (id) => {
|
|
119
|
-
if (!itemsContainer)
|
|
185
|
+
if (!item.id)
|
|
120
186
|
return;
|
|
121
|
-
|
|
122
|
-
const
|
|
123
|
-
|
|
124
|
-
|
|
187
|
+
setActive(item.id);
|
|
188
|
+
const detail = { id: item.id, value: item.id, label: item.label || "", index, originalEvent: event };
|
|
189
|
+
component.emit(DRAWER_EVENTS.SELECT, detail);
|
|
190
|
+
});
|
|
191
|
+
mountRipple(element, item.disabled);
|
|
192
|
+
container.append(element);
|
|
193
|
+
};
|
|
194
|
+
currentItems.forEach((item, index) => {
|
|
195
|
+
if (!isSection(item)) {
|
|
196
|
+
renderItem(item, itemsContainer);
|
|
125
197
|
return;
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
if (
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
198
|
+
}
|
|
199
|
+
let id = item.id;
|
|
200
|
+
if (!id) {
|
|
201
|
+
do {
|
|
202
|
+
id = String(sectionIndex++);
|
|
203
|
+
} while (namedSections.has(id));
|
|
204
|
+
}
|
|
205
|
+
const slug = id.replace(/[^\w-]+/g, "-");
|
|
206
|
+
const scope = shadow ? "" : `${instance ??= ++drawerCount}-`;
|
|
207
|
+
const base = cls(`drawer-sec-${scope}${slug}`);
|
|
208
|
+
let prefix = base;
|
|
209
|
+
for (let suffix = 1; usedDomIds.has(prefix); suffix++)
|
|
210
|
+
prefix = `${base}-${suffix}`;
|
|
211
|
+
usedDomIds.add(prefix);
|
|
212
|
+
const label = item.label || item.sectionLabel || "";
|
|
213
|
+
const group = document.createElement("div");
|
|
214
|
+
group.className = cls(DRAWER_CLASSES.GROUP);
|
|
215
|
+
group.dataset.sectionId = id;
|
|
216
|
+
const button = createItemElement({ ...item, id, label, href: undefined, active: false }, cls);
|
|
217
|
+
button.id = `${prefix}-btn`;
|
|
218
|
+
button.classList.add(cls(DRAWER_CLASSES.ITEM_EXPANDABLE));
|
|
219
|
+
const chevron = document.createElement("span");
|
|
220
|
+
chevron.className = cls(DRAWER_CLASSES.ITEM_CHEVRON);
|
|
221
|
+
chevron.setAttribute("aria-hidden", "true");
|
|
222
|
+
setHTML(chevron, chevronMarkup);
|
|
223
|
+
button.append(chevron);
|
|
224
|
+
const panel = document.createElement("div");
|
|
225
|
+
panel.className = cls(DRAWER_CLASSES.GROUP_ITEMS);
|
|
226
|
+
panel.id = `${prefix}-panel`;
|
|
227
|
+
panel.setAttribute("role", "group");
|
|
228
|
+
panel.setAttribute("aria-labelledby", button.id);
|
|
229
|
+
button.setAttribute("aria-controls", panel.id);
|
|
230
|
+
const inner = document.createElement("div");
|
|
231
|
+
inner.className = cls(DRAWER_CLASSES.GROUP_ITEMS_INNER);
|
|
232
|
+
panel.append(inner);
|
|
233
|
+
group.append(button, panel);
|
|
234
|
+
itemsContainer.append(group);
|
|
235
|
+
const section = { id, index, label, group, button, panel, expanded: false };
|
|
236
|
+
sections.set(id, section);
|
|
237
|
+
item.items.forEach(child => renderItem(child, inner, section));
|
|
238
|
+
const active = !!activeId && item.items.some(child => isDestination(child) && !child.disabled && child.id === activeId);
|
|
239
|
+
paintParent(section, active);
|
|
240
|
+
paintSection(section, item.expanded ?? active);
|
|
241
|
+
button.addEventListener("click", event => {
|
|
242
|
+
if (!item.disabled)
|
|
243
|
+
setExpanded(section, !section.expanded, event);
|
|
244
|
+
});
|
|
245
|
+
mountRipple(button, item.disabled);
|
|
133
246
|
});
|
|
134
|
-
activeId = target ? id : null;
|
|
135
|
-
currentItems = currentItems.map((item) => ({
|
|
136
|
-
...item,
|
|
137
|
-
active: item.id === id,
|
|
138
|
-
}));
|
|
139
247
|
};
|
|
140
|
-
const getActive = () => activeId;
|
|
141
248
|
const setItems = (items) => {
|
|
142
249
|
currentItems = items;
|
|
143
|
-
|
|
144
|
-
activeId = active?.id || null;
|
|
250
|
+
activeId = initialActive(items);
|
|
145
251
|
renderItems();
|
|
146
252
|
};
|
|
147
|
-
const getItems = () => currentItems;
|
|
148
253
|
const setBadge = (id, badge) => {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
if (!itemEl)
|
|
254
|
+
const section = sections.get(id);
|
|
255
|
+
const element = section?.button ?? destinations.find(row => row.element.dataset.id === id)?.element;
|
|
256
|
+
if (!element)
|
|
153
257
|
return;
|
|
154
|
-
let
|
|
258
|
+
let badgeElement = element.querySelector(`.${cls(DRAWER_CLASSES.ITEM_BADGE)}`);
|
|
155
259
|
if (hasBadge(badge)) {
|
|
156
|
-
if (!
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
260
|
+
if (!badgeElement) {
|
|
261
|
+
badgeElement = document.createElement("span");
|
|
262
|
+
badgeElement.className = cls(DRAWER_CLASSES.ITEM_BADGE);
|
|
263
|
+
element.insertBefore(badgeElement, element.querySelector(`.${cls(DRAWER_CLASSES.ITEM_CHEVRON)}`));
|
|
160
264
|
}
|
|
161
|
-
|
|
265
|
+
badgeElement.textContent = badge;
|
|
162
266
|
}
|
|
163
|
-
else
|
|
164
|
-
|
|
267
|
+
else
|
|
268
|
+
badgeElement?.remove();
|
|
269
|
+
if (section && currentItems[section.index].id !== id) {
|
|
270
|
+
currentItems = currentItems.map((item, index) => index === section.index ? { ...item, badge } : item);
|
|
165
271
|
}
|
|
166
|
-
|
|
272
|
+
else
|
|
273
|
+
mapItems(item => item.id === id ? { ...item, badge } : item);
|
|
167
274
|
};
|
|
168
|
-
const
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
const items =
|
|
275
|
+
const visibleItems = (container) => [...container.querySelectorAll(`.${cls(DRAWER_CLASSES.ITEM)}`)].filter(visible);
|
|
276
|
+
const handleKeydown = (event) => {
|
|
277
|
+
const { key } = event;
|
|
278
|
+
const items = visibleItems(itemsContainer);
|
|
172
279
|
if (!items.length)
|
|
173
280
|
return;
|
|
174
|
-
const
|
|
175
|
-
|
|
176
|
-
|
|
281
|
+
const current = activeElementOf(itemsContainer);
|
|
282
|
+
const index = items.indexOf(current);
|
|
283
|
+
let next;
|
|
284
|
+
switch (key) {
|
|
177
285
|
case "ArrowDown":
|
|
178
|
-
|
|
179
|
-
nextIndex = currentIndex < items.length - 1 ? currentIndex + 1 : 0;
|
|
286
|
+
next = items[(index + 1) % items.length];
|
|
180
287
|
break;
|
|
181
288
|
case "ArrowUp":
|
|
182
|
-
|
|
183
|
-
nextIndex = currentIndex > 0 ? currentIndex - 1 : items.length - 1;
|
|
289
|
+
next = items[index > 0 ? index - 1 : items.length - 1];
|
|
184
290
|
break;
|
|
185
291
|
case "Home":
|
|
186
|
-
|
|
187
|
-
nextIndex = 0;
|
|
292
|
+
next = items[0];
|
|
188
293
|
break;
|
|
189
294
|
case "End":
|
|
190
|
-
|
|
191
|
-
nextIndex = items.length - 1;
|
|
295
|
+
next = items.at(-1);
|
|
192
296
|
break;
|
|
193
297
|
case "Enter":
|
|
194
298
|
case " ":
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
299
|
+
if (key === "Enter" && current?.localName === "a" && hasModifiers(event))
|
|
300
|
+
return;
|
|
301
|
+
event.preventDefault();
|
|
302
|
+
if (index >= 0) {
|
|
303
|
+
const section = sections.get(current.dataset.id ?? "");
|
|
304
|
+
if (section?.button === current)
|
|
305
|
+
setExpanded(section, !section.expanded, event);
|
|
306
|
+
else
|
|
307
|
+
current.click();
|
|
198
308
|
}
|
|
199
309
|
return;
|
|
200
|
-
|
|
310
|
+
case "ArrowRight":
|
|
311
|
+
case "ArrowLeft": {
|
|
312
|
+
if (index < 0 || !sections.size)
|
|
313
|
+
return;
|
|
314
|
+
const opens = key === (itemsContainer.matches(":dir(rtl)") ? "ArrowLeft" : "ArrowRight");
|
|
315
|
+
const section = sections.get(current.dataset.id ?? "");
|
|
316
|
+
const parent = sections.get(current.dataset.parentId ?? "");
|
|
317
|
+
if (section?.button === current) {
|
|
318
|
+
event.preventDefault();
|
|
319
|
+
if (opens && section.expanded)
|
|
320
|
+
visibleItems(section.panel)[0]?.focus();
|
|
321
|
+
else
|
|
322
|
+
setExpanded(section, opens, event);
|
|
323
|
+
}
|
|
324
|
+
else if (!opens && parent && enabled(parent.button)) {
|
|
325
|
+
event.preventDefault();
|
|
326
|
+
parent.button.focus();
|
|
327
|
+
}
|
|
201
328
|
return;
|
|
202
329
|
}
|
|
203
|
-
|
|
204
|
-
items[nextIndex].focus();
|
|
330
|
+
default: return;
|
|
205
331
|
}
|
|
332
|
+
event.preventDefault();
|
|
333
|
+
next?.focus();
|
|
206
334
|
};
|
|
207
|
-
itemsContainer = document.createElement("div");
|
|
208
|
-
itemsContainer.className = component.getClass("drawer__items");
|
|
209
335
|
itemsContainer.addEventListener("keydown", handleKeydown);
|
|
210
336
|
renderItems();
|
|
211
337
|
return {
|
|
@@ -213,10 +339,27 @@ return {
|
|
|
213
339
|
itemsContainer,
|
|
214
340
|
drawerItems: {
|
|
215
341
|
setActive,
|
|
216
|
-
getActive,
|
|
342
|
+
getActive: () => activeId,
|
|
217
343
|
setItems,
|
|
218
|
-
getItems
|
|
344
|
+
getItems: () => {
|
|
345
|
+
if (!sections.size)
|
|
346
|
+
return currentItems;
|
|
347
|
+
const items = currentItems.slice();
|
|
348
|
+
for (const { index, expanded } of sections.values()) {
|
|
349
|
+
items[index] = { ...items[index], expanded };
|
|
350
|
+
}
|
|
351
|
+
return items;
|
|
352
|
+
},
|
|
219
353
|
setBadge,
|
|
354
|
+
expandSection: id => setExpanded(sections.get(id), true),
|
|
355
|
+
collapseSection: id => setExpanded(sections.get(id), false),
|
|
356
|
+
toggleSection: id => {
|
|
357
|
+
const section = sections.get(id);
|
|
358
|
+
if (section)
|
|
359
|
+
setExpanded(section, !section.expanded);
|
|
360
|
+
},
|
|
361
|
+
isSectionExpanded: id => sections.get(id)?.expanded ?? false,
|
|
362
|
+
getExpandedSections: () => [...sections.values()].filter(section => section.expanded).map(section => section.id),
|
|
220
363
|
renderItems,
|
|
221
364
|
destroy: unmountRipples,
|
|
222
365
|
},
|
|
@@ -5,7 +5,9 @@ import type { ForwardedEventPayload } from "../../core/dom/index.js";
|
|
|
5
5
|
* Creates a new Drawer component with the specified configuration.
|
|
6
6
|
* @param config
|
|
7
7
|
*/
|
|
8
|
-
declare
|
|
8
|
+
declare function createDrawer(config?: DrawerConfig): DrawerComponent;
|
|
9
|
+
|
|
10
|
+
declare const createShadowDrawer: (config: DrawerConfig) => DrawerComponent;
|
|
9
11
|
|
|
10
12
|
/** Drawer variant type — controls layout behavior */
|
|
11
13
|
type DrawerVariant = "standard" | "modal";
|
|
@@ -25,6 +27,12 @@ interface DrawerItemConfig {
|
|
|
25
27
|
icon?: string;
|
|
26
28
|
/** Icon HTML content shown while this item is the active destination, in place of `icon`. */
|
|
27
29
|
activeIcon?: string;
|
|
30
|
+
/** Navigation URL for a leaf destination. */
|
|
31
|
+
href?: string;
|
|
32
|
+
/** A non-empty list makes this item a disclosure section. */
|
|
33
|
+
items?: DrawerItemConfig[];
|
|
34
|
+
/** Initial section state. */
|
|
35
|
+
expanded?: boolean;
|
|
28
36
|
/** Badge text (e.g. */
|
|
29
37
|
badge?: string;
|
|
30
38
|
/** Whether this item is initially active/selected */
|
|
@@ -49,12 +57,23 @@ interface DrawerSelectEvent {
|
|
|
49
57
|
originalEvent: Event;
|
|
50
58
|
}
|
|
51
59
|
|
|
60
|
+
/** The new state of a drawer disclosure section. */
|
|
61
|
+
interface DrawerSectionEvent {
|
|
62
|
+
id: string;
|
|
63
|
+
value: string;
|
|
64
|
+
label: string;
|
|
65
|
+
expanded: boolean;
|
|
66
|
+
originalEvent?: Event;
|
|
67
|
+
}
|
|
68
|
+
|
|
52
69
|
/** Events emitted by drawer state, navigation items and root DOM forwarding. */
|
|
53
70
|
interface DrawerEvents {
|
|
54
71
|
open: () => void;
|
|
55
72
|
close: () => void;
|
|
56
73
|
/** User activation only; setActive() is silent. */
|
|
57
74
|
select: (payload: DrawerSelectEvent) => void;
|
|
75
|
+
expand: (payload: DrawerSectionEvent) => void;
|
|
76
|
+
collapse: (payload: DrawerSectionEvent) => void;
|
|
58
77
|
click: (payload: ForwardedEventPayload<MouseEvent, HTMLElement>) => void;
|
|
59
78
|
keydown: (payload: ForwardedEventPayload<KeyboardEvent, HTMLElement>) => void;
|
|
60
79
|
}
|
|
@@ -119,6 +138,9 @@ interface DrawerConfig extends BaseComponentConfig {
|
|
|
119
138
|
onOpen?: DrawerEvents["open"];
|
|
120
139
|
/** `close` listener registered at creation. */
|
|
121
140
|
onClose?: DrawerEvents["close"];
|
|
141
|
+
/** Section listeners registered at creation. */
|
|
142
|
+
onExpand?: DrawerEvents["expand"];
|
|
143
|
+
onCollapse?: DrawerEvents["collapse"];
|
|
122
144
|
}
|
|
123
145
|
|
|
124
146
|
/** Drawer component interface — public API */
|
|
@@ -167,6 +189,13 @@ interface DrawerComponent {
|
|
|
167
189
|
* @param badge
|
|
168
190
|
*/
|
|
169
191
|
setBadge: (id: string, badge: string) => DrawerComponent;
|
|
192
|
+
/** Open, close or toggle the disclosure with this id. */
|
|
193
|
+
expandSection: (id: string) => DrawerComponent;
|
|
194
|
+
collapseSection: (id: string) => DrawerComponent;
|
|
195
|
+
toggleSection: (id: string) => DrawerComponent;
|
|
196
|
+
isSectionExpanded: (id: string) => boolean;
|
|
197
|
+
/** Expanded section ids in document order. */
|
|
198
|
+
getExpandedSections: () => string[];
|
|
170
199
|
/**
|
|
171
200
|
* Adds an event listener
|
|
172
201
|
* @param event
|
|
@@ -192,5 +221,5 @@ interface DrawerComponent {
|
|
|
192
221
|
destroy: () => void;
|
|
193
222
|
};
|
|
194
223
|
}
|
|
195
|
-
export type { DrawerComponent, DrawerConfig, DrawerEvents, DrawerItemConfig, DrawerPosition, DrawerSelectEvent, DrawerVariant };
|
|
224
|
+
export type { DrawerComponent, DrawerConfig, DrawerEvents, DrawerItemConfig, DrawerPosition, DrawerSectionEvent, DrawerSelectEvent, DrawerVariant };
|
|
196
225
|
export { createDrawer, createDrawer as default };
|