Skip to content

Commit 14ae3f8

Browse files
sissbrueckerjouni
andauthored
refactor: apply content styles on part instead of slotted element (#11458)
## Description Updates Aura App Layout theme to apply content styles on the content part instead of slotted content elements: - Introduce a content part, update style rules to use it - Update Aura theme to style the content part instead of the slotted element - Modify the content offset mechanism so that the offset of the content from drawer / navbar is applied via padding on the host element - The previous approach used a padding on the content part, resulting in the content part stretching below the drawer, which does not give the correct visual result when applying a background / border on the content - Using a margin on the content part works as an alternative, however that makes customizing the margin of the content part more complex for users, as they would need to write CSS rules that account for the drawer offset Fixes #11431 ## Type of change - Refactor --------- Co-authored-by: Jouni Koivuviita <jouni@vaadin.com>
1 parent 1d0c03d commit 14ae3f8

8 files changed

Lines changed: 28 additions & 38 deletions

File tree

packages/app-layout/src/styles/vaadin-app-layout-base-styles.js

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@ export const appLayoutStyles = css`
1717
--vaadin-app-layout-touch-optimized: false;
1818
--vaadin-app-layout-navbar-offset-top: var(--_vaadin-app-layout-navbar-offset-size);
1919
--vaadin-app-layout-navbar-offset-bottom: var(--_vaadin-app-layout-navbar-offset-size-bottom);
20+
--vaadin-app-layout-drawer-offset-left: 0px;
2021
padding-top: max(var(--vaadin-app-layout-navbar-offset-top), var(--safe-area-inset-top));
2122
padding-bottom: max(var(--vaadin-app-layout-navbar-offset-bottom), var(--safe-area-inset-bottom));
2223
}
2324
24-
:host(:dir(ltr)) [content] {
25+
:host(:dir(ltr)) {
2526
padding-left: max(var(--vaadin-app-layout-drawer-offset-left), var(--safe-area-inset-left));
2627
padding-right: var(--safe-area-inset-right);
2728
}
2829
29-
:host(:dir(rtl)) [content] {
30+
:host(:dir(rtl)) {
3031
padding-left: var(--safe-area-inset-left);
3132
padding-right: max(var(--vaadin-app-layout-drawer-offset-left), var(--safe-area-inset-right));
3233
}
@@ -50,11 +51,11 @@ export const appLayoutStyles = css`
5051
--vaadin-app-layout-drawer-offset-left: 0px;
5152
}
5253
53-
:host(:not([no-scroll])) [content] {
54+
:host(:not([no-scroll])) [part~='content'] {
5455
overflow: auto;
5556
}
5657
57-
[content] {
58+
[part~='content'] {
5859
height: 100%;
5960
transition: inherit;
6061
}
@@ -137,21 +138,10 @@ export const appLayoutStyles = css`
137138
padding-right: var(--safe-area-inset-right);
138139
}
139140
140-
:host([has-navbar]:not([overlay])) [part='drawer'],
141-
:host([has-navbar]) [content] {
141+
:host([has-navbar]:not([overlay])) [part='drawer'] {
142142
--safe-area-inset-top: 0px;
143143
}
144144
145-
:host([has-drawer]:not([overlay])[drawer-opened]) [content] {
146-
&:dir(ltr) {
147-
--safe-area-inset-left: 0px;
148-
}
149-
150-
&:dir(rtl) {
151-
--safe-area-inset-right: 0px;
152-
}
153-
}
154-
155145
:host([drawer-opened]) [part='drawer'] {
156146
/* The drawer should be accessible by the tabbing navigation when it is opened. */
157147
visibility: visible;
@@ -209,6 +199,7 @@ export const appLayoutStyles = css`
209199
}
210200
211201
/* If a vaadin-scroller is used in the drawer, allow it to take all remaining space and contain scrolling */
202+
212203
[part='drawer'] ::slotted(vaadin-scroller) {
213204
flex: 1;
214205
overscroll-behavior: contain;

packages/app-layout/src/vaadin-app-layout.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export type AppLayoutEventMap = AppLayoutCustomEventMap & HTMLElementEventMap;
7878
* `navbar-top` | Container for the top navigation bar
7979
* `navbar-bottom` | Container for the bottom navigation bar
8080
* `drawer` | Container for the drawer area
81+
* `content` | Container for the content area
8182
*
8283
* The following state attributes are available for styling:
8384
*
@@ -140,8 +141,8 @@ export type AppLayoutEventMap = AppLayoutCustomEventMap & HTMLElementEventMap;
140141
*
141142
* To use the "content scrolling", in case of the content of the page relies on a pre-defined height (for instance,
142143
* it has a `height:100%`), then the developer can set `height: 100%` to both `html` and `body`.
143-
* That will make the `[content]` element of app layout scrollable.
144-
* On this case, the toolbars on mobile device won't collapse.
144+
* That makes the content part of App Layout scrollable.
145+
* In that case, the toolbars on mobile device won't collapse.
145146
*
146147
* @fires {CustomEvent} drawer-opened-changed - Fired when the `drawerOpened` property changes.
147148
* @fires {CustomEvent} overlay-changed - Fired when the `overlay` property changes.

packages/app-layout/src/vaadin-app-layout.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import { AppLayoutMixin } from './vaadin-app-layout-mixin.js';
5757
* `navbar-top` | Container for the top navigation bar
5858
* `navbar-bottom` | Container for the bottom navigation bar
5959
* `drawer` | Container for the drawer area
60+
* `content` | Container for the content area
6061
*
6162
* The following state attributes are available for styling:
6263
*
@@ -119,8 +120,8 @@ import { AppLayoutMixin } from './vaadin-app-layout-mixin.js';
119120
*
120121
* To use the "content scrolling", in case of the content of the page relies on a pre-defined height (for instance,
121122
* it has a `height:100%`), then the developer can set `height: 100%` to both `html` and `body`.
122-
* That will make the `[content]` element of app layout scrollable.
123-
* On this case, the toolbars on mobile device won't collapse.
123+
* That makes the content part of App Layout scrollable.
124+
* In that case, the toolbars on mobile device won't collapse.
124125
*
125126
* @fires {CustomEvent} drawer-opened-changed - Fired when the `drawerOpened` property changes.
126127
* @fires {CustomEvent} overlay-changed - Fired when the `overlay` property changes.
@@ -151,7 +152,7 @@ class AppLayout extends AppLayoutMixin(ElementMixin(ThemableMixin(PolylitMixin(L
151152
<div part="drawer" id="drawer">
152153
<slot name="drawer" id="drawerSlot" @slotchange="${this.__onDrawerSlotChange}"></slot>
153154
</div>
154-
<div content>
155+
<div part="content">
155156
<slot></slot>
156157
</div>
157158
<div part="navbar navbar-bottom" id="navbarBottom" hidden>

packages/app-layout/test/app-layout.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,13 @@ describe('vaadin-app-layout', () => {
311311
drawerContent.style.width = '200px';
312312
await nextResize(layout);
313313
await nextRender();
314-
const content = layout.shadowRoot.querySelector('[content]');
315-
const initialOffset = parseInt(getComputedStyle(content).getPropertyValue('padding-left'));
314+
const initialOffset = parseInt(getComputedStyle(layout).getPropertyValue('padding-left'));
316315
expect(initialOffset).to.equal(200);
317316
// Decrease drawer content size and measure decrease
318317
drawerContent.style.width = '100px';
319318
await nextResize(layout);
320319
await nextRender();
321-
const updatedOffset = parseInt(getComputedStyle(content).getPropertyValue('padding-left'));
320+
const updatedOffset = parseInt(getComputedStyle(layout).getPropertyValue('padding-left'));
322321
expect(updatedOffset).to.equal(100);
323322
});
324323
});

packages/app-layout/test/dom/__snapshots__/app-layout.test.snap.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ snapshots["vaadin-app-layout shadow desktop layout default"] =
6060
>
6161
</slot>
6262
</div>
63-
<div content="">
63+
<div part="content">
6464
<slot>
6565
</slot>
6666
</div>
@@ -102,7 +102,7 @@ snapshots["vaadin-app-layout shadow desktop layout drawer closed"] =
102102
>
103103
</slot>
104104
</div>
105-
<div content="">
105+
<div part="content">
106106
<slot>
107107
</slot>
108108
</div>
@@ -147,7 +147,7 @@ snapshots["vaadin-app-layout shadow mobile layout default"] =
147147
>
148148
</slot>
149149
</div>
150-
<div content="">
150+
<div part="content">
151151
<slot>
152152
</slot>
153153
</div>
@@ -192,7 +192,7 @@ snapshots["vaadin-app-layout shadow mobile layout drawer opened"] =
192192
>
193193
</slot>
194194
</div>
195-
<div content="">
195+
<div part="content">
196196
<slot>
197197
</slot>
198198
</div>

packages/aura/src/color.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
:where(:root),
5858
:where(:host),
59-
vaadin-app-layout > :not([slot]),
59+
vaadin-app-layout::part(content),
6060
vaadin-avatar,
6161
vaadin-badge,
6262
vaadin-button,

packages/aura/src/components/app-layout.css

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ vaadin-app-layout > vaadin-scroller[slot='drawer'] {
5757
scrollbar-width: thin;
5858
}
5959

60-
vaadin-app-layout > :nth-child(1 of :not([slot])):nth-last-child(1 of :not([slot])) {
60+
vaadin-app-layout::part(content) {
6161
color-scheme: var(--aura-content-color-scheme);
6262
color: var(--vaadin-text-color);
6363
background: linear-gradient(var(--aura-surface-color), var(--aura-surface-color)), var(--aura-app-background);
6464
background-clip: padding-box;
6565
background-origin: border-box;
66-
min-height: 100%;
66+
min-height: 1lh;
6767
box-sizing: border-box;
6868
border-radius: var(--_app-layout-radius);
6969
border: var(--aura-app-layout-border-width) solid var(--vaadin-border-color-secondary);
@@ -108,17 +108,15 @@ vaadin-app-layout > vaadin-master-detail-layout:nth-child(1 of :not([slot])):nth
108108
}
109109
}
110110

111-
vaadin-app-layout[has-drawer][drawer-opened]:not([overlay])
112-
> :nth-child(1 of :not([slot])):nth-last-child(1 of :not([slot])) {
111+
vaadin-app-layout[has-drawer][drawer-opened]:not([overlay])::part(content) {
113112
border-inline-start-width: var(--aura-app-layout-border-width);
114113
}
115114

116-
vaadin-app-layout[has-navbar][has-drawer][drawer-opened]:not([overlay])
117-
> :nth-child(1 of :not([slot])):nth-last-child(1 of :not([slot])) {
115+
vaadin-app-layout[has-navbar][has-drawer][drawer-opened]:not([overlay])::part(content) {
118116
border-start-start-radius: var(--aura-app-layout-radius);
119117
}
120118

121-
vaadin-app-layout[has-navbar] > :nth-child(1 of :not([slot])):nth-last-child(1 of :not([slot])) {
119+
vaadin-app-layout[has-navbar]::part(content) {
122120
border-top-width: var(--aura-app-layout-border-width);
123121
}
124122

packages/vaadin-lumo-styles/src/components/app-layout.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@
3636
--vaadin-app-layout-navbar-offset-left: 0;
3737
}
3838

39-
:host(:not([no-scroll])) [content] {
39+
:host(:not([no-scroll])) [part~='content'] {
4040
overflow: auto;
4141
}
4242

43-
[content] {
43+
[part~='content'] {
4444
height: 100%;
4545
}
4646

0 commit comments

Comments
 (0)