Skip to content

Commit 427e99a

Browse files
authored
refactor(material/core): clean up focus indicators (#25078)
I skipped over some of the feedback on #25039 in order to get the focus indicator changes merged in quicker. This is a follow-up to address the remaining feedback.
1 parent 2fc61f4 commit 427e99a

File tree

24 files changed

+56
-60
lines changed

24 files changed

+56
-60
lines changed

src/dev-app/theme.scss

+8-7
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ $candy-app-theme: mat.define-light-theme((
3232
@include experimental.popover-edit-theme($candy-app-theme);
3333

3434
.demo-strong-focus {
35-
// Include base styles for strong focus indicators.
36-
$indicators-config: (border-color: mat.get-color-from-palette($candy-app-primary));
37-
@include mat.strong-focus-indicators($indicators-config);
38-
@include experimental.mdc-strong-focus-indicators($indicators-config);
35+
// Note: we can theme the indicators directly through `strong-focus-indicators` as well.
36+
// Use the theme so we have some coverage over the entire API surface.
37+
@include mat.strong-focus-indicators();
38+
@include mat.strong-focus-indicators-theme($candy-app-theme);
39+
@include experimental.mdc-strong-focus-indicators();
40+
@include experimental.mdc-strong-focus-indicators-theme($candy-app-theme);
3941
}
4042

4143
// Include the alternative theme styles inside of a block with a CSS class. You can make this
@@ -57,9 +59,8 @@ $candy-app-theme: mat.define-light-theme((
5759

5860
// Include the dark theme colors for focus indicators.
5961
&.demo-strong-focus {
60-
$indicators-config: (border-color: mat.get-color-from-palette($dark-primary));
61-
@include mat.strong-focus-indicators($indicators-config);
62-
@include experimental.mdc-strong-focus-indicators($indicators-config);
62+
@include mat.strong-focus-indicators-theme($dark-colors);
63+
@include experimental.mdc-strong-focus-indicators-theme($dark-colors);
6364
}
6465
}
6566

src/material-experimental/mdc-button/button.scss

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,15 @@
113113
.mat-mdc-unelevated-button,
114114
.mat-mdc-raised-button {
115115
.mat-mdc-focus-indicator::before {
116-
$default-border-width: mat.$focus-indicators-private-default-border-width;
116+
$default-border-width: mat.$private-strong-focus-indicators-default-border-width;
117117
$border-width: var(--mat-mdc-focus-indicator-border-width, #{$default-border-width});
118118
$offset: calc(#{$border-width} + 2px);
119119
margin: calc(#{$offset} * -1);
120120
}
121121
}
122122

123123
.mat-mdc-outlined-button .mat-mdc-focus-indicator::before {
124-
$default-border-width: mat.$focus-indicators-private-default-border-width;
124+
$default-border-width: mat.$private-strong-focus-indicators-default-border-width;
125125
$border-width: var(--mat-mdc-focus-indicator-border-width, #{$default-border-width});
126126
$offset: calc(#{$border-width} + 3px);
127127
margin: calc(#{$offset} * -1);

src/material-experimental/mdc-button/fab.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
}
6868

6969
.mat-mdc-focus-indicator::before {
70-
$default-border-width: mat.$focus-indicators-private-default-border-width;
70+
$default-border-width: mat.$private-strong-focus-indicators-default-border-width;
7171
$border-width: var(--mat-mdc-focus-indicator-border-width, #{$default-border-width});
7272
$offset: calc(#{$border-width} + 2px);
7373
margin: calc(#{$offset} * -1);

src/material-experimental/mdc-checkbox/BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ sass_binary(
4949
],
5050
deps = [
5151
":mdc_checkbox_scss_lib",
52-
"//src/cdk:sass_lib",
5352
"//src/material:sass_lib",
5453
"//src/material-experimental/mdc-helpers:mdc_helpers_scss_lib",
5554
"//src/material-experimental/mdc-helpers:mdc_scss_deps_lib",

src/material-experimental/mdc-chips/chip.scss

+4-3
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@
179179
// For the chip element, default inset/offset values are necessary to ensure that
180180
// the focus indicator is sufficiently contrastive and renders appropriately.
181181
.mat-mdc-focus-indicator::before {
182-
$default-border-width: mat.$focus-indicators-private-default-border-width;
182+
$default-border-width: mat.$private-strong-focus-indicators-default-border-width;
183183
$border-width: var(--mat-mdc-focus-indicator-border-width, #{$default-border-width});
184184
$offset: calc(#{$border-width} + 2px);
185185
margin: calc(#{$offset} * -1);
@@ -196,7 +196,7 @@
196196
}
197197

198198
&::before {
199-
$default-border-width: mat.$focus-indicators-private-default-border-width;
199+
$default-border-width: mat.$private-strong-focus-indicators-default-border-width;
200200
$offset: var(--mat-mdc-focus-indicator-border-width, #{$default-border-width});
201201
margin: calc(#{$offset} * -1);
202202

@@ -228,7 +228,8 @@
228228
}
229229
}
230230

231-
// In the chips the individual actions have focus so we target a different element.
231+
// The chip has multiple focus targets so we have to put the indicator on
232+
// a separate element, rather than on the focusable element itself.
232233
.mat-mdc-chip-action:focus .mat-mdc-focus-indicator::before {
233234
content: '';
234235
}

src/material-experimental/mdc-core/BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ sass_binary(
4343
"external/npm/node_modules",
4444
],
4545
deps = [
46-
"//src/cdk:sass_lib",
4746
"//src/material:sass_lib",
4847
"//src/material-experimental/mdc-helpers:mdc_helpers_scss_lib",
4948
"//src/material-experimental/mdc-helpers:mdc_scss_deps_lib",

src/material-experimental/mdc-helpers/_focus-indicators-theme.scss

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,30 @@
22
@use 'sass:map';
33
@use 'sass:meta';
44

5+
$_prefix: 'mat-mdc';
6+
57
// stylelint-disable-next-line material/theme-mixin-api
68
@mixin color($config-or-theme-or-color) {
79
@if meta.type-of($config-or-theme-or-color) == 'color' {
8-
@include mat.focus-indicators-private-private-customize-focus-indicators((
10+
@include mat.private-strong-focus-indicators-customize-focus-indicators((
911
border-color: $config-or-theme-or-color
10-
), 'mat-mdc');
12+
), $_prefix);
1113
}
1214
@else {
1315
$config: mat.get-color-config($config-or-theme-or-color);
1416
$border-color: mat.get-color-from-palette(map.get($config, primary));
15-
@include mat.focus-indicators-private-private-customize-focus-indicators((
17+
@include mat.private-strong-focus-indicators-customize-focus-indicators((
1618
border-color: $border-color
17-
), 'mat-mdc');
19+
), $_prefix);
1820
}
1921
}
2022

2123
// stylelint-disable-next-line material/theme-mixin-api
2224
@mixin theme($theme-or-color-config-or-color) {
2325
@if meta.type-of($theme-or-color-config-or-color) == 'color' {
24-
@include mat.focus-indicators-private-private-customize-focus-indicators((
26+
@include mat.private-strong-focus-indicators-customize-focus-indicators((
2527
border-color: $theme-or-color-config-or-color
26-
), 'mat-mdc');
28+
), $_prefix);
2729
}
2830
@else {
2931
$theme: mat.private-legacy-get-theme($theme-or-color-config-or-color);

src/material-experimental/mdc-helpers/_focus-indicators.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
// Merge default config with user config.
1212
$config: map.merge($default-config, $config);
1313

14-
@include mat.focus-indicators-private-private-customize-focus-indicators($config, 'mat-mdc');
14+
@include mat.private-strong-focus-indicators-customize-focus-indicators($config, 'mat-mdc');
1515
}

src/material-experimental/mdc-list/list.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
// focus/selected/hover state. Hence, we need to have a separate list-item spanning
6060
// element that can be used for strong focus indicators.
6161
.mat-mdc-list-item {
62-
& > .mat-mdc-focus-indicator {
62+
> .mat-mdc-focus-indicator {
6363
@include mat.private-fill();
6464
pointer-events: none;
6565
}

src/material-experimental/mdc-radio/BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ sass_binary(
4646
"external/npm/node_modules",
4747
],
4848
deps = [
49-
"//src/cdk:sass_lib",
5049
"//src/material:sass_lib",
5150
"//src/material-experimental/mdc-helpers:mdc_helpers_scss_lib",
5251
"//src/material-experimental/mdc-helpers:mdc_scss_deps_lib",

src/material-experimental/mdc-radio/radio.scss

-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@
7070
opacity: mdc-radio-theme.$ripple-opacity;
7171
}
7272

73-
// Radio components have to set `border-radius: 50%` in order to support density scaling
74-
// which will clip a square focus indicator so we have to turn it into a circle.
7573
&::before {
7674
border-radius: 50%;
7775
}

src/material-experimental/mdc-slide-toggle/BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ sass_binary(
5050
"external/npm/node_modules",
5151
],
5252
deps = [
53-
"//src/cdk:sass_lib",
5453
"//src/material:sass_lib",
5554
"//src/material-experimental/mdc-helpers:mdc_helpers_scss_lib",
5655
"//src/material-experimental/mdc-helpers:mdc_scss_deps_lib",

src/material-experimental/mdc-tabs/BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ sass_library(
4646
name = "mdc_tabs_scss_lib",
4747
srcs = glob(["**/_*.scss"]),
4848
deps = [
49-
"//src/cdk:sass_lib",
5049
"//src/material:sass_lib",
5150
"//src/material-experimental/mdc-helpers:mdc_helpers_scss_lib",
5251
"//src/material-experimental/mdc-helpers:mdc_scss_deps_lib",

src/material/_index.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232
@forward './table/table-flex-styles' show private-table-flex-styles;
3333
@forward './core/style/menu-common' as private-menu-common-*;
3434
@forward './core/style/button-common' as private-button-common-*;
35-
@forward './core/style/focus-indicators' as focus-indicators-private-*;
3635

3736
// Structural
3837
@forward './core/core' show core;
3938
@forward './core/ripple/ripple' show ripple;
4039
@forward './core/focus-indicators/focus-indicators' show strong-focus-indicators;
40+
@forward './core/focus-indicators/private' as private-strong-focus-indicators-*;
4141
@forward './core/style/elevation' show elevation, overridable-elevation, elevation-transition;
4242

4343
// Theme bundles

src/material/button/button.scss

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
@use './button-base';
44
@use '../core/style/layout-common';
5-
@use '../core/style/focus-indicators';
5+
@use '../core/focus-indicators/private';
66

77
// TODO(jelbourn): Measure perf benefits for translate3d and will-change.
88
// TODO(jelbourn): Figure out if anchor hover underline actually happens in any browser.
@@ -163,16 +163,14 @@
163163
.mat-fab,
164164
.mat-mini-fab {
165165
&::before {
166-
$border-width:
167-
var(--mat-focus-indicator-border-width, #{focus-indicators.$default-border-width});
166+
$border-width: var(--mat-focus-indicator-border-width, #{private.$default-border-width});
168167
$offset: calc(#{$border-width} + 2px);
169168
margin: calc(#{$offset} * -1);
170169
}
171170
}
172171

173172
.mat-stroked-button::before {
174-
$border-width:
175-
var(--mat-focus-indicator-border-width, #{focus-indicators.$default-border-width});
173+
$border-width: var(--mat-focus-indicator-border-width, #{private.$default-border-width});
176174
$offset: calc(#{$border-width} + 3px);
177175
margin: calc(#{$offset} * -1);
178176
}

src/material/chips/chips.scss

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
@use '../core/style/variables';
44
@use '../core/style/elevation';
55
@use '../core/style/layout-common';
6-
@use '../core/style/private';
7-
@use '../core/style/focus-indicators';
6+
@use '../core/style/private' as style-private;
7+
@use '../core/focus-indicators/private' as focus-indicators-private;
88

99
$chip-min-height: 32px;
1010
$chip-vertical-padding: 7px;
@@ -41,15 +41,15 @@ $chip-remove-size: 18px;
4141
// the focus indicator is sufficiently contrastive and renders appropriately.
4242
&::before {
4343
$border-width:
44-
var(--mat-focus-indicator-border-width, #{focus-indicators.$default-border-width});
44+
var(--mat-focus-indicator-border-width, #{focus-indicators-private.$default-border-width});
4545
$offset: calc(#{$border-width} + 2px);
4646
margin: calc(#{$offset} * -1);
4747
}
4848
}
4949

5050
.mat-standard-chip {
5151
@include elevation.elevation-transition;
52-
@include private.private-animation-noop;
52+
@include style-private.private-animation-noop;
5353
display: inline-flex;
5454
padding: $chip-vertical-padding $chip-horizontal-padding;
5555
border-radius: 16px;

src/material/core/_core.scss

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// Core styles that can be used to apply material design treatments to any element.
44
@use './ripple/ripple';
5-
@use './style/focus-indicators';
5+
@use './focus-indicators/private';
66
@use './typography/all-typography';
77

88
// Mixin that renders all of the core styles that are not theme-dependent.
@@ -13,6 +13,6 @@
1313
@include cdk.overlay();
1414
@include cdk.text-field-autosize();
1515
@include cdk.text-field-autofill();
16-
@include focus-indicators.private-structural-styling('mat');
17-
@include focus-indicators.private-structural-styling('mat-mdc');
16+
@include private.structural-styling('mat');
17+
@include private.structural-styling('mat-mdc');
1818
}

src/material/core/focus-indicators/_focus-indicators-theme.scss

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
@use 'sass:map';
22
@use 'sass:meta';
33
@use '../theming/theming';
4-
@use '../style/focus-indicators';
4+
@use './private';
5+
6+
$_prefix: 'mat';
57

68
// stylelint-disable-next-line material/theme-mixin-api
79
@mixin color($config-or-theme-or-color) {
810
@if meta.type-of($config-or-theme-or-color) == 'color' {
9-
@include focus-indicators.private-customize-focus-indicators((
11+
@include private.customize-focus-indicators((
1012
border-color: $config-or-theme-or-color
11-
), 'mat');
13+
), $_prefix);
1214
}
1315
@else {
1416
$config: theming.get-color-config($config-or-theme-or-color);
1517
$border-color: theming.get-color-from-palette(map.get($config, primary));
16-
@include focus-indicators.private-customize-focus-indicators((
18+
@include private.customize-focus-indicators((
1719
border-color: $border-color
18-
), 'mat');
20+
), $_prefix);
1921
}
2022
}
2123

2224
// stylelint-disable-next-line material/theme-mixin-api
2325
@mixin theme($theme-or-color-config-or-color) {
2426
@if meta.type-of($theme-or-color-config-or-color) == 'color' {
25-
@include focus-indicators.private-customize-focus-indicators((
27+
@include private.customize-focus-indicators((
2628
border-color: $theme-or-color-config-or-color
27-
), 'mat');
29+
), $_prefix);
2830
}
2931
@else {
3032
$theme: theming.private-legacy-get-theme($theme-or-color-config-or-color);

src/material/core/focus-indicators/_focus-indicators.scss

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@use 'sass:map';
2-
@use '../style/focus-indicators';
2+
@use './private';
33

44
@mixin strong-focus-indicators($config: ()) {
55
// Default focus indicator config.
@@ -11,5 +11,5 @@
1111
// Merge default config with user config.
1212
$config: map.merge($default-config, $config);
1313

14-
@include focus-indicators.private-customize-focus-indicators($config, 'mat');
14+
@include private.customize-focus-indicators($config, 'mat');
1515
}

src/material/core/style/_focus-indicators.scss renamed to src/material/core/focus-indicators/_private.scss

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ $default-border-color: transparent;
99
$default-border-radius: 4px;
1010

1111
// Mixin that renders the focus indicator structural styles.
12-
@mixin private-structural-styling($prefix) {
12+
@mixin structural-styling($prefix) {
1313
.#{$prefix}-focus-indicator {
1414
position: relative;
1515

@@ -45,7 +45,7 @@ $default-border-radius: 4px;
4545

4646
// Enable the indicator in high contrast mode.
4747
@include cdk.high-contrast(active, off) {
48-
@include private-customize-focus-indicators((display: block), $prefix);
48+
@include customize-focus-indicators((display: block), $prefix);
4949
}
5050
}
5151

@@ -59,7 +59,7 @@ $default-border-radius: 4px;
5959
}
6060

6161
// Mixin that dedups CSS variables for the strong-focus-indicators mixin.
62-
@mixin private-customize-focus-indicators($config, $prefix) {
62+
@mixin customize-focus-indicators($config, $prefix) {
6363
$border-style: map.get($config, border-style);
6464
$border-width: map.get($config, border-width);
6565
$border-radius: map.get($config, border-radius);

src/material/core/theming/tests/test-theming-bundle.scss

+2
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,5 @@ $theme: mat.define-light-theme((
2424
@include mat.all-component-colors($theme);
2525
@include mat.core-theme($theme);
2626
@include mat.button-theme($theme);
27+
@include mat.strong-focus-indicators();
28+
@include mat.strong-focus-indicators-theme($theme);

src/material/datepicker/calendar.scss

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@use '@angular/cdk';
22

33
@use '../core/style/layout-common';
4-
@use '../core/style/focus-indicators';
4+
@use '../core/focus-indicators/private';
55

66
$calendar-padding: 8px !default;
77
$calendar-header-divider-width: 1px !default;
@@ -129,8 +129,7 @@ $calendar-next-icon-transform: translateX(-2px) rotate(45deg);
129129
// For the calendar element, default inset/offset values are necessary to ensure that
130130
// the focus indicator is sufficiently contrastive and renders appropriately.
131131
.mat-calendar-body-cell-content::before {
132-
$border-width:
133-
var(--mat-focus-indicator-border-width, #{focus-indicators.$default-border-width});
132+
$border-width: var(--mat-focus-indicator-border-width, #{private.$default-border-width});
134133
$offset: calc(#{$border-width} + 3px);
135134
margin: calc(#{$offset} * -1);
136135
}

src/material/expansion/BUILD.bazel

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ sass_binary(
4848
name = "expansion_panel_scss",
4949
src = "expansion-panel.scss",
5050
deps = [
51-
"//src/cdk:sass_lib",
5251
"//src/material/core:core_scss_lib",
5352
],
5453
)

0 commit comments

Comments
 (0)