Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit 7be9e4a

Browse files
authored
fix(checkbox): Replace unique-id with custom color hash functio… (#5404)
1 parent 4e0451b commit 7be9e4a

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

packages/mdc-checkbox/_mixins.scss

+7-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
@import "@material/feature-targeting/mixins";
2727
@import "@material/ripple/mixins";
2828
@import "@material/touch-target/mixins";
29+
@import "@material/theme/functions";
2930
@import "./functions";
3031
@import "./keyframes";
3132
@import "./variables";
@@ -297,7 +298,11 @@ $mdc-checkbox-ripple-target: ".mdc-checkbox__ripple";
297298
}
298299

299300
@if $generate-keyframes {
300-
$uid: unique-id();
301+
$uid: mdc-theme-color-hash($unmarked-stroke-color) +
302+
mdc-theme-color-hash($marked-stroke-color) +
303+
mdc-theme-color-hash($unmarked-fill-color) +
304+
mdc-theme-color-hash($marked-fill-color);
305+
301306
$anim-selector: if(&, "&.mdc-checkbox--anim", ".mdc-checkbox--anim");
302307

303308
@include mdc-feature-targets($feat-animation, $feat-color) {
@@ -306,7 +311,7 @@ $mdc-checkbox-ripple-target: ".mdc-checkbox__ripple";
306311
$to-stroke-color: $marked-stroke-color,
307312
$from-fill-color: $unmarked-fill-color,
308313
$to-fill-color: $marked-fill-color,
309-
$uid: $uid
314+
$uid: #{$uid}
310315
);
311316
}
312317

packages/mdc-theme/_functions.scss

+56
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,59 @@
7979

8080
@return var(#{$var}, $fallback);
8181
}
82+
83+
///
84+
/// @param $color Target color in any color format.
85+
/// @return Returns hash in string format that uniquely represents
86+
/// any given color format. Useful for generating unique keyframe names.
87+
/// @example
88+
/// `color-hash(#6200ee)` => "6200ee"
89+
/// `color-hash(rgb(255, 112, 112))` => "ff7070"
90+
/// `color-hash(var(--my-fancy-color))` => "--my-fancy-color"
91+
///
92+
@function mdc-theme-color-hash($color) {
93+
@if mdc-theme-is-var-with-fallback_($color) {
94+
$color: map-get($color, "fallback");
95+
}
96+
97+
@if mdc-theme-is-css-var_($color) {
98+
@return mdc-theme-get-css-varname_($color);
99+
}
100+
101+
@if type-of($color) == "string" {
102+
@return $color;
103+
}
104+
105+
@return str-slice(ie-hex-str($color), 2); // Index starts at 1
106+
}
107+
108+
///
109+
/// @return Returns true if given color is set to CSS variable.
110+
/// @access Private
111+
///
112+
@function mdc-theme-is-css-var_($color) {
113+
@return str_slice(inspect($color), 1, 4) == "var(";
114+
}
115+
116+
///
117+
/// @return Returns CSS variable name from color value in CSS variable format.
118+
/// Returns original color value if not in CSS variable format.
119+
/// @example
120+
/// mdc-theme-get-css-varname_(var(--my-fancy-color, #4CAF50)) => --my-fancy-color
121+
/// mdc-theme-get-css-varname_(var(--my-fancy-color)) => --my-fancy-color
122+
/// @access Private
123+
///
124+
@function mdc-theme-get-css-varname_($color) {
125+
@if not mdc-theme-is-css-var_($color) {
126+
@return $color;
127+
}
128+
129+
$color: inspect($color);
130+
131+
$var-start-index: str-index($color, "--");
132+
$color: str_slice($color, $var-start-index);
133+
$var-end-index: str-index($color, ",") or str-index($color, ")");
134+
$color: str_slice($color, 0, $var-end-index - 1);
135+
136+
@return $color;
137+
}

0 commit comments

Comments
 (0)