Skip to content

Commit 7ec9697

Browse files
asyncLizcopybara-github
authored andcommitted
fix(typography): ensure global variables can override styles with module system
PiperOrigin-RevId: 296927628
1 parent 2f275e2 commit 7ec9697

File tree

5 files changed

+107
-46
lines changed

5 files changed

+107
-46
lines changed

packages/mdc-typography/README.md

+51-9
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,14 @@ These styles can be used as the `$style` argument for the `mdc-typography` mixin
146146

147147
#### Overriding Styles
148148

149-
All styles can be overridden using CSS custom properties or Sass global variables.
149+
All styles can be overridden using CSS custom properties or Sass module/global variables.
150150

151-
When using Sass global variables, they must be defined _before_ the component is imported by setting a global
152-
variable named `$mdc-typography-styles-{style}`. The variable should be assigned a map that contains all the properties
153-
you want to override for a particular style.
151+
When using Sass **module** variables, the module must be configured _before_ any other `@use`
152+
statements with a variable named `$styles-{style}`. The variable should be assigned to a map
153+
that contains all the properties you want to override for a particular style.
154+
155+
When using Sass **global** variables, they must be defined _before_ the component is imported by setting a global
156+
variable named `$mdc-typography-styles-{style}`.
154157

155158
**Example:** Overriding the button `font-size` and `text-transform` properties.
156159

@@ -163,6 +166,20 @@ html {
163166
}
164167
```
165168

169+
Sass module variables:
170+
171+
```scss
172+
@use "@material/typography" with (
173+
$styles-button: (
174+
font-size: 16px,
175+
text-transform: none,
176+
)
177+
);
178+
179+
@use "@material/button";
180+
@include button.core-styles;
181+
```
182+
166183
Sass global variables:
167184

168185
```scss
@@ -171,7 +188,7 @@ $mdc-typography-styles-button: (
171188
text-transform: none,
172189
);
173190

174-
@use "@material/button/mdc-button";
191+
@import "@material/button/mdc-button";
175192
```
176193

177194
**Example:** Overriding the global `font-family` property.
@@ -184,13 +201,23 @@ html {
184201
}
185202
```
186203

204+
Sass module variables:
205+
206+
```scss
207+
@use "@material/typography" with (
208+
$font-family: unquote("Arial, Helvetica, sans-serif")
209+
);
210+
211+
@use "@material/button";
212+
@include button.core-styles;
213+
```
214+
187215
Sass global variables:
188216

189217
```scss
190218
$mdc-typography-font-family: unquote("Arial, Helvetica, sans-serif");
191219

192-
...
193-
@use ...
220+
@import "@material/button/mdc-button";
194221
```
195222

196223
**Example:** Overriding the `font-family` property for `headline1` and `font-family` and `font-size` for `headline2`.
@@ -205,6 +232,22 @@ html {
205232
}
206233
```
207234

235+
Sass module variables:
236+
237+
```scss
238+
@use "@material/typography" with (
239+
$styles-headline1: (
240+
$font-family: unquote("Arial, Helvetica, sans-serif")
241+
),
242+
$styles-headline2: (
243+
$font-family: unquote("Arial, Helvetica, sans-serif"),
244+
$font-size: 3.25rem
245+
)
246+
);
247+
248+
@use ...
249+
```
250+
208251
Sass global variables:
209252

210253
```scss
@@ -216,6 +259,5 @@ $mdc-typography-styles-headline2: (
216259
font-size: 3.25rem
217260
);
218261

219-
...
220-
@use ...
262+
@import ...
221263
```

packages/mdc-typography/_functions.scss

+3-37
Original file line numberDiff line numberDiff line change
@@ -23,48 +23,14 @@
2323
@use "sass:map";
2424
@use "sass:string";
2525

26-
@function get-global-variable_($style) {
27-
@if $style == "headline1" {
28-
@return $mdc-typography-styles-headline1;
29-
} @else if $style == "headline2" {
30-
@return $mdc-typography-styles-headline2;
31-
} @else if $style == "headline3" {
32-
@return $mdc-typography-styles-headline3;
33-
} @else if $style == "headline4" {
34-
@return $mdc-typography-styles-headline4;
35-
} @else if $style == "headline5" {
36-
@return $mdc-typography-styles-headline5;
37-
} @else if $style == "headline6" {
38-
@return $mdc-typography-styles-headline6;
39-
} @else if $style == "subtitle1" {
40-
@return $mdc-typography-styles-subtitle1;
41-
} @else if $style == "subtitle2" {
42-
@return $mdc-typography-styles-subtitle2;
43-
} @else if $style == "body1" {
44-
@return $mdc-typography-styles-body1;
45-
} @else if $style == "body2" {
46-
@return $mdc-typography-styles-body2;
47-
} @else if $style == "caption" {
48-
@return $mdc-typography-styles-caption;
49-
} @else if $style == "button" {
50-
@return $mdc-typography-styles-button;
51-
} @else if $style == "overline" {
52-
@return $mdc-typography-styles-overline;
53-
} @else {
54-
@return ();
55-
}
56-
}
57-
58-
@function set-styles_($base-styles, $scale-styles) {
26+
@function set-styles_($base-styles, $scale-styles, $override-styles) {
5927
@each $style, $style-props in $scale-styles {
6028

6129
// Merge base properties for all styles.
6230
$style-props: map.merge($base-styles, $style-props);
6331

64-
// Merge global overrides onto each style.
65-
@if global_variable_exists(string.unquote("mdc-typography-styles-#{$style}")) {
66-
$style-props: map.merge($style-props, get-global-variable_(#{$style}));
67-
}
32+
// Merge overrides onto each style.
33+
$style-props: map.merge($style-props, map.get($override-styles, $style));
6834

6935
// Override original styles with new styles.
7036
$scale-styles: map.merge($scale-styles, (#{$style}: $style-props));

packages/mdc-typography/_variables.scss

+30
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@
2626

2727
$font-family: string.unquote("Roboto, sans-serif") !default;
2828

29+
// Override styles
30+
$styles-headline1: () !default;
31+
$styles-headline2: () !default;
32+
$styles-headline3: () !default;
33+
$styles-headline4: () !default;
34+
$styles-headline5: () !default;
35+
$styles-headline6: () !default;
36+
$styles-subtitle1: () !default;
37+
$styles-subtitle2: () !default;
38+
$styles-body1: () !default;
39+
$styles-body2: () !default;
40+
$styles-caption: () !default;
41+
$styles-button: () !default;
42+
$styles-overline: () !default;
43+
2944
$base: (
3045
font-family: $font-family
3146
) !default;
@@ -146,5 +161,20 @@ $styles: functions.set-styles_(
146161
text-decoration: none,
147162
text-transform: uppercase
148163
),
164+
),
165+
(
166+
headline1: $styles-headline1,
167+
headline2: $styles-headline2,
168+
headline3: $styles-headline3,
169+
headline4: $styles-headline4,
170+
headline5: $styles-headline5,
171+
headline6: $styles-headline6,
172+
subtitle1: $styles-subtitle1,
173+
subtitle2: $styles-subtitle2,
174+
body1: $styles-body1,
175+
body2: $styles-body2,
176+
caption: $styles-caption,
177+
button: $styles-button,
178+
overline: $styles-overline,
149179
)
150180
) !default;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@use "sass:string";
2+
3+
$mdc-typography-font-family: string.unquote("Arial");
4+
$mdc-typography-styles-headline1: (
5+
font-size: 1rem
6+
);
7+
8+
@import '../mdc-typography';

packages/mdc-typography/test/mdc-typography.scss.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,25 @@
2323

2424
import 'jasmine';
2525

26+
import * as fs from 'fs';
2627
import * as path from 'path';
2728
import {expectStylesWithNoFeaturesToBeEmpty} from '../../../testing/featuretargeting';
2829

2930
describe('mdc-typography.scss', () => {
3031
expectStylesWithNoFeaturesToBeEmpty(
3132
path.join(__dirname, 'feature-targeting-any.test.css'));
33+
34+
it('should allow global variable overrides with @import', () => {
35+
const css = fs.readFileSync(
36+
path.join(__dirname, 'global-variables.test.css'), 'utf8')
37+
.trim();
38+
const headline1Start = css.indexOf('.mdc-typography--headline1 {');
39+
const headline1End = css.indexOf('}', headline1Start);
40+
const headline1Css = css.substring(headline1Start, headline1End);
41+
expect(headline1Css.includes('font-family: Arial'))
42+
.toBe(true, '$mdc-typography-font-family should override');
43+
expect(headline1Css.includes('font-size: 1rem'))
44+
.toBe(
45+
true, '$mdc-typography-styles-headline1-font-size should override');
46+
});
3247
});

0 commit comments

Comments
 (0)