@webtides/element-library 0.3.1 → 0.4.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 +193 -0
- package/README.md +72 -56
- package/all.js +16 -0
- package/catalog.js +17 -1
- package/custom-elements.json +14723 -10220
- package/package.json +1 -1
- package/src/components/accordion/accordion.changelog.md +11 -0
- package/src/components/accordion/accordion.js +15 -3
- package/src/components/accordion/accordion.readme.md +27 -1
- package/src/components/accordion/accordion.style.js +19 -0
- package/src/components/app-bar/app-bar.changelog.md +24 -0
- package/src/components/app-bar/app-bar.define.js +3 -0
- package/src/components/app-bar/app-bar.js +91 -0
- package/src/components/app-bar/app-bar.readme.md +130 -0
- package/src/components/app-bar/app-bar.style.js +103 -0
- package/src/components/app-frame/app-frame.changelog.md +45 -0
- package/src/components/app-frame/app-frame.define.js +3 -0
- package/src/components/app-frame/app-frame.events.js +6 -0
- package/src/components/app-frame/app-frame.js +1073 -0
- package/src/components/app-frame/app-frame.readme.md +416 -0
- package/src/components/app-frame/app-frame.style.js +527 -0
- package/src/components/button/button.changelog.md +15 -0
- package/src/components/button/button.events.js +3 -0
- package/src/components/button/button.js +145 -3
- package/src/components/button/button.readme.md +72 -4
- package/src/components/button/button.style.js +161 -7
- package/src/components/description-list/description-list.changelog.md +25 -0
- package/src/components/description-list/description-list.define.js +3 -0
- package/src/components/description-list/description-list.js +44 -0
- package/src/components/description-list/description-list.readme.md +135 -0
- package/src/components/description-list/description-list.style.js +140 -0
- package/src/components/dialog/dialog.changelog.md +7 -0
- package/src/components/dialog/dialog.readme.md +5 -0
- package/src/components/dialog/dialog.style.js +17 -2
- package/src/components/drawer/drawer.changelog.md +7 -0
- package/src/components/drawer/drawer.readme.md +7 -6
- package/src/components/drawer/drawer.style.js +35 -2
- package/src/components/dropdown/dropdown.readme.md +6 -1
- package/src/components/list/list.changelog.md +24 -0
- package/src/components/list/list.define.js +3 -0
- package/src/components/list/list.js +66 -0
- package/src/components/list/list.readme.md +122 -0
- package/src/components/list/list.style.js +56 -0
- package/src/components/list-item/list-item.changelog.md +24 -0
- package/src/components/list-item/list-item.define.js +3 -0
- package/src/components/list-item/list-item.js +128 -0
- package/src/components/list-item/list-item.readme.md +126 -0
- package/src/components/list-item/list-item.style.js +234 -0
- package/src/components/menu/menu.changelog.md +27 -0
- package/src/components/menu/menu.define.js +3 -0
- package/src/components/menu/menu.events.js +5 -0
- package/src/components/menu/menu.js +570 -0
- package/src/components/menu/menu.readme.md +272 -0
- package/src/components/menu/menu.style.js +55 -0
- package/src/components/menu-group/menu-group.changelog.md +20 -0
- package/src/components/menu-group/menu-group.define.js +3 -0
- package/src/components/menu-group/menu-group.js +44 -0
- package/src/components/menu-group/menu-group.readme.md +74 -0
- package/src/components/menu-group/menu-group.style.js +38 -0
- package/src/components/menu-item/menu-item.changelog.md +24 -0
- package/src/components/menu-item/menu-item.define.js +3 -0
- package/src/components/menu-item/menu-item.js +130 -0
- package/src/components/menu-item/menu-item.readme.md +94 -0
- package/src/components/menu-item/menu-item.style.js +87 -0
- package/src/components/nav/nav.changelog.md +35 -0
- package/src/components/nav/nav.define.js +3 -0
- package/src/components/nav/nav.js +212 -0
- package/src/components/nav/nav.readme.md +248 -0
- package/src/components/nav/nav.style.js +269 -0
- package/src/components/nav-item/nav-item.changelog.md +25 -0
- package/src/components/nav-item/nav-item.define.js +3 -0
- package/src/components/nav-item/nav-item.js +75 -0
- package/src/components/nav-item/nav-item.readme.md +111 -0
- package/src/components/nav-item/nav-item.style.js +159 -0
- package/src/components/pagination/pagination.changelog.md +8 -0
- package/src/components/pagination/pagination.js +123 -49
- package/src/components/pagination/pagination.readme.md +24 -6
- package/src/components/pagination/pagination.style.js +11 -1
- package/src/components/scroll-to-top/scroll-to-top.changelog.md +7 -5
- package/src/components/scroll-to-top/scroll-to-top.js +16 -12
- package/src/components/scroll-to-top/scroll-to-top.readme.md +2 -0
- package/src/components/segmented-field/segmented-field.changelog.md +27 -0
- package/src/components/segmented-field/segmented-field.define.js +3 -0
- package/src/components/segmented-field/segmented-field.js +175 -0
- package/src/components/segmented-field/segmented-field.readme.md +140 -0
- package/src/components/segmented-field/segmented-field.style.js +193 -0
- package/src/components/splitter/splitter.changelog.md +24 -0
- package/src/components/splitter/splitter.define.js +3 -0
- package/src/components/splitter/splitter.events.js +4 -0
- package/src/components/splitter/splitter.js +237 -0
- package/src/components/splitter/splitter.readme.md +164 -0
- package/src/components/splitter/splitter.style.js +100 -0
- package/src/components/status-bar/status-bar.changelog.md +25 -0
- package/src/components/status-bar/status-bar.define.js +3 -0
- package/src/components/status-bar/status-bar.js +74 -0
- package/src/components/status-bar/status-bar.readme.md +139 -0
- package/src/components/status-bar/status-bar.style.js +122 -0
- package/src/components/tab-group/tab-group.changelog.md +7 -0
- package/src/components/tab-group/tab-group.js +30 -9
- package/src/components/tab-group/tab-group.readme.md +14 -0
- package/src/components/toast/toast.changelog.md +6 -0
- package/src/components/toast/toast.readme.md +5 -3
- package/src/components/toast/toast.style.js +22 -6
- package/src/components/toolbar/toolbar.changelog.md +24 -0
- package/src/components/toolbar/toolbar.define.js +3 -0
- package/src/components/toolbar/toolbar.js +233 -0
- package/src/components/toolbar/toolbar.readme.md +156 -0
- package/src/components/toolbar/toolbar.style.js +49 -0
- package/src/components/tooltip/tooltip.changelog.md +11 -0
- package/src/components/tooltip/tooltip.js +51 -4
- package/src/components/tooltip/tooltip.readme.md +26 -1
- package/src/components/tree/tree.changelog.md +23 -0
- package/src/components/tree/tree.define.js +3 -0
- package/src/components/tree/tree.js +138 -0
- package/src/components/tree/tree.readme.md +119 -0
- package/src/components/tree/tree.style.js +16 -0
- package/src/components/tree-item/tree-item.changelog.md +23 -0
- package/src/components/tree-item/tree-item.define.js +3 -0
- package/src/components/tree-item/tree-item.events.js +3 -0
- package/src/components/tree-item/tree-item.js +246 -0
- package/src/components/tree-item/tree-item.readme.md +128 -0
- package/src/components/tree-item/tree-item.style.js +226 -0
- package/src/styles/appearance.style.js +47 -0
- package/src/styles/choice-group.style.js +1 -1
- package/src/styles/modal.style.js +14 -1
- package/src/styles/size.style.js +41 -0
- package/src/utils/focus.js +69 -0
- package/src/utils/scroll.js +24 -0
- package/types/all.d.ts +16 -0
- package/types/src/components/accordion/accordion.d.ts +3 -1
- package/types/src/components/app-bar/app-bar.d.ts +43 -0
- package/types/src/components/app-bar/app-bar.define.d.ts +1 -0
- package/types/src/components/app-bar/app-bar.style.d.ts +2 -0
- package/types/src/components/app-frame/app-frame.d.ts +491 -0
- package/types/src/components/app-frame/app-frame.define.d.ts +1 -0
- package/types/src/components/app-frame/app-frame.events.d.ts +7 -0
- package/types/src/components/app-frame/app-frame.style.d.ts +8 -0
- package/types/src/components/button/button.d.ts +61 -0
- package/types/src/components/button/button.events.d.ts +4 -0
- package/types/src/components/description-list/description-list.d.ts +29 -0
- package/types/src/components/description-list/description-list.define.d.ts +1 -0
- package/types/src/components/description-list/description-list.style.d.ts +2 -0
- package/types/src/components/list/list.d.ts +39 -0
- package/types/src/components/list/list.define.d.ts +1 -0
- package/types/src/components/list/list.style.d.ts +2 -0
- package/types/src/components/list-item/list-item.d.ts +62 -0
- package/types/src/components/list-item/list-item.define.d.ts +1 -0
- package/types/src/components/list-item/list-item.style.d.ts +2 -0
- package/types/src/components/menu/menu.d.ts +266 -0
- package/types/src/components/menu/menu.define.d.ts +1 -0
- package/types/src/components/menu/menu.events.d.ts +6 -0
- package/types/src/components/menu/menu.style.d.ts +2 -0
- package/types/src/components/menu-group/menu-group.d.ts +24 -0
- package/types/src/components/menu-group/menu-group.define.d.ts +1 -0
- package/types/src/components/menu-group/menu-group.style.d.ts +2 -0
- package/types/src/components/menu-item/menu-item.d.ts +59 -0
- package/types/src/components/menu-item/menu-item.define.d.ts +1 -0
- package/types/src/components/menu-item/menu-item.style.d.ts +2 -0
- package/types/src/components/nav/nav.d.ts +103 -0
- package/types/src/components/nav/nav.define.d.ts +1 -0
- package/types/src/components/nav/nav.style.d.ts +2 -0
- package/types/src/components/nav-item/nav-item.d.ts +39 -0
- package/types/src/components/nav-item/nav-item.define.d.ts +1 -0
- package/types/src/components/nav-item/nav-item.style.d.ts +2 -0
- package/types/src/components/pagination/pagination.d.ts +31 -2
- package/types/src/components/scroll-to-top/scroll-to-top.d.ts +6 -5
- package/types/src/components/segmented-field/segmented-field.d.ts +85 -0
- package/types/src/components/segmented-field/segmented-field.define.d.ts +1 -0
- package/types/src/components/segmented-field/segmented-field.style.d.ts +2 -0
- package/types/src/components/splitter/splitter.d.ts +127 -0
- package/types/src/components/splitter/splitter.define.d.ts +1 -0
- package/types/src/components/splitter/splitter.events.d.ts +5 -0
- package/types/src/components/splitter/splitter.style.d.ts +2 -0
- package/types/src/components/status-bar/status-bar.d.ts +44 -0
- package/types/src/components/status-bar/status-bar.define.d.ts +1 -0
- package/types/src/components/status-bar/status-bar.style.d.ts +2 -0
- package/types/src/components/tab-group/tab-group.d.ts +17 -0
- package/types/src/components/toolbar/toolbar.d.ts +96 -0
- package/types/src/components/toolbar/toolbar.define.d.ts +1 -0
- package/types/src/components/toolbar/toolbar.style.d.ts +2 -0
- package/types/src/components/tooltip/tooltip.d.ts +26 -0
- package/types/src/components/tree/tree.d.ts +62 -0
- package/types/src/components/tree/tree.define.d.ts +1 -0
- package/types/src/components/tree/tree.style.d.ts +2 -0
- package/types/src/components/tree-item/tree-item.d.ts +106 -0
- package/types/src/components/tree-item/tree-item.define.d.ts +1 -0
- package/types/src/components/tree-item/tree-item.events.d.ts +4 -0
- package/types/src/components/tree-item/tree-item.style.d.ts +2 -0
- package/types/src/styles/appearance.style.d.ts +31 -0
- package/types/src/styles/choice-group.style.d.ts +1 -1
- package/types/src/styles/modal.style.d.ts +5 -0
- package/types/src/styles/size.style.d.ts +24 -0
- package/types/src/utils/focus.d.ts +25 -0
- package/types/src/utils/scroll.d.ts +11 -0
- package/web-types.json +2530 -393
|
@@ -0,0 +1,570 @@
|
|
|
1
|
+
import { TemplateElement, defineElement, html } from '@webtides/element-js';
|
|
2
|
+
import { activeElement } from '../../utils/focus.js';
|
|
3
|
+
import { followAnchor, placeAtAnchor } from '../../utils/floating.js';
|
|
4
|
+
import style from './menu.style.js';
|
|
5
|
+
import Events from './menu.events.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The menu that is open. One at a time: a menu that opens closes the one before it.
|
|
9
|
+
* @type {HTMLElement | undefined}
|
|
10
|
+
*/
|
|
11
|
+
let openMenu;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A menu button: a button that opens a list of actions. "Edit, Duplicate, Delete" for a row of a
|
|
15
|
+
* table, the account in a header, "More" at the end of a toolbar. The button is what the page
|
|
16
|
+
* puts into the `trigger` slot, the actions are `el-menu-item` elements.
|
|
17
|
+
*
|
|
18
|
+
* To assistive technology it is a menu (`role="menu"`) of menu items, and the button says that it
|
|
19
|
+
* opens one and whether it is open. The keyboard moves through the entries with the arrow keys,
|
|
20
|
+
* `Home`, `End` and the first letter of an entry. `Escape` closes the menu and gives the focus
|
|
21
|
+
* back to the button, the Tab key closes it and moves on.
|
|
22
|
+
*
|
|
23
|
+
* The panel is a native popover: it is in the top layer, so nothing clips it and it moves nothing
|
|
24
|
+
* on the page. It is placed at the button, at the side that `placement` names, on the other side
|
|
25
|
+
* without room there, and never outside of the viewport. A press beside it closes it.
|
|
26
|
+
*
|
|
27
|
+
* Choosing an entry closes the menu and fires `menu-select`.
|
|
28
|
+
*
|
|
29
|
+
* One menu can serve many things, the rows of a list: `showAt()` opens it at a point of the
|
|
30
|
+
* viewport, for a right click or a long press, or at an element of the page, a button of a row.
|
|
31
|
+
* What it was opened for is the `opener`, which gets the focus back and which `menu-select`
|
|
32
|
+
* names.
|
|
33
|
+
*
|
|
34
|
+
* @element el-menu
|
|
35
|
+
*
|
|
36
|
+
* @fires {CustomEvent} menu-show - Fired when the menu has opened.
|
|
37
|
+
* @fires {CustomEvent} menu-hide - Fired when the menu has closed.
|
|
38
|
+
* @fires {CustomEvent<{ item: HTMLElement, value: string, opener: Element | undefined }>} menu-select -
|
|
39
|
+
* Fired when the user has chosen an entry: the `el-menu-item` is in `detail.item`, its `value`
|
|
40
|
+
* in `detail.value`, and what the menu was opened for in `detail.opener`. The menu closes after
|
|
41
|
+
* it.
|
|
42
|
+
*
|
|
43
|
+
* @slot - The entries: `el-menu-item` elements, in `el-menu-group` elements where they are
|
|
44
|
+
* divided into groups. An `el-divider` is a line between two of them.
|
|
45
|
+
* @slot trigger - The button that opens the menu. It gets `aria-haspopup="menu"` and
|
|
46
|
+
* `aria-expanded`.
|
|
47
|
+
*
|
|
48
|
+
* @csspart trigger - Forwarded onto the `trigger` slot.
|
|
49
|
+
* @csspart panel - The floating panel, which is the menu (`role="menu"`): background, border,
|
|
50
|
+
* radius, elevation, width.
|
|
51
|
+
*
|
|
52
|
+
* @cssstate open - Set while the menu is shown.
|
|
53
|
+
*
|
|
54
|
+
* @property {boolean} open - Whether the menu is shown. Reflected to the `open` attribute.
|
|
55
|
+
* @property {string} label - The name of the menu for assistive technology. Without one the menu
|
|
56
|
+
* has the name of its button.
|
|
57
|
+
* @property {'top'|'top-start'|'top-end'|'bottom'|'bottom-start'|'bottom-end'|'left'|'left-start'|'left-end'|'right'|'right-start'|'right-end'} placement -
|
|
58
|
+
* The side of the button the menu is placed at, and with `-start` or `-end` the edge of the
|
|
59
|
+
* button it is aligned with. Without room there, the menu goes to the opposite side. Default
|
|
60
|
+
* `'bottom-start'`.
|
|
61
|
+
* @property {number} distance - The gap between the button and the menu, in pixels. Default `4`.
|
|
62
|
+
*/
|
|
63
|
+
export default class Menu extends TemplateElement {
|
|
64
|
+
/**
|
|
65
|
+
* Which entry gets the focus when the menu opens: the first one, or the last one for
|
|
66
|
+
* `ArrowUp` on the button.
|
|
67
|
+
* @private
|
|
68
|
+
* @type {'first'|'last'}
|
|
69
|
+
*/
|
|
70
|
+
_enterAt = 'first';
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Whether the focus goes back to the button when the menu closes. It does after a choice and
|
|
74
|
+
* after `Escape`, and not where the user has moved it on.
|
|
75
|
+
* @private
|
|
76
|
+
*/
|
|
77
|
+
_restoreFocus = false;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Where the menu is placed while it is open somewhere else than at its own button: an element
|
|
81
|
+
* of the page, or a point of the viewport.
|
|
82
|
+
* @private
|
|
83
|
+
* @type {Element | { x: number, y: number } | undefined}
|
|
84
|
+
*/
|
|
85
|
+
_place = undefined;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* What the menu was opened for with `showAt()`.
|
|
89
|
+
* @private
|
|
90
|
+
* @type {Element | undefined}
|
|
91
|
+
*/
|
|
92
|
+
_opener = undefined;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The button of the page that was told that the menu is open.
|
|
96
|
+
* @private
|
|
97
|
+
* @type {Element | undefined}
|
|
98
|
+
*/
|
|
99
|
+
_expanded = undefined;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Where the menu opens next, and what for: what `showAt()` was given. It is taken when the
|
|
103
|
+
* menu opens, which is after the element has rendered.
|
|
104
|
+
* @private
|
|
105
|
+
* @type {{ place: Element | { x: number, y: number }, opener: Element | undefined } | undefined}
|
|
106
|
+
*/
|
|
107
|
+
_next = undefined;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Whether the Tab key was pressed in the menu, and the focus is about to leave it.
|
|
111
|
+
* @private
|
|
112
|
+
*/
|
|
113
|
+
_leaving = false;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* A pointer that goes down beside the open menu closes it. The panel is a popover the menu
|
|
117
|
+
* opens and closes itself (`popover="manual"`): the platform would close one of its own
|
|
118
|
+
* (`auto`) when the pointer that opened it is let go, the finger after a long press.
|
|
119
|
+
* @private
|
|
120
|
+
* @param {PointerEvent} event
|
|
121
|
+
*/
|
|
122
|
+
_onPressOutside = (event) => {
|
|
123
|
+
const path = event.composedPath();
|
|
124
|
+
if (path.includes(this.$refs.panel)) return;
|
|
125
|
+
// The button of the menu closes it with its click.
|
|
126
|
+
if (!this._place && path.includes(this._trigger())) return;
|
|
127
|
+
|
|
128
|
+
this._restoreFocus = false;
|
|
129
|
+
this.open = false;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
constructor() {
|
|
133
|
+
super({ styles: [style], shadowRender: true, propertyOptions: { open: { reflect: true } } });
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
properties() {
|
|
137
|
+
return {
|
|
138
|
+
open: false,
|
|
139
|
+
label: '',
|
|
140
|
+
placement: 'bottom-start',
|
|
141
|
+
distance: 4,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
connected() {
|
|
146
|
+
this._syncTrigger();
|
|
147
|
+
// The watcher does not run for the first value, so a menu that is open from the start is
|
|
148
|
+
// shown here.
|
|
149
|
+
if (this.open) this._applyOpen();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
disconnected() {
|
|
153
|
+
this._stopPositioning();
|
|
154
|
+
document.removeEventListener('pointerdown', this._onPressOutside, true);
|
|
155
|
+
if (openMenu === this) openMenu = undefined;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
afterUpdate() {
|
|
159
|
+
this._syncTrigger();
|
|
160
|
+
// A new placement or distance, other entries in the panel.
|
|
161
|
+
if (this.open) this._position();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
watch() {
|
|
165
|
+
return {
|
|
166
|
+
open: (open) => {
|
|
167
|
+
if (open) this._applyOpen();
|
|
168
|
+
else this._applyClose();
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
events() {
|
|
174
|
+
return {
|
|
175
|
+
'[data-trigger]': {
|
|
176
|
+
pointerdown: () => {
|
|
177
|
+
this._openAtPointerDown = this.open;
|
|
178
|
+
},
|
|
179
|
+
click: (event) => {
|
|
180
|
+
// A press beside the open menu closes it, and where the menu is open for a
|
|
181
|
+
// row, its own button is beside it. A click that found it closed would open
|
|
182
|
+
// it again, so a click of a pointer goes by what was the case when the
|
|
183
|
+
// pointer went down. A click of the keyboard has no pointer (`detail` is 0).
|
|
184
|
+
const byPointer = event.detail > 0 && this._openAtPointerDown !== undefined;
|
|
185
|
+
const wasOpen = byPointer ? this._openAtPointerDown : this.open;
|
|
186
|
+
this._openAtPointerDown = undefined;
|
|
187
|
+
this._enterAt = 'first';
|
|
188
|
+
this.open = !wasOpen;
|
|
189
|
+
},
|
|
190
|
+
keydown: (event) => this._onTriggerKeyDown(event),
|
|
191
|
+
slotchange: () => this._syncTrigger(),
|
|
192
|
+
},
|
|
193
|
+
"[part~='panel']": {
|
|
194
|
+
keydown: (event) => this._onKeyDown(event),
|
|
195
|
+
click: (event) => this._onClick(event),
|
|
196
|
+
focusout: (event) => this._onFocusOut(event),
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Opens the menu, with the focus on its first entry.
|
|
203
|
+
* @returns {void}
|
|
204
|
+
*/
|
|
205
|
+
show() {
|
|
206
|
+
this.open = true;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Opens the menu somewhere else than at its own button: one menu for the rows of a list.
|
|
211
|
+
*
|
|
212
|
+
* At a point of the viewport, from a `contextmenu` event or a long press: the corner of the
|
|
213
|
+
* menu is at the point, and the menu stays inside of the viewport. Or at an element of the
|
|
214
|
+
* page, the button of a row: the menu is placed at it as at its own button.
|
|
215
|
+
*
|
|
216
|
+
* `opener` is what the menu is opened for. It gets the focus back when the menu closes, and
|
|
217
|
+
* `menu-select` names it (`detail.opener`). Without one it is the element the menu is placed
|
|
218
|
+
* at.
|
|
219
|
+
* @param {Element | { x: number, y: number }} place - The element, or the point in the
|
|
220
|
+
* coordinates of the viewport (`clientX`, `clientY`).
|
|
221
|
+
* @param {Element} [opener] - The row or the button the menu is opened for.
|
|
222
|
+
* @returns {void}
|
|
223
|
+
*/
|
|
224
|
+
showAt(place, opener) {
|
|
225
|
+
const next = { place, opener: opener ?? (place instanceof Element ? place : undefined) };
|
|
226
|
+
this._enterAt = 'first';
|
|
227
|
+
|
|
228
|
+
// Open already, for something else: the panel stays where the platform has it, in the
|
|
229
|
+
// top layer, and moves. The page is told of both, the row it has left and the row it is
|
|
230
|
+
// open for now.
|
|
231
|
+
if (this.open && this.$refs.panel?.matches(':popover-open')) {
|
|
232
|
+
this.dispatch(Events.HIDE);
|
|
233
|
+
this._place = next.place;
|
|
234
|
+
this._opener = next.opener;
|
|
235
|
+
this._syncTrigger();
|
|
236
|
+
this._startPositioning();
|
|
237
|
+
this._focusEntry();
|
|
238
|
+
this.dispatch(Events.SHOW);
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// The property tells of its change after the element has rendered: the place waits.
|
|
243
|
+
this._next = next;
|
|
244
|
+
this.open = true;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Closes the menu.
|
|
249
|
+
* @returns {void}
|
|
250
|
+
*/
|
|
251
|
+
hide() {
|
|
252
|
+
this.open = false;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* What the menu is open for: the row or the button it was opened for with `showAt()`, or its
|
|
257
|
+
* own button. Nothing while it is closed.
|
|
258
|
+
* @returns {Element | undefined}
|
|
259
|
+
*/
|
|
260
|
+
get opener() {
|
|
261
|
+
return this.open ? (this._opener ?? this._trigger()) : undefined;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Opens the menu when it is closed, and closes it when it is open.
|
|
266
|
+
* @returns {void}
|
|
267
|
+
*/
|
|
268
|
+
toggle() {
|
|
269
|
+
this.open = !this.open;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* The button of the menu: what is in the `trigger` slot.
|
|
274
|
+
* @private
|
|
275
|
+
* @returns {HTMLElement | undefined}
|
|
276
|
+
*/
|
|
277
|
+
_trigger() {
|
|
278
|
+
return this.$refs.trigger?.assignedElements({ flatten: true })[0];
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* The entries the user can reach, in the order of the page. An entry that is disabled is one
|
|
283
|
+
* of them: the keyboard gets to it, so a screen reader finds it, and it cannot be chosen.
|
|
284
|
+
* @private
|
|
285
|
+
* @returns {HTMLElement[]}
|
|
286
|
+
*/
|
|
287
|
+
_items() {
|
|
288
|
+
return [...this.querySelectorAll('el-menu-item')].filter(
|
|
289
|
+
(item) => item.closest('el-menu') === this && !item.closest('[hidden]'),
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* The text of an entry, without its icon: what a letter of the keyboard is compared with.
|
|
295
|
+
* @private
|
|
296
|
+
* @param {Element} item
|
|
297
|
+
* @returns {string}
|
|
298
|
+
*/
|
|
299
|
+
_textOf(item) {
|
|
300
|
+
return [...item.childNodes]
|
|
301
|
+
.filter((node) => node.nodeType === Node.TEXT_NODE || !node.getAttribute?.('slot'))
|
|
302
|
+
.map((node) => node.textContent)
|
|
303
|
+
.join(' ')
|
|
304
|
+
.replace(/\s+/g, ' ')
|
|
305
|
+
.trim();
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* The button says that it opens a menu, and whether the menu is open. An `el-button` passes
|
|
310
|
+
* both on to the control inside of it.
|
|
311
|
+
* @private
|
|
312
|
+
*/
|
|
313
|
+
_syncTrigger() {
|
|
314
|
+
const trigger = this._trigger();
|
|
315
|
+
// Open for something else, the menu is not the one of its own button for that time.
|
|
316
|
+
const own = this.open && !this._place;
|
|
317
|
+
if (trigger) {
|
|
318
|
+
if (trigger.getAttribute('aria-haspopup') !== 'menu') trigger.setAttribute('aria-haspopup', 'menu');
|
|
319
|
+
const expanded = own ? 'true' : 'false';
|
|
320
|
+
if (trigger.getAttribute('aria-expanded') !== expanded) trigger.setAttribute('aria-expanded', expanded);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// A button of the page that opens the menu says that it does (`aria-haspopup`): the page
|
|
324
|
+
// writes that, the menu does not know its buttons. Such a button is told whether the
|
|
325
|
+
// menu is open. A row is no control, and has nothing to say.
|
|
326
|
+
const button = this.open && this._opener?.matches('[aria-haspopup]') ? this._opener : undefined;
|
|
327
|
+
if (this._expanded && this._expanded !== button) this._expanded.setAttribute('aria-expanded', 'false');
|
|
328
|
+
if (button) button.setAttribute('aria-expanded', 'true');
|
|
329
|
+
this._expanded = button;
|
|
330
|
+
|
|
331
|
+
// Without a name of its own the menu has the one of the button it is open for.
|
|
332
|
+
const panel = this.$refs.panel;
|
|
333
|
+
if (panel && 'ariaLabelledByElements' in panel) {
|
|
334
|
+
const named = this._place ? button : trigger;
|
|
335
|
+
panel.ariaLabelledByElements = this.label || !named ? null : [named];
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Whether the focus is on an entry of the menu.
|
|
341
|
+
* @private
|
|
342
|
+
* @returns {boolean}
|
|
343
|
+
*/
|
|
344
|
+
_hasFocusInPanel() {
|
|
345
|
+
const active = activeElement();
|
|
346
|
+
return this._items().some((item) => item === active || item.shadowRoot?.contains(active));
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* The arrow keys on the button open the menu: down at its first entry, up at its last one.
|
|
351
|
+
* `Enter` and `Space` are a click of the button.
|
|
352
|
+
* @private
|
|
353
|
+
* @param {KeyboardEvent} event
|
|
354
|
+
*/
|
|
355
|
+
_onTriggerKeyDown(event) {
|
|
356
|
+
if (event.key !== 'ArrowDown' && event.key !== 'ArrowUp') return;
|
|
357
|
+
if (event.altKey || event.ctrlKey || event.metaKey) return;
|
|
358
|
+
event.preventDefault();
|
|
359
|
+
|
|
360
|
+
this._enterAt = event.key === 'ArrowUp' ? 'last' : 'first';
|
|
361
|
+
if (this.open) this._focusEntry();
|
|
362
|
+
else this.open = true;
|
|
363
|
+
}
|
|
364
|
+
|
|
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
|
+
/**
|
|
418
|
+
* A click in the panel: the user has chosen the entry it is on, unless that one is disabled.
|
|
419
|
+
* @private
|
|
420
|
+
* @param {MouseEvent} event
|
|
421
|
+
*/
|
|
422
|
+
_onClick(event) {
|
|
423
|
+
const item = event.composedPath().find((node) => node instanceof Element && node.localName === 'el-menu-item');
|
|
424
|
+
if (!item || item.closest('el-menu') !== this || item.disabled) return;
|
|
425
|
+
|
|
426
|
+
this._restoreFocus = true;
|
|
427
|
+
this.dispatch(Events.SELECT, { item, value: item.value, opener: this.opener });
|
|
428
|
+
this.open = false;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* The focus has left the menu with the Tab key, which closes it and moves on. The focus is
|
|
433
|
+
* where the user has put it, and stays there.
|
|
434
|
+
*
|
|
435
|
+
* Only the key closes it this way. A pointer that goes down beside the menu closes it by
|
|
436
|
+
* itself, and the finger of a long press gives the focus to the row it is on when it is
|
|
437
|
+
* lifted: the menu it has just opened stays open.
|
|
438
|
+
* @private
|
|
439
|
+
* @param {FocusEvent} event
|
|
440
|
+
*/
|
|
441
|
+
_onFocusOut(event) {
|
|
442
|
+
if (!this.open || !this._leaving) return;
|
|
443
|
+
if (event.relatedTarget?.closest('el-menu-item') && this.contains(event.relatedTarget)) return;
|
|
444
|
+
|
|
445
|
+
this._restoreFocus = false;
|
|
446
|
+
this.open = false;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Puts the focus on the entry the menu is entered at.
|
|
451
|
+
* @private
|
|
452
|
+
*/
|
|
453
|
+
_focusEntry() {
|
|
454
|
+
const items = this._items();
|
|
455
|
+
(this._enterAt === 'last' ? items.at(-1) : items[0])?.focus();
|
|
456
|
+
this._enterAt = 'first';
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/** @private */
|
|
460
|
+
_applyOpen() {
|
|
461
|
+
const panel = this.$refs.panel;
|
|
462
|
+
if (!panel || !this.isConnected) return;
|
|
463
|
+
|
|
464
|
+
// What showAt() was given, or nothing: the menu of its own button.
|
|
465
|
+
this._place = this._next?.place;
|
|
466
|
+
this._opener = this._next?.opener;
|
|
467
|
+
this._next = undefined;
|
|
468
|
+
|
|
469
|
+
// One menu at a time.
|
|
470
|
+
if (openMenu && openMenu !== this) openMenu.open = false;
|
|
471
|
+
openMenu = this;
|
|
472
|
+
|
|
473
|
+
this._restoreFocus = false;
|
|
474
|
+
if (!panel.matches(':popover-open')) panel.showPopover();
|
|
475
|
+
document.addEventListener('pointerdown', this._onPressOutside, true);
|
|
476
|
+
this._internals.states.add('open');
|
|
477
|
+
this._syncTrigger();
|
|
478
|
+
this._startPositioning();
|
|
479
|
+
this._focusEntry();
|
|
480
|
+
this.dispatch(Events.SHOW);
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/** @private */
|
|
484
|
+
_applyClose() {
|
|
485
|
+
const panel = this.$refs.panel;
|
|
486
|
+
if (!panel) return;
|
|
487
|
+
|
|
488
|
+
// Asked before the panel is hidden: an entry that has the focus loses it then.
|
|
489
|
+
const restore = this._restoreFocus || this._hasFocusInPanel();
|
|
490
|
+
this._restoreFocus = false;
|
|
491
|
+
|
|
492
|
+
// What the menu was open for gets the focus back: the row, the button, its own button.
|
|
493
|
+
const opener = this._opener ?? this._trigger();
|
|
494
|
+
this._place = undefined;
|
|
495
|
+
this._opener = undefined;
|
|
496
|
+
|
|
497
|
+
if (openMenu === this) openMenu = undefined;
|
|
498
|
+
document.removeEventListener('pointerdown', this._onPressOutside, true);
|
|
499
|
+
this._stopPositioning();
|
|
500
|
+
if (panel.matches(':popover-open')) panel.hidePopover();
|
|
501
|
+
this._internals.states.delete('open');
|
|
502
|
+
this._syncTrigger();
|
|
503
|
+
if (restore) opener?.focus?.();
|
|
504
|
+
this.dispatch(Events.HIDE);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* What the panel is placed at: its own button, the element of `showAt()`, or its point. A
|
|
509
|
+
* point is an element for the placing that has no size and does not move.
|
|
510
|
+
* @private
|
|
511
|
+
* @returns {Element | { getBoundingClientRect: () => DOMRect }}
|
|
512
|
+
*/
|
|
513
|
+
_anchor() {
|
|
514
|
+
const place = this._place;
|
|
515
|
+
if (!place) return this._trigger() ?? this;
|
|
516
|
+
if (place instanceof Element) return place;
|
|
517
|
+
return { getBoundingClientRect: () => new DOMRect(Number(place.x) || 0, Number(place.y) || 0, 0, 0) };
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* Places the panel now, and again whenever the button moves: on scroll, on resize, when the
|
|
522
|
+
* button or the panel changes its size.
|
|
523
|
+
* @private
|
|
524
|
+
*/
|
|
525
|
+
_startPositioning() {
|
|
526
|
+
this._stopPositioning();
|
|
527
|
+
const anchor = this._anchor();
|
|
528
|
+
// A point does not move: the menu is placed at it once.
|
|
529
|
+
if (anchor instanceof Element) {
|
|
530
|
+
this._stopAutoUpdate = followAnchor(anchor, this.$refs.panel, () => this._position());
|
|
531
|
+
} else {
|
|
532
|
+
this._position();
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/** @private */
|
|
537
|
+
_stopPositioning() {
|
|
538
|
+
this._stopAutoUpdate?.();
|
|
539
|
+
this._stopAutoUpdate = undefined;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/** @private */
|
|
543
|
+
async _position() {
|
|
544
|
+
const panel = this.$refs.panel;
|
|
545
|
+
if (!panel || !this.open) return;
|
|
546
|
+
|
|
547
|
+
const atPoint = this._place && !(this._place instanceof Element);
|
|
548
|
+
await placeAtAnchor(this._anchor(), panel, {
|
|
549
|
+
// At a point the corner of the menu is at the pointer, as the menu of the system is.
|
|
550
|
+
placement: atPoint ? 'bottom-start' : this.placement,
|
|
551
|
+
distance: atPoint ? 0 : this.distance,
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
template() {
|
|
556
|
+
// A plain binding leaves the name out while there is none.
|
|
557
|
+
return html`
|
|
558
|
+
<slot part="trigger" name="trigger" ref="trigger" data-trigger></slot>
|
|
559
|
+
<div part="panel" ref="panel" class="panel" popover="manual" role="menu" aria-label="${this.label || null}">
|
|
560
|
+
<slot></slot>
|
|
561
|
+
</div>
|
|
562
|
+
`;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
export function define() {
|
|
567
|
+
defineElement('el-menu', Menu);
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
export { Events };
|