Skip to content

Commit 560878a

Browse files
authoredSep 12, 2024··
fix(cdk/overlay): load structural styles in a cascade layer (#29725)
Recently we switched to loading the styles of the overlay dynamically which can make them more specific than pre-existing themes. These changes use a cascade layer in order to reduce their specificity.
1 parent b8ec0a6 commit 560878a

File tree

4 files changed

+61
-14
lines changed

4 files changed

+61
-14
lines changed
 

‎src/cdk/overlay/BUILD.bazel

+9-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ ng_module(
1919
exclude = ["**/*.spec.ts"],
2020
),
2121
assets = [
22-
":overlay-prebuilt.css",
22+
":overlay-structure.css",
2323
],
2424
deps = [
2525
"//src:dev_mode_types",
@@ -46,7 +46,14 @@ sass_binary(
4646
src = "overlay-prebuilt.scss",
4747
deps = [
4848
":overlay_scss_lib",
49-
"//src/cdk/a11y:a11y_scss_lib",
49+
],
50+
)
51+
52+
sass_binary(
53+
name = "overlay_structure_scss",
54+
src = "overlay-structure.scss",
55+
deps = [
56+
":overlay_scss_lib",
5057
],
5158
)
5259

‎src/cdk/overlay/_index.scss

+44-11
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,20 @@ $overlay-backdrop-color: rgba(0, 0, 0, 0.32) !default;
1212
$backdrop-animation-duration: 400ms !default;
1313
$backdrop-animation-timing-function: cubic-bezier(0.25, 0.8, 0.25, 1) !default;
1414

15-
/// Emits structural styles required for cdk/overlay to function.
16-
@mixin overlay() {
15+
// Conditionally wraps some styles in a layer depending on a flag.
16+
@mixin _conditional-layer($should-wrap) {
17+
@if ($should-wrap) {
18+
@layer cdk-overlay {
19+
@content;
20+
}
21+
} @else {
22+
@content;
23+
}
24+
}
25+
26+
// Structural styles for the overlay. Pass `$wrap-customizable-styles` to emit
27+
// the styles that support customization in a way that makes them easier to change.
28+
@mixin private-overlay-structure($wrap-customizable-styles) {
1729
.cdk-overlay-container, .cdk-global-overlay-wrapper {
1830
// Disable events from being captured on the overlay container.
1931
pointer-events: none;
@@ -28,7 +40,10 @@ $backdrop-animation-timing-function: cubic-bezier(0.25, 0.8, 0.25, 1) !default;
2840
// The overlay-container is an invisible element which contains all individual overlays.
2941
.cdk-overlay-container {
3042
position: fixed;
31-
z-index: $overlay-container-z-index;
43+
44+
@include _conditional-layer($wrap-customizable-styles) {
45+
z-index: $overlay-container-z-index;
46+
}
3247

3348
&:empty {
3449
// Hide the element when it doesn't have any child nodes. This doesn't
@@ -44,7 +59,10 @@ $backdrop-animation-timing-function: cubic-bezier(0.25, 0.8, 0.25, 1) !default;
4459
.cdk-global-overlay-wrapper {
4560
display: flex;
4661
position: absolute;
47-
z-index: $overlay-z-index;
62+
63+
@include _conditional-layer($wrap-customizable-styles) {
64+
z-index: $overlay-z-index;
65+
}
4866
}
4967

5068
// A single overlay pane.
@@ -54,13 +72,16 @@ $backdrop-animation-timing-function: cubic-bezier(0.25, 0.8, 0.25, 1) !default;
5472
position: absolute;
5573
pointer-events: auto;
5674
box-sizing: border-box;
57-
z-index: $overlay-z-index;
5875

5976
// For connected-position overlays, we set `display: flex` in
6077
// order to force `max-width` and `max-height` to take effect.
6178
display: flex;
6279
max-width: 100%;
6380
max-height: 100%;
81+
82+
@include _conditional-layer($wrap-customizable-styles) {
83+
z-index: $overlay-z-index;
84+
}
6485
}
6586

6687
.cdk-overlay-backdrop {
@@ -71,11 +92,14 @@ $backdrop-animation-timing-function: cubic-bezier(0.25, 0.8, 0.25, 1) !default;
7192
left: 0;
7293
right: 0;
7394

74-
z-index: $overlay-backdrop-z-index;
7595
pointer-events: auto;
7696
-webkit-tap-highlight-color: transparent;
77-
transition: opacity $backdrop-animation-duration $backdrop-animation-timing-function;
7897
opacity: 0;
98+
99+
@include _conditional-layer($wrap-customizable-styles) {
100+
z-index: $overlay-backdrop-z-index;
101+
transition: opacity $backdrop-animation-duration $backdrop-animation-timing-function;
102+
}
79103
}
80104

81105
.cdk-overlay-backdrop-showing {
@@ -84,16 +108,17 @@ $backdrop-animation-timing-function: cubic-bezier(0.25, 0.8, 0.25, 1) !default;
84108
// Note that we can't import and use the `high-contrast` mixin from `_a11y.scss`, because
85109
// this file will be copied to the top-level `cdk` package when putting together the files
86110
// for npm. Any relative import paths we use here will become invalid once the file is copied.
87-
.cdk-high-contrast-active & {
111+
@media (forced-colors: active) {
88112
// In high contrast mode the rgba background will become solid
89113
// so we need to fall back to making it opaque using `opacity`.
90114
opacity: 0.6;
91115
}
92116
}
93117

94118
.cdk-overlay-dark-backdrop {
95-
// Add a CSS variable to make this easier to override.
96-
background: var(--cdk-overlay-backdrop-dark-color, $overlay-backdrop-color);
119+
@include _conditional-layer($wrap-customizable-styles) {
120+
background: $overlay-backdrop-color;
121+
}
97122
}
98123

99124
.cdk-overlay-transparent-backdrop {
@@ -121,7 +146,6 @@ $backdrop-animation-timing-function: cubic-bezier(0.25, 0.8, 0.25, 1) !default;
121146
// overlay element's size to fit within the viewport.
122147
.cdk-overlay-connected-position-bounding-box {
123148
position: absolute;
124-
z-index: $overlay-z-index;
125149

126150
// We use `display: flex` on this element exclusively for centering connected overlays.
127151
// When *not* centering, a top/left/bottom/right will be set which overrides the normal
@@ -135,6 +159,10 @@ $backdrop-animation-timing-function: cubic-bezier(0.25, 0.8, 0.25, 1) !default;
135159
// Add some dimensions so the element has an `innerText` which some people depend on in tests.
136160
min-width: 1px;
137161
min-height: 1px;
162+
163+
@include _conditional-layer($wrap-customizable-styles) {
164+
z-index: $overlay-z-index;
165+
}
138166
}
139167

140168
// Used when disabling global scrolling.
@@ -152,3 +180,8 @@ $backdrop-animation-timing-function: cubic-bezier(0.25, 0.8, 0.25, 1) !default;
152180
overflow-y: scroll;
153181
}
154182
}
183+
184+
/// Emits structural styles required for cdk/overlay to function.
185+
@mixin overlay {
186+
@include private-overlay-structure(false);
187+
}

‎src/cdk/overlay/overlay-container.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {Platform, _isTestEnvironment} from '@angular/cdk/platform';
2323
changeDetection: ChangeDetectionStrategy.OnPush,
2424
encapsulation: ViewEncapsulation.None,
2525
standalone: true,
26-
styleUrl: 'overlay-prebuilt.css',
26+
styleUrl: 'overlay-structure.css',
2727
host: {'cdk-overlay-style-loader': ''},
2828
})
2929
export class _CdkOverlayStyleLoader {}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@use './index' as overlay;
2+
3+
// We don't emit the layer internally, because all the breaking changes
4+
// have been resolved already and the `@layer` seems to break some targets.
5+
$_is-external-build: true;
6+
7+
@include overlay.private-overlay-structure($_is-external-build);

0 commit comments

Comments
 (0)
Please sign in to comment.