-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathindex.js
More file actions
187 lines (159 loc) · 5.42 KB
/
Copy pathindex.js
File metadata and controls
187 lines (159 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
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
52
53
54
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
140
141
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
183
184
185
186
187
import Expander from "makeup-expander";
import Menu from "makeup-menu";
const defaultOptions = {
customElementMode: false,
expandedClass: "menu-button--expanded",
menuSelector: ".menu-button__menu",
buttonTextSelector: `.btn__text`,
buttonValueType: "text", // ["text", "icon", "both"],
menuItemIconSelector: ".icon",
menuItemButtonAriaLabelSelector: null,
};
export default class {
constructor(widgetEl, selectedOptions) {
this._options = Object.assign({}, defaultOptions, selectedOptions);
this.el = widgetEl;
this._buttonEl = widgetEl.querySelector("button");
this._buttonEl.setAttribute("aria-haspopup", "true");
this.menu = new Menu(widgetEl.querySelector(this._options.menuSelector), {
autoReset: "interactive",
});
this._buttonPrefix = this._buttonEl.dataset?.makeupMenuButtonPrefix;
this._buttonTextEl = this._buttonEl.querySelector(this._options.buttonTextSelector);
this._expander = new Expander(widgetEl, {
alwaysDoFocusManagement: true,
autoCollapse: true,
collapseOnHostReFocus: true,
contentSelector: this._options.menuSelector,
expandedClass: this._options.expandedClass,
expandOnClick: true,
focusManagement: "focusable",
hostSelector: "button",
});
this._onButtonFirstClickListener = _onButtonFirstClick.bind(this);
this._onMenuKeyDownListener = _onMenuKeyDown.bind(this);
this._onMenuItemSelectListener = _onMenuItemSelect.bind(this);
this._onMenuItemChangeListener = _onMenuItemChange.bind(this);
this._onMutationListener = _onMutation.bind(this);
this.el.classList.add("menu-button--js");
if (!this._options.customElementMode) {
this._mutationObserver = new MutationObserver(this._onMutationListener);
this._observeMutations();
this._observeEvents();
}
}
_observeMutations() {
if (!this._options.customElementMode) {
this._mutationObserver.observe(this.el, {
attributeFilter: ["aria-expanded", "disabled"],
attributes: true,
childList: false,
subtree: false,
});
}
}
_unobserveMutations() {
if (!this._options.customElementMode) {
this._mutationObserver.disconnect();
}
}
_observeEvents() {
if (this._destroyed !== true) {
this._buttonEl.addEventListener("click", this._onButtonFirstClickListener, { once: true });
this.menu.el.addEventListener("keydown", this._onMenuKeyDownListener);
this.menu.el.addEventListener("makeup-menu-select", this._onMenuItemSelectListener);
this.menu.el.addEventListener("makeup-menu-change", this._onMenuItemChangeListener);
}
}
_unobserveEvents() {
this._buttonEl.removeEventListener("click", this._onButtonFirstClickListener);
this.menu.el.removeEventListener("keydown", this._onMenuKeyDownListener);
this.menu.el.removeEventListener("makeup-menu-select", this._onMenuItemSelectListener);
this.menu.el.removeEventListener("makeup-menu-change", this._onMenuItemChangeListener);
}
// this should maybe be moved to expander as an option callback for after collapse
_collapseMenuAfterTimeout() {
const widget = this;
setTimeout(function () {
widget._expander.expanded = false;
widget._buttonEl.focus();
}, 150);
}
destroy() {
this._destroyed = true;
this._unobserveMutations();
this._unobserveEvents();
this._onButtonFirstClickListener = null;
this._onMenuKeyDownListener = null;
this._onMenuItemSelectListener = null;
this._onMenuItemChangeListener = null;
this._onMutationListener = null;
}
}
function _onMutation(mutationsList) {
for (const mutation of mutationsList) {
if (mutation.type === "attributes") {
this.el.dispatchEvent(
new CustomEvent("makeup-menu-button-mutation", {
detail: {
attributeName: mutation.attributeName,
},
}),
);
}
}
}
function _onButtonFirstClick() {
this.menu.el.hidden = false;
}
function _onMenuKeyDown(e) {
if (e.keyCode === 27) {
this._expander.expanded = false;
this._buttonEl.focus();
}
}
function _onMenuItemSelect(e) {
this._collapseMenuAfterTimeout();
this.el.dispatchEvent(
new CustomEvent("makeup-menu-button-select", {
detail: e.detail,
}),
);
}
function _onMenuItemChange(e) {
const { el } = e.detail;
this._collapseMenuAfterTimeout();
this.el.dispatchEvent(
new CustomEvent("makeup-menu-button-change", {
detail: e.detail,
}),
);
if (el.getAttribute("role") !== "menuitemradio") {
return;
}
const { menuItemIconSelector, menuItemButtonAriaLabelSelector, buttonValueType } = this._options;
const icon = el.querySelector(menuItemIconSelector).cloneNode(true);
const text = el.innerText.trim();
let btnContent = this._buttonPrefix ? `${this._buttonPrefix} ${text}` : text;
if (icon) {
switch (buttonValueType) {
case "both":
btnContent = `${icon.outerHTML} <span>${btnContent}</span>`;
break;
case "icon":
this._buttonEl.setAttribute("aria-label", btnContent);
btnContent = icon.outerHTML;
break;
default:
break;
}
}
if (menuItemButtonAriaLabelSelector) {
const selectorText = el.querySelector(menuItemButtonAriaLabelSelector)?.innerText.trim();
this._buttonEl.setAttribute(
"aria-label",
this._buttonPrefix ? `${this._buttonPrefix} ${selectorText}` : selectorText,
);
}
this._buttonTextEl.innerHTML = btnContent;
}