Skip to content

Commit 023a438

Browse files
committed
updated selectors for common tokens in public themed CSS files to avoid CSS variables staleness
1 parent 03362e2 commit 023a438

File tree

3 files changed

+88
-3
lines changed
  • showcase/public/assets/styles/@hashicorp/themed-tokens

3 files changed

+88
-3
lines changed

showcase/public/assets/styles/@hashicorp/themed-tokens/with-combined-strategies/tokens.css

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,40 @@
141141
--token-typography-font-stack-text: Impact, "Arial Black", "sans serif";
142142
}
143143

144-
:root {
144+
/*
145+
WHY ARE ALL "COMPUTED" CSS VARIABLES REDECLARED IN EACH THEME SCOPE?
146+
147+
CSS custom properties (variables) inherit from parent to child, but when a variable is defined
148+
as a calculation or alias of another variable (e.g., `--alias-value: var(--source-value)`, its value is fixed
149+
at the scope where it is declared—using whatever is visible at that level in the DOM (eg. `:root`).
150+
151+
This block of "common/shared" CSS variables contains a lot of these aliases, so if we only declare these
152+
"computed" variables at `:root` level, their values do not update in themed blocks that override
153+
the "source" variables (e.g., `--source-value`) at a lower scope.
154+
This results in _stale_ variables, where the computed value is always stuck on the `:root` value,
155+
even inside a themed block where the source variable is assigned.
156+
157+
To make sure every themed block/scoped context (eg. `.hds-theme-light`) correctly computes the aliased values,
158+
we redeclare each "computed" variable in every theme scope where its "source" variable is overridden.
159+
*/
160+
161+
:root,
162+
.hds-theme-default,
163+
.hds-theme-system,
164+
.hds-theme-light,
165+
.hds-theme-cds-g0,
166+
.hds-theme-cds-g10,
167+
.hds-theme-cds-g90,
168+
.hds-theme-dark,
169+
.hds-theme-cds-g100,
170+
[data-hds-theme="default"],
171+
[data-hds-theme="system"],
172+
[data-hds-theme="light"],
173+
[data-hds-theme="cds-g0"],
174+
[data-hds-theme="cds-g10"],
175+
[data-hds-theme="cds-g90"],
176+
[data-hds-theme="dark"],
177+
[data-hds-theme="cds-g100"] {
145178
--token-border-radius-x-small: 3px;
146179
--token-border-radius-small: 5px;
147180
--token-border-radius-medium: 6px;

showcase/public/assets/styles/@hashicorp/themed-tokens/with-css-selectors/tokens.css

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,38 @@
9696
--token-typography-font-stack-text: Impact, "Arial Black", "sans serif";
9797
}
9898

99-
:root {
99+
/*
100+
WHY ARE ALL "COMPUTED" CSS VARIABLES REDECLARED IN EACH THEME SCOPE?
101+
102+
CSS custom properties (variables) inherit from parent to child, but when a variable is defined
103+
as a calculation or alias of another variable (e.g., `--alias-value: var(--source-value)`, its value is fixed
104+
at the scope where it is declared—using whatever is visible at that level in the DOM (eg. `:root`).
105+
106+
This block of "common/shared" CSS variables contains a lot of these aliases, so if we only declare these
107+
"computed" variables at `:root` level, their values do not update in themed blocks that override
108+
the "source" variables (e.g., `--source-value`) at a lower scope.
109+
This results in _stale_ variables, where the computed value is always stuck on the `:root` value,
110+
even inside a themed block where the source variable is assigned.
111+
112+
To make sure every themed block/scoped context (eg. `.hds-theme-light`) correctly computes the aliased values,
113+
we redeclare each "computed" variable in every theme scope where its "source" variable is overridden.
114+
*/
115+
116+
:root,
117+
.hds-theme-default,
118+
.hds-theme-light,
119+
.hds-theme-cds-g0,
120+
.hds-theme-cds-g10,
121+
.hds-theme-cds-g90,
122+
.hds-theme-dark,
123+
.hds-theme-cds-g100,
124+
[data-hds-theme="default"],
125+
[data-hds-theme="light"],
126+
[data-hds-theme="cds-g0"],
127+
[data-hds-theme="cds-g10"],
128+
[data-hds-theme="cds-g90"],
129+
[data-hds-theme="dark"],
130+
[data-hds-theme="cds-g100"] {
100131
--token-border-radius-x-small: 3px;
101132
--token-border-radius-small: 5px;
102133
--token-border-radius-medium: 6px;

showcase/public/assets/styles/@hashicorp/themed-tokens/with-prefers-color-scheme/tokens.css

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,28 @@
6666
}
6767
}
6868

69-
:root {
69+
/*
70+
WHY ARE ALL "COMPUTED" CSS VARIABLES REDECLARED IN EACH THEME SCOPE?
71+
72+
CSS custom properties (variables) inherit from parent to child, but when a variable is defined
73+
as a calculation or alias of another variable (e.g., `--alias-value: var(--source-value)`, its value is fixed
74+
at the scope where it is declared—using whatever is visible at that level in the DOM (eg. `:root`).
75+
76+
This block of "common/shared" CSS variables contains a lot of these aliases, so if we only declare these
77+
"computed" variables at `:root` level, their values do not update in themed blocks that override
78+
the "source" variables (e.g., `--source-value`) at a lower scope.
79+
This results in _stale_ variables, where the computed value is always stuck on the `:root` value,
80+
even inside a themed block where the source variable is assigned.
81+
82+
To make sure every themed block/scoped context (eg. `.hds-theme-light`) correctly computes the aliased values,
83+
we redeclare each "computed" variable in every theme scope where its "source" variable is overridden.
84+
*/
85+
86+
:root,
87+
.hds-theme-default,
88+
.hds-theme-system,
89+
[data-hds-theme="default"],
90+
[data-hds-theme="system"] {
7091
--token-border-radius-x-small: 3px;
7192
--token-border-radius-small: 5px;
7293
--token-border-radius-medium: 6px;

0 commit comments

Comments
 (0)