Skip to content

Commit 972af5c

Browse files
authored
refactor: reflect overlay state attributes to owner (#9750)
* reflect position mixin state attributes * reflect animation state attributes * reflect modeless state attribute * reflect with-backdrop state attribute * update snapshot tests * refactor to setOverlayStateAttribute * update avatar group snapshots * update TS declaration
1 parent d4e4781 commit 972af5c

File tree

16 files changed

+254
-36
lines changed

16 files changed

+254
-36
lines changed

packages/avatar-group/test/dom/__snapshots__/avatar-group.test.snap.js

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ snapshots["vaadin-avatar-group default"] =
1111
slot="overflow"
1212
tabindex="0"
1313
>
14-
<vaadin-tooltip slot="tooltip">
14+
<vaadin-tooltip
15+
modeless=""
16+
slot="tooltip"
17+
>
1518
<div
1619
id="vaadin-tooltip-0"
1720
role="tooltip"
@@ -34,7 +37,10 @@ snapshots["vaadin-avatar-group items"] =
3437
tabindex="0"
3538
with-tooltip=""
3639
>
37-
<vaadin-tooltip slot="tooltip">
40+
<vaadin-tooltip
41+
modeless=""
42+
slot="tooltip"
43+
>
3844
<div
3945
id="vaadin-tooltip-2"
4046
role="tooltip"
@@ -53,7 +59,10 @@ snapshots["vaadin-avatar-group items"] =
5359
tabindex="0"
5460
with-tooltip=""
5561
>
56-
<vaadin-tooltip slot="tooltip">
62+
<vaadin-tooltip
63+
modeless=""
64+
slot="tooltip"
65+
>
5766
<div
5867
id="vaadin-tooltip-3"
5968
role="tooltip"
@@ -74,7 +83,10 @@ snapshots["vaadin-avatar-group items"] =
7483
slot="overflow"
7584
tabindex="0"
7685
>
77-
<vaadin-tooltip slot="tooltip">
86+
<vaadin-tooltip
87+
modeless=""
88+
slot="tooltip"
89+
>
7890
<div
7991
id="vaadin-tooltip-1"
8092
role="tooltip"
@@ -103,7 +115,10 @@ snapshots["vaadin-avatar-group theme"] =
103115
theme="small"
104116
with-tooltip=""
105117
>
106-
<vaadin-tooltip slot="tooltip">
118+
<vaadin-tooltip
119+
modeless=""
120+
slot="tooltip"
121+
>
107122
<div
108123
id="vaadin-tooltip-5"
109124
role="tooltip"
@@ -123,7 +138,10 @@ snapshots["vaadin-avatar-group theme"] =
123138
theme="small"
124139
with-tooltip=""
125140
>
126-
<vaadin-tooltip slot="tooltip">
141+
<vaadin-tooltip
142+
modeless=""
143+
slot="tooltip"
144+
>
127145
<div
128146
id="vaadin-tooltip-6"
129147
role="tooltip"
@@ -145,7 +163,10 @@ snapshots["vaadin-avatar-group theme"] =
145163
tabindex="0"
146164
theme="small"
147165
>
148-
<vaadin-tooltip slot="tooltip">
166+
<vaadin-tooltip
167+
modeless=""
168+
slot="tooltip"
169+
>
149170
<div
150171
id="vaadin-tooltip-4"
151172
role="tooltip"
@@ -164,6 +185,8 @@ snapshots["vaadin-avatar-group opened default"] =
164185
`<vaadin-avatar-group
165186
aria-label="Currently 4 active users"
166187
has-overflow=""
188+
start-aligned=""
189+
top-aligned=""
167190
>
168191
<vaadin-avatar
169192
abbr="AD"
@@ -174,7 +197,10 @@ snapshots["vaadin-avatar-group opened default"] =
174197
tabindex="0"
175198
with-tooltip=""
176199
>
177-
<vaadin-tooltip slot="tooltip">
200+
<vaadin-tooltip
201+
modeless=""
202+
slot="tooltip"
203+
>
178204
<div
179205
id="vaadin-tooltip-8"
180206
role="tooltip"
@@ -193,7 +219,10 @@ snapshots["vaadin-avatar-group opened default"] =
193219
tabindex="0"
194220
with-tooltip=""
195221
>
196-
<vaadin-tooltip slot="tooltip">
222+
<vaadin-tooltip
223+
modeless=""
224+
slot="tooltip"
225+
>
197226
<div
198227
id="vaadin-tooltip-9"
199228
role="tooltip"
@@ -214,7 +243,10 @@ snapshots["vaadin-avatar-group opened default"] =
214243
slot="overflow"
215244
tabindex="0"
216245
>
217-
<vaadin-tooltip slot="tooltip">
246+
<vaadin-tooltip
247+
modeless=""
248+
slot="tooltip"
249+
>
218250
<div
219251
id="vaadin-tooltip-7"
220252
role="tooltip"

packages/combo-box/test/dom/__snapshots__/combo-box.test.snap.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ snapshots["vaadin-combo-box host opened default"] =
319319
`<vaadin-combo-box
320320
focused=""
321321
opened=""
322+
start-aligned=""
323+
top-aligned=""
322324
>
323325
<label
324326
for="input-vaadin-combo-box-4"

packages/context-menu/test/dom/__snapshots__/context-menu.test.snap.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ snapshots["context-menu items"] =
5151
Menu Item 5
5252
</div>
5353
</vaadin-context-menu-list-box>
54-
<vaadin-context-menu hidden="">
54+
<vaadin-context-menu
55+
hidden=""
56+
modeless=""
57+
>
5558
</vaadin-context-menu>
5659
</vaadin-context-menu-overlay>
5760
`;
@@ -89,7 +92,12 @@ snapshots["context-menu items nested"] =
8992
Menu Item 2-2
9093
</vaadin-context-menu-item>
9194
</vaadin-context-menu-list-box>
92-
<vaadin-context-menu hidden="">
95+
<vaadin-context-menu
96+
hidden=""
97+
modeless=""
98+
start-aligned=""
99+
top-aligned=""
100+
>
93101
</vaadin-context-menu>
94102
</vaadin-context-menu-overlay>
95103
`;
@@ -148,7 +156,10 @@ snapshots["context-menu items overlay class"] =
148156
Menu Item 5
149157
</div>
150158
</vaadin-context-menu-list-box>
151-
<vaadin-context-menu hidden="">
159+
<vaadin-context-menu
160+
hidden=""
161+
modeless=""
162+
>
152163
</vaadin-context-menu>
153164
</vaadin-context-menu-overlay>
154165
`;
@@ -187,7 +198,12 @@ snapshots["context-menu items overlay class nested"] =
187198
Menu Item 2-2
188199
</vaadin-context-menu-item>
189200
</vaadin-context-menu-list-box>
190-
<vaadin-context-menu hidden="">
201+
<vaadin-context-menu
202+
hidden=""
203+
modeless=""
204+
start-aligned=""
205+
top-aligned=""
206+
>
191207
</vaadin-context-menu>
192208
</vaadin-context-menu-overlay>
193209
`;

packages/crud/test/dom/__snapshots__/crud.test.snap.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ snapshots["vaadin-crud shadow default"] =
231231
cancel-button-visible=""
232232
confirm-theme="primary"
233233
id="confirmCancel"
234+
with-backdrop=""
234235
>
235236
<h3 slot="header">
236237
Discard changes
@@ -273,6 +274,7 @@ snapshots["vaadin-crud shadow default"] =
273274
cancel-button-visible=""
274275
confirm-theme="primary error"
275276
id="confirmDelete"
277+
with-backdrop=""
276278
>
277279
<h3 slot="header">
278280
Delete item

packages/menu-bar/test/dom/__snapshots__/menu-bar.test.snap.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ snapshots["menu-bar overlay"] =
8989
Generate Report
9090
</vaadin-menu-bar-item>
9191
</vaadin-menu-bar-list-box>
92-
<vaadin-menu-bar-submenu hidden="">
92+
<vaadin-menu-bar-submenu
93+
hidden=""
94+
modeless=""
95+
>
9396
</vaadin-menu-bar-submenu>
9497
</vaadin-menu-bar-overlay>
9598
`;
@@ -124,7 +127,10 @@ snapshots["menu-bar overlay class"] =
124127
Generate Report
125128
</vaadin-menu-bar-item>
126129
</vaadin-menu-bar-list-box>
127-
<vaadin-menu-bar-submenu hidden="">
130+
<vaadin-menu-bar-submenu
131+
hidden=""
132+
modeless=""
133+
>
128134
</vaadin-menu-bar-submenu>
129135
</vaadin-menu-bar-overlay>
130136
`;

packages/overlay/src/vaadin-overlay-mixin.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import { isIOS } from '@vaadin/component-base/src/browser-utils.js';
77
import { OverlayFocusMixin } from './vaadin-overlay-focus-mixin.js';
88
import { OverlayStackMixin } from './vaadin-overlay-stack-mixin.js';
9+
import { setOverlayStateAttribute } from './vaadin-overlay-utils.js';
910

1011
/**
1112
* @polymerMixin
@@ -91,6 +92,7 @@ export const OverlayMixin = (superClass) =>
9192
type: Boolean,
9293
value: false,
9394
reflectToAttribute: true,
95+
observer: '_withBackdropChanged',
9496
sync: true,
9597
},
9698
};
@@ -279,6 +281,12 @@ export const OverlayMixin = (superClass) =>
279281
this._removeGlobalListeners();
280282
this._exitModalState();
281283
}
284+
setOverlayStateAttribute(this, 'modeless', modeless);
285+
}
286+
287+
/** @private */
288+
_withBackdropChanged(withBackdrop) {
289+
setOverlayStateAttribute(this, 'with-backdrop', withBackdrop);
282290
}
283291

284292
/** @private */
@@ -378,7 +386,7 @@ export const OverlayMixin = (superClass) =>
378386
if (!this.modeless) {
379387
this._enterModalState();
380388
}
381-
this.setAttribute('opening', '');
389+
setOverlayStateAttribute(this, 'opening', true);
382390

383391
if (this._shouldAnimate()) {
384392
this._enqueueAnimation('opening', () => {
@@ -398,15 +406,15 @@ export const OverlayMixin = (superClass) =>
398406

399407
/** @private */
400408
_finishOpening() {
401-
this.removeAttribute('opening');
409+
setOverlayStateAttribute(this, 'opening', false);
402410
}
403411

404412
/** @private */
405413
_finishClosing() {
406414
this._detachOverlay();
407415
this._removeAttachedInstance();
408416
this.$.overlay.style.removeProperty('pointer-events');
409-
this.removeAttribute('closing');
417+
setOverlayStateAttribute(this, 'closing', false);
410418
this.dispatchEvent(new CustomEvent('vaadin-overlay-closed'));
411419
}
412420

@@ -417,7 +425,7 @@ export const OverlayMixin = (superClass) =>
417425
}
418426
if (this._isAttached) {
419427
this._exitModalState();
420-
this.setAttribute('closing', '');
428+
setOverlayStateAttribute(this, 'closing', true);
421429
this.dispatchEvent(new CustomEvent('vaadin-overlay-closing'));
422430

423431
if (this._shouldAnimate()) {

packages/overlay/src/vaadin-overlay-position-mixin.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
66
import { getAncestorRootNodes } from '@vaadin/component-base/src/dom-utils.js';
7-
import { observeMove } from './vaadin-overlay-utils.js';
7+
import { observeMove, setOverlayStateAttribute } from './vaadin-overlay-utils.js';
88

99
const PROP_NAMES_VERTICAL = {
1010
start: 'top',
@@ -271,11 +271,11 @@ export const PositionMixin = (superClass) =>
271271
// Apply the positioning properties to the overlay
272272
Object.assign(this.style, verticalProps, horizontalProps);
273273

274-
this.toggleAttribute('bottom-aligned', !shouldAlignStartVertically);
275-
this.toggleAttribute('top-aligned', shouldAlignStartVertically);
274+
setOverlayStateAttribute(this, 'bottom-aligned', !shouldAlignStartVertically);
275+
setOverlayStateAttribute(this, 'top-aligned', shouldAlignStartVertically);
276276

277-
this.toggleAttribute('end-aligned', !flexStart);
278-
this.toggleAttribute('start-aligned', flexStart);
277+
setOverlayStateAttribute(this, 'end-aligned', !flexStart);
278+
setOverlayStateAttribute(this, 'start-aligned', flexStart);
279279
}
280280

281281
__shouldAlignStartHorizontally(targetRect, rtl) {

packages/overlay/src/vaadin-overlay-utils.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@
1111
* https://github.com/floating-ui/floating-ui/blob/58ed169/packages/dom/src/autoUpdate.ts#L45
1212
*/
1313
export function observeMove(element: HTMLElement, callback: () => void): () => void;
14+
15+
/**
16+
* Toggle the state attribute on the overlay element and also its owner element. This allows targeting state attributes
17+
* in the light DOM in case the overlay is in the shadow DOM of its owner.
18+
*/
19+
export function setOverlayStateAttribute(overlay: HTMLElement, name: string, value: string | boolean): void;

packages/overlay/src/vaadin-overlay-utils.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,32 @@ export function observeMove(element, callback) {
8787

8888
return cleanup;
8989
}
90+
91+
/**
92+
* Toggle the state attribute on the overlay element and also its owner element. This allows targeting state attributes
93+
* in the light DOM in case the overlay is in the shadow DOM of its owner.
94+
* @param {HTMLElement} overlay The overlay element on which to toggle the attribute.
95+
* @param {string} name The name of the attribute to toggle.
96+
* @param {string|boolean} value The value of the attribute. If a string is provided, it will be set as the attribute
97+
* value. Otherwise, the attribute will be added or removed depending on whether `value` is truthy or falsy.
98+
*/
99+
export function setOverlayStateAttribute(overlay, name, value) {
100+
const elements = [overlay];
101+
if (overlay.owner) {
102+
elements.push(overlay.owner);
103+
}
104+
105+
if (typeof value === 'string') {
106+
elements.forEach((element) => {
107+
element.setAttribute(name, value);
108+
});
109+
} else if (value) {
110+
elements.forEach((element) => {
111+
element.setAttribute(name, '');
112+
});
113+
} else {
114+
elements.forEach((element) => {
115+
element.removeAttribute(name);
116+
});
117+
}
118+
}

0 commit comments

Comments
 (0)