@vaadin/context-menu 22.0.0-alpha7
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/LICENSE +190 -0
- package/README.md +85 -0
- package/package.json +52 -0
- package/src/interfaces.d.ts +43 -0
- package/src/vaadin-context-menu-overlay.js +113 -0
- package/src/vaadin-context-menu.d.ts +281 -0
- package/src/vaadin-context-menu.js +733 -0
- package/src/vaadin-contextmenu-event.js +103 -0
- package/src/vaadin-contextmenu-items-mixin.d.ts +74 -0
- package/src/vaadin-contextmenu-items-mixin.js +397 -0
- package/src/vaadin-device-detector.js +68 -0
- package/theme/lumo/vaadin-context-menu-styles.js +127 -0
- package/theme/lumo/vaadin-context-menu.js +4 -0
- package/theme/material/vaadin-context-menu-styles.js +82 -0
- package/theme/material/vaadin-context-menu.js +4 -0
- package/vaadin-context-menu.d.ts +2 -0
- package/vaadin-context-menu.js +2 -0
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
import { GestureEventListeners } from '@polymer/polymer/lib/mixins/gesture-event-listeners.js';
|
|
2
|
+
|
|
3
|
+
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
|
|
4
|
+
|
|
5
|
+
import { ThemePropertyMixin } from '@vaadin/vaadin-themable-mixin/vaadin-theme-property-mixin.js';
|
|
6
|
+
|
|
7
|
+
import { ContextMenuEventMap, ContextMenuRenderer } from './interfaces';
|
|
8
|
+
|
|
9
|
+
import { ItemsMixin } from './vaadin-contextmenu-items-mixin.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* `<vaadin-context-menu>` is a Web Component for creating context menus.
|
|
13
|
+
*
|
|
14
|
+
* ### Items
|
|
15
|
+
*
|
|
16
|
+
* Items is a higher level convenience API for defining a (hierarchical) menu structure for the component.
|
|
17
|
+
* If a menu item has a non-empty `children` set, a sub-menu with the child items is opened
|
|
18
|
+
* next to the parent menu on mouseover, tap or a right arrow keypress.
|
|
19
|
+
*
|
|
20
|
+
* When an item is selected, `<vaadin-context-menu>` dispatches an "item-selected" event
|
|
21
|
+
* with the selected item as `event.detail.value` property.
|
|
22
|
+
*
|
|
23
|
+
* ```javascript
|
|
24
|
+
* contextMenu.items = [
|
|
25
|
+
* {text: 'Menu Item 1', theme: 'primary', children:
|
|
26
|
+
* [
|
|
27
|
+
* {text: 'Menu Item 1-1', checked: true},
|
|
28
|
+
* {text: 'Menu Item 1-2'}
|
|
29
|
+
* ]
|
|
30
|
+
* },
|
|
31
|
+
* {component: 'hr'},
|
|
32
|
+
* {text: 'Menu Item 2', children:
|
|
33
|
+
* [
|
|
34
|
+
* {text: 'Menu Item 2-1'},
|
|
35
|
+
* {text: 'Menu Item 2-2', disabled: true}
|
|
36
|
+
* ]
|
|
37
|
+
* },
|
|
38
|
+
* {text: 'Menu Item 3', disabled: true}
|
|
39
|
+
* ];
|
|
40
|
+
*
|
|
41
|
+
* contextMenu.addEventListener('item-selected', e => {
|
|
42
|
+
* const item = e.detail.value;
|
|
43
|
+
* console.log(`${item.text} selected`);
|
|
44
|
+
* });
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* **NOTE:** when the `items` array is defined, the renderer cannot be used.
|
|
48
|
+
*
|
|
49
|
+
* ### Rendering
|
|
50
|
+
*
|
|
51
|
+
* The content of the menu can be populated by using the renderer callback function.
|
|
52
|
+
*
|
|
53
|
+
* The renderer function provides `root`, `contextMenu`, `model` arguments when applicable.
|
|
54
|
+
* Generate DOM content by using `model` object properties if needed, append it to the `root`
|
|
55
|
+
* element and control the state of the host element by accessing `contextMenu`. Before generating
|
|
56
|
+
* new content, the renderer function should check if there is already content in `root` for reusing it.
|
|
57
|
+
*
|
|
58
|
+
* ```html
|
|
59
|
+
* <vaadin-context-menu id="contextMenu">
|
|
60
|
+
* <p>This paragraph has a context menu.</p>
|
|
61
|
+
* </vaadin-context-menu>
|
|
62
|
+
* ```
|
|
63
|
+
* ```js
|
|
64
|
+
* const contextMenu = document.querySelector('#contextMenu');
|
|
65
|
+
* contextMenu.renderer = (root, contextMenu, context) => {
|
|
66
|
+
* let listBox = root.firstElementChild;
|
|
67
|
+
* if (!listBox) {
|
|
68
|
+
* listBox = document.createElement('vaadin-list-box');
|
|
69
|
+
* root.appendChild(listBox);
|
|
70
|
+
* }
|
|
71
|
+
*
|
|
72
|
+
* let item = listBox.querySelector('vaadin-item');
|
|
73
|
+
* if (!item) {
|
|
74
|
+
* item = document.createElement('vaadin-item');
|
|
75
|
+
* listBox.appendChild(item);
|
|
76
|
+
* }
|
|
77
|
+
* item.textContent = 'Content of the selector: ' + context.target.textContent;
|
|
78
|
+
* };
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* You can access the menu context inside the renderer using
|
|
82
|
+
* `context.target` and `context.detail`.
|
|
83
|
+
*
|
|
84
|
+
* Renderer is called on the opening of the context-menu and each time the related context is updated.
|
|
85
|
+
* DOM generated during the renderer call can be reused
|
|
86
|
+
* in the next renderer call and will be provided with the `root` argument.
|
|
87
|
+
* On first call it will be empty.
|
|
88
|
+
*
|
|
89
|
+
* ### “vaadin-contextmenu” Gesture Event
|
|
90
|
+
*
|
|
91
|
+
* `vaadin-contextmenu` is a gesture event (a custom event),
|
|
92
|
+
* which is dispatched after either `contextmenu` or long touch events.
|
|
93
|
+
* This enables support for both mouse and touch environments in a uniform way.
|
|
94
|
+
*
|
|
95
|
+
* `<vaadin-context-menu>` opens the menu overlay on the `vaadin-contextmenu`
|
|
96
|
+
* event by default.
|
|
97
|
+
*
|
|
98
|
+
* ### Menu Listener
|
|
99
|
+
*
|
|
100
|
+
* By default, the `<vaadin-context-menu>` element listens for the menu opening
|
|
101
|
+
* event on itself. In case if you do not want to wrap the target, you can listen for
|
|
102
|
+
* events on an element outside the `<vaadin-context-menu>` by setting the
|
|
103
|
+
* `listenOn` property:
|
|
104
|
+
*
|
|
105
|
+
* ```html
|
|
106
|
+
* <vaadin-context-menu id="contextMenu"></vaadin-context-menu>
|
|
107
|
+
*
|
|
108
|
+
* <div id="menuListener">The element that listens for the contextmenu event.</div>
|
|
109
|
+
* ```
|
|
110
|
+
* ```javascript
|
|
111
|
+
* const contextMenu = document.querySelector('#contextMenu');
|
|
112
|
+
* contextMenu.listenOn = document.querySelector('#menuListener');
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
115
|
+
* ### Filtering Menu Targets
|
|
116
|
+
*
|
|
117
|
+
* By default, the listener element and all its descendants open the context
|
|
118
|
+
* menu. You can filter the menu targets to a smaller set of elements inside
|
|
119
|
+
* the listener element by setting the `selector` property.
|
|
120
|
+
*
|
|
121
|
+
* In the following example, only the elements matching `.has-menu` will open the context menu:
|
|
122
|
+
*
|
|
123
|
+
* ```html
|
|
124
|
+
* <vaadin-context-menu selector=".has-menu">
|
|
125
|
+
* <p class="has-menu">This paragraph opens the context menu</p>
|
|
126
|
+
* <p>This paragraph does not open the context menu</p>
|
|
127
|
+
* </vaadin-context-menu>
|
|
128
|
+
* ```
|
|
129
|
+
*
|
|
130
|
+
* ### Menu Context
|
|
131
|
+
*
|
|
132
|
+
* You can use the following properties in the renderer function:
|
|
133
|
+
*
|
|
134
|
+
* - `target` is the menu opening event target, which is the element that
|
|
135
|
+
* the user has called the context menu for
|
|
136
|
+
* - `detail` is the menu opening event detail
|
|
137
|
+
*
|
|
138
|
+
* In the following example, the menu item text is composed with the contents
|
|
139
|
+
* of the element that opened the menu:
|
|
140
|
+
*
|
|
141
|
+
* ```html
|
|
142
|
+
* <vaadin-context-menu selector="li" id="contextMenu">
|
|
143
|
+
* <ul>
|
|
144
|
+
* <li>Foo</li>
|
|
145
|
+
* <li>Bar</li>
|
|
146
|
+
* <li>Baz</li>
|
|
147
|
+
* </ul>
|
|
148
|
+
* </vaadin-context-menu>
|
|
149
|
+
* ```
|
|
150
|
+
* ```js
|
|
151
|
+
* const contextMenu = document.querySelector('#contextMenu');
|
|
152
|
+
* contextMenu.renderer = (root, contextMenu, context) => {
|
|
153
|
+
* let listBox = root.firstElementChild;
|
|
154
|
+
* if (!listBox) {
|
|
155
|
+
* listBox = document.createElement('vaadin-list-box');
|
|
156
|
+
* root.appendChild(listBox);
|
|
157
|
+
* }
|
|
158
|
+
*
|
|
159
|
+
* let item = listBox.querySelector('vaadin-item');
|
|
160
|
+
* if (!item) {
|
|
161
|
+
* item = document.createElement('vaadin-item');
|
|
162
|
+
* listBox.appendChild(item);
|
|
163
|
+
* }
|
|
164
|
+
* item.textContent = 'The menu target: ' + context.target.textContent;
|
|
165
|
+
* };
|
|
166
|
+
* ```
|
|
167
|
+
*
|
|
168
|
+
* ### Styling
|
|
169
|
+
*
|
|
170
|
+
* `<vaadin-context-menu>` uses `<vaadin-context-menu-overlay>` internal
|
|
171
|
+
* themable component as the actual visible context menu overlay.
|
|
172
|
+
*
|
|
173
|
+
* See [`<vaadin-overlay>`](#/elements/vaadin-overlay)
|
|
174
|
+
* documentation for `<vaadin-context-menu-overlay>` stylable parts.
|
|
175
|
+
*
|
|
176
|
+
* See [Styling Components](https://vaadin.com/docs/latest/ds/customization/styling-components) documentation.
|
|
177
|
+
*
|
|
178
|
+
* ### Internal components
|
|
179
|
+
*
|
|
180
|
+
* When using `items` API, in addition `<vaadin-context-menu-overlay>`, the following
|
|
181
|
+
* internal components are themable:
|
|
182
|
+
*
|
|
183
|
+
* - `<vaadin-context-menu-item>` - has the same API as [`<vaadin-item>`](#/elements/vaadin-item).
|
|
184
|
+
* - `<vaadin-context-menu-list-box>` - has the same API as [`<vaadin-list-box>`](#/elements/vaadin-list-box).
|
|
185
|
+
*
|
|
186
|
+
* Note: the `theme` attribute value set on `<vaadin-context-menu>` is
|
|
187
|
+
* propagated to the internal components listed above.
|
|
188
|
+
*
|
|
189
|
+
* @fires {CustomEvent} opened-changed - Fired when the `opened` property changes.
|
|
190
|
+
* @fires {CustomEvent} item-selected - Fired when an item is selected when the context menu is populated using the `items` API.
|
|
191
|
+
*/
|
|
192
|
+
declare class ContextMenu extends ElementMixin(ThemePropertyMixin(ItemsMixin(GestureEventListeners(HTMLElement)))) {
|
|
193
|
+
/**
|
|
194
|
+
* CSS selector that can be used to target any child element
|
|
195
|
+
* of the context menu to listen for `openOn` events.
|
|
196
|
+
*/
|
|
197
|
+
selector: string | null | undefined;
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* True if the overlay is currently displayed.
|
|
201
|
+
*/
|
|
202
|
+
readonly opened: boolean;
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Event name to listen for opening the context menu.
|
|
206
|
+
* @attr {string} open-on
|
|
207
|
+
*/
|
|
208
|
+
openOn: string;
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* The target element that's listened to for context menu opening events.
|
|
212
|
+
* By default the vaadin-context-menu listens to the target's `vaadin-contextmenu`
|
|
213
|
+
* events.
|
|
214
|
+
*/
|
|
215
|
+
listenOn: HTMLElement;
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Event name to listen for closing the context menu.
|
|
219
|
+
* @attr {string} close-on
|
|
220
|
+
*/
|
|
221
|
+
closeOn: string;
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Custom function for rendering the content of the menu overlay.
|
|
225
|
+
* Receives three arguments:
|
|
226
|
+
*
|
|
227
|
+
* - `root` The root container DOM element. Append your content to it.
|
|
228
|
+
* - `contextMenu` The reference to the `<vaadin-context-menu>` element.
|
|
229
|
+
* - `context` The object with the menu context, contains:
|
|
230
|
+
* - `context.target` the target of the menu opening event,
|
|
231
|
+
* - `context.detail` the menu opening event detail.
|
|
232
|
+
*/
|
|
233
|
+
renderer: ContextMenuRenderer | null | undefined;
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Requests an update for the content of the menu overlay.
|
|
237
|
+
* While performing the update, it invokes the renderer passed in the `renderer` property.
|
|
238
|
+
*
|
|
239
|
+
* It is not guaranteed that the update happens immediately (synchronously) after it is requested.
|
|
240
|
+
*/
|
|
241
|
+
requestContentUpdate(): void;
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Manually invoke existing renderer.
|
|
245
|
+
*
|
|
246
|
+
* @deprecated Since Vaadin 21, `render()` is deprecated. Please use `requestContentUpdate()` instead.
|
|
247
|
+
*/
|
|
248
|
+
render(): void;
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Closes the overlay.
|
|
252
|
+
*/
|
|
253
|
+
close(): void;
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Opens the overlay.
|
|
257
|
+
*
|
|
258
|
+
* @param e used as the context for the menu. Overlay coordinates are taken from this event.
|
|
259
|
+
*/
|
|
260
|
+
open(e: Event | undefined): void;
|
|
261
|
+
|
|
262
|
+
addEventListener<K extends keyof ContextMenuEventMap>(
|
|
263
|
+
type: K,
|
|
264
|
+
listener: (this: ContextMenu, ev: ContextMenuEventMap[K]) => void,
|
|
265
|
+
options?: boolean | AddEventListenerOptions
|
|
266
|
+
): void;
|
|
267
|
+
|
|
268
|
+
removeEventListener<K extends keyof ContextMenuEventMap>(
|
|
269
|
+
type: K,
|
|
270
|
+
listener: (this: ContextMenu, ev: ContextMenuEventMap[K]) => void,
|
|
271
|
+
options?: boolean | EventListenerOptions
|
|
272
|
+
): void;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
declare global {
|
|
276
|
+
interface HTMLElementTagNameMap {
|
|
277
|
+
'vaadin-context-menu': ContextMenu;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export { ContextMenu };
|