Skip to content

Commit fa9985b

Browse files
committed
refactor: add a safe-rgba function in case palettes is empty map
1 parent bb8d32a commit fa9985b

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/material/core/tokens/m2/_md-sys-color.scss

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
inverse-on-surface: rgba(black, 0.87),
2424
outline: rgba(white, 0.12),
2525
outline-variant: rgba(white, 0.38),
26-
error-container: rgba(map.get($palettes, warn, 300), 0.24),
26+
error-container: safe-rgba(map.get($palettes, warn, 300), 0.24),
2727
on-background: white,
2828
on-error-container: map.get($palettes, warn, 100),
2929
on-primary-container: map.get($palettes, primary, 100),
@@ -36,11 +36,11 @@
3636
on-tertiary-container: map.get($palettes, accent, 100),
3737
on-tertiary-fixed: map.get($palettes, accent, default-contrast),
3838
on-tertiary-fixed-variant: map.get($palettes, accent, default-contrast),
39-
primary-container: rgba(map.get($palettes, primary, 300), 0.24),
39+
primary-container: safe-rgba(map.get($palettes, primary, 300), 0.24),
4040
primary-fixed: map.get($palettes, primary, default),
4141
primary-fixed-dim: map.get($palettes, primary, default),
4242
scrim: black,
43-
secondary-container: rgba(map.get($palettes, accent, 300), 0.24),
43+
secondary-container: safe-rgba(map.get($palettes, accent, 300), 0.24),
4444
secondary-fixed: map.get($palettes, accent, default),
4545
secondary-fixed-dim: map.get($palettes, accent, default),
4646
surface-bright: map.get(palette.$grey-palette, 800),
@@ -52,7 +52,7 @@
5252
surface-dim: map.get(palette.$grey-palette, 800),
5353
surface-tint: map.get(palette.$grey-palette, 800),
5454
tertiary: map.get($palettes, accent, default),
55-
tertiary-container: rgba(map.get($palettes, accent, 300), 0.24),
55+
tertiary-container: safe-rgba(map.get($palettes, accent, 300), 0.24),
5656
tertiary-fixed: map.get($palettes, accent, default),
5757
tertiary-fixed-dim: map.get($palettes, accent, default),
5858
);
@@ -121,3 +121,12 @@
121121

122122
@return $values;
123123
}
124+
125+
// Returns the rgba value for the provided color and opacity. If color is null,
126+
// then it returns null. If rgba is used in these cases, Sass throws an error.
127+
@function safe-rgba($color, $opacity) {
128+
@if $color == null {
129+
@return null;
130+
}
131+
@return rgba($color, $opacity);
132+
}

0 commit comments

Comments
 (0)