Skip to content

Commit 574345c

Browse files
authored
feat(material/focus-indicators): add config map to base focus indicators mixin, adjust default styles (#19206)
This is the first PR in a series of PRs to refresh the focus indicators API in preparation for formal documentation being added. This PR includes the following changes: * The mixin mat-strong-focus-indicators (and the MDC equivalent) now both accept a $config map that developers can use to configure focus indicator styles (e.g. border-style, border-width, border-radius). * I changed some of the default focus indicator styles (e.g. increased width from 2px -> 3px, updated some of the default offsets). Future PRs will: * Add per-component customization mixins.
1 parent a9ca95e commit 574345c

File tree

4 files changed

+74
-42
lines changed

4 files changed

+74
-42
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
/src/material/core/common-behaviors/** @jelbourn @devversion
4646
/src/material/core/datetime/** @mmalerba
4747
/src/material/core/error/** @crisbeto @mmalerba
48-
/src/material/core/focus-indicator/** @jelbourn @zelliott
48+
/src/material/core/focus-indicators/** @jelbourn @zelliott
4949
/src/material/core/gestures/** @jelbourn
5050
/src/material/core/label/** @mmalerba
5151
/src/material/core/line/** @jelbourn

src/dev-app/theme.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
@import '../material/core/theming/all-theme';
2-
@import '../material/core/focus-indicator/focus-indicator';
2+
@import '../material/core/focus-indicators/focus-indicators';
33
@import '../material-experimental/column-resize/column-resize';
44
@import '../material-experimental/mdc-helpers/mdc-helpers';
55
@import '../material-experimental/mdc-theming/all-theme';

src/material-experimental/mdc-helpers/_mdc-helpers.scss

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -217,37 +217,55 @@ $mat-typography-level-mappings: (
217217
///
218218
/// @example
219219
/// .my-app {
220-
/// @include mat-mdc-strong-focus-indicators();
220+
/// @include mat-mdc-strong-focus-indicators($config);
221221
/// }
222-
@mixin mat-mdc-strong-focus-indicators() {
223-
// Base styles for the focus indicators.
222+
@mixin mat-mdc-strong-focus-indicators($config: ()) {
223+
// Default focus indicator config.
224+
$default-config: (
225+
border-style: solid,
226+
border-width: 3px,
227+
border-radius: 4px,
228+
);
229+
230+
// Merge default config with user config.
231+
$config: map-merge($default-config, $config);
232+
$border-style: map-get($config, border-style);
233+
$border-width: map-get($config, border-width);
234+
$border-radius: map-get($config, border-radius);
235+
236+
// Base styles for focus indicators.
224237
.mat-mdc-focus-indicator::before {
225238
@include mat-fill();
226-
227-
border-radius: $mat-focus-indicator-border-radius;
228-
border: $mat-focus-indicator-border-width $mat-focus-indicator-border-style transparent;
229239
box-sizing: border-box;
230240
pointer-events: none;
241+
border: $border-width $border-style transparent;
242+
border-radius: $border-radius;
231243
}
232244

233245
// By default, all focus indicators are flush with the bounding box of their
234246
// host element. For particular elements (listed below), default inset/offset
235247
// values are necessary to ensure that the focus indicator is sufficiently
236248
// contrastive and renders appropriately.
237249

238-
.mat-mdc-button-base .mat-mdc-focus-indicator::before,
250+
.mat-mdc-unelevated-button .mat-mdc-focus-indicator::before,
251+
.mat-mdc-raised-button .mat-mdc-focus-indicator::before,
252+
.mdc-fab .mat-mdc-focus-indicator::before,
239253
.mat-mdc-focus-indicator.mdc-chip::before {
240-
margin: $mat-focus-indicator-border-width * -2;
254+
margin: -($border-width + 2px);
255+
}
256+
257+
.mat-mdc-outlined-button .mat-mdc-focus-indicator::before {
258+
margin: -($border-width + 3px);
241259
}
242260

243261
.mat-mdc-focus-indicator.mat-mdc-chip-remove::before,
244262
.mat-mdc-focus-indicator.mat-chip-row-focusable-text-content::before {
245-
margin: $mat-focus-indicator-border-width * -1;
263+
margin: -$border-width;
246264
}
247265

248266
.mat-mdc-focus-indicator.mat-mdc-tab::before,
249267
.mat-mdc-focus-indicator.mat-mdc-tab-link::before {
250-
margin: $mat-focus-indicator-border-width * 2;
268+
margin: 5px;
251269
}
252270

253271
// Render the focus indicator on focus. Defining a pseudo element's
@@ -258,6 +276,8 @@ $mat-typography-level-mappings: (
258276
.mdc-checkbox__native-control:focus ~ .mat-mdc-focus-indicator::before,
259277
.mat-mdc-slide-toggle-focused .mat-mdc-focus-indicator::before,
260278
.mat-mdc-radio-button.cdk-focused .mat-mdc-focus-indicator::before,
279+
280+
// For buttons, render the focus indicator when the parent button is focused.
261281
.mat-mdc-button-base:focus .mat-mdc-focus-indicator::before,
262282

263283
// For all other components, render the focus indicator on focus.
@@ -268,24 +288,24 @@ $mat-typography-level-mappings: (
268288

269289
/// Mixin that sets the color of the focus indicators.
270290
///
271-
/// @param {color|map} $themeOrMap
291+
/// @param {color|map} $theme-or-color
272292
/// If theme, focus indicators are set to the primary color of the theme. If
273293
/// color, focus indicators are set to that color.
274294
///
275295
/// @example
276296
/// .demo-dark-theme {
277-
/// @include mat-mdc-strong-focus-indicators-theme($darkThemeMap);
297+
/// @include mat-mdc-strong-focus-indicators-theme($dark-theme-map);
278298
/// }
279299
///
280300
/// @example
281301
/// .demo-red-theme {
282-
/// @include mat-mdc-strong-focus-indicators-theme(#F00);
302+
/// @include mat-mdc-strong-focus-indicators-theme(#f00);
283303
/// }
284-
@mixin mat-mdc-strong-focus-indicators-theme($themeOrColor) {
304+
@mixin mat-mdc-strong-focus-indicators-theme($theme-or-color) {
285305
.mat-mdc-focus-indicator::before {
286306
border-color: if(
287-
type-of($themeOrColor) == 'map',
288-
mat-color(map_get($themeOrColor, primary)),
289-
$themeOrColor);
307+
type-of($theme-or-color) == 'map',
308+
mat-color(map_get($theme-or-color, primary)),
309+
$theme-or-color);
290310
}
291311
}
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,60 @@
11
@import '../theming/theming';
22
@import '../style/layout-common';
33

4-
// Focus indicator styles.
5-
$mat-focus-indicator-border-radius: 4px;
6-
$mat-focus-indicator-border-width: 2px;
7-
$mat-focus-indicator-border-style: solid;
8-
94
/// Mixin that turns on strong focus indicators.
105
///
116
/// @example
127
/// .my-app {
13-
/// @include mat-strong-focus-indicators();
8+
/// @include mat-strong-focus-indicators($config);
149
/// }
15-
@mixin mat-strong-focus-indicators() {
10+
@mixin mat-strong-focus-indicators($config: ()) {
11+
// Default focus indicator config.
12+
$default-config: (
13+
border-style: solid,
14+
border-width: 3px,
15+
border-radius: 4px,
16+
);
17+
18+
// Merge default config with user config.
19+
$config: map-merge($default-config, $config);
20+
$border-style: map-get($config, border-style);
21+
$border-width: map-get($config, border-width);
22+
$border-radius: map-get($config, border-radius);
1623

17-
// Base styles for the focus indicators.
24+
// Base styles for focus indicators.
1825
.mat-focus-indicator::before {
1926
@include mat-fill();
20-
21-
border-radius: $mat-focus-indicator-border-radius;
22-
border: $mat-focus-indicator-border-width $mat-focus-indicator-border-style transparent;
2327
box-sizing: border-box;
2428
pointer-events: none;
29+
border: $border-width $border-style transparent;
30+
border-radius: $border-radius;
2531
}
2632

2733
// By default, all focus indicators are flush with the bounding box of their
2834
// host element. For particular elements (listed below), default inset/offset
2935
// values are necessary to ensure that the focus indicator is sufficiently
3036
// contrastive and renders appropriately.
3137

32-
.mat-focus-indicator.mat-button-base::before,
33-
.mat-focus-indicator.mat-card::before,
38+
.mat-focus-indicator.mat-flat-button::before,
39+
.mat-focus-indicator.mat-raised-button::before,
40+
.mat-focus-indicator.mat-fab::before,
41+
.mat-focus-indicator.mat-mini-fab::before,
3442
.mat-focus-indicator.mat-chip::before,
3543
.mat-focus-indicator.mat-sort-header-button::before {
36-
margin: $mat-focus-indicator-border-width * -2;
44+
margin: -($border-width + 2px);
45+
}
46+
47+
.mat-focus-indicator.mat-stroked-button::before {
48+
margin: -($border-width + 3px);
3749
}
3850

3951
.mat-focus-indicator.mat-calendar-body-cell::before {
40-
margin: $mat-focus-indicator-border-width * -1;
52+
margin: -$border-width;
4153
}
4254

4355
.mat-focus-indicator.mat-tab-link::before,
4456
.mat-focus-indicator.mat-tab-label::before {
45-
margin: $mat-focus-indicator-border-width * 2;
57+
margin: 5px;
4658
}
4759

4860
// Render the focus indicator on focus. Defining a pseudo element's
@@ -66,24 +78,24 @@ $mat-focus-indicator-border-style: solid;
6678

6779
/// Mixin that sets the color of the focus indicators.
6880
///
69-
/// @param {color|map} $themeOrMap
81+
/// @param {color|map} $theme-or-color
7082
/// If theme, focus indicators are set to the primary color of the theme. If
7183
/// color, focus indicators are set to that color.
7284
///
7385
/// @example
7486
/// .demo-dark-theme {
75-
/// @include mat-strong-focus-indicators-theme($darkThemeMap);
87+
/// @include mat-strong-focus-indicators-theme($dark-theme-map);
7688
/// }
7789
///
7890
/// @example
7991
/// .demo-red-theme {
80-
/// @include mat-strong-focus-indicators-theme(#F00);
92+
/// @include mat-strong-focus-indicators-theme(#f00);
8193
/// }
82-
@mixin mat-strong-focus-indicators-theme($themeOrColor) {
94+
@mixin mat-strong-focus-indicators-theme($theme-or-color) {
8395
.mat-focus-indicator::before {
8496
border-color: if(
85-
type-of($themeOrColor) == 'map',
86-
mat-color(map_get($themeOrColor, primary)),
87-
$themeOrColor);
97+
type-of($theme-or-color) == 'map',
98+
mat-color(map_get($theme-or-color, primary)),
99+
$theme-or-color);
88100
}
89101
}

0 commit comments

Comments
 (0)