-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathvaadin-context-menu.js
More file actions
357 lines (341 loc) · 12.5 KB
/
vaadin-context-menu.js
File metadata and controls
357 lines (341 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/**
* @license
* Copyright (c) 2016 - 2026 Vaadin Ltd.
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import './vaadin-contextmenu-event.js';
import './vaadin-context-menu-item.js';
import './vaadin-context-menu-list-box.js';
import './vaadin-context-menu-overlay.js';
import { css, html, LitElement } from 'lit';
import { ifDefined } from 'lit/directives/if-defined.js';
import { defineCustomElement } from '@vaadin/component-base/src/define.js';
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
import { PolylitMixin } from '@vaadin/component-base/src/polylit-mixin.js';
import { ThemePropertyMixin } from '@vaadin/vaadin-themable-mixin/vaadin-theme-property-mixin.js';
import { ContextMenuMixin } from './vaadin-context-menu-mixin.js';
/**
* `<vaadin-context-menu>` is a Web Component for creating context menus.
*
* ### Items
*
* Items is a higher level convenience API for defining a (hierarchical) menu structure for the component.
* If a menu item has a non-empty `children` set, a sub-menu with the child items is opened
* next to the parent menu on mouseover, tap or a right arrow keypress.
*
* When an item is selected, `<vaadin-context-menu>` dispatches an "item-selected" event
* with the selected item as `event.detail.value` property.
* If item does not have `keepOpen` property the menu will be closed.
*
* ```javascript
* contextMenu.items = [
* { text: 'Menu Item 1', theme: 'primary', className: 'first', children:
* [
* { text: 'Menu Item 1-1', checked: true, keepOpen: true },
* { text: 'Menu Item 1-2' }
* ]
* },
* { component: 'hr' },
* { text: 'Menu Item 2', children:
* [
* { text: 'Menu Item 2-1' },
* { text: 'Menu Item 2-2', disabled: true }
* ]
* },
* { text: 'Menu Item 3', disabled: true, className: 'last' }
* ];
*
* contextMenu.addEventListener('item-selected', e => {
* const item = e.detail.value;
* console.log(`${item.text} selected`);
* });
* ```
*
* **NOTE:** when the `items` array is defined, the renderer cannot be used.
*
* #### Disabled menu items
*
* When disabled, menu items are rendered as "dimmed".
*
* By default, disabled items are not focusable and don't react to hover.
* As a result, they are hidden from assistive technologies, and it's not
* possible to show a tooltip to explain why they are disabled. This can
* be addressed by enabling the feature flag `accessibleDisabledMenuItems`,
* which makes disabled items focusable and hoverable, while still
* preventing them from being activated:
*
* ```js
* // Set before any context menu is attached to the DOM.
* window.Vaadin.featureFlags.accessibleDisabledMenuItems = true;
* ```
*
* #### Item tooltips
*
* Menu items can have tooltips that are shown on hover and keyboard
* focus. To enable them, add a slotted `<vaadin-tooltip>` element
* and set the `tooltip` property on each item that should have one:
*
* ```html
* <vaadin-context-menu>
* <vaadin-tooltip slot="tooltip"></vaadin-tooltip>
* </vaadin-context-menu>
* ```
*
* ### Rendering
*
* The content of the menu can be populated by using the renderer callback function.
*
* The renderer function provides `root`, `contextMenu`, `model` arguments when applicable.
* Generate DOM content by using `model` object properties if needed, append it to the `root`
* element and control the state of the host element by accessing `contextMenu`. Before generating
* new content, the renderer function should check if there is already content in `root` for reusing it.
*
* ```html
* <vaadin-context-menu id="contextMenu">
* <p>This paragraph has a context menu.</p>
* </vaadin-context-menu>
* ```
* ```js
* const contextMenu = document.querySelector('#contextMenu');
* contextMenu.renderer = (root, contextMenu, context) => {
* let listBox = root.firstElementChild;
* if (!listBox) {
* listBox = document.createElement('vaadin-list-box');
* root.appendChild(listBox);
* }
*
* let item = listBox.querySelector('vaadin-item');
* if (!item) {
* item = document.createElement('vaadin-item');
* listBox.appendChild(item);
* }
* item.textContent = 'Content of the selector: ' + context.target.textContent;
* };
* ```
*
* You can access the menu context inside the renderer using
* `context.target` and `context.detail`.
*
* Renderer is called on the opening of the context-menu and each time the related context is updated.
* DOM generated during the renderer call can be reused
* in the next renderer call and will be provided with the `root` argument.
* On first call it will be empty.
*
* ### `vaadin-contextmenu` Gesture Event
*
* `vaadin-contextmenu` is a gesture event (a custom event),
* which is dispatched after either `contextmenu` or long touch events.
* This enables support for both mouse and touch environments in a uniform way.
*
* `<vaadin-context-menu>` opens the menu overlay on the `vaadin-contextmenu`
* event by default.
*
* ### Menu Listener
*
* By default, the `<vaadin-context-menu>` element listens for the menu opening
* event on itself. In case if you do not want to wrap the target, you can listen for
* events on an element outside the `<vaadin-context-menu>` by setting the
* `listenOn` property:
*
* ```html
* <vaadin-context-menu id="contextMenu"></vaadin-context-menu>
*
* <div id="menuListener">The element that listens for the contextmenu event.</div>
* ```
* ```javascript
* const contextMenu = document.querySelector('#contextMenu');
* contextMenu.listenOn = document.querySelector('#menuListener');
* ```
*
* ### Filtering Menu Targets
*
* By default, the listener element and all its descendants open the context
* menu. You can filter the menu targets to a smaller set of elements inside
* the listener element by setting the `selector` property.
*
* In the following example, only the elements matching `.has-menu` will open the context menu:
*
* ```html
* <vaadin-context-menu selector=".has-menu">
* <p class="has-menu">This paragraph opens the context menu</p>
* <p>This paragraph does not open the context menu</p>
* </vaadin-context-menu>
* ```
*
* ### Menu Context
*
* The following properties are available in the `context` argument:
*
* - `target` is the menu opening event target, which is the element that
* the user has called the context menu for
* - `detail` is the menu opening event detail
*
* In the following example, the menu item text is composed with the contents
* of the element that opened the menu:
*
* ```html
* <vaadin-context-menu selector="li" id="contextMenu">
* <ul>
* <li>Foo</li>
* <li>Bar</li>
* <li>Baz</li>
* </ul>
* </vaadin-context-menu>
* ```
* ```js
* const contextMenu = document.querySelector('#contextMenu');
* contextMenu.renderer = (root, contextMenu, context) => {
* let listBox = root.firstElementChild;
* if (!listBox) {
* listBox = document.createElement('vaadin-list-box');
* root.appendChild(listBox);
* }
*
* let item = listBox.querySelector('vaadin-item');
* if (!item) {
* item = document.createElement('vaadin-item');
* listBox.appendChild(item);
* }
* item.textContent = 'The menu target: ' + context.target.textContent;
* };
* ```
*
* ### Styling
*
* The following shadow DOM parts are available for styling:
*
* Part name | Description
* -----------------|-------------------------------------------
* `backdrop` | Backdrop of the overlay
* `overlay` | The overlay container
* `content` | The overlay content
*
* ### Custom CSS Properties
*
* The following custom CSS properties are available for styling:
*
* Custom CSS property | Description
* --------------------------------------|-------------
* `--vaadin-context-menu-offset-top` | Used as an offset when using `position` and the context menu is aligned vertically below the target
* `--vaadin-context-menu-offset-bottom` | Used as an offset when using `position` and the context menu is aligned vertically above the target
* `--vaadin-context-menu-offset-start` | Used as an offset when using `position` and the context menu is aligned horizontally after the target
* `--vaadin-context-menu-offset-end` | Used as an offset when using `position` and the context menu is aligned horizontally before the target
*
* See [Styling Components](https://vaadin.com/docs/latest/styling/styling-components) documentation.
*
* ### Internal components
*
* When using `items` API the following internal components are themable:
*
* - `<vaadin-context-menu-item>` - has the same API as [`<vaadin-item>`](#/elements/vaadin-item).
* - `<vaadin-context-menu-list-box>` - has the same API as [`<vaadin-list-box>`](#/elements/vaadin-list-box).
*
* The `<vaadin-context-menu-item>` sub-menu elements have the following additional state attributes
* on top of the built-in `<vaadin-item>` state attributes:
*
* Attribute | Description
* ---------- |-------------
* `expanded` | Expanded parent item.
*
* @fires {CustomEvent} opened-changed - Fired when the `opened` property changes.
* @fires {CustomEvent} item-selected - Fired when an item is selected when the context menu is populated using the `items` API.
* @fires {CustomEvent} closed - Fired when the context menu is closed.
* @fires {CustomEvent} close-all-menus - Fired when all menus should close, e.g., after pressing Tab or on submenu close.
* @fires {CustomEvent} items-outside-click - Fired when a click happens outside any open sub-menus.
*
* @customElement vaadin-context-menu
* @extends HTMLElement
* @mixes ElementMixin
* @mixes ContextMenuMixin
* @mixes ThemePropertyMixin
*/
class ContextMenu extends ContextMenuMixin(ElementMixin(ThemePropertyMixin(PolylitMixin(LitElement)))) {
static get is() {
return 'vaadin-context-menu';
}
static get styles() {
return css`
:host {
display: block;
}
:host([hidden]) {
display: none !important;
}
`;
}
static get properties() {
return {
/**
* Position of the overlay with respect to the target.
* Supported values: null, `top-start`, `top`, `top-end`,
* `bottom-start`, `bottom`, `bottom-end`, `start-top`,
* `start`, `start-bottom`, `end-top`, `end`, `end-bottom`.
*/
position: {
type: String,
},
};
}
/** @protected */
render() {
const { _context: context, position } = this;
return html`
<slot id="slot"></slot>
<vaadin-context-menu-overlay
id="overlay"
.owner="${this}"
.opened="${this.opened}"
.model="${context}"
.modeless="${this._modeless}"
.renderer="${this.items ? this.__itemsRenderer : this.renderer}"
.position="${position}"
.positionTarget="${position ? context?.target : this._positionTarget}"
.horizontalAlign="${this.__computeHorizontalAlign(position)}"
.verticalAlign="${this.__computeVerticalAlign(position)}"
?no-horizontal-overlap="${this.__computeNoHorizontalOverlap(position)}"
?no-vertical-overlap="${this.__computeNoVerticalOverlap(position)}"
.withBackdrop="${this._phone}"
?phone="${this._phone}"
theme="${ifDefined(this._theme)}"
exportparts="backdrop, overlay, content"
@opened-changed="${this._onOverlayOpened}"
@vaadin-overlay-open="${this._onVaadinOverlayOpen}"
@vaadin-overlay-closed="${this._onVaadinOverlayClosed}"
>
<slot name="overlay"></slot>
<slot name="submenu" slot="submenu"></slot>
</vaadin-context-menu-overlay>
<slot name="tooltip"></slot>
`;
}
/** @private */
__computeHorizontalAlign(position) {
if (!position) {
return 'start';
}
return ['top-end', 'bottom-end', 'start-top', 'start', 'start-bottom'].includes(position) ? 'end' : 'start';
}
/** @private */
__computeNoHorizontalOverlap(position) {
if (!position) {
return !!this._positionTarget;
}
return ['start-top', 'start', 'start-bottom', 'end-top', 'end', 'end-bottom'].includes(position);
}
/** @private */
__computeNoVerticalOverlap(position) {
if (!position) {
return false;
}
return ['top-start', 'top-end', 'top', 'bottom-start', 'bottom', 'bottom-end'].includes(position);
}
/** @private */
__computeVerticalAlign(position) {
if (!position) {
return 'top';
}
return ['top-start', 'top-end', 'top', 'start-bottom', 'end-bottom'].includes(position) ? 'bottom' : 'top';
}
}
defineCustomElement(ContextMenu);
export { ContextMenu };