@vaadin/context-menu 25.0.0-alpha2 → 25.0.0-alpha20

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.
Files changed (27) hide show
  1. package/package.json +13 -14
  2. package/src/styles/vaadin-context-menu-item-base-styles.d.ts +8 -0
  3. package/src/styles/vaadin-context-menu-item-base-styles.js +34 -0
  4. package/src/styles/vaadin-context-menu-overlay-base-styles.d.ts +8 -0
  5. package/src/styles/vaadin-context-menu-overlay-base-styles.js +36 -0
  6. package/src/{vaadin-menu-overlay-styles.d.ts → styles/vaadin-menu-overlay-base-styles.d.ts} +1 -1
  7. package/src/{vaadin-menu-overlay-styles.js → styles/vaadin-menu-overlay-base-styles.js} +7 -8
  8. package/src/vaadin-context-menu-item.js +5 -11
  9. package/src/vaadin-context-menu-list-box.js +5 -18
  10. package/src/vaadin-context-menu-mixin.js +118 -75
  11. package/src/vaadin-context-menu-overlay.js +59 -4
  12. package/src/vaadin-context-menu.d.ts +48 -11
  13. package/src/vaadin-context-menu.js +98 -23
  14. package/src/vaadin-contextmenu-items-mixin.js +61 -41
  15. package/src/vaadin-menu-overlay-mixin.d.ts +0 -5
  16. package/src/vaadin-menu-overlay-mixin.js +23 -28
  17. package/vaadin-context-menu.js +1 -1
  18. package/web-types.json +10 -6
  19. package/web-types.lit.json +11 -4
  20. package/theme/lumo/vaadin-context-menu-item-styles.d.ts +0 -6
  21. package/theme/lumo/vaadin-context-menu-item-styles.js +0 -45
  22. package/theme/lumo/vaadin-context-menu-list-box-styles.d.ts +0 -5
  23. package/theme/lumo/vaadin-context-menu-list-box-styles.js +0 -47
  24. package/theme/lumo/vaadin-context-menu-overlay-styles.d.ts +0 -4
  25. package/theme/lumo/vaadin-context-menu-overlay-styles.js +0 -35
  26. package/theme/lumo/vaadin-context-menu.d.ts +0 -4
  27. package/theme/lumo/vaadin-context-menu.js +0 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vaadin/context-menu",
3
- "version": "25.0.0-alpha2",
3
+ "version": "25.0.0-alpha20",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -23,7 +23,6 @@
23
23
  "lit.d.ts",
24
24
  "lit.js",
25
25
  "src",
26
- "theme",
27
26
  "vaadin-*.d.ts",
28
27
  "vaadin-*.js",
29
28
  "web-types.json",
@@ -37,25 +36,25 @@
37
36
  ],
38
37
  "dependencies": {
39
38
  "@open-wc/dedupe-mixin": "^1.3.0",
40
- "@vaadin/a11y-base": "25.0.0-alpha2",
41
- "@vaadin/component-base": "25.0.0-alpha2",
42
- "@vaadin/item": "25.0.0-alpha2",
43
- "@vaadin/list-box": "25.0.0-alpha2",
44
- "@vaadin/lit-renderer": "25.0.0-alpha2",
45
- "@vaadin/overlay": "25.0.0-alpha2",
46
- "@vaadin/vaadin-lumo-styles": "25.0.0-alpha2",
47
- "@vaadin/vaadin-themable-mixin": "25.0.0-alpha2",
39
+ "@vaadin/a11y-base": "25.0.0-alpha20",
40
+ "@vaadin/component-base": "25.0.0-alpha20",
41
+ "@vaadin/item": "25.0.0-alpha20",
42
+ "@vaadin/list-box": "25.0.0-alpha20",
43
+ "@vaadin/lit-renderer": "25.0.0-alpha20",
44
+ "@vaadin/overlay": "25.0.0-alpha20",
45
+ "@vaadin/vaadin-themable-mixin": "25.0.0-alpha20",
48
46
  "lit": "^3.0.0"
49
47
  },
50
48
  "devDependencies": {
51
- "@vaadin/chai-plugins": "25.0.0-alpha2",
52
- "@vaadin/test-runner-commands": "25.0.0-alpha2",
49
+ "@vaadin/chai-plugins": "25.0.0-alpha20",
50
+ "@vaadin/test-runner-commands": "25.0.0-alpha20",
53
51
  "@vaadin/testing-helpers": "^2.0.0",
54
- "sinon": "^18.0.0"
52
+ "@vaadin/vaadin-lumo-styles": "25.0.0-alpha20",
53
+ "sinon": "^21.0.0"
55
54
  },
56
55
  "web-types": [
57
56
  "web-types.json",
58
57
  "web-types.lit.json"
59
58
  ],
60
- "gitHead": "67ffcd5355cf21ce1b5039c598525109fc4c164b"
59
+ "gitHead": "c948aae591a30b432f3784000d4677674cae56e0"
61
60
  }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2016 - 2025 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import type { CSSResult } from 'lit';
7
+
8
+ export const contextMenuItemStyles: CSSResult;
@@ -0,0 +1,34 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2016 - 2025 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import '@vaadin/component-base/src/styles/style-props.js';
7
+ import { css } from 'lit';
8
+ import { itemStyles } from '@vaadin/item/src/styles/vaadin-item-base-styles.js';
9
+
10
+ const menuItemStyles = css`
11
+ :host::after {
12
+ background: var(--vaadin-text-color-secondary);
13
+ content: '';
14
+ display: block;
15
+ height: var(--vaadin-icon-size, 1lh);
16
+ mask-image: var(--_vaadin-icon-chevron-down);
17
+ rotate: -90deg;
18
+ visibility: hidden;
19
+ width: var(--vaadin-icon-size, 1lh);
20
+ }
21
+
22
+ :host([dir='rtl'])::after {
23
+ rotate: 90deg;
24
+ }
25
+
26
+ /* TODO would be nice to only reserve the space for these if
27
+ one or mote items in the list is checkable or has child items */
28
+ :host([aria-haspopup='true'])::after,
29
+ :host([menu-item-checked]) [part='checkmark'] {
30
+ visibility: visible;
31
+ }
32
+ `;
33
+
34
+ export const contextMenuItemStyles = [itemStyles, menuItemStyles];
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2016 - 2025 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import type { CSSResult } from 'lit';
7
+
8
+ export const contextMenuOverlayStyles: CSSResult;
@@ -0,0 +1,36 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2016 - 2025 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import { css } from 'lit';
7
+ import { overlayStyles } from '@vaadin/overlay/src/styles/vaadin-overlay-base-styles.js';
8
+ import { menuOverlayStyles } from './vaadin-menu-overlay-base-styles.js';
9
+
10
+ const contextMenuOverlay = css`
11
+ :host {
12
+ --_default-offset: 4px;
13
+ }
14
+
15
+ :host([position^='top'][top-aligned]) [part='overlay'],
16
+ :host([position^='bottom'][top-aligned]) [part='overlay'] {
17
+ margin-top: var(--vaadin-context-menu-offset-top, var(--_default-offset));
18
+ }
19
+
20
+ :host([position^='top'][bottom-aligned]) [part='overlay'],
21
+ :host([position^='bottom'][bottom-aligned]) [part='overlay'] {
22
+ margin-bottom: var(--vaadin-context-menu-offset-bottom, var(--_default-offset));
23
+ }
24
+
25
+ :host([position^='start'][start-aligned]) [part='overlay'],
26
+ :host([position^='end'][start-aligned]) [part='overlay'] {
27
+ margin-inline-start: var(--vaadin-context-menu-offset-start, var(--_default-offset));
28
+ }
29
+
30
+ :host([position^='start'][end-aligned]) [part='overlay'],
31
+ :host([position^='end'][end-aligned]) [part='overlay'] {
32
+ margin-inline-end: var(--vaadin-context-menu-offset-end, var(--_default-offset));
33
+ }
34
+ `;
35
+
36
+ export const contextMenuOverlayStyles = [overlayStyles, menuOverlayStyles, contextMenuOverlay];
@@ -5,4 +5,4 @@
5
5
  */
6
6
  import type { CSSResult } from 'lit';
7
7
 
8
- export const styles: CSSResult;
8
+ export const menuOverlayStyles: CSSResult;
@@ -3,9 +3,9 @@
3
3
  * Copyright (c) 2016 - 2025 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
- import { css } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
6
+ import { css } from 'lit';
7
7
 
8
- export const styles = css`
8
+ export const menuOverlayStyles = css`
9
9
  :host {
10
10
  align-items: flex-start;
11
11
  justify-content: flex-start;
@@ -20,13 +20,12 @@ export const styles = css`
20
20
  justify-content: flex-end;
21
21
  }
22
22
 
23
- [part='overlay'] {
24
- background-color: #fff;
23
+ [part='content'] {
24
+ padding: var(--vaadin-item-overlay-padding, 4px);
25
25
  }
26
26
 
27
- @media (forced-colors: active) {
28
- [part='overlay'] {
29
- outline: 3px solid !important;
30
- }
27
+ /* TODO keyboard focus becomes visible even when navigating the menu with the mouse */
28
+ [part='overlay']:focus-visible {
29
+ outline: none;
31
30
  }
32
31
  `;
@@ -3,12 +3,14 @@
3
3
  * Copyright (c) 2016 - 2025 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
- import { css, html, LitElement } from 'lit';
6
+ import { html, LitElement } from 'lit';
7
7
  import { defineCustomElement } from '@vaadin/component-base/src/define.js';
8
8
  import { DirMixin } from '@vaadin/component-base/src/dir-mixin.js';
9
9
  import { PolylitMixin } from '@vaadin/component-base/src/polylit-mixin.js';
10
10
  import { ItemMixin } from '@vaadin/item/src/vaadin-item-mixin.js';
11
+ import { LumoInjectionMixin } from '@vaadin/vaadin-themable-mixin/lumo-injection-mixin.js';
11
12
  import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
13
+ import { contextMenuItemStyles } from './styles/vaadin-context-menu-item-base-styles.js';
12
14
 
13
15
  /**
14
16
  * An element used internally by `<vaadin-context-menu>`. Not intended to be used separately.
@@ -20,21 +22,13 @@ import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mix
20
22
  * @mixes ThemableMixin
21
23
  * @protected
22
24
  */
23
- class ContextMenuItem extends ItemMixin(ThemableMixin(DirMixin(PolylitMixin(LitElement)))) {
25
+ class ContextMenuItem extends ItemMixin(ThemableMixin(DirMixin(PolylitMixin(LumoInjectionMixin(LitElement))))) {
24
26
  static get is() {
25
27
  return 'vaadin-context-menu-item';
26
28
  }
27
29
 
28
30
  static get styles() {
29
- return css`
30
- :host {
31
- display: inline-block;
32
- }
33
-
34
- :host([hidden]) {
35
- display: none !important;
36
- }
37
- `;
31
+ return contextMenuItemStyles;
38
32
  }
39
33
 
40
34
  /** @protected */
@@ -3,11 +3,13 @@
3
3
  * Copyright (c) 2016 - 2025 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
- import { css, html, LitElement } from 'lit';
6
+ import { html, LitElement } from 'lit';
7
7
  import { ListMixin } from '@vaadin/a11y-base/src/list-mixin.js';
8
8
  import { defineCustomElement } from '@vaadin/component-base/src/define.js';
9
9
  import { DirMixin } from '@vaadin/component-base/src/dir-mixin.js';
10
10
  import { PolylitMixin } from '@vaadin/component-base/src/polylit-mixin.js';
11
+ import { listBoxStyles } from '@vaadin/list-box/src/styles/vaadin-list-box-base-styles.js';
12
+ import { LumoInjectionMixin } from '@vaadin/vaadin-themable-mixin/lumo-injection-mixin.js';
11
13
  import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
12
14
 
13
15
  /**
@@ -20,28 +22,13 @@ import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mix
20
22
  * @mixes ThemableMixin
21
23
  * @protected
22
24
  */
23
- class ContextMenuListBox extends ListMixin(ThemableMixin(DirMixin(PolylitMixin(LitElement)))) {
25
+ class ContextMenuListBox extends ListMixin(ThemableMixin(DirMixin(PolylitMixin(LumoInjectionMixin(LitElement))))) {
24
26
  static get is() {
25
27
  return 'vaadin-context-menu-list-box';
26
28
  }
27
29
 
28
30
  static get styles() {
29
- return css`
30
- :host {
31
- display: flex;
32
- }
33
-
34
- :host([hidden]) {
35
- display: none !important;
36
- }
37
-
38
- [part='items'] {
39
- height: 100%;
40
- width: 100%;
41
- overflow-y: auto;
42
- -webkit-overflow-scrolling: touch;
43
- }
44
- `;
31
+ return listBoxStyles;
45
32
  }
46
33
 
47
34
  static get properties() {
@@ -3,7 +3,7 @@
3
3
  * Copyright (c) 2016 - 2025 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
- import { isElementFocusable } from '@vaadin/a11y-base/src/focus-utils.js';
6
+ import { isElementFocusable, isKeyboardActive } from '@vaadin/a11y-base/src/focus-utils.js';
7
7
  import { isAndroid, isIOS } from '@vaadin/component-base/src/browser-utils.js';
8
8
  import { addListener, deepTargetFind, gestures, removeListener } from '@vaadin/component-base/src/gestures.js';
9
9
  import { MediaQueryController } from '@vaadin/component-base/src/media-query-controller.js';
@@ -31,6 +31,8 @@ export const ContextMenuMixin = (superClass) =>
31
31
  */
32
32
  opened: {
33
33
  type: Boolean,
34
+ reflectToAttribute: true,
35
+ observer: '_openedChanged',
34
36
  value: false,
35
37
  notify: true,
36
38
  readOnly: true,
@@ -123,22 +125,15 @@ export const ContextMenuMixin = (superClass) =>
123
125
 
124
126
  static get observers() {
125
127
  return [
126
- '_openedChanged(opened)',
127
128
  '_targetOrOpenOnChanged(listenOn, openOn)',
128
129
  '_rendererChanged(renderer, items)',
129
130
  '_fullscreenChanged(_fullscreen)',
130
- '_overlayContextChanged(_overlayElement, _context)',
131
- '_overlayModelessChanged(_overlayElement, _modeless)',
132
- '_overlayPhoneChanged(_overlayElement, _phone)',
133
- '_overlayThemeChanged(_overlayElement, _theme)',
134
131
  ];
135
132
  }
136
133
 
137
134
  constructor() {
138
135
  super();
139
136
 
140
- this._createOverlay();
141
-
142
137
  this._boundOpen = this.open.bind(this);
143
138
  this._boundClose = this.close.bind(this);
144
139
  this._boundPreventDefault = this._preventDefault.bind(this);
@@ -170,8 +165,10 @@ export const ContextMenuMixin = (superClass) =>
170
165
  }
171
166
 
172
167
  /** @protected */
173
- ready() {
174
- super.ready();
168
+ firstUpdated() {
169
+ super.firstUpdated();
170
+
171
+ this._overlayElement = this.$.overlay;
175
172
 
176
173
  this.addController(
177
174
  new MediaQueryController(this._fullscreenMediaQuery, (matches) => {
@@ -180,29 +177,17 @@ export const ContextMenuMixin = (superClass) =>
180
177
  );
181
178
  }
182
179
 
183
- /** @private */
184
- _createOverlay() {
185
- // Create an overlay in the constructor to use in observers before `ready()`
186
- const overlay = document.createElement(`${this._tagNamePrefix}-overlay`);
187
- overlay.owner = this;
188
-
189
- overlay.addEventListener('opened-changed', (e) => {
190
- this._onOverlayOpened(e);
191
- });
192
-
193
- overlay.addEventListener('vaadin-overlay-open', (e) => {
194
- this._onVaadinOverlayOpen(e);
195
- });
196
-
197
- this._overlayElement = overlay;
198
- }
199
-
200
180
  /**
201
181
  * Runs before overlay is fully rendered
202
182
  * @private
203
183
  */
204
- _onOverlayOpened(e) {
205
- const opened = e.detail.value;
184
+ _onOverlayOpened(event) {
185
+ // Ignore events from submenus
186
+ if (event.target !== this._overlayElement) {
187
+ return;
188
+ }
189
+
190
+ const opened = event.detail.value;
206
191
  this._setOpened(opened);
207
192
  if (opened) {
208
193
  this.__alignOverlayPosition();
@@ -213,43 +198,23 @@ export const ContextMenuMixin = (superClass) =>
213
198
  * Runs after overlay is fully rendered
214
199
  * @private
215
200
  */
216
- _onVaadinOverlayOpen() {
217
- this.__alignOverlayPosition();
218
- this._overlayElement.style.opacity = '';
219
- this.__forwardFocus();
220
- }
221
-
222
- /** @private */
223
- _overlayContextChanged(overlay, context) {
224
- if (overlay) {
225
- overlay.model = context;
226
- }
227
- }
228
-
229
- /** @private */
230
- _overlayModelessChanged(overlay, modeless) {
231
- if (overlay) {
232
- overlay.modeless = modeless;
201
+ _onVaadinOverlayOpen(event) {
202
+ // Ignore events from submenus
203
+ if (event.target !== this._overlayElement) {
204
+ return;
233
205
  }
234
- }
235
206
 
236
- /** @private */
237
- _overlayPhoneChanged(overlay, phone) {
238
- if (overlay) {
239
- overlay.toggleAttribute('phone', phone);
240
- overlay.withBackdrop = phone;
241
- }
207
+ this.__alignOverlayPosition();
208
+ this._overlayElement.style.visibility = '';
209
+ this.__forwardFocus();
242
210
  }
243
211
 
244
- /** @private */
245
- _overlayThemeChanged(overlay, theme) {
246
- if (overlay) {
247
- if (theme) {
248
- overlay.setAttribute('theme', theme);
249
- } else {
250
- overlay.removeAttribute('theme');
251
- }
252
- }
212
+ /**
213
+ * Runs after overlay's closing animation is finished
214
+ * @private
215
+ */
216
+ _onVaadinOverlayClosed() {
217
+ this.dispatchEvent(new CustomEvent('closed'));
253
218
  }
254
219
 
255
220
  /** @private */
@@ -318,17 +283,14 @@ export const ContextMenuMixin = (superClass) =>
318
283
  }
319
284
 
320
285
  /** @private */
321
- _openedChanged(opened) {
286
+ _openedChanged(opened, oldOpened) {
322
287
  if (opened) {
323
288
  document.documentElement.addEventListener('contextmenu', this._boundOnGlobalContextMenu, true);
324
- } else {
289
+ } else if (oldOpened) {
325
290
  document.documentElement.removeEventListener('contextmenu', this._boundOnGlobalContextMenu, true);
326
291
  }
327
292
 
328
293
  this.__setListenOnUserSelect(opened);
329
-
330
- // Has to be set after instance has been created
331
- this._overlayElement.opened = opened;
332
294
  }
333
295
 
334
296
  /**
@@ -338,11 +300,18 @@ export const ContextMenuMixin = (superClass) =>
338
300
  * It is not guaranteed that the update happens immediately (synchronously) after it is requested.
339
301
  */
340
302
  requestContentUpdate() {
341
- if (!this._overlayElement || !this.renderer) {
303
+ if (!this._overlayElement) {
342
304
  return;
343
305
  }
344
306
 
307
+ // Store state to be restored after re-rendering
308
+ this.__preserveMenuState();
309
+
310
+ // Run overlay renderer to re-create DOM elements
345
311
  this._overlayElement.requestContentUpdate();
312
+
313
+ // Restore focused item, update sub-menu if needed
314
+ this.__restoreMenuState();
346
315
  }
347
316
 
348
317
  /** @private */
@@ -355,11 +324,7 @@ export const ContextMenuMixin = (superClass) =>
355
324
  if (this.closeOn === 'click') {
356
325
  this.closeOn = '';
357
326
  }
358
-
359
- renderer = this.__itemsRenderer;
360
327
  }
361
-
362
- this._overlayElement.renderer = renderer;
363
328
  }
364
329
 
365
330
  /**
@@ -377,6 +342,9 @@ export const ContextMenuMixin = (superClass) =>
377
342
  return Array.prototype.filter.call(targets, (el) => {
378
343
  return e.composedPath().indexOf(el) > -1;
379
344
  })[0];
345
+ } else if (this.listenOn && this.listenOn !== this && this.position) {
346
+ // If listenOn has been set on a different element than the context menu root, then use listenOn as the target.
347
+ return this.listenOn;
380
348
  }
381
349
  return e.target;
382
350
  }
@@ -386,6 +354,11 @@ export const ContextMenuMixin = (superClass) =>
386
354
  * @param {!Event | undefined} e used as the context for the menu. Overlay coordinates are taken from this event.
387
355
  */
388
356
  open(e) {
357
+ // Ignore events from the overlay
358
+ if (this._overlayElement && e.composedPath().includes(this._overlayElement)) {
359
+ return;
360
+ }
361
+
389
362
  if (e && !this.opened) {
390
363
  this._context = {
391
364
  detail: e.detail,
@@ -403,15 +376,75 @@ export const ContextMenuMixin = (superClass) =>
403
376
  this.__y = this._getEventCoordinate(e, 'y');
404
377
  this.__pageYOffset = window.pageYOffset;
405
378
 
406
- this._overlayElement.style.opacity = '0';
379
+ // Hide overlay until it is fully rendered and positioned
380
+ this._overlayElement.style.visibility = 'hidden';
407
381
  this._setOpened(true);
408
382
  }
409
383
  }
410
384
  }
411
385
 
386
+ /** @private */
387
+ __preserveMenuState() {
388
+ const listBox = this.__getListBox();
389
+ if (listBox) {
390
+ this.__focusedIndex = listBox.items.indexOf(listBox.focused);
391
+
392
+ if (this._subMenu && this._subMenu.opened) {
393
+ this.__subMenuIndex = listBox.items.indexOf(this._subMenu.listenOn);
394
+ }
395
+ }
396
+ }
397
+
398
+ /** @private */
399
+ __restoreMenuState() {
400
+ const focusedIndex = this.__focusedIndex;
401
+ const subMenuIndex = this.__subMenuIndex;
402
+ const selectedIndex = this.__selectedIndex;
403
+
404
+ const listBox = this.__getListBox();
405
+
406
+ if (listBox) {
407
+ // Initialize menu items synchronously
408
+ listBox._observer.flush();
409
+
410
+ if (subMenuIndex > -1) {
411
+ const itemToOpen = listBox.items[subMenuIndex];
412
+ if (itemToOpen) {
413
+ if (Array.isArray(itemToOpen._item.children) && itemToOpen._item.children.length) {
414
+ // Keep nested sub-menu opened and update it
415
+ this.__updateSubMenuForItem(this._subMenu, itemToOpen);
416
+ this._subMenu.requestContentUpdate();
417
+ } else {
418
+ // Item no longer has children, close sub-menu
419
+ this._subMenu.close();
420
+ this.__focusItem(itemToOpen);
421
+ }
422
+ } else {
423
+ // Item no longer exists, focus the list-box
424
+ listBox.focus();
425
+ }
426
+ }
427
+
428
+ // Focus previously selected item, in case it was clicked with
429
+ // `keepOpen` option, or the previously focused item, if any
430
+ this.__focusItem(selectedIndex > -1 ? listBox.children[selectedIndex] : listBox.items[focusedIndex]);
431
+ }
432
+
433
+ this.__focusedIndex = undefined;
434
+ this.__subMenuIndex = undefined;
435
+ this.__selectedIndex = undefined;
436
+ }
437
+
438
+ /** @private */
439
+ __focusItem(item) {
440
+ if (item) {
441
+ item.focus({ focusVisible: isKeyboardActive() });
442
+ }
443
+ }
444
+
412
445
  /** @private */
413
446
  __onScroll() {
414
- if (!this.opened) {
447
+ if (!this.opened || this.position) {
415
448
  return;
416
449
  }
417
450
 
@@ -600,7 +633,11 @@ export const ContextMenuMixin = (superClass) =>
600
633
  // Dispatch another contextmenu at the same coordinates after the overlay is closed
601
634
  this._overlayElement.addEventListener(
602
635
  'vaadin-overlay-closed',
603
- () => this.__contextMenuAt(e.clientX, e.clientY),
636
+ (closeEvent) => {
637
+ if (closeEvent.target === this._overlayElement) {
638
+ this.__contextMenuAt(e.clientX, e.clientY);
639
+ }
640
+ },
604
641
  {
605
642
  once: true,
606
643
  },
@@ -611,4 +648,10 @@ export const ContextMenuMixin = (superClass) =>
611
648
  this.close();
612
649
  }
613
650
  }
651
+
652
+ /**
653
+ * Fired when the context menu is closed.
654
+ *
655
+ * @event closed
656
+ */
614
657
  };
@@ -8,10 +8,10 @@ import { defineCustomElement } from '@vaadin/component-base/src/define.js';
8
8
  import { DirMixin } from '@vaadin/component-base/src/dir-mixin.js';
9
9
  import { PolylitMixin } from '@vaadin/component-base/src/polylit-mixin.js';
10
10
  import { OverlayMixin } from '@vaadin/overlay/src/vaadin-overlay-mixin.js';
11
- import { overlayStyles } from '@vaadin/overlay/src/vaadin-overlay-styles.js';
11
+ import { LumoInjectionMixin } from '@vaadin/vaadin-themable-mixin/lumo-injection-mixin.js';
12
12
  import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
13
+ import { contextMenuOverlayStyles } from './styles/vaadin-context-menu-overlay-base-styles.js';
13
14
  import { MenuOverlayMixin } from './vaadin-menu-overlay-mixin.js';
14
- import { styles } from './vaadin-menu-overlay-styles.js';
15
15
 
16
16
  /**
17
17
  * An element used internally by `<vaadin-context-menu>`. Not intended to be used separately.
@@ -25,14 +25,68 @@ import { styles } from './vaadin-menu-overlay-styles.js';
25
25
  * @protected
26
26
  */
27
27
  export class ContextMenuOverlay extends MenuOverlayMixin(
28
- OverlayMixin(DirMixin(ThemableMixin(PolylitMixin(LitElement)))),
28
+ OverlayMixin(DirMixin(ThemableMixin(PolylitMixin(LumoInjectionMixin(LitElement))))),
29
29
  ) {
30
30
  static get is() {
31
31
  return 'vaadin-context-menu-overlay';
32
32
  }
33
33
 
34
+ static get properties() {
35
+ return {
36
+ /**
37
+ * Position of the overlay with respect to the target.
38
+ * Supported values: null, `top-start`, `top`, `top-end`,
39
+ * `bottom-start`, `bottom`, `bottom-end`, `start-top`,
40
+ * `start`, `start-bottom`, `end-top`, `end`, `end-bottom`.
41
+ */
42
+ position: {
43
+ type: String,
44
+ reflectToAttribute: true,
45
+ },
46
+ };
47
+ }
48
+
34
49
  static get styles() {
35
- return [overlayStyles, styles];
50
+ return contextMenuOverlayStyles;
51
+ }
52
+
53
+ /**
54
+ * @protected
55
+ * @override
56
+ */
57
+ _updatePosition() {
58
+ super._updatePosition();
59
+
60
+ if (this.parentOverlay == null && this.positionTarget && this.position && this.opened) {
61
+ if (this.position === 'bottom' || this.position === 'top') {
62
+ const targetRect = this.positionTarget.getBoundingClientRect();
63
+ const overlayRect = this.$.overlay.getBoundingClientRect();
64
+
65
+ const offset = targetRect.width / 2 - overlayRect.width / 2;
66
+
67
+ if (this.style.left) {
68
+ const left = overlayRect.left + offset;
69
+ if (left > 0) {
70
+ this.style.left = `${left}px`;
71
+ }
72
+ }
73
+
74
+ if (this.style.right) {
75
+ const right = parseFloat(this.style.right) + offset;
76
+ if (right > 0) {
77
+ this.style.right = `${right}px`;
78
+ }
79
+ }
80
+ }
81
+
82
+ if (this.position === 'start' || this.position === 'end') {
83
+ const targetRect = this.positionTarget.getBoundingClientRect();
84
+ const overlayRect = this.$.overlay.getBoundingClientRect();
85
+
86
+ const offset = targetRect.height / 2 - overlayRect.height / 2;
87
+ this.style.top = `${overlayRect.top + offset}px`;
88
+ }
89
+ }
36
90
  }
37
91
 
38
92
  /** @protected */
@@ -42,6 +96,7 @@ export class ContextMenuOverlay extends MenuOverlayMixin(
42
96
  <div part="overlay" id="overlay" tabindex="0">
43
97
  <div part="content" id="content">
44
98
  <slot></slot>
99
+ <slot name="submenu"></slot>
45
100
  </div>
46
101
  </div>
47
102
  `;