@vaadin/context-menu 24.3.0-alpha1 → 24.3.0-alpha10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vaadin/context-menu",
3
- "version": "24.3.0-alpha1",
3
+ "version": "24.3.0-alpha10",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -39,25 +39,25 @@
39
39
  "dependencies": {
40
40
  "@open-wc/dedupe-mixin": "^1.3.0",
41
41
  "@polymer/polymer": "^3.0.0",
42
- "@vaadin/a11y-base": "24.3.0-alpha1",
43
- "@vaadin/component-base": "24.3.0-alpha1",
44
- "@vaadin/item": "24.3.0-alpha1",
45
- "@vaadin/list-box": "24.3.0-alpha1",
46
- "@vaadin/lit-renderer": "24.3.0-alpha1",
47
- "@vaadin/overlay": "24.3.0-alpha1",
48
- "@vaadin/vaadin-lumo-styles": "24.3.0-alpha1",
49
- "@vaadin/vaadin-material-styles": "24.3.0-alpha1",
50
- "@vaadin/vaadin-themable-mixin": "24.3.0-alpha1"
42
+ "@vaadin/a11y-base": "24.3.0-alpha10",
43
+ "@vaadin/component-base": "24.3.0-alpha10",
44
+ "@vaadin/item": "24.3.0-alpha10",
45
+ "@vaadin/list-box": "24.3.0-alpha10",
46
+ "@vaadin/lit-renderer": "24.3.0-alpha10",
47
+ "@vaadin/overlay": "24.3.0-alpha10",
48
+ "@vaadin/vaadin-lumo-styles": "24.3.0-alpha10",
49
+ "@vaadin/vaadin-material-styles": "24.3.0-alpha10",
50
+ "@vaadin/vaadin-themable-mixin": "24.3.0-alpha10"
51
51
  },
52
52
  "devDependencies": {
53
53
  "@esm-bundle/chai": "^4.3.4",
54
- "@vaadin/testing-helpers": "^0.5.0",
55
- "lit": "^2.0.0",
54
+ "@vaadin/testing-helpers": "^0.6.0",
55
+ "lit": "^3.0.0",
56
56
  "sinon": "^13.0.2"
57
57
  },
58
58
  "web-types": [
59
59
  "web-types.json",
60
60
  "web-types.lit.json"
61
61
  ],
62
- "gitHead": "9ca6f3ca220a777e8eea181a1f5717e39a732240"
62
+ "gitHead": "0271523d93fe5df0425ff64206886614f3c6f401"
63
63
  }
@@ -0,0 +1,81 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2016 - 2023 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import type { Constructor } from '@open-wc/dedupe-mixin';
7
+ import type { ContextMenuRenderer } from './vaadin-context-menu.js';
8
+ import type { ItemsMixinClass } from './vaadin-contextmenu-items-mixin.js';
9
+
10
+ export declare function ContextMenuMixin<T extends Constructor<HTMLElement>>(
11
+ base: T,
12
+ ): Constructor<ContextMenuMixinClass> & Constructor<ItemsMixinClass> & T;
13
+
14
+ export declare class ContextMenuMixinClass {
15
+ /**
16
+ * CSS selector that can be used to target any child element
17
+ * of the context menu to listen for `openOn` events.
18
+ */
19
+ selector: string | null | undefined;
20
+
21
+ /**
22
+ * True if the overlay is currently displayed.
23
+ */
24
+ readonly opened: boolean;
25
+
26
+ /**
27
+ * Event name to listen for opening the context menu.
28
+ * @attr {string} open-on
29
+ */
30
+ openOn: string;
31
+
32
+ /**
33
+ * The target element that's listened to for context menu opening events.
34
+ * By default the vaadin-context-menu listens to the target's `vaadin-contextmenu`
35
+ * events.
36
+ */
37
+ listenOn: HTMLElement;
38
+
39
+ /**
40
+ * Event name to listen for closing the context menu.
41
+ * @attr {string} close-on
42
+ */
43
+ closeOn: string;
44
+
45
+ /**
46
+ * Custom function for rendering the content of the menu overlay.
47
+ * Receives three arguments:
48
+ *
49
+ * - `root` The root container DOM element. Append your content to it.
50
+ * - `contextMenu` The reference to the `<vaadin-context-menu>` element.
51
+ * - `context` The object with the menu context, contains:
52
+ * - `context.target` the target of the menu opening event,
53
+ * - `context.detail` the menu opening event detail.
54
+ */
55
+ renderer: ContextMenuRenderer | null | undefined;
56
+
57
+ /**
58
+ * When true, the menu overlay is modeless.
59
+ */
60
+ protected _modeless: boolean;
61
+
62
+ /**
63
+ * Requests an update for the content of the menu overlay.
64
+ * While performing the update, it invokes the renderer passed in the `renderer` property.
65
+ *
66
+ * It is not guaranteed that the update happens immediately (synchronously) after it is requested.
67
+ */
68
+ requestContentUpdate(): void;
69
+
70
+ /**
71
+ * Closes the overlay.
72
+ */
73
+ close(): void;
74
+
75
+ /**
76
+ * Opens the overlay.
77
+ *
78
+ * @param e used as the context for the menu. Overlay coordinates are taken from this event.
79
+ */
80
+ open(e: Event | undefined): void;
81
+ }
@@ -0,0 +1,494 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2016 - 2023 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import { isTouch } from '@vaadin/component-base/src/browser-utils.js';
7
+ import { addListener, gestures, removeListener } from '@vaadin/component-base/src/gestures.js';
8
+ import { MediaQueryController } from '@vaadin/component-base/src/media-query-controller.js';
9
+ import { ItemsMixin } from './vaadin-contextmenu-items-mixin.js';
10
+
11
+ /**
12
+ * @polymerMixin
13
+ * @mixes ItemsMixin
14
+ */
15
+ export const ContextMenuMixin = (superClass) =>
16
+ class ContextMenuMixinClass extends ItemsMixin(superClass) {
17
+ static get properties() {
18
+ return {
19
+ /**
20
+ * CSS selector that can be used to target any child element
21
+ * of the context menu to listen for `openOn` events.
22
+ */
23
+ selector: {
24
+ type: String,
25
+ },
26
+
27
+ /**
28
+ * True if the overlay is currently displayed.
29
+ * @type {boolean}
30
+ */
31
+ opened: {
32
+ type: Boolean,
33
+ value: false,
34
+ notify: true,
35
+ readOnly: true,
36
+ },
37
+
38
+ /**
39
+ * Event name to listen for opening the context menu.
40
+ * @attr {string} open-on
41
+ * @type {string}
42
+ */
43
+ openOn: {
44
+ type: String,
45
+ value: 'vaadin-contextmenu',
46
+ },
47
+
48
+ /**
49
+ * The target element that's listened to for context menu opening events.
50
+ * By default the vaadin-context-menu listens to the target's `vaadin-contextmenu`
51
+ * events.
52
+ * @type {!HTMLElement}
53
+ * @default self
54
+ */
55
+ listenOn: {
56
+ type: Object,
57
+ value() {
58
+ return this;
59
+ },
60
+ },
61
+
62
+ /**
63
+ * Event name to listen for closing the context menu.
64
+ * @attr {string} close-on
65
+ * @type {string}
66
+ */
67
+ closeOn: {
68
+ type: String,
69
+ value: 'click',
70
+ observer: '_closeOnChanged',
71
+ },
72
+
73
+ /**
74
+ * Custom function for rendering the content of the menu overlay.
75
+ * Receives three arguments:
76
+ *
77
+ * - `root` The root container DOM element. Append your content to it.
78
+ * - `contextMenu` The reference to the `<vaadin-context-menu>` element.
79
+ * - `context` The object with the menu context, contains:
80
+ * - `context.target` the target of the menu opening event,
81
+ * - `context.detail` the menu opening event detail.
82
+ * @type {ContextMenuRenderer | undefined}
83
+ */
84
+ renderer: {
85
+ type: Function,
86
+ },
87
+
88
+ /**
89
+ * When true, the menu overlay is modeless.
90
+ * @protected
91
+ */
92
+ _modeless: {
93
+ type: Boolean,
94
+ },
95
+
96
+ /** @private */
97
+ _context: Object,
98
+
99
+ /** @private */
100
+ _phone: {
101
+ type: Boolean,
102
+ },
103
+
104
+ /** @private */
105
+ _touch: {
106
+ type: Boolean,
107
+ value: isTouch,
108
+ },
109
+
110
+ /** @private */
111
+ _wide: {
112
+ type: Boolean,
113
+ },
114
+
115
+ /** @private */
116
+ _wideMediaQuery: {
117
+ type: String,
118
+ value: '(min-device-width: 750px)',
119
+ },
120
+ };
121
+ }
122
+
123
+ static get observers() {
124
+ return [
125
+ '_openedChanged(opened)',
126
+ '_targetOrOpenOnChanged(listenOn, openOn)',
127
+ '_rendererChanged(renderer, items)',
128
+ '_touchOrWideChanged(_touch, _wide)',
129
+ ];
130
+ }
131
+
132
+ constructor() {
133
+ super();
134
+ this._boundOpen = this.open.bind(this);
135
+ this._boundClose = this.close.bind(this);
136
+ this._boundPreventDefault = this._preventDefault.bind(this);
137
+ this._boundOnGlobalContextMenu = this._onGlobalContextMenu.bind(this);
138
+ }
139
+
140
+ /** @protected */
141
+ connectedCallback() {
142
+ super.connectedCallback();
143
+
144
+ this.__boundOnScroll = this.__onScroll.bind(this);
145
+ window.addEventListener('scroll', this.__boundOnScroll, true);
146
+
147
+ // Restore opened state if overlay was opened when disconnecting
148
+ if (this.__restoreOpened) {
149
+ this._setOpened(true);
150
+ }
151
+ }
152
+
153
+ /** @protected */
154
+ disconnectedCallback() {
155
+ super.disconnectedCallback();
156
+
157
+ window.removeEventListener('scroll', this.__boundOnScroll, true);
158
+
159
+ // Close overlay and memorize opened state
160
+ this.__restoreOpened = this.opened;
161
+ this.close();
162
+ }
163
+
164
+ /** @protected */
165
+ ready() {
166
+ super.ready();
167
+
168
+ this._overlayElement = this.$.overlay;
169
+ this._overlayElement.owner = this;
170
+
171
+ this.addController(
172
+ new MediaQueryController(this._wideMediaQuery, (matches) => {
173
+ this._wide = matches;
174
+ }),
175
+ );
176
+ }
177
+
178
+ /**
179
+ * Runs before overlay is fully rendered
180
+ * @private
181
+ */
182
+ _onOverlayOpened(e) {
183
+ this._setOpened(e.detail.value);
184
+ this.__alignOverlayPosition();
185
+ }
186
+
187
+ /**
188
+ * Runs after overlay is fully rendered
189
+ * @private
190
+ */
191
+ _onVaadinOverlayOpen() {
192
+ this.__alignOverlayPosition();
193
+ this.$.overlay.style.opacity = '';
194
+ this.__forwardFocus();
195
+ }
196
+
197
+ /** @private */
198
+ _targetOrOpenOnChanged(listenOn, openOn) {
199
+ if (this._oldListenOn && this._oldOpenOn) {
200
+ this._unlisten(this._oldListenOn, this._oldOpenOn, this._boundOpen);
201
+
202
+ this._oldListenOn.style.webkitTouchCallout = '';
203
+ this._oldListenOn.style.webkitUserSelect = '';
204
+ this._oldListenOn.style.userSelect = '';
205
+
206
+ this._oldListenOn = null;
207
+ this._oldOpenOn = null;
208
+ }
209
+
210
+ if (listenOn && openOn) {
211
+ this._listen(listenOn, openOn, this._boundOpen);
212
+
213
+ this._oldListenOn = listenOn;
214
+ this._oldOpenOn = openOn;
215
+ }
216
+ }
217
+
218
+ /** @private */
219
+ _touchOrWideChanged(touch, wide) {
220
+ this._phone = !wide && touch;
221
+ }
222
+
223
+ /** @private */
224
+ _setListenOnUserSelect(value) {
225
+ // Note: these styles don't seem to work in Firefox on iOS.
226
+ this.listenOn.style.webkitTouchCallout = value;
227
+ this.listenOn.style.webkitUserSelect = value; // Chrome, Safari, Firefox
228
+ this.listenOn.style.userSelect = value;
229
+
230
+ // Note: because user-selection is disabled on the overlay
231
+ // before opening the menu the text could be already selected
232
+ // so we need to clear that selection
233
+ document.getSelection().removeAllRanges();
234
+ }
235
+
236
+ /** @private */
237
+ _closeOnChanged(closeOn, oldCloseOn) {
238
+ // Outside click event from overlay
239
+ const evtOverlay = 'vaadin-overlay-outside-click';
240
+
241
+ const overlay = this.$.overlay;
242
+
243
+ if (oldCloseOn) {
244
+ this._unlisten(overlay, oldCloseOn, this._boundClose);
245
+ }
246
+ if (closeOn) {
247
+ this._listen(overlay, closeOn, this._boundClose);
248
+ overlay.removeEventListener(evtOverlay, this._boundPreventDefault);
249
+ } else {
250
+ overlay.addEventListener(evtOverlay, this._boundPreventDefault);
251
+ }
252
+ }
253
+
254
+ /** @private */
255
+ _preventDefault(e) {
256
+ e.preventDefault();
257
+ }
258
+
259
+ /** @private */
260
+ _openedChanged(opened) {
261
+ if (opened) {
262
+ document.documentElement.addEventListener('contextmenu', this._boundOnGlobalContextMenu, true);
263
+ this._setListenOnUserSelect('none');
264
+ } else {
265
+ document.documentElement.removeEventListener('contextmenu', this._boundOnGlobalContextMenu, true);
266
+ this._setListenOnUserSelect('');
267
+ }
268
+
269
+ // Has to be set after instance has been created
270
+ this.$.overlay.opened = opened;
271
+ }
272
+
273
+ /**
274
+ * Requests an update for the content of the menu overlay.
275
+ * While performing the update, it invokes the renderer passed in the `renderer` property.
276
+ *
277
+ * It is not guaranteed that the update happens immediately (synchronously) after it is requested.
278
+ */
279
+ requestContentUpdate() {
280
+ if (!this._overlayElement || !this.renderer) {
281
+ return;
282
+ }
283
+
284
+ this._overlayElement.requestContentUpdate();
285
+ }
286
+
287
+ /** @private */
288
+ _rendererChanged(renderer, items) {
289
+ if (items) {
290
+ if (renderer) {
291
+ throw new Error('The items API cannot be used together with a renderer');
292
+ }
293
+
294
+ if (this.closeOn === 'click') {
295
+ this.closeOn = '';
296
+ }
297
+
298
+ renderer = this.__itemsRenderer;
299
+ }
300
+
301
+ this.$.overlay.renderer = renderer;
302
+ }
303
+
304
+ /**
305
+ * Closes the overlay.
306
+ */
307
+ close() {
308
+ this._setOpened(false);
309
+ }
310
+
311
+ /** @private */
312
+ _contextTarget(e) {
313
+ if (this.selector) {
314
+ const targets = this.listenOn.querySelectorAll(this.selector);
315
+
316
+ return Array.prototype.filter.call(targets, (el) => {
317
+ return e.composedPath().indexOf(el) > -1;
318
+ })[0];
319
+ }
320
+ return e.target;
321
+ }
322
+
323
+ /**
324
+ * Opens the overlay.
325
+ * @param {!Event | undefined} e used as the context for the menu. Overlay coordinates are taken from this event.
326
+ */
327
+ open(e) {
328
+ if (e && !this.opened) {
329
+ this._context = {
330
+ detail: e.detail,
331
+ target: this._contextTarget(e),
332
+ };
333
+
334
+ if (this._context.target) {
335
+ e.preventDefault();
336
+ e.stopPropagation();
337
+
338
+ // Used in alignment which is delayed until overlay is rendered
339
+ this.__x = this._getEventCoordinate(e, 'x');
340
+ this.__pageXOffset = window.pageXOffset;
341
+
342
+ this.__y = this._getEventCoordinate(e, 'y');
343
+ this.__pageYOffset = window.pageYOffset;
344
+
345
+ this.$.overlay.style.opacity = '0';
346
+ this._setOpened(true);
347
+ }
348
+ }
349
+ }
350
+
351
+ /** @private */
352
+ __onScroll() {
353
+ if (!this.opened) {
354
+ return;
355
+ }
356
+
357
+ const yDiff = window.pageYOffset - this.__pageYOffset;
358
+ const xDiff = window.pageXOffset - this.__pageXOffset;
359
+
360
+ this.__adjustPosition('left', -xDiff);
361
+ this.__adjustPosition('right', xDiff);
362
+
363
+ this.__adjustPosition('top', -yDiff);
364
+ this.__adjustPosition('bottom', yDiff);
365
+
366
+ this.__pageYOffset += yDiff;
367
+ this.__pageXOffset += xDiff;
368
+ }
369
+
370
+ /** @private */
371
+ __adjustPosition(coord, diff) {
372
+ const overlay = this.$.overlay;
373
+ const style = overlay.style;
374
+
375
+ style[coord] = `${(parseInt(style[coord]) || 0) + diff}px`;
376
+ }
377
+
378
+ /** @private */
379
+ __alignOverlayPosition() {
380
+ const overlay = this.$.overlay;
381
+
382
+ if (overlay.positionTarget) {
383
+ // The overlay is positioned relative to another node, for example, a
384
+ // menu item in a nested submenu structure where this overlay lists
385
+ // the items for another submenu.
386
+ // It means that the overlay positioning is controlled by
387
+ // vaadin-overlay-position-mixin so no manual alignment is needed.
388
+ return;
389
+ }
390
+
391
+ const style = overlay.style;
392
+
393
+ // Reset all properties before measuring
394
+ ['top', 'right', 'bottom', 'left'].forEach((prop) => style.removeProperty(prop));
395
+ ['right-aligned', 'end-aligned', 'bottom-aligned'].forEach((attr) => overlay.removeAttribute(attr));
396
+
397
+ // Maximum x and y values are imposed by content size and overlay limits.
398
+ const { xMax, xMin, yMax } = overlay.getBoundaries();
399
+ // Reuse saved x and y event values, in order to this method be used async
400
+ // in the `vaadin-overlay-change` which guarantees that overlay is ready.
401
+ // The valus represent an anchor position on the page where the contextmenu
402
+ // event took place.
403
+ const x = this.__x;
404
+ const y = this.__y;
405
+
406
+ // Select one overlay corner and move to the event x/y position.
407
+ // Then set styling attrs for flex-aligning the content appropriately.
408
+ const wdthVport = document.documentElement.clientWidth;
409
+ const hghtVport = document.documentElement.clientHeight;
410
+
411
+ if (!this.__isRTL) {
412
+ if (x < wdthVport / 2 || x < xMax) {
413
+ // Menu is displayed in the right side of the anchor
414
+ style.left = `${x}px`;
415
+ } else {
416
+ // Menu is displayed in the left side of the anchor
417
+ style.right = `${Math.max(0, wdthVport - x)}px`;
418
+ this._setEndAligned(overlay);
419
+ }
420
+ } else if (x > wdthVport / 2 || x > xMin) {
421
+ // Menu is displayed in the right side of the anchor
422
+ style.right = `${Math.max(0, wdthVport - x)}px`;
423
+ } else {
424
+ // Menu is displayed in the left side of the anchor
425
+ style.left = `${x}px`;
426
+ this._setEndAligned(overlay);
427
+ }
428
+
429
+ if (y < hghtVport / 2 || y < yMax) {
430
+ style.top = `${y}px`;
431
+ } else {
432
+ style.bottom = `${Math.max(0, hghtVport - y)}px`;
433
+ overlay.setAttribute('bottom-aligned', '');
434
+ }
435
+ }
436
+
437
+ /** @private */
438
+ _setEndAligned(element) {
439
+ element.setAttribute('end-aligned', '');
440
+ if (!this.__isRTL) {
441
+ element.setAttribute('right-aligned', '');
442
+ }
443
+ }
444
+
445
+ /** @private */
446
+ _getEventCoordinate(event, coord) {
447
+ if (event.detail instanceof Object) {
448
+ if (event.detail[coord]) {
449
+ // Polymer gesture events, get coordinate from detail
450
+ return event.detail[coord];
451
+ } else if (event.detail.sourceEvent) {
452
+ // Unwrap detailed event
453
+ return this._getEventCoordinate(event.detail.sourceEvent, coord);
454
+ }
455
+ } else {
456
+ const prop = `client${coord.toUpperCase()}`;
457
+ const position = event.changedTouches ? event.changedTouches[0][prop] : event[prop];
458
+
459
+ if (position === 0) {
460
+ // Native keyboard event
461
+ const rect = event.target.getBoundingClientRect();
462
+ return coord === 'x' ? rect.left : rect.top + rect.height;
463
+ }
464
+ // Native mouse or touch event
465
+ return position;
466
+ }
467
+ }
468
+
469
+ /** @private */
470
+ _listen(node, evType, handler) {
471
+ if (gestures[evType]) {
472
+ addListener(node, evType, handler);
473
+ } else {
474
+ node.addEventListener(evType, handler);
475
+ }
476
+ }
477
+
478
+ /** @private */
479
+ _unlisten(node, evType, handler) {
480
+ if (gestures[evType]) {
481
+ removeListener(node, evType, handler);
482
+ } else {
483
+ node.removeEventListener(evType, handler);
484
+ }
485
+ }
486
+
487
+ /** @private */
488
+ _onGlobalContextMenu(e) {
489
+ if (!e.shiftKey) {
490
+ e.preventDefault();
491
+ this.close();
492
+ }
493
+ }
494
+ };