@spectrum-web-components/menu 0.12.2 → 0.12.4
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/package.json +8 -8
- package/sp-menu-divider.d.ts +6 -0
- package/sp-menu-divider.js +14 -0
- package/sp-menu-divider.js.map +1 -0
- package/sp-menu-group.d.ts +6 -0
- package/sp-menu-group.js +14 -0
- package/sp-menu-group.js.map +1 -0
- package/sp-menu-item.d.ts +6 -0
- package/sp-menu-item.js +14 -0
- package/sp-menu-item.js.map +1 -0
- package/sp-menu.d.ts +6 -0
- package/sp-menu.js +14 -0
- package/sp-menu.js.map +1 -0
- package/src/Menu.d.ts +104 -0
- package/src/Menu.js +625 -0
- package/src/Menu.js.map +1 -0
- package/src/MenuDivider.d.ts +8 -0
- package/src/MenuDivider.js +25 -0
- package/src/MenuDivider.js.map +1 -0
- package/src/MenuGroup.d.ts +20 -0
- package/src/MenuGroup.js +86 -0
- package/src/MenuGroup.js.map +1 -0
- package/src/MenuItem.d.ts +110 -0
- package/src/MenuItem.js +465 -0
- package/src/MenuItem.js.map +1 -0
- package/src/index.d.ts +4 -0
- package/src/index.js +16 -0
- package/src/index.js.map +1 -0
- package/src/menu-divider.css.d.ts +2 -0
- package/src/menu-divider.css.js +19 -0
- package/src/menu-divider.css.js.map +1 -0
- package/src/menu-group.css.d.ts +2 -0
- package/src/menu-group.css.js +19 -0
- package/src/menu-group.css.js.map +1 -0
- package/src/menu-item.css.d.ts +2 -0
- package/src/menu-item.css.js +76 -0
- package/src/menu-item.css.js.map +1 -0
- package/src/menu.css.d.ts +2 -0
- package/src/menu.css.js +74 -0
- package/src/menu.css.js.map +1 -0
- package/src/spectrum-checkmark.css.d.ts +2 -0
- package/src/spectrum-checkmark.css.js +21 -0
- package/src/spectrum-checkmark.css.js.map +1 -0
- package/src/spectrum-chevron.css.d.ts +2 -0
- package/src/spectrum-chevron.css.js +21 -0
- package/src/spectrum-chevron.css.js.map +1 -0
- package/src/spectrum-itemLabel.css.d.ts +2 -0
- package/src/spectrum-itemLabel.css.js +17 -0
- package/src/spectrum-itemLabel.css.js.map +1 -0
- package/src/spectrum-menu-divider.css.d.ts +2 -0
- package/src/spectrum-menu-divider.css.js +19 -0
- package/src/spectrum-menu-divider.css.js.map +1 -0
- package/src/spectrum-menu-item.css.d.ts +2 -0
- package/src/spectrum-menu-item.css.js +68 -0
- package/src/spectrum-menu-item.css.js.map +1 -0
- package/src/spectrum-menu-sectionHeading.css.d.ts +2 -0
- package/src/spectrum-menu-sectionHeading.css.js +19 -0
- package/src/spectrum-menu-sectionHeading.css.js.map +1 -0
- package/src/spectrum-menu.css.d.ts +2 -0
- package/src/spectrum-menu.css.js +74 -0
- package/src/spectrum-menu.css.js.map +1 -0
- package/stories/menu-group.stories.js +136 -0
- package/stories/menu-group.stories.js.map +1 -0
- package/stories/menu-item.stories.js +82 -0
- package/stories/menu-item.stories.js.map +1 -0
- package/stories/menu.stories.js +247 -0
- package/stories/menu.stories.js.map +1 -0
- package/stories/submenu.stories.js +302 -0
- package/stories/submenu.stories.js.map +1 -0
package/src/Menu.js
ADDED
|
@@ -0,0 +1,625 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
|
|
7
|
+
Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
governing permissions and limitations under the License.
|
|
11
|
+
*/
|
|
12
|
+
import { __decorate } from "tslib";
|
|
13
|
+
import { html, SpectrumElement, } from '@spectrum-web-components/base';
|
|
14
|
+
import { property, query, } from '@spectrum-web-components/base/src/decorators.js';
|
|
15
|
+
import { MenuItem, } from './MenuItem.js';
|
|
16
|
+
import menuStyles from './menu.css.js';
|
|
17
|
+
function elementIsOrContains(el, isOrContains) {
|
|
18
|
+
return !!isOrContains && (el === isOrContains || el.contains(isOrContains));
|
|
19
|
+
}
|
|
20
|
+
/* c8 ignore next 3 */
|
|
21
|
+
const noop = () => {
|
|
22
|
+
return;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Spectrum Menu Component
|
|
26
|
+
* @element sp-menu
|
|
27
|
+
*
|
|
28
|
+
* @slot - menu items to be listed in the menu
|
|
29
|
+
* @fires change - Announces that the `value` of the element has changed
|
|
30
|
+
* @attr selects - whether the element has a specific selection algorithm that it applies
|
|
31
|
+
* to its item descendants. `single` allows only one descendent to be selected at a time.
|
|
32
|
+
* `multiple` allows many descendants to be selected. `inherit` will be applied dynamically
|
|
33
|
+
* when an ancestor of this element is actively managing the selection of its descendents.
|
|
34
|
+
* When the `selects` attribute is not present a `value` will not be maintained and the Menu
|
|
35
|
+
* Item children of this Menu will not have their `selected` state managed.
|
|
36
|
+
*/
|
|
37
|
+
export class Menu extends SpectrumElement {
|
|
38
|
+
constructor() {
|
|
39
|
+
super();
|
|
40
|
+
this._closeSelfAsSubmenu = noop;
|
|
41
|
+
this.isSubmenu = false;
|
|
42
|
+
this._closeOpenSubmenu = noop;
|
|
43
|
+
this.hasOpenSubmenu = false;
|
|
44
|
+
this.label = '';
|
|
45
|
+
this.value = '';
|
|
46
|
+
// For the multiple select case, we'll join the value strings together
|
|
47
|
+
// for the value property with this separator
|
|
48
|
+
this.valueSeparator = ',';
|
|
49
|
+
// TODO: which of these to keep?
|
|
50
|
+
// TODO: allow setting this in the API to change the values
|
|
51
|
+
this.selected = [];
|
|
52
|
+
this.selectedItems = [];
|
|
53
|
+
this.childItemSet = new Set();
|
|
54
|
+
this.focusedItemIndex = 0;
|
|
55
|
+
this.focusInItemIndex = 0;
|
|
56
|
+
this.selectedItemsMap = new Map();
|
|
57
|
+
this._willUpdateItems = false;
|
|
58
|
+
this._notFirstUpdated = false;
|
|
59
|
+
this.cacheUpdated = Promise.resolve();
|
|
60
|
+
this.addEventListener('sp-menu-item-added-or-updated', this.onSelectableItemAddedOrUpdated);
|
|
61
|
+
this.addEventListener('sp-menu-item-added-or-updated', this.onFocusableItemAddedOrUpdated, {
|
|
62
|
+
capture: true,
|
|
63
|
+
});
|
|
64
|
+
this.addEventListener('sp-menu-item-removed', this.removeChildItem);
|
|
65
|
+
this.addEventListener('click', this.onClick);
|
|
66
|
+
this.addEventListener('focusin', this.handleFocusin);
|
|
67
|
+
}
|
|
68
|
+
static get styles() {
|
|
69
|
+
return [menuStyles];
|
|
70
|
+
}
|
|
71
|
+
get closeSelfAsSubmenu() {
|
|
72
|
+
this.isSubmenu = false;
|
|
73
|
+
return this._closeSelfAsSubmenu;
|
|
74
|
+
}
|
|
75
|
+
setCloseSelfAsSubmenu(cb) {
|
|
76
|
+
if (cb === this._closeSelfAsSubmenu) {
|
|
77
|
+
this.isSubmenu = false;
|
|
78
|
+
this._closeSelfAsSubmenu = noop;
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
this.isSubmenu = true;
|
|
82
|
+
this._closeSelfAsSubmenu = cb;
|
|
83
|
+
}
|
|
84
|
+
get closeOpenSubmenu() {
|
|
85
|
+
this.hasOpenSubmenu = false;
|
|
86
|
+
return this._closeOpenSubmenu;
|
|
87
|
+
}
|
|
88
|
+
setCloseOpenSubmenu(cb) {
|
|
89
|
+
if (cb === this._closeOpenSubmenu) {
|
|
90
|
+
this.hasOpenSubmenu = false;
|
|
91
|
+
this._closeOpenSubmenu = noop;
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
this.hasOpenSubmenu = true;
|
|
95
|
+
this._closeOpenSubmenu = cb;
|
|
96
|
+
}
|
|
97
|
+
get childItems() {
|
|
98
|
+
if (!this.cachedChildItems) {
|
|
99
|
+
this.cachedChildItems = this.updateCachedMenuItems();
|
|
100
|
+
}
|
|
101
|
+
return this.cachedChildItems;
|
|
102
|
+
}
|
|
103
|
+
updateCachedMenuItems() {
|
|
104
|
+
this.cachedChildItems = [];
|
|
105
|
+
const slotElements = this.menuSlot
|
|
106
|
+
? this.menuSlot.assignedElements({ flatten: true })
|
|
107
|
+
: [];
|
|
108
|
+
for (const slotElement of slotElements) {
|
|
109
|
+
const childMenuItems = slotElement instanceof MenuItem
|
|
110
|
+
? [slotElement]
|
|
111
|
+
: [...slotElement.querySelectorAll(`*`)];
|
|
112
|
+
for (const childMenuItem of childMenuItems) {
|
|
113
|
+
if (this.childItemSet.has(childMenuItem)) {
|
|
114
|
+
this.cachedChildItems.push(childMenuItem);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return this.cachedChildItems;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Hide this getter from web-component-analyzer until
|
|
122
|
+
* https://github.com/runem/web-component-analyzer/issues/131
|
|
123
|
+
* has been addressed.
|
|
124
|
+
*
|
|
125
|
+
* @private
|
|
126
|
+
*/
|
|
127
|
+
get childRole() {
|
|
128
|
+
if (this.resolvedRole === 'listbox') {
|
|
129
|
+
return 'option';
|
|
130
|
+
}
|
|
131
|
+
switch (this.resolvedSelects) {
|
|
132
|
+
case 'single':
|
|
133
|
+
return 'menuitemradio';
|
|
134
|
+
case 'multiple':
|
|
135
|
+
return 'menuitemcheckbox';
|
|
136
|
+
default:
|
|
137
|
+
return 'menuitem';
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
get ownRole() {
|
|
141
|
+
return 'menu';
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* When a descendant `<sp-menu-item>` element is added or updated it will dispatch
|
|
145
|
+
* this event to announce its presence in the DOM. During the capture phase the first
|
|
146
|
+
* Menu based element that the event encounters will manage the focus state of the
|
|
147
|
+
* dispatching `<sp-menu-item>` element.
|
|
148
|
+
* @param event
|
|
149
|
+
*/
|
|
150
|
+
onFocusableItemAddedOrUpdated(event) {
|
|
151
|
+
var _a;
|
|
152
|
+
if (event.item.menuData.focusRoot) {
|
|
153
|
+
// Only have one tab stop per Menu tree
|
|
154
|
+
this.tabIndex = -1;
|
|
155
|
+
}
|
|
156
|
+
event.focusRoot = this;
|
|
157
|
+
this.addChildItem(event.item);
|
|
158
|
+
if (this.selects === 'inherit') {
|
|
159
|
+
this.resolvedSelects = 'inherit';
|
|
160
|
+
this.resolvedRole = (((_a = event.currentAncestorWithSelects) === null || _a === void 0 ? void 0 : _a.getAttribute('role')) ||
|
|
161
|
+
this.getAttribute('role') ||
|
|
162
|
+
undefined);
|
|
163
|
+
}
|
|
164
|
+
else if (this.selects) {
|
|
165
|
+
this.resolvedRole = (this.getAttribute('role') ||
|
|
166
|
+
undefined);
|
|
167
|
+
this.resolvedSelects = this.selects;
|
|
168
|
+
event.currentAncestorWithSelects = this;
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
this.resolvedRole = (this.getAttribute('role') ||
|
|
172
|
+
undefined);
|
|
173
|
+
this.resolvedSelects =
|
|
174
|
+
this.resolvedRole === 'none' ? 'ignore' : 'none';
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* When a descendant `<sp-menu-item>` element is added or updated it will dispatch
|
|
179
|
+
* this event to announce its presence in the DOM. During the bubble phase the first
|
|
180
|
+
* Menu based element that the event encounters that does not inherit selection will
|
|
181
|
+
* manage the selection state of the dispatching `<sp-menu-item>` element.
|
|
182
|
+
* @param event
|
|
183
|
+
*/
|
|
184
|
+
onSelectableItemAddedOrUpdated(event) {
|
|
185
|
+
const selects = this.resolvedSelects === 'single' ||
|
|
186
|
+
this.resolvedSelects === 'multiple';
|
|
187
|
+
if ((selects || (!this.selects && this.resolvedSelects !== 'ignore')) &&
|
|
188
|
+
!event.item.menuData.selectionRoot) {
|
|
189
|
+
event.item.setRole(this.childRole);
|
|
190
|
+
event.selectionRoot = this;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
addChildItem(item) {
|
|
194
|
+
this.childItemSet.add(item);
|
|
195
|
+
this.handleItemsChanged();
|
|
196
|
+
}
|
|
197
|
+
async removeChildItem(event) {
|
|
198
|
+
this.childItemSet.delete(event.item);
|
|
199
|
+
this.cachedChildItems = undefined;
|
|
200
|
+
if (event.item.focused) {
|
|
201
|
+
this.handleItemsChanged();
|
|
202
|
+
await this.updateComplete;
|
|
203
|
+
this.focus();
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
focus({ preventScroll } = {}) {
|
|
207
|
+
if (!this.childItems.length ||
|
|
208
|
+
this.childItems.every((childItem) => childItem.disabled)) {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
if (this.childItems.some((childItem) => childItem.menuData.focusRoot !== this)) {
|
|
212
|
+
super.focus({ preventScroll });
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
this.focusMenuItemByOffset(0);
|
|
216
|
+
super.focus({ preventScroll });
|
|
217
|
+
const selectedItem = this.querySelector('[selected]');
|
|
218
|
+
if (selectedItem && !preventScroll) {
|
|
219
|
+
selectedItem.scrollIntoView({ block: 'nearest' });
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
submenuWillCloseOn(menuItem) {
|
|
223
|
+
this.focusedItemIndex = this.childItems.indexOf(menuItem);
|
|
224
|
+
this.focusInItemIndex = this.focusedItemIndex;
|
|
225
|
+
}
|
|
226
|
+
onClick(event) {
|
|
227
|
+
if (event.defaultPrevented) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
const path = event.composedPath();
|
|
231
|
+
const target = path.find((el) => {
|
|
232
|
+
/* c8 ignore next 3 */
|
|
233
|
+
if (!(el instanceof Element)) {
|
|
234
|
+
return false;
|
|
235
|
+
}
|
|
236
|
+
return el.getAttribute('role') === this.childRole;
|
|
237
|
+
});
|
|
238
|
+
if ((target === null || target === void 0 ? void 0 : target.href) && target.href.length) {
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
else if ((target === null || target === void 0 ? void 0 : target.menuData.selectionRoot) === this &&
|
|
242
|
+
this.childItems.length) {
|
|
243
|
+
event.preventDefault();
|
|
244
|
+
if (target.hasSubmenu || target.open) {
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
if (this.hasOpenSubmenu) {
|
|
248
|
+
this.closeOpenSubmenu();
|
|
249
|
+
}
|
|
250
|
+
if (this.isSubmenu) {
|
|
251
|
+
this.closeSelfAsSubmenu();
|
|
252
|
+
}
|
|
253
|
+
this.selectOrToggleItem(target);
|
|
254
|
+
}
|
|
255
|
+
else {
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
this.prepareToCleanUp();
|
|
259
|
+
}
|
|
260
|
+
handleFocusin(event) {
|
|
261
|
+
var _a;
|
|
262
|
+
const isOrContainsRelatedTarget = elementIsOrContains(this, event.relatedTarget);
|
|
263
|
+
if (isOrContainsRelatedTarget ||
|
|
264
|
+
this.childItems.some((childItem) => childItem.menuData.focusRoot !== this)) {
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
const activeElement = this.getRootNode().activeElement;
|
|
268
|
+
const selectionRoot = ((_a = this.childItems[this.focusedItemIndex]) === null || _a === void 0 ? void 0 : _a.menuData.selectionRoot) ||
|
|
269
|
+
this;
|
|
270
|
+
if (activeElement !== selectionRoot || !isOrContainsRelatedTarget) {
|
|
271
|
+
selectionRoot.focus({ preventScroll: true });
|
|
272
|
+
if (activeElement && this.focusedItemIndex === 0) {
|
|
273
|
+
const offset = this.childItems.findIndex((childItem) => childItem === activeElement);
|
|
274
|
+
if (offset > 0) {
|
|
275
|
+
this.focusMenuItemByOffset(offset);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
this.startListeningToKeyboard();
|
|
280
|
+
}
|
|
281
|
+
startListeningToKeyboard() {
|
|
282
|
+
this.addEventListener('keydown', this.handleKeydown);
|
|
283
|
+
this.addEventListener('focusout', this.handleFocusout);
|
|
284
|
+
}
|
|
285
|
+
handleFocusout(event) {
|
|
286
|
+
if (elementIsOrContains(this, event.relatedTarget)) {
|
|
287
|
+
event.composedPath()[0].focused = false;
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
this.stopListeningToKeyboard();
|
|
291
|
+
if (event.target === this &&
|
|
292
|
+
this.childItems.some((childItem) => childItem.menuData.focusRoot === this)) {
|
|
293
|
+
const focusedItem = this.childItems[this.focusedItemIndex];
|
|
294
|
+
if (focusedItem) {
|
|
295
|
+
focusedItem.focused = false;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
this.removeAttribute('aria-activedescendant');
|
|
299
|
+
}
|
|
300
|
+
stopListeningToKeyboard() {
|
|
301
|
+
this.removeEventListener('keydown', this.handleKeydown);
|
|
302
|
+
this.removeEventListener('focusout', this.handleFocusout);
|
|
303
|
+
}
|
|
304
|
+
async selectOrToggleItem(targetItem) {
|
|
305
|
+
const resolvedSelects = this.resolvedSelects;
|
|
306
|
+
const oldSelectedItemsMap = new Map(this.selectedItemsMap);
|
|
307
|
+
const oldSelected = this.selected.slice();
|
|
308
|
+
const oldSelectedItems = this.selectedItems.slice();
|
|
309
|
+
const oldValue = this.value;
|
|
310
|
+
this.childItems[this.focusedItemIndex].focused = false;
|
|
311
|
+
this.focusedItemIndex = this.childItems.indexOf(targetItem);
|
|
312
|
+
this.forwardFocusVisibleToItem(targetItem);
|
|
313
|
+
if (resolvedSelects === 'multiple') {
|
|
314
|
+
if (this.selectedItemsMap.has(targetItem)) {
|
|
315
|
+
this.selectedItemsMap.delete(targetItem);
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
this.selectedItemsMap.set(targetItem, true);
|
|
319
|
+
}
|
|
320
|
+
// Match HTML select and set the first selected
|
|
321
|
+
// item as the value. Also set the selected array
|
|
322
|
+
// in the order of the menu items.
|
|
323
|
+
const selected = [];
|
|
324
|
+
const selectedItems = [];
|
|
325
|
+
this.childItemSet.forEach((childItem) => {
|
|
326
|
+
if (childItem.menuData.selectionRoot !== this)
|
|
327
|
+
return;
|
|
328
|
+
if (this.selectedItemsMap.has(childItem)) {
|
|
329
|
+
selected.push(childItem.value);
|
|
330
|
+
selectedItems.push(childItem);
|
|
331
|
+
}
|
|
332
|
+
});
|
|
333
|
+
this.selected = selected;
|
|
334
|
+
this.selectedItems = selectedItems;
|
|
335
|
+
this.value = this.selected.join(this.valueSeparator);
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
this.selectedItemsMap.clear();
|
|
339
|
+
this.selectedItemsMap.set(targetItem, true);
|
|
340
|
+
this.value = targetItem.value;
|
|
341
|
+
this.selected = [targetItem.value];
|
|
342
|
+
this.selectedItems = [targetItem];
|
|
343
|
+
}
|
|
344
|
+
await this.updateComplete;
|
|
345
|
+
const applyDefault = this.dispatchEvent(new Event('change', {
|
|
346
|
+
cancelable: true,
|
|
347
|
+
bubbles: true,
|
|
348
|
+
composed: true,
|
|
349
|
+
}));
|
|
350
|
+
if (!applyDefault) {
|
|
351
|
+
// Cancel the event & don't apply the selection
|
|
352
|
+
this.selected = oldSelected;
|
|
353
|
+
this.selectedItems = oldSelectedItems;
|
|
354
|
+
this.selectedItemsMap = oldSelectedItemsMap;
|
|
355
|
+
this.value = oldValue;
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
// Apply the selection changes to the menu items
|
|
359
|
+
if (resolvedSelects === 'single') {
|
|
360
|
+
for (const oldItem of oldSelectedItemsMap.keys()) {
|
|
361
|
+
if (oldItem !== targetItem) {
|
|
362
|
+
oldItem.selected = false;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
targetItem.selected = true;
|
|
366
|
+
}
|
|
367
|
+
else if (resolvedSelects === 'multiple') {
|
|
368
|
+
targetItem.selected = !targetItem.selected;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
navigateWithinMenu(event) {
|
|
372
|
+
const { code } = event;
|
|
373
|
+
const lastFocusedItem = this.childItems[this.focusedItemIndex];
|
|
374
|
+
const direction = code === 'ArrowDown' ? 1 : -1;
|
|
375
|
+
const itemToFocus = this.focusMenuItemByOffset(direction);
|
|
376
|
+
if (itemToFocus === lastFocusedItem) {
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
event.preventDefault();
|
|
380
|
+
itemToFocus.scrollIntoView({ block: 'nearest' });
|
|
381
|
+
}
|
|
382
|
+
navigateBetweenRelatedMenus(code) {
|
|
383
|
+
const shouldOpenSubmenu = (this.isLTR && code === 'ArrowRight') ||
|
|
384
|
+
(!this.isLTR && code === 'ArrowLeft');
|
|
385
|
+
const shouldCloseSelfAsSubmenu = (this.isLTR && code === 'ArrowLeft') ||
|
|
386
|
+
(!this.isLTR && code === 'ArrowRight');
|
|
387
|
+
if (shouldOpenSubmenu) {
|
|
388
|
+
const lastFocusedItem = this.childItems[this.focusedItemIndex];
|
|
389
|
+
if (lastFocusedItem.hasSubmenu) {
|
|
390
|
+
// Remove focus while opening overlay from keyboard or the visible focus
|
|
391
|
+
// will slip back to the first item in the menu.
|
|
392
|
+
this.blur();
|
|
393
|
+
lastFocusedItem.openOverlay({ immediate: true });
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
else if (this.isSubmenu && shouldCloseSelfAsSubmenu) {
|
|
397
|
+
this.closeSelfAsSubmenu(true);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
handleKeydown(event) {
|
|
401
|
+
var _a;
|
|
402
|
+
const { code } = event;
|
|
403
|
+
if (code === 'Tab') {
|
|
404
|
+
this.prepareToCleanUp();
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
if (code === 'Space') {
|
|
408
|
+
const lastFocusedItem = this.childItems[this.focusedItemIndex];
|
|
409
|
+
if (lastFocusedItem.hasSubmenu) {
|
|
410
|
+
// Remove focus while opening overlay from keyboard or the visible focus
|
|
411
|
+
// will slip back to the first item in the menu.
|
|
412
|
+
this.blur();
|
|
413
|
+
lastFocusedItem.openOverlay({ immediate: true });
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
if (code === 'Space' || code === 'Enter') {
|
|
418
|
+
(_a = this.childItems[this.focusedItemIndex]) === null || _a === void 0 ? void 0 : _a.click();
|
|
419
|
+
return;
|
|
420
|
+
}
|
|
421
|
+
if (code === 'ArrowDown' || code === 'ArrowUp') {
|
|
422
|
+
this.navigateWithinMenu(event);
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
this.navigateBetweenRelatedMenus(code);
|
|
426
|
+
}
|
|
427
|
+
focusMenuItemByOffset(offset) {
|
|
428
|
+
const step = offset || 1;
|
|
429
|
+
const focusedItem = this.childItems[this.focusedItemIndex];
|
|
430
|
+
focusedItem.focused = false;
|
|
431
|
+
this.focusedItemIndex =
|
|
432
|
+
(this.childItems.length + this.focusedItemIndex + offset) %
|
|
433
|
+
this.childItems.length;
|
|
434
|
+
let itemToFocus = this.childItems[this.focusedItemIndex];
|
|
435
|
+
let availableItems = this.childItems.length;
|
|
436
|
+
// cycle through the available items in the directions of the offset to find the next non-disabled item
|
|
437
|
+
while (itemToFocus.disabled && availableItems) {
|
|
438
|
+
availableItems -= 1;
|
|
439
|
+
this.focusedItemIndex =
|
|
440
|
+
(this.childItems.length + this.focusedItemIndex + step) %
|
|
441
|
+
this.childItems.length;
|
|
442
|
+
itemToFocus = this.childItems[this.focusedItemIndex];
|
|
443
|
+
}
|
|
444
|
+
// if there are no non-disabled items, skip the work to focus a child
|
|
445
|
+
if (!(itemToFocus === null || itemToFocus === void 0 ? void 0 : itemToFocus.disabled)) {
|
|
446
|
+
this.forwardFocusVisibleToItem(itemToFocus);
|
|
447
|
+
}
|
|
448
|
+
return itemToFocus;
|
|
449
|
+
}
|
|
450
|
+
prepareToCleanUp() {
|
|
451
|
+
document.addEventListener('focusout', () => {
|
|
452
|
+
requestAnimationFrame(() => {
|
|
453
|
+
const focusedItem = this.childItems[this.focusedItemIndex];
|
|
454
|
+
if (focusedItem) {
|
|
455
|
+
focusedItem.focused = false;
|
|
456
|
+
this.updateSelectedItemIndex();
|
|
457
|
+
}
|
|
458
|
+
});
|
|
459
|
+
}, { once: true });
|
|
460
|
+
}
|
|
461
|
+
updateSelectedItemIndex() {
|
|
462
|
+
let firstOrFirstSelectedIndex = 0;
|
|
463
|
+
const selectedItemsMap = new Map();
|
|
464
|
+
const selected = [];
|
|
465
|
+
const selectedItems = [];
|
|
466
|
+
let itemIndex = this.childItems.length;
|
|
467
|
+
while (itemIndex) {
|
|
468
|
+
itemIndex -= 1;
|
|
469
|
+
const childItem = this.childItems[itemIndex];
|
|
470
|
+
if (childItem.menuData.selectionRoot === this) {
|
|
471
|
+
if (childItem.selected) {
|
|
472
|
+
firstOrFirstSelectedIndex = itemIndex;
|
|
473
|
+
selectedItemsMap.set(childItem, true);
|
|
474
|
+
selected.unshift(childItem.value);
|
|
475
|
+
selectedItems.unshift(childItem);
|
|
476
|
+
}
|
|
477
|
+
// Remove "focused" from non-"selected" items ONLY
|
|
478
|
+
// Preserve "focused" on index===0 when no selection
|
|
479
|
+
if (itemIndex !== firstOrFirstSelectedIndex) {
|
|
480
|
+
childItem.focused = false;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
selectedItems.map((item, i) => {
|
|
485
|
+
// When there is more than one "selected" item,
|
|
486
|
+
// ensure only the first one can be "focused"
|
|
487
|
+
if (i > 0) {
|
|
488
|
+
item.focused = false;
|
|
489
|
+
}
|
|
490
|
+
});
|
|
491
|
+
this.selectedItemsMap = selectedItemsMap;
|
|
492
|
+
this.selected = selected;
|
|
493
|
+
this.selectedItems = selectedItems;
|
|
494
|
+
this.value = this.selected.join(this.valueSeparator);
|
|
495
|
+
this.focusedItemIndex = firstOrFirstSelectedIndex;
|
|
496
|
+
this.focusInItemIndex = firstOrFirstSelectedIndex;
|
|
497
|
+
}
|
|
498
|
+
handleItemsChanged() {
|
|
499
|
+
this.cachedChildItems = undefined;
|
|
500
|
+
if (!this._willUpdateItems) {
|
|
501
|
+
/* c8 ignore next 3 */
|
|
502
|
+
let resolve = () => {
|
|
503
|
+
return;
|
|
504
|
+
};
|
|
505
|
+
this.cacheUpdated = new Promise((res) => (resolve = res));
|
|
506
|
+
this._willUpdateItems = true;
|
|
507
|
+
// Debounce the update so we only update once
|
|
508
|
+
// if multiple items have changed
|
|
509
|
+
window.requestAnimationFrame(() => {
|
|
510
|
+
if (this.cachedChildItems === undefined) {
|
|
511
|
+
this.updateSelectedItemIndex();
|
|
512
|
+
this.updateItemFocus();
|
|
513
|
+
}
|
|
514
|
+
this._willUpdateItems = false;
|
|
515
|
+
resolve();
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
updateItemFocus() {
|
|
520
|
+
if (this.childItems.length == 0) {
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
const focusInItem = this.childItems[this.focusInItemIndex];
|
|
524
|
+
if (this.getRootNode().activeElement ===
|
|
525
|
+
focusInItem.menuData.focusRoot) {
|
|
526
|
+
this.forwardFocusVisibleToItem(focusInItem);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
forwardFocusVisibleToItem(item) {
|
|
530
|
+
if (item.menuData.focusRoot !== this) {
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
item.focused = this.hasVisibleFocusInTree();
|
|
534
|
+
this.setAttribute('aria-activedescendant', item.id);
|
|
535
|
+
if (item.menuData.selectionRoot &&
|
|
536
|
+
item.menuData.selectionRoot !== this) {
|
|
537
|
+
item.menuData.selectionRoot.focus();
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
render() {
|
|
541
|
+
return html `
|
|
542
|
+
<slot></slot>
|
|
543
|
+
`;
|
|
544
|
+
}
|
|
545
|
+
firstUpdated(changed) {
|
|
546
|
+
super.firstUpdated(changed);
|
|
547
|
+
if (!this.hasAttribute('tabindex')) {
|
|
548
|
+
const role = this.getAttribute('role');
|
|
549
|
+
if (role === 'group') {
|
|
550
|
+
this.tabIndex = -1;
|
|
551
|
+
}
|
|
552
|
+
else if (role !== 'none') {
|
|
553
|
+
this.tabIndex = 0;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
const updates = [
|
|
557
|
+
new Promise((res) => requestAnimationFrame(() => res(true))),
|
|
558
|
+
];
|
|
559
|
+
[...this.children].forEach((item) => {
|
|
560
|
+
if (item.localName === 'sp-menu-item') {
|
|
561
|
+
updates.push(item.updateComplete);
|
|
562
|
+
}
|
|
563
|
+
});
|
|
564
|
+
this.childItemsUpdated = Promise.all(updates);
|
|
565
|
+
}
|
|
566
|
+
updated(changes) {
|
|
567
|
+
super.updated(changes);
|
|
568
|
+
if (changes.has('selects') && this._notFirstUpdated) {
|
|
569
|
+
this.selectsChanged();
|
|
570
|
+
}
|
|
571
|
+
if (changes.has('label')) {
|
|
572
|
+
if (this.label) {
|
|
573
|
+
this.setAttribute('aria-label', this.label);
|
|
574
|
+
}
|
|
575
|
+
else {
|
|
576
|
+
this.removeAttribute('aria-label');
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
this._notFirstUpdated = true;
|
|
580
|
+
}
|
|
581
|
+
selectsChanged() {
|
|
582
|
+
const updates = [
|
|
583
|
+
new Promise((res) => requestAnimationFrame(() => res(true))),
|
|
584
|
+
];
|
|
585
|
+
this.childItemSet.forEach((childItem) => {
|
|
586
|
+
updates.push(childItem.triggerUpdate());
|
|
587
|
+
});
|
|
588
|
+
this.childItemsUpdated = Promise.all(updates);
|
|
589
|
+
}
|
|
590
|
+
connectedCallback() {
|
|
591
|
+
super.connectedCallback();
|
|
592
|
+
if (!this.hasAttribute('role')) {
|
|
593
|
+
this.setAttribute('role', this.ownRole);
|
|
594
|
+
}
|
|
595
|
+
this.updateComplete.then(() => this.updateItemFocus());
|
|
596
|
+
}
|
|
597
|
+
async getUpdateComplete() {
|
|
598
|
+
const complete = (await super.getUpdateComplete());
|
|
599
|
+
await this.childItemsUpdated;
|
|
600
|
+
await this.cacheUpdated;
|
|
601
|
+
return complete;
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
__decorate([
|
|
605
|
+
property({ type: String, reflect: true })
|
|
606
|
+
], Menu.prototype, "label", void 0);
|
|
607
|
+
__decorate([
|
|
608
|
+
property({ type: String, reflect: true })
|
|
609
|
+
], Menu.prototype, "selects", void 0);
|
|
610
|
+
__decorate([
|
|
611
|
+
property({ type: String })
|
|
612
|
+
], Menu.prototype, "value", void 0);
|
|
613
|
+
__decorate([
|
|
614
|
+
property({ type: String, attribute: 'value-separator' })
|
|
615
|
+
], Menu.prototype, "valueSeparator", void 0);
|
|
616
|
+
__decorate([
|
|
617
|
+
property({ attribute: false })
|
|
618
|
+
], Menu.prototype, "selected", void 0);
|
|
619
|
+
__decorate([
|
|
620
|
+
property({ attribute: false })
|
|
621
|
+
], Menu.prototype, "selectedItems", void 0);
|
|
622
|
+
__decorate([
|
|
623
|
+
query('slot:not([name])')
|
|
624
|
+
], Menu.prototype, "menuSlot", void 0);
|
|
625
|
+
//# sourceMappingURL=Menu.js.map
|