Skip to content

Commit 43406b3

Browse files
crisbetojelbourn
authored andcommitted
refactor(option): move styles out of mat-core and into styleUrls (#9115)
Moves the styling for `MatOption` and `MatOptgroup` out of the `mat-core` mixin and into their components' respective `styleUrls`. This avoids the styles potentially being included multiple times if `mat-core` is used in different places and is consistent with all other components.
1 parent 53d48f6 commit 43406b3

File tree

8 files changed

+102
-87
lines changed

8 files changed

+102
-87
lines changed

src/lib/core/BUILD.bazel

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ng_module(
77
name = "core",
88
srcs = glob(["**/*.ts"], exclude=["**/*.spec.ts"]),
99
module_name = "@angular/material/core",
10-
assets = [":pseudo_checkbox_css"],
10+
assets = [":pseudo_checkbox_css", ":option_css", ":optgroup_css"],
1111
deps = [
1212
"//src/cdk/bidi",
1313
"//src/cdk/coercion",
@@ -32,6 +32,18 @@ sass_binary(
3232
deps = [":core_scss_lib"],
3333
)
3434

35+
sass_binary(
36+
name = "option_scss",
37+
src = "option/option.scss",
38+
deps = [":core_scss_lib"],
39+
)
40+
41+
sass_binary(
42+
name = "optgroup_scss",
43+
src = "option/optgroup.scss",
44+
deps = [":core_scss_lib"],
45+
)
46+
3547
# TODO(jelbourn): remove this when sass_binary supports specifying an output filename and dir.
3648
# Copy the output of the sass_binary such that the filename and path match what we expect.
3749
genrule(
@@ -40,3 +52,17 @@ genrule(
4052
outs = ["selection/pseudo-checkbox/pseudo-checkbox.css"],
4153
cmd = "cat $(locations :pseudo_checkbox_scss) > $@",
4254
)
55+
56+
genrule(
57+
name = "option_css",
58+
srcs = [":option_scss"],
59+
outs = ["option/option.css"],
60+
cmd = "cat $(locations :option_scss) > $@",
61+
)
62+
63+
genrule(
64+
name = "optgroup_css",
65+
srcs = [":optgroup_scss"],
66+
outs = ["option/optgroup.css"],
67+
cmd = "cat $(locations :optgroup_scss) > $@",
68+
)

src/lib/core/_core.scss

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
// Core styles that can be used to apply material design treatments to any element.
77
@import 'style/elevation';
88
@import 'ripple/ripple';
9-
@import 'option/option';
109
@import 'option/option-theme';
11-
@import 'option/optgroup';
1210
@import 'option/optgroup-theme';
1311
@import 'selection/pseudo-checkbox/pseudo-checkbox-theme';
1412
@import 'typography/all-typography';
@@ -26,8 +24,6 @@
2624

2725
@include angular-material-typography($typography-config);
2826
@include mat-ripple();
29-
@include mat-option();
30-
@include mat-optgroup();
3127
@include cdk-a11y();
3228
@include cdk-overlay();
3329
}

src/lib/core/option/_optgroup.scss

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/lib/core/option/_option.scss

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/lib/core/option/optgroup.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@import '../style/menu-common';
2+
@import '../style/vendor-prefixes';
3+
4+
.mat-optgroup-label {
5+
@include mat-menu-item-base();
6+
@include user-select(none);
7+
cursor: default;
8+
}

src/lib/core/option/optgroup.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ let _uniqueOptgroupIdCounter = 0;
2929
preserveWhitespaces: false,
3030
changeDetection: ChangeDetectionStrategy.OnPush,
3131
inputs: ['disabled'],
32+
styleUrls: ['optgroup.css'],
3233
host: {
3334
'class': 'mat-optgroup',
3435
'role': 'group',

src/lib/core/option/option.scss

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
@import '../style/menu-common';
2+
@import '../style/vendor-prefixes';
3+
@import '../style/layout-common';
4+
@import '../../../cdk/a11y/a11y';
5+
6+
.mat-option {
7+
@include mat-menu-item-base();
8+
position: relative;
9+
cursor: pointer;
10+
outline: none;
11+
display: flex;
12+
flex-direction: row;
13+
max-width: 100%;
14+
box-sizing: border-box;
15+
align-items: center;
16+
17+
&[aria-disabled='true'] {
18+
@include user-select(none);
19+
cursor: default;
20+
}
21+
22+
.mat-optgroup &:not(.mat-option-multiple) {
23+
padding-left: $mat-menu-side-padding * 2;
24+
25+
[dir='rtl'] & {
26+
padding-left: $mat-menu-side-padding;
27+
padding-right: $mat-menu-side-padding * 2;
28+
}
29+
}
30+
}
31+
32+
// Collapses unwanted whitespace created by newlines in code like the following:
33+
// <mat-option>
34+
// {{value}}
35+
// </mat-option>
36+
.mat-option-text {
37+
display: inline-block;
38+
flex-grow: 1;
39+
overflow: hidden;
40+
text-overflow: ellipsis;
41+
}
42+
43+
.mat-option-ripple {
44+
@include mat-fill;
45+
46+
// Disable pointer events for the ripple container because the container will overlay the
47+
// user content and we don't want to disable mouse events on the user content.
48+
// Pointer events can be safely disabled because the ripple trigger element is the host element.
49+
pointer-events: none;
50+
51+
// Prevents the ripple from completely covering the option in high contrast mode.
52+
@include cdk-high-contrast {
53+
opacity: 0.5;
54+
}
55+
}
56+
57+
.mat-option-pseudo-checkbox {
58+
$margin: $mat-menu-side-padding / 2;
59+
margin-right: $margin;
60+
61+
[dir='rtl'] & {
62+
margin-left: $margin;
63+
margin-right: 0;
64+
}
65+
}

src/lib/core/option/option.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export const MAT_OPTION_PARENT_COMPONENT =
7676
'(keydown)': '_handleKeydown($event)',
7777
'class': 'mat-option',
7878
},
79+
styleUrls: ['option.css'],
7980
templateUrl: 'option.html',
8081
encapsulation: ViewEncapsulation.None,
8182
preserveWhitespaces: false,

0 commit comments

Comments
 (0)