Skip to content

Commit d1971e5

Browse files
authored
refactor!: extract shared combo-box overlay logic to mixin (#5393)
1 parent 38385f6 commit d1971e5

22 files changed

Lines changed: 237 additions & 74 deletions
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2015 - 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 { PositionMixinClass } from '@vaadin/overlay/src/vaadin-overlay-position-mixin.js';
8+
9+
export declare function ComboBoxOverlayMixin<T extends Constructor<HTMLElement>>(
10+
base: T,
11+
): Constructor<PositionMixinClass> & T;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2015 - 2023 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
import { PositionMixin } from '@vaadin/overlay/src/vaadin-overlay-position-mixin.js';
7+
8+
/**
9+
* @polymerMixin
10+
* @mixes PositionMixin
11+
*/
12+
export const ComboBoxOverlayMixin = (superClass) =>
13+
class ComboBoxOverlayMixin extends PositionMixin(superClass) {
14+
static get observers() {
15+
return ['_setOverlayWidth(positionTarget, opened)'];
16+
}
17+
18+
/** @protected */
19+
connectedCallback() {
20+
super.connectedCallback();
21+
22+
const comboBox = this._comboBox;
23+
24+
const hostDir = comboBox && comboBox.getAttribute('dir');
25+
if (hostDir) {
26+
this.setAttribute('dir', hostDir);
27+
}
28+
}
29+
30+
/**
31+
* Override method inherited from `Overlay`
32+
* to not close on position target click.
33+
*
34+
* @param {Event} event
35+
* @return {boolean}
36+
* @protected
37+
*/
38+
_shouldCloseOnOutsideClick(event) {
39+
const eventPath = event.composedPath();
40+
return !eventPath.includes(this.positionTarget) && !eventPath.includes(this);
41+
}
42+
43+
/** @private */
44+
_setOverlayWidth(positionTarget, opened) {
45+
if (positionTarget && opened) {
46+
const propPrefix = this.localName;
47+
this.style.setProperty(`--_${propPrefix}-default-width`, `${positionTarget.clientWidth}px`);
48+
49+
const customWidth = getComputedStyle(this._comboBox).getPropertyValue(`--${propPrefix}-width`);
50+
51+
if (customWidth === '') {
52+
this.style.removeProperty(`--${propPrefix}-width`);
53+
} else {
54+
this.style.setProperty(`--${propPrefix}-width`, customWidth);
55+
}
56+
57+
this._updatePosition();
58+
}
59+
}
60+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2015 - 2023 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
import { Overlay } from '@vaadin/overlay/src/vaadin-overlay.js';
7+
import { ComboBoxOverlayMixin } from './vaadin-combo-box-overlay-mixin.js';
8+
9+
/**
10+
* An element used internally by `<vaadin-combo-box>`. Not intended to be used separately.
11+
*/
12+
declare class ComboBoxOverlay extends ComboBoxOverlayMixin(Overlay) {}
13+
14+
declare global {
15+
interface HTMLElementTagNameMap {
16+
'vaadin-combo-box-overlay': ComboBoxOverlay;
17+
}
18+
}
19+
20+
export { ComboBoxOverlay };

‎packages/combo-box/src/vaadin-combo-box-overlay.js‎

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
66
import { Overlay } from '@vaadin/overlay/src/vaadin-overlay.js';
7-
import { PositionMixin } from '@vaadin/overlay/src/vaadin-overlay-position-mixin.js';
87
import { css, registerStyles } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
8+
import { ComboBoxOverlayMixin } from './vaadin-combo-box-overlay-mixin.js';
99

1010
registerStyles(
1111
'vaadin-combo-box-overlay',
@@ -29,73 +29,28 @@ let memoizedTemplate;
2929
* An element used internally by `<vaadin-combo-box>`. Not intended to be used separately.
3030
*
3131
* @extends Overlay
32+
* @mixes ComboBoxOverlayMixin
3233
* @private
3334
*/
34-
export class ComboBoxOverlay extends PositionMixin(Overlay) {
35+
export class ComboBoxOverlay extends ComboBoxOverlayMixin(Overlay) {
3536
static get is() {
3637
return 'vaadin-combo-box-overlay';
3738
}
3839

3940
static get template() {
4041
if (!memoizedTemplate) {
4142
memoizedTemplate = super.template.cloneNode(true);
42-
memoizedTemplate.content.querySelector('[part~="overlay"]').removeAttribute('tabindex');
43-
}
44-
45-
return memoizedTemplate;
46-
}
4743

48-
static get observers() {
49-
return ['_setOverlayWidth(positionTarget, opened)'];
50-
}
51-
52-
connectedCallback() {
53-
super.connectedCallback();
44+
const overlay = memoizedTemplate.content.querySelector('[part~="overlay"]');
45+
overlay.removeAttribute('tabindex');
5446

55-
const comboBox = this._comboBox;
47+
const loader = document.createElement('div');
48+
loader.setAttribute('part', 'loader');
5649

57-
const hostDir = comboBox && comboBox.getAttribute('dir');
58-
if (hostDir) {
59-
this.setAttribute('dir', hostDir);
50+
overlay.insertBefore(loader, overlay.firstElementChild);
6051
}
61-
}
62-
63-
ready() {
64-
super.ready();
65-
const loader = document.createElement('div');
66-
loader.setAttribute('part', 'loader');
67-
const content = this.shadowRoot.querySelector('[part~="content"]');
68-
content.parentNode.insertBefore(loader, content);
69-
}
70-
71-
/**
72-
* Override method inherited from `Overlay`
73-
* to not close on position target click.
74-
*
75-
* @param {Event} _event
76-
* @return {boolean}
77-
* @protected
78-
*/
79-
_shouldCloseOnOutsideClick(event) {
80-
const eventPath = event.composedPath();
81-
return !eventPath.includes(this.positionTarget) && !eventPath.includes(this);
82-
}
83-
84-
_setOverlayWidth(positionTarget, opened) {
85-
if (positionTarget && opened) {
86-
const propPrefix = this.localName;
87-
this.style.setProperty(`--_${propPrefix}-default-width`, `${positionTarget.clientWidth}px`);
8852

89-
const customWidth = getComputedStyle(this._comboBox).getPropertyValue(`--${propPrefix}-width`);
90-
91-
if (customWidth === '') {
92-
this.style.removeProperty(`--${propPrefix}-width`);
93-
} else {
94-
this.style.setProperty(`--${propPrefix}-width`, customWidth);
95-
}
96-
97-
this._updatePosition();
98-
}
53+
return memoizedTemplate;
9954
}
10055
}
10156

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import './vaadin-combo-box-dropdown-styles.js';
21
import './vaadin-combo-box-item-styles.js';
2+
import './vaadin-combo-box-overlay-styles.js';
33
import '../../src/vaadin-combo-box-light.js';

packages/combo-box/theme/lumo/vaadin-combo-box-dropdown-styles.js renamed to packages/combo-box/theme/lumo/vaadin-combo-box-overlay-styles.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ const comboBoxOverlay = css`
1818
--_vaadin-combo-box-items-container-border-color: transparent;
1919
}
2020
21-
/* Loading state */
22-
2321
/* When items are empty, the spinner needs some room */
2422
:host(:not([closing])) [part~='content'] {
2523
min-height: calc(2 * var(--lumo-space-s) + var(--lumo-icon-size-s));
@@ -36,7 +34,9 @@ const comboBoxOverlay = css`
3634
:host([bottom-aligned]) [part~='overlay'] {
3735
margin-bottom: var(--lumo-space-xs);
3836
}
37+
`;
3938

39+
const comboBoxLoader = css`
4040
[part~='loader'] {
4141
position: absolute;
4242
z-index: 1;
@@ -48,8 +48,6 @@ const comboBoxOverlay = css`
4848
margin-inline-end: 0;
4949
}
5050
51-
/* RTL specific styles */
52-
5351
:host([dir='rtl']) [part~='loader'] {
5452
left: auto;
5553
margin-left: 0;
@@ -59,6 +57,8 @@ const comboBoxOverlay = css`
5957
}
6058
`;
6159

62-
registerStyles('vaadin-combo-box-overlay', [overlay, menuOverlayCore, comboBoxOverlay, loader], {
60+
registerStyles('vaadin-combo-box-overlay', [overlay, menuOverlayCore, comboBoxOverlay, loader, comboBoxLoader], {
6361
moduleId: 'lumo-combo-box-overlay',
6462
});
63+
64+
export { comboBoxLoader, comboBoxOverlay };
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import '@vaadin/input-container/theme/lumo/vaadin-input-container.js';
2-
import './vaadin-combo-box-dropdown-styles.js';
32
import './vaadin-combo-box-item-styles.js';
3+
import './vaadin-combo-box-overlay-styles.js';
44
import './vaadin-combo-box-styles.js';
55
import '../../src/vaadin-combo-box.js';
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import './vaadin-combo-box-dropdown-styles.js';
21
import './vaadin-combo-box-item-styles.js';
2+
import './vaadin-combo-box-overlay-styles.js';
33
import '../../src/vaadin-combo-box-light.js';

packages/combo-box/theme/material/vaadin-combo-box-dropdown-styles.js renamed to packages/combo-box/theme/material/vaadin-combo-box-overlay-styles.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ const comboBoxOverlay = css`
2121
[part='content'] {
2222
padding: 0;
2323
}
24+
`;
2425

26+
const comboBoxLoader = css`
2527
[part~='loader'] {
2628
position: absolute;
2729
z-index: 1;
@@ -31,6 +33,8 @@ const comboBoxOverlay = css`
3133
}
3234
`;
3335

34-
registerStyles('vaadin-combo-box-overlay', [menuOverlay, comboBoxOverlay, loader], {
36+
registerStyles('vaadin-combo-box-overlay', [menuOverlay, comboBoxOverlay, loader, comboBoxLoader], {
3537
moduleId: 'material-combo-box-overlay',
3638
});
39+
40+
export { comboBoxLoader, comboBoxOverlay };
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import '@vaadin/input-container/theme/material/vaadin-input-container.js';
2-
import './vaadin-combo-box-dropdown-styles.js';
32
import './vaadin-combo-box-item-styles.js';
3+
import './vaadin-combo-box-overlay-styles.js';
44
import './vaadin-combo-box-styles.js';
55
import '../../src/vaadin-combo-box.js';

0 commit comments

Comments
 (0)