@webtides/element-library 0.4.0 → 0.5.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/CHANGELOG.md +107 -0
- package/custom-elements.json +1543 -199
- package/package.json +1 -1
- package/src/components/app-frame/app-frame.changelog.md +14 -0
- package/src/components/app-frame/app-frame.js +14 -2
- package/src/components/app-frame/app-frame.readme.md +12 -3
- package/src/components/app-frame/app-frame.style.js +7 -0
- package/src/components/button/button.changelog.md +6 -0
- package/src/components/button/button.style.js +6 -1
- package/src/components/button-group/button-group.changelog.md +10 -0
- package/src/components/button-group/button-group.readme.md +2 -0
- package/src/components/button-group/button-group.style.js +6 -2
- package/src/components/checkbox-group-field/checkbox-group-field.changelog.md +6 -0
- package/src/components/checkbox-group-field/checkbox-group-field.js +7 -2
- package/src/components/checkbox-group-field/checkbox-group-field.readme.md +23 -1
- package/src/components/combobox-field/combobox-field.changelog.md +6 -0
- package/src/components/combobox-field/combobox-field.readme.md +8 -0
- package/src/components/combobox-field/combobox-field.style.js +2 -0
- package/src/components/date-field/date-field.changelog.md +6 -0
- package/src/components/date-field/date-field.readme.md +10 -0
- package/src/components/date-field/date-field.style.js +8 -3
- package/src/components/description-list/description-list.changelog.md +17 -0
- package/src/components/description-list/description-list.js +7 -2
- package/src/components/description-list/description-list.readme.md +30 -4
- package/src/components/description-list/description-list.style.js +57 -28
- package/src/components/divider/divider.changelog.md +6 -0
- package/src/components/divider/divider.readme.md +1 -1
- package/src/components/divider/divider.style.js +6 -0
- package/src/components/file-field/file-field.changelog.md +8 -0
- package/src/components/file-field/file-field.js +135 -5
- package/src/components/file-field/file-field.readme.md +35 -6
- package/src/components/file-field/file-field.style.js +71 -0
- package/src/components/form-field/form-field.js +1 -1
- package/src/components/input-field/input-field.changelog.md +10 -0
- package/src/components/input-field/input-field.js +192 -14
- package/src/components/input-field/input-field.readme.md +88 -2
- package/src/components/input-field/input-field.style.js +2 -0
- package/src/components/list/list.changelog.md +6 -0
- package/src/components/list/list.js +2 -0
- package/src/components/list/list.readme.md +1 -0
- package/src/components/list-item/list-item.changelog.md +16 -0
- package/src/components/list-item/list-item.js +2 -0
- package/src/components/list-item/list-item.readme.md +19 -1
- package/src/components/list-item/list-item.style.js +23 -11
- package/src/components/menu/menu.changelog.md +17 -0
- package/src/components/menu/menu.js +293 -70
- package/src/components/menu/menu.readme.md +47 -6
- package/src/components/menu/menu.style.js +6 -2
- package/src/components/menu-item/menu-item.changelog.md +8 -0
- package/src/components/menu-item/menu-item.js +41 -3
- package/src/components/menu-item/menu-item.readme.md +45 -7
- package/src/components/menu-item/menu-item.style.js +30 -0
- package/src/components/otp-field/otp-field.changelog.md +6 -0
- package/src/components/otp-field/otp-field.readme.md +8 -0
- package/src/components/otp-field/otp-field.style.js +8 -3
- package/src/components/password-field/password-field.changelog.md +7 -0
- package/src/components/password-field/password-field.readme.md +11 -1
- package/src/components/password-field/password-field.style.js +2 -0
- package/src/components/popover/popover.changelog.md +6 -0
- package/src/components/popover/popover.style.js +1 -0
- package/src/components/radio-field/radio-field.changelog.md +6 -0
- package/src/components/radio-field/radio-field.js +7 -2
- package/src/components/radio-field/radio-field.readme.md +23 -1
- package/src/components/textarea-field/textarea-field.changelog.md +6 -0
- package/src/components/textarea-field/textarea-field.js +14 -1
- package/src/components/textarea-field/textarea-field.readme.md +23 -1
- package/src/styles/choice-group.style.js +26 -0
- package/src/styles/input-control.style.js +87 -0
- package/src/styles/native.css +100 -0
- package/src/utils/floating.js +15 -1
- package/src/utils/options.js +35 -0
- package/src/utils/text-entry-attributes.js +13 -0
- package/types/src/components/app-frame/app-frame.d.ts +7 -1
- package/types/src/components/checkbox-group-field/checkbox-group-field.d.ts +5 -2
- package/types/src/components/combobox-field/combobox-field.d.ts +10 -0
- package/types/src/components/date-field/date-field.d.ts +10 -0
- package/types/src/components/description-list/description-list.d.ts +7 -2
- package/types/src/components/file-field/file-field.d.ts +68 -3
- package/types/src/components/form-field/form-field.d.ts +1 -1
- package/types/src/components/input-field/input-field.d.ts +99 -0
- package/types/src/components/list/list.d.ts +2 -0
- package/types/src/components/list-item/list-item.d.ts +2 -0
- package/types/src/components/menu/menu.d.ts +105 -9
- package/types/src/components/menu-item/menu-item.d.ts +21 -0
- package/types/src/components/otp-field/otp-field.d.ts +10 -0
- package/types/src/components/password-field/password-field.d.ts +10 -0
- package/types/src/components/radio-field/radio-field.d.ts +5 -2
- package/types/src/components/textarea-field/textarea-field.d.ts +7 -0
- package/types/src/styles/input-control.style.d.ts +15 -0
- package/types/src/utils/floating.d.ts +11 -0
- package/types/src/utils/options.d.ts +17 -0
- package/types/src/utils/text-entry-attributes.d.ts +10 -0
- package/web-types.json +514 -24
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { TemplateElement, defineElement, html } from '@webtides/element-js';
|
|
2
2
|
import { activeElement } from '../../utils/focus.js';
|
|
3
|
-
import { followAnchor, placeAtAnchor } from '../../utils/floating.js';
|
|
3
|
+
import { followAnchor, placeAtAnchor, scrollersOf } from '../../utils/floating.js';
|
|
4
4
|
import style from './menu.style.js';
|
|
5
5
|
import Events from './menu.events.js';
|
|
6
6
|
|
|
@@ -112,6 +112,37 @@ export default class Menu extends TemplateElement {
|
|
|
112
112
|
*/
|
|
113
113
|
_leaving = false;
|
|
114
114
|
|
|
115
|
+
/**
|
|
116
|
+
* Whether the panel is being placed, and whether it was asked to be placed again meanwhile.
|
|
117
|
+
* @private
|
|
118
|
+
*/
|
|
119
|
+
_placing = false;
|
|
120
|
+
|
|
121
|
+
/** @private */
|
|
122
|
+
_placeAgain = false;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The entry that had the focus last while the menu is open. It gets the focus back when a
|
|
126
|
+
* press beside the entries has given it to the panel.
|
|
127
|
+
* @private
|
|
128
|
+
* @type {HTMLElement | undefined}
|
|
129
|
+
*/
|
|
130
|
+
_current = undefined;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Whether the menu is open and has not put the focus on an entry yet: its entries are not
|
|
134
|
+
* there, or not defined. The focus goes to them once they are.
|
|
135
|
+
* @private
|
|
136
|
+
*/
|
|
137
|
+
_entering = false;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Whether the pointer that went down last is on the element the menu is open at, the button
|
|
141
|
+
* of a row. The `showAt()` the page calls with the click of that press closes the menu.
|
|
142
|
+
* @private
|
|
143
|
+
*/
|
|
144
|
+
_pressedAtPlace = false;
|
|
145
|
+
|
|
115
146
|
/**
|
|
116
147
|
* A pointer that goes down beside the open menu closes it. The panel is a popover the menu
|
|
117
148
|
* opens and closes itself (`popover="manual"`): the platform would close one of its own
|
|
@@ -121,14 +152,130 @@ export default class Menu extends TemplateElement {
|
|
|
121
152
|
*/
|
|
122
153
|
_onPressOutside = (event) => {
|
|
123
154
|
const path = event.composedPath();
|
|
155
|
+
this._pressedAtPlace = false;
|
|
124
156
|
if (path.includes(this.$refs.panel)) return;
|
|
125
157
|
// The button of the menu closes it with its click.
|
|
126
158
|
if (!this._place && path.includes(this._trigger())) return;
|
|
159
|
+
// So does the element the menu is open at, the button of a row. Its click is the one of
|
|
160
|
+
// the page, which calls showAt() with it: closed here, the menu would be opened again
|
|
161
|
+
// by that call, and the button could not close what it has opened.
|
|
162
|
+
if (this._place instanceof Element && path.includes(this._place)) {
|
|
163
|
+
this._pressedAtPlace = true;
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
this._restoreFocus = false;
|
|
168
|
+
this.open = false;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Whether the menu closes because the page has scrolled under it. The focus goes back to
|
|
173
|
+
* what the menu was opened for without scrolling to it then: the scroll of the user goes on.
|
|
174
|
+
* @private
|
|
175
|
+
*/
|
|
176
|
+
_closedByScroll = false;
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* What the menu listens on for a scroll while it is open at a point.
|
|
180
|
+
* @private
|
|
181
|
+
* @type {Array<Element | Window | Document> | undefined}
|
|
182
|
+
*/
|
|
183
|
+
_scrollers = undefined;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* What a menu at a point was opened for scrolls away under it: the page, or a region around
|
|
187
|
+
* the row. The menu closes, as the menu of the system does. At an element it follows the
|
|
188
|
+
* element instead, and it has no point to be left at.
|
|
189
|
+
*
|
|
190
|
+
* A scroll inside of the menu is none of these: a long menu scrolls in its panel. Neither is
|
|
191
|
+
* one of a region that does not move what the menu was opened for, another column of the
|
|
192
|
+
* page. A menu that was opened for nothing closes for every scroll in the page beside it.
|
|
193
|
+
* @private
|
|
194
|
+
* @param {Event} event
|
|
195
|
+
*/
|
|
196
|
+
_onScroll = (event) => {
|
|
197
|
+
if (event.target instanceof Node && this.contains(event.target)) return;
|
|
127
198
|
|
|
128
199
|
this._restoreFocus = false;
|
|
200
|
+
this._closedByScroll = true;
|
|
129
201
|
this.open = false;
|
|
130
202
|
};
|
|
131
203
|
|
|
204
|
+
/**
|
|
205
|
+
* The keys of a menu, for as long as it is open and wherever the focus is: an open menu that
|
|
206
|
+
* has lost the focus would be left with no key at all. The window hears a key before the
|
|
207
|
+
* page does, so `Escape` closes the menu and nothing behind it.
|
|
208
|
+
* @private
|
|
209
|
+
* @param {KeyboardEvent} event
|
|
210
|
+
*/
|
|
211
|
+
_onKeyDown = (event) => {
|
|
212
|
+
// A key is no press of a pointer: `Enter` on the button of a row is a click as well.
|
|
213
|
+
this._pressedAtPlace = false;
|
|
214
|
+
if (event.altKey || event.ctrlKey || event.metaKey) return;
|
|
215
|
+
|
|
216
|
+
const items = this._items();
|
|
217
|
+
const active = activeElement();
|
|
218
|
+
const index = items.findIndex((item) => item === active || item.shadowRoot?.contains(active));
|
|
219
|
+
const inMenu = index >= 0 || active === this.$refs.panel;
|
|
220
|
+
|
|
221
|
+
// The focus leaves the menu with this key, and may leave the page: the browser says
|
|
222
|
+
// where it goes only while it stays in the page. Outside of the menu the focus moves on
|
|
223
|
+
// from where it is, and the menu closes as well.
|
|
224
|
+
if (event.key === 'Tab') {
|
|
225
|
+
if (inMenu) {
|
|
226
|
+
this._leaving = true;
|
|
227
|
+
requestAnimationFrame(() => (this._leaving = false));
|
|
228
|
+
} else {
|
|
229
|
+
this._restoreFocus = false;
|
|
230
|
+
this.open = false;
|
|
231
|
+
}
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
// The key is the one of the menu: a dialog around it stays open.
|
|
235
|
+
if (event.key === 'Escape') {
|
|
236
|
+
event.preventDefault();
|
|
237
|
+
event.stopPropagation();
|
|
238
|
+
this._restoreFocus = true;
|
|
239
|
+
this.open = false;
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
// The focus is somewhere else in the page: the arrow keys bring it into the menu, and
|
|
243
|
+
// every other key is the one of the page.
|
|
244
|
+
if (!inMenu) {
|
|
245
|
+
if (event.key !== 'ArrowDown' && event.key !== 'ArrowUp') return;
|
|
246
|
+
event.preventDefault();
|
|
247
|
+
event.stopPropagation();
|
|
248
|
+
this._enterAt = event.key === 'ArrowUp' ? 'last' : 'first';
|
|
249
|
+
this._focusEntry();
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const go = (to) => {
|
|
254
|
+
event.preventDefault();
|
|
255
|
+
items[(to + items.length) % items.length]?.focus();
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
if (event.key === 'ArrowDown') go(index + 1);
|
|
259
|
+
else if (event.key === 'ArrowUp') go(index < 0 ? items.length - 1 : index - 1);
|
|
260
|
+
else if (event.key === 'Home') go(0);
|
|
261
|
+
else if (event.key === 'End') go(items.length - 1);
|
|
262
|
+
else if (event.key === ' ') {
|
|
263
|
+
// A button is pressed with the space bar by the browser. A link is not, and in a menu
|
|
264
|
+
// the key chooses every entry. On the panel it chooses nothing, and scrolls nothing.
|
|
265
|
+
event.preventDefault();
|
|
266
|
+
if (index >= 0) active.click();
|
|
267
|
+
} else if (event.key.length === 1 && event.key.trim()) {
|
|
268
|
+
// A letter: the next entry that starts with it, from the one that has the focus on.
|
|
269
|
+
const letter = event.key.toLocaleLowerCase();
|
|
270
|
+
const starts = (item) => this._textOf(item).toLocaleLowerCase().startsWith(letter);
|
|
271
|
+
const next = [...items.slice(index + 1), ...items.slice(0, index + 1)].find(starts);
|
|
272
|
+
if (next) {
|
|
273
|
+
event.preventDefault();
|
|
274
|
+
next.focus();
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
|
|
132
279
|
constructor() {
|
|
133
280
|
super({ styles: [style], shadowRender: true, propertyOptions: { open: { reflect: true } } });
|
|
134
281
|
}
|
|
@@ -152,12 +299,13 @@ export default class Menu extends TemplateElement {
|
|
|
152
299
|
disconnected() {
|
|
153
300
|
this._stopPositioning();
|
|
154
301
|
document.removeEventListener('pointerdown', this._onPressOutside, true);
|
|
302
|
+
window.removeEventListener('keydown', this._onKeyDown, true);
|
|
155
303
|
if (openMenu === this) openMenu = undefined;
|
|
156
304
|
}
|
|
157
305
|
|
|
158
306
|
afterUpdate() {
|
|
159
307
|
this._syncTrigger();
|
|
160
|
-
// A new placement or distance
|
|
308
|
+
// A new placement or distance.
|
|
161
309
|
if (this.open) this._position();
|
|
162
310
|
}
|
|
163
311
|
|
|
@@ -191,9 +339,12 @@ export default class Menu extends TemplateElement {
|
|
|
191
339
|
slotchange: () => this._syncTrigger(),
|
|
192
340
|
},
|
|
193
341
|
"[part~='panel']": {
|
|
194
|
-
keydown: (event) => this._onKeyDown(event),
|
|
195
342
|
click: (event) => this._onClick(event),
|
|
343
|
+
focusin: (event) => this._onFocusIn(event),
|
|
196
344
|
focusout: (event) => this._onFocusOut(event),
|
|
345
|
+
// The element does not render again when an entry comes or goes: the slot tells
|
|
346
|
+
// of it. An entry that has come renders with the next frame.
|
|
347
|
+
slotchange: () => requestAnimationFrame(() => this._entriesChanged()),
|
|
197
348
|
},
|
|
198
349
|
};
|
|
199
350
|
}
|
|
@@ -216,6 +367,9 @@ export default class Menu extends TemplateElement {
|
|
|
216
367
|
* `opener` is what the menu is opened for. It gets the focus back when the menu closes, and
|
|
217
368
|
* `menu-select` names it (`detail.opener`). Without one it is the element the menu is placed
|
|
218
369
|
* at.
|
|
370
|
+
*
|
|
371
|
+
* Called with the click of a press on the element the menu is open at, it closes the menu:
|
|
372
|
+
* the button of a row closes what it has opened, with the listener that opens it.
|
|
219
373
|
* @param {Element | { x: number, y: number }} place - The element, or the point in the
|
|
220
374
|
* coordinates of the viewport (`clientX`, `clientY`).
|
|
221
375
|
* @param {Element} [opener] - The row or the button the menu is opened for.
|
|
@@ -229,9 +383,18 @@ export default class Menu extends TemplateElement {
|
|
|
229
383
|
// top layer, and moves. The page is told of both, the row it has left and the row it is
|
|
230
384
|
// open for now.
|
|
231
385
|
if (this.open && this.$refs.panel?.matches(':popover-open')) {
|
|
386
|
+
// The pointer is on what the menu is open at, and the page calls this with the click
|
|
387
|
+
// of that press: a second press on a button closes what the first one has opened.
|
|
388
|
+
if (this._pressedAtPlace && next.place === this._place) {
|
|
389
|
+
this.open = false;
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
|
|
232
393
|
this.dispatch(Events.HIDE);
|
|
233
394
|
this._place = next.place;
|
|
234
395
|
this._opener = next.opener;
|
|
396
|
+
this._current = undefined;
|
|
397
|
+
this._pressedAtPlace = false;
|
|
235
398
|
this._syncTrigger();
|
|
236
399
|
this._startPositioning();
|
|
237
400
|
this._focusEntry();
|
|
@@ -341,11 +504,20 @@ export default class Menu extends TemplateElement {
|
|
|
341
504
|
* @private
|
|
342
505
|
* @returns {boolean}
|
|
343
506
|
*/
|
|
344
|
-
|
|
507
|
+
_hasFocusOnEntry() {
|
|
345
508
|
const active = activeElement();
|
|
346
509
|
return this._items().some((item) => item === active || item.shadowRoot?.contains(active));
|
|
347
510
|
}
|
|
348
511
|
|
|
512
|
+
/**
|
|
513
|
+
* Whether the focus is in the menu: on an entry, or on the panel around the entries.
|
|
514
|
+
* @private
|
|
515
|
+
* @returns {boolean}
|
|
516
|
+
*/
|
|
517
|
+
_hasFocusInPanel() {
|
|
518
|
+
return activeElement() === this.$refs.panel || this._hasFocusOnEntry();
|
|
519
|
+
}
|
|
520
|
+
|
|
349
521
|
/**
|
|
350
522
|
* The arrow keys on the button open the menu: down at its first entry, up at its last one.
|
|
351
523
|
* `Enter` and `Space` are a click of the button.
|
|
@@ -362,58 +534,6 @@ export default class Menu extends TemplateElement {
|
|
|
362
534
|
else this.open = true;
|
|
363
535
|
}
|
|
364
536
|
|
|
365
|
-
/**
|
|
366
|
-
* The keys of a menu, while the focus is on one of its entries.
|
|
367
|
-
* @private
|
|
368
|
-
* @param {KeyboardEvent} event
|
|
369
|
-
*/
|
|
370
|
-
_onKeyDown(event) {
|
|
371
|
-
if (event.altKey || event.ctrlKey || event.metaKey) return;
|
|
372
|
-
// The focus leaves the menu with this key, and may leave the page: the browser says
|
|
373
|
-
// where it goes only while it stays in the page.
|
|
374
|
-
if (event.key === 'Tab') {
|
|
375
|
-
this._leaving = true;
|
|
376
|
-
requestAnimationFrame(() => (this._leaving = false));
|
|
377
|
-
return;
|
|
378
|
-
}
|
|
379
|
-
// The key is the one of the menu: a dialog around it stays open.
|
|
380
|
-
if (event.key === 'Escape') {
|
|
381
|
-
event.preventDefault();
|
|
382
|
-
event.stopPropagation();
|
|
383
|
-
this._restoreFocus = true;
|
|
384
|
-
this.open = false;
|
|
385
|
-
return;
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
const items = this._items();
|
|
389
|
-
const active = activeElement();
|
|
390
|
-
const index = items.findIndex((item) => item === active || item.shadowRoot?.contains(active));
|
|
391
|
-
const go = (to) => {
|
|
392
|
-
event.preventDefault();
|
|
393
|
-
items[(to + items.length) % items.length]?.focus();
|
|
394
|
-
};
|
|
395
|
-
|
|
396
|
-
if (event.key === 'ArrowDown') go(index + 1);
|
|
397
|
-
else if (event.key === 'ArrowUp') go(index < 0 ? items.length - 1 : index - 1);
|
|
398
|
-
else if (event.key === 'Home') go(0);
|
|
399
|
-
else if (event.key === 'End') go(items.length - 1);
|
|
400
|
-
else if (event.key === ' ' && index >= 0) {
|
|
401
|
-
// A button is pressed with the space bar by the browser. A link is not, and in a menu
|
|
402
|
-
// the key chooses every entry.
|
|
403
|
-
event.preventDefault();
|
|
404
|
-
active.click();
|
|
405
|
-
} else if (event.key.length === 1 && event.key.trim()) {
|
|
406
|
-
// A letter: the next entry that starts with it, from the one that has the focus on.
|
|
407
|
-
const letter = event.key.toLocaleLowerCase();
|
|
408
|
-
const starts = (item) => this._textOf(item).toLocaleLowerCase().startsWith(letter);
|
|
409
|
-
const next = [...items.slice(index + 1), ...items.slice(0, index + 1)].find(starts);
|
|
410
|
-
if (next) {
|
|
411
|
-
event.preventDefault();
|
|
412
|
-
next.focus();
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
|
|
417
537
|
/**
|
|
418
538
|
* A click in the panel: the user has chosen the entry it is on, unless that one is disabled.
|
|
419
539
|
* @private
|
|
@@ -447,13 +567,54 @@ export default class Menu extends TemplateElement {
|
|
|
447
567
|
}
|
|
448
568
|
|
|
449
569
|
/**
|
|
450
|
-
*
|
|
570
|
+
* The focus has come into the menu. On an entry: the menu remembers it. On the panel itself:
|
|
571
|
+
* the user has pressed beside the entries, on the heading of a group, a line or the padding,
|
|
572
|
+
* and the entry that had the focus gets it back. The keyboard goes on from there.
|
|
573
|
+
* @private
|
|
574
|
+
* @param {FocusEvent} event
|
|
575
|
+
*/
|
|
576
|
+
_onFocusIn(event) {
|
|
577
|
+
if (event.target !== this.$refs.panel) {
|
|
578
|
+
const path = event.composedPath();
|
|
579
|
+
this._current = this._items().find((item) => path.includes(item)) ?? this._current;
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
if (this._current && this._items().includes(this._current)) this._current.focus();
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
/**
|
|
586
|
+
* Puts the focus on the entry the menu is entered at. An entry that is not there yet, or
|
|
587
|
+
* whose element is not defined yet, cannot take it: the menu waits for it.
|
|
451
588
|
* @private
|
|
452
589
|
*/
|
|
453
590
|
_focusEntry() {
|
|
454
591
|
const items = this._items();
|
|
455
592
|
(this._enterAt === 'last' ? items.at(-1) : items[0])?.focus();
|
|
456
|
-
this.
|
|
593
|
+
if (this._hasFocusOnEntry()) {
|
|
594
|
+
this._entering = false;
|
|
595
|
+
this._enterAt = 'first';
|
|
596
|
+
return;
|
|
597
|
+
}
|
|
598
|
+
if (this._entering) return;
|
|
599
|
+
|
|
600
|
+
// The page renders the entries when the menu is asked for, or an autoloader defines the
|
|
601
|
+
// element of an entry when it first sees one. An element that is defined renders with
|
|
602
|
+
// the next frame.
|
|
603
|
+
this._entering = true;
|
|
604
|
+
customElements.whenDefined('el-menu-item').then(() => requestAnimationFrame(() => this._entriesChanged()));
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
/**
|
|
608
|
+
* Other entries in the open menu, or entries that have rendered by now. The panel has
|
|
609
|
+
* another size, and a menu at a point is placed once. And the focus goes to the entry the
|
|
610
|
+
* menu is entered at, where none could take it when the menu opened and nothing in the menu
|
|
611
|
+
* has it by now.
|
|
612
|
+
* @private
|
|
613
|
+
*/
|
|
614
|
+
_entriesChanged() {
|
|
615
|
+
if (!this.open) return;
|
|
616
|
+
this._position();
|
|
617
|
+
if (this._entering && !this._hasFocusInPanel()) this._focusEntry();
|
|
457
618
|
}
|
|
458
619
|
|
|
459
620
|
/** @private */
|
|
@@ -471,8 +632,11 @@ export default class Menu extends TemplateElement {
|
|
|
471
632
|
openMenu = this;
|
|
472
633
|
|
|
473
634
|
this._restoreFocus = false;
|
|
635
|
+
this._current = undefined;
|
|
636
|
+
this._entering = false;
|
|
474
637
|
if (!panel.matches(':popover-open')) panel.showPopover();
|
|
475
638
|
document.addEventListener('pointerdown', this._onPressOutside, true);
|
|
639
|
+
window.addEventListener('keydown', this._onKeyDown, true);
|
|
476
640
|
this._internals.states.add('open');
|
|
477
641
|
this._syncTrigger();
|
|
478
642
|
this._startPositioning();
|
|
@@ -493,14 +657,19 @@ export default class Menu extends TemplateElement {
|
|
|
493
657
|
const opener = this._opener ?? this._trigger();
|
|
494
658
|
this._place = undefined;
|
|
495
659
|
this._opener = undefined;
|
|
660
|
+
this._current = undefined;
|
|
661
|
+
this._entering = false;
|
|
662
|
+
this._pressedAtPlace = false;
|
|
496
663
|
|
|
497
664
|
if (openMenu === this) openMenu = undefined;
|
|
498
665
|
document.removeEventListener('pointerdown', this._onPressOutside, true);
|
|
666
|
+
window.removeEventListener('keydown', this._onKeyDown, true);
|
|
499
667
|
this._stopPositioning();
|
|
500
668
|
if (panel.matches(':popover-open')) panel.hidePopover();
|
|
501
669
|
this._internals.states.delete('open');
|
|
502
670
|
this._syncTrigger();
|
|
503
|
-
if (restore) opener?.focus?.();
|
|
671
|
+
if (restore) opener?.focus?.({ preventScroll: this._closedByScroll });
|
|
672
|
+
this._closedByScroll = false;
|
|
504
673
|
this.dispatch(Events.HIDE);
|
|
505
674
|
}
|
|
506
675
|
|
|
@@ -525,11 +694,27 @@ export default class Menu extends TemplateElement {
|
|
|
525
694
|
_startPositioning() {
|
|
526
695
|
this._stopPositioning();
|
|
527
696
|
const anchor = this._anchor();
|
|
528
|
-
// A point does not move: the menu is placed at it once
|
|
697
|
+
// A point does not move: the menu is placed at it once, and closes when what it was
|
|
698
|
+
// opened for scrolls away under it.
|
|
529
699
|
if (anchor instanceof Element) {
|
|
530
700
|
this._stopAutoUpdate = followAnchor(anchor, this.$refs.panel, () => this._position());
|
|
531
701
|
} else {
|
|
532
702
|
this._position();
|
|
703
|
+
this._listenForScroll();
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
/**
|
|
708
|
+
* A scroll does not bubble and does not leave a shadow root, so the menu listens where it
|
|
709
|
+
* happens: on every region around what it was opened for, and on the window for the page.
|
|
710
|
+
* Opened for nothing, it hears every scroll of the page on its way down from the document.
|
|
711
|
+
* @private
|
|
712
|
+
*/
|
|
713
|
+
_listenForScroll() {
|
|
714
|
+
const capture = !this._opener;
|
|
715
|
+
this._scrollers = this._opener ? scrollersOf(this._opener) : [document];
|
|
716
|
+
for (const scroller of this._scrollers) {
|
|
717
|
+
scroller.addEventListener('scroll', this._onScroll, { capture, passive: true });
|
|
533
718
|
}
|
|
534
719
|
}
|
|
535
720
|
|
|
@@ -537,26 +722,64 @@ export default class Menu extends TemplateElement {
|
|
|
537
722
|
_stopPositioning() {
|
|
538
723
|
this._stopAutoUpdate?.();
|
|
539
724
|
this._stopAutoUpdate = undefined;
|
|
725
|
+
for (const scroller of this._scrollers ?? []) {
|
|
726
|
+
scroller.removeEventListener('scroll', this._onScroll, false);
|
|
727
|
+
scroller.removeEventListener('scroll', this._onScroll, true);
|
|
728
|
+
}
|
|
729
|
+
this._scrollers = undefined;
|
|
540
730
|
}
|
|
541
731
|
|
|
542
|
-
/**
|
|
732
|
+
/**
|
|
733
|
+
* Places the panel at what the menu is open at.
|
|
734
|
+
*
|
|
735
|
+
* Not before the panel is shown: the element renders when `open` is set, before the menu has
|
|
736
|
+
* taken its place from `showAt()`, and would be placed at its own element. And one placement
|
|
737
|
+
* at a time: a placement takes a few turns, more where the panel has to try the other side,
|
|
738
|
+
* so one that was asked for first can be done last. A menu whose own element is out of view,
|
|
739
|
+
* below a long list, was taken there that way. What is asked for while the panel is being
|
|
740
|
+
* placed is placed after it, at what the menu is open at by then.
|
|
741
|
+
* @private
|
|
742
|
+
*/
|
|
543
743
|
async _position() {
|
|
544
744
|
const panel = this.$refs.panel;
|
|
545
|
-
if (!panel || !this.open) return;
|
|
745
|
+
if (!panel || !this.open || !panel.matches(':popover-open')) return;
|
|
746
|
+
if (this._placing) {
|
|
747
|
+
this._placeAgain = true;
|
|
748
|
+
return;
|
|
749
|
+
}
|
|
546
750
|
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
751
|
+
this._placing = true;
|
|
752
|
+
try {
|
|
753
|
+
do {
|
|
754
|
+
this._placeAgain = false;
|
|
755
|
+
const atPoint = this._place && !(this._place instanceof Element);
|
|
756
|
+
await placeAtAnchor(this._anchor(), panel, {
|
|
757
|
+
// At a point the corner of the menu is at the pointer, as the menu of the
|
|
758
|
+
// system is.
|
|
759
|
+
placement: atPoint ? 'bottom-start' : this.placement,
|
|
760
|
+
distance: atPoint ? 0 : this.distance,
|
|
761
|
+
});
|
|
762
|
+
} while (this._placeAgain && this.open);
|
|
763
|
+
} finally {
|
|
764
|
+
this._placing = false;
|
|
765
|
+
}
|
|
553
766
|
}
|
|
554
767
|
|
|
555
768
|
template() {
|
|
556
|
-
// A plain binding leaves the name out while there is none.
|
|
769
|
+
// A plain binding leaves the name out while there is none. The panel takes the focus of a
|
|
770
|
+
// press beside its entries, and is no stop of the Tab key: without it the focus of such
|
|
771
|
+
// a press would be nowhere in the page.
|
|
557
772
|
return html`
|
|
558
773
|
<slot part="trigger" name="trigger" ref="trigger" data-trigger></slot>
|
|
559
|
-
<div
|
|
774
|
+
<div
|
|
775
|
+
part="panel"
|
|
776
|
+
ref="panel"
|
|
777
|
+
class="panel"
|
|
778
|
+
popover="manual"
|
|
779
|
+
role="menu"
|
|
780
|
+
tabindex="-1"
|
|
781
|
+
aria-label="${this.label || null}"
|
|
782
|
+
>
|
|
560
783
|
<slot></slot>
|
|
561
784
|
</div>
|
|
562
785
|
`;
|
|
@@ -54,7 +54,7 @@ document.querySelector('el-menu').addEventListener('menu-select', (event) => {
|
|
|
54
54
|
| `show()` | Opens the menu, with the focus on its first entry. |
|
|
55
55
|
| `hide()` | Closes the menu. |
|
|
56
56
|
| `toggle()` | Opens the menu when it is closed, and closes it when open. |
|
|
57
|
-
| `showAt(place, opener?)` | Opens the menu somewhere else than at its own button: at an element of the page, or at a point `{ x, y }` of the viewport. `opener` is what it is opened for. |
|
|
57
|
+
| `showAt(place, opener?)` | Opens the menu somewhere else than at its own button: at an element of the page, or at a point `{ x, y }` of the viewport. `opener` is what it is opened for. Called with the click of a press on the element the menu is open at, it closes the menu. |
|
|
58
58
|
|
|
59
59
|
#### Read-only properties
|
|
60
60
|
|
|
@@ -131,6 +131,16 @@ An `el-button` passes both on to the control inside of it. A button that shows a
|
|
|
131
131
|
|
|
132
132
|
The entries are no stops of the Tab key: the menu is one, through its button, and moves the focus itself. An entry that is disabled is reached by the arrow keys, so a screen reader finds it, and cannot be chosen.
|
|
133
133
|
|
|
134
|
+
#### While the focus is not on an entry
|
|
135
|
+
|
|
136
|
+
An open menu keeps the keyboard wherever the focus is: after a press beside the entries, on a row whose finger has just been lifted, before the entries are there. It hears the keys at the window for as long as it is open, before the page does.
|
|
137
|
+
|
|
138
|
+
- **`Escape` closes the menu from anywhere**, and the focus goes back to its button, or to what the menu was opened for. The key does not reach the page behind: a dialog around the menu stays open. Only a listener in the capture phase of the window that was there before the menu opened hears it first.
|
|
139
|
+
- **`ArrowDown` and `ArrowUp` bring the focus into the menu** from anywhere in the page, to its first and to its last entry.
|
|
140
|
+
- **The Tab key closes the menu from anywhere**, and the focus moves on from where it is.
|
|
141
|
+
- **Every other key is the one of the page** while the focus is not in the menu. A page with shortcuts of single letters asks itself whether a menu is open (`menu.open`).
|
|
142
|
+
- **A press beside the entries**, on the heading of a group, on a line or on the padding of the panel, leaves the focus on the entry that had it, and the arrow keys go on from there. Where no entry had it, the panel takes the focus of the press itself: it has `tabindex="-1"`, and is no stop of the Tab key.
|
|
143
|
+
|
|
134
144
|
## Choosing an entry
|
|
135
145
|
|
|
136
146
|
Choosing is a click on an entry, with the pointer, `Enter` or `Space`. The menu fires `menu-select` and closes, and the focus is on the button again.
|
|
@@ -142,7 +152,7 @@ Choosing is a click on an entry, with the pointer, `Enter` or `Space`. The menu
|
|
|
142
152
|
|
|
143
153
|
## Groups
|
|
144
154
|
|
|
145
|
-
Entries that belong together go into an [`el-menu-group`](../menu-group/menu-group.readme.md), which has a line over it and a heading where it has a `label`. An [`el-divider`](../divider/divider.readme.md) is a line alone.
|
|
155
|
+
Entries that belong together go into an [`el-menu-group`](../menu-group/menu-group.readme.md), which has a line over it and a heading where it has a `label`. An [`el-divider`](../divider/divider.readme.md) is a line alone, with `--el-space-1` above and below it. That room is the menu's, not a margin of the element: a reset of the page that takes the margin off every element changes nothing.
|
|
146
156
|
|
|
147
157
|
```html
|
|
148
158
|
<el-menu>
|
|
@@ -158,6 +168,19 @@ Entries that belong together go into an [`el-menu-group`](../menu-group/menu-gro
|
|
|
158
168
|
</el-menu>
|
|
159
169
|
```
|
|
160
170
|
|
|
171
|
+
## Entries that are checked
|
|
172
|
+
|
|
173
|
+
An [`el-menu-item`](../menu-item/menu-item.readme.md) with `type="checkbox"` is on or off, one with `type="radio"` is the chosen one of several, and `checked` is the state. The entry says the state and shows it with a mark in front of its text.
|
|
174
|
+
|
|
175
|
+
```html
|
|
176
|
+
<el-menu-group label="Layout">
|
|
177
|
+
<el-menu-item type="radio" value="list" checked="true">List</el-menu-item>
|
|
178
|
+
<el-menu-item type="radio" value="grid">Grid</el-menu-item>
|
|
179
|
+
</el-menu-group>
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Choosing such an entry is a choice as every other: `menu-select`, and the menu closes. The menu changes no state: the page sets `checked` in its listener, or renders the menu again with the answer of a server.
|
|
183
|
+
|
|
161
184
|
## The panel
|
|
162
185
|
|
|
163
186
|
- **In the top layer.** The panel is a native popover: an element around the menu that clips (`overflow: hidden`) does not cut it off, and no `z-index` is needed. It takes no room on the page.
|
|
@@ -165,7 +188,7 @@ Entries that belong together go into an [`el-menu-group`](../menu-group/menu-gro
|
|
|
165
188
|
- **As wide as its entries**, between `--el-menu-min-width` and `--el-menu-max-width`. A text that is longer wraps.
|
|
166
189
|
- **A long menu scrolls** in what the browser shows.
|
|
167
190
|
- **One at a time.** A menu that opens closes another one that is open.
|
|
168
|
-
- **A press beside it closes it**, when the pointer goes down. The panel is a popover the menu opens and closes itself (`popover="manual"`): one the platform closes would be gone when the finger of a long press is lifted.
|
|
191
|
+
- **A press beside it closes it**, when the pointer goes down. Its own button, and the element it was opened at with `showAt()`, are not beside it: they close it with their click. The panel is a popover the menu opens and closes itself (`popover="manual"`): one the platform closes would be gone when the finger of a long press is lifted.
|
|
169
192
|
|
|
170
193
|
Its place comes from [Floating UI](https://floating-ui.com), which the element imports (`@floating-ui/dom`).
|
|
171
194
|
|
|
@@ -216,8 +239,10 @@ menu.addEventListener('menu-select', (event) => {
|
|
|
216
239
|
```
|
|
217
240
|
|
|
218
241
|
- **`showAt({ x, y }, opener)`** opens the menu at a point of the viewport, the `clientX` and `clientY` of an event. The corner of the menu is at the point, as the menu of the system is, and the menu stays inside of the viewport.
|
|
219
|
-
- **`showAt(element)`** opens it at an element, placed as at its own button (`placement`, `distance`).
|
|
242
|
+
- **`showAt(element)`** opens it at an element, placed as at its own button (`placement`, `distance`). It follows the element when the page scrolls.
|
|
243
|
+
- **A menu at a point closes when what it was opened for scrolls away under it**: the page, or a region around the row, the list in a column of an `el-app-frame`. A point does not move with the row, and the menu would be left over another one. The focus goes back to the row without scrolling to it, so the scroll goes on. A scroll inside of a long menu leaves it open, and so does one of a region that does not move the row. A menu that was opened for nothing (`showAt({ x, y })` alone) closes for every scroll of the page beside it.
|
|
220
244
|
- **The opener** is what the menu is opened for: the second argument, or the element it is placed at. It is `menu.opener` while the menu is open and `detail.opener` of `menu-select`, and it gets the focus back when the menu closes after a choice or `Escape`. A row that should get the focus back has to be able to take it (`tabindex`).
|
|
245
|
+
- **A second press on the button of a row closes the menu**, with the listener above and nothing more. While the pointer is down on the element the menu is open at, the menu stays open, and the `showAt()` the page calls with the click of that press closes it. The button says `aria-expanded="false"` after. A call of `showAt()` with no press before it, from a script or for `Enter` on the button, opens the menu.
|
|
221
246
|
- **An open menu moves** when `showAt()` is called again, for a right click on another row. It tells of both: `menu-hide` for the row it has left, `menu-show` for the one it is open for.
|
|
222
247
|
- **The roles and the keys** are the ones of the menu: the focus is on the first entry, the arrow keys move through the entries, `Escape` closes.
|
|
223
248
|
- **Nothing in the menu of the right click alone.** A right click and a long press are found by a user who knows them. The button of the row offers the same entries to everybody else, and to the keyboard.
|
|
@@ -250,10 +275,25 @@ A long press on a text selects it in Safari, and the selection takes the finger
|
|
|
250
275
|
}
|
|
251
276
|
```
|
|
252
277
|
|
|
278
|
+
## Entries that come later
|
|
279
|
+
|
|
280
|
+
The page can render the entries when the menu is asked for, as the answer of a server, and an autoloader can define `el-menu-item` when it first sees one. The menu is open then before an entry can take the focus, and the focus stays where it was.
|
|
281
|
+
|
|
282
|
+
```js
|
|
283
|
+
menu.addEventListener('menu-show', async () => {
|
|
284
|
+
const actions = await load(menu.opener);
|
|
285
|
+
menu.replaceChildren(...actions.map(entry));
|
|
286
|
+
});
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
- **The focus goes to the first entry once the entries are there**: when they have come into the menu, and when their element is defined. To the last one where `ArrowUp` has opened the menu. Not where the user has put the focus into the menu by then.
|
|
290
|
+
- **The menu is placed again**: it has another size with its entries, and stays inside of the viewport.
|
|
291
|
+
- **Until then the keys work as above**: `Escape` closes the menu.
|
|
292
|
+
|
|
253
293
|
## Not in this element
|
|
254
294
|
|
|
255
295
|
- A menu in a menu (a submenu).
|
|
256
|
-
-
|
|
296
|
+
- A menu that stays open after a choice.
|
|
257
297
|
- A menu bar.
|
|
258
298
|
|
|
259
299
|
For a panel in the flow of the page there is [`el-dropdown`](../dropdown/dropdown.readme.md), and for a floating panel with any content [`el-popover`](../popover/popover.readme.md).
|
|
@@ -264,9 +304,10 @@ The menu and its entries render on a server with `@webtides/element-js-ssr-rende
|
|
|
264
304
|
|
|
265
305
|
## Accessibility
|
|
266
306
|
|
|
267
|
-
- The panel has `role="menu"`, with the name of `label` or the one of its button. The entries have `role="menuitem"`, the groups `role="group"` with their heading as their name.
|
|
307
|
+
- The panel has `role="menu"`, with the name of `label` or the one of its button. The entries have `role="menuitem"`, or `menuitemcheckbox` and `menuitemradio` with their state (`aria-checked`), the groups `role="group"` with their heading as their name.
|
|
268
308
|
- The button says that it opens a menu and whether the menu is open.
|
|
269
309
|
- The focus is on an entry while the menu is open, and goes back to the button, or to what the menu was opened for, when the menu closes after a choice or `Escape`.
|
|
310
|
+
- The keys of the menu work wherever the focus is while the menu is open: `Escape` closes it, the arrow keys bring the focus into it.
|
|
270
311
|
- The entry that has the focus of the keyboard has a ring inside of it, which shows where the user forces colours.
|
|
271
312
|
- On a touch screen every entry is at least `--el-touch-target` high.
|
|
272
313
|
- Without a theme the menu has the colours of the system, and reads in a light and in a dark scheme.
|
|
@@ -44,11 +44,15 @@ const style = css`
|
|
|
44
44
|
--_el-group-radius-start: initial;
|
|
45
45
|
--_el-group-radius-end: initial;
|
|
46
46
|
--_el-group-divider: initial;
|
|
47
|
+
--_el-group-overlap: initial;
|
|
47
48
|
}
|
|
48
49
|
|
|
49
|
-
/* A line between two entries, which the page puts there.
|
|
50
|
+
/* A line between two entries, which the page puts there. The menu names the room above and
|
|
51
|
+
below it, in a property that inherits, and the line takes that room in its own shadow root.
|
|
52
|
+
A margin of the slotted element would be gone on a page that takes the margin off every
|
|
53
|
+
element: a rule of the page wins over a ::slotted rule. */
|
|
50
54
|
::slotted(el-divider) {
|
|
51
|
-
|
|
55
|
+
--_el-context-divider-space: var(--el-space-1, 0.25rem);
|
|
52
56
|
}
|
|
53
57
|
`;
|
|
54
58
|
|
|
@@ -12,6 +12,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/).
|
|
|
12
12
|
|
|
13
13
|
## Unreleased
|
|
14
14
|
|
|
15
|
+
## [0.5.0] - 2026-11-27
|
|
16
|
+
|
|
17
|
+
### Added
|
|
18
|
+
|
|
19
|
+
- `type`: an entry that sets something instead of doing something. With `type="checkbox"` it is on or off (`role="menuitemcheckbox"`), with `type="radio"` it is one of several of which one is chosen (`role="menuitemradio"`). `checked` is its state, which it says (`aria-checked`), also while it is off and while it is disabled. A page could put a mark into the `icon` slot before, and a screen reader heard nothing of it: the role of an entry is inside of its shadow root. ([#205](https://github.com/webtides/element-library/issues/205))
|
|
20
|
+
- The state shows as a mark in front of the text, a check or a dot, in the new part `mark`. Its place is there while the entry is not checked as well, so the text does not move when the state changes. Where the user forces colours the mark has the colour of the text: the check is text, the dot is drawn with a border. ([#205](https://github.com/webtides/element-library/issues/205))
|
|
21
|
+
- The page sets the state: the entry does not change `checked` when it is chosen, and the menu fires `menu-select` and closes as for every entry. The state is in the server-rendered markup. ([#205](https://github.com/webtides/element-library/issues/205))
|
|
22
|
+
|
|
15
23
|
## [0.4.0] - 2026-11-27
|
|
16
24
|
|
|
17
25
|
### Added
|