Skip to content

Commit c85d76f

Browse files
committed
chore: Remove useCssVars parameter and make the default behavior
1 parent f65707f commit c85d76f

File tree

18 files changed

+227
-275
lines changed

18 files changed

+227
-275
lines changed

src/__fixtures__/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const emptyTheme: Theme = {
9090

9191
export const rootTheme: Theme = {
9292
id: 'root',
93-
selector: ':root',
93+
selector: 'body',
9494
tokens: {
9595
fontFamilyBase: '"Helvetica Neue", Arial, sans-serif',
9696
fontFamilyBody: '{fontFamilyBase}',

src/browser/__tests__/__snapshots__/index.test.ts.snap

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ exports[`applyTheme > with baseThemeId > attaches one style node containing over
3131
`;
3232

3333
exports[`applyTheme > with secondary theme > attaches one style node containing override 1`] = `
34-
":root:root{
34+
"body:not(#\\9){
3535
--black-css:black;
3636
--grey-css:grey;
3737
--brown-css:brown;
@@ -59,7 +59,7 @@ exports[`applyTheme > with secondary theme > attaches one style node containing
5959
`;
6060

6161
exports[`applyTheme > with targetDocument > attaches one style node containing override on the target document 1`] = `
62-
":root:root{
62+
"body:not(#\\9){
6363
--black-css:black;
6464
--grey-css:grey;
6565
--brown-css:brown;
@@ -87,7 +87,7 @@ exports[`applyTheme > with targetDocument > attaches one style node containing o
8787
`;
8888

8989
exports[`applyTheme > without secondary theme > attaches one style node containing override 1`] = `
90-
":root:root{
90+
"body:not(#\\9){
9191
--black-css:black;
9292
--grey-css:grey;
9393
--brown-css:brown;
@@ -116,14 +116,17 @@ exports[`applyTheme > without secondary theme > attaches one style node containi
116116

117117
exports[`generateThemeStylesheet > with baseThemeId > creates override styles 1`] = `
118118
".secondary-theme.secondary-theme:not(#\\9){
119+
--black-css:purple;
120+
--grey-css:grey;
121+
--brown-css:black;
119122
--shadow-css:yellow;
120123
--buttonShadow-css:red;
121124
--boxShadow-css:green;
122125
--lineShadow-css:pink;
123126
}
124127
@media not print {.dark.dark.secondary-theme:not(#\\9){
125128
--shadow-css:orange;
126-
--boxShadow-css:black;
129+
--boxShadow-css:var(--brown-css);
127130
}}
128131
.secondary-theme.secondary-theme .navigation:not(#\\9){
129132
--shadow-css:pink;
@@ -132,79 +135,74 @@ exports[`generateThemeStylesheet > with baseThemeId > creates override styles 1`
132135
--shadow-css:pink;
133136
}
134137
@media not print {.dark.dark.secondary-theme .navigation:not(#\\9){
135-
--shadow-css:grey;
138+
--shadow-css:var(--grey-css);
136139
--buttonShadow-css:green;
137140
}}
138141
@media not print {.dark.dark.navigation.secondary-theme:not(#\\9){
139-
--shadow-css:grey;
142+
--shadow-css:var(--grey-css);
140143
--buttonShadow-css:green;
141144
}}"
142145
`;
143146

144-
exports[`generateThemeStylesheet > with reference tokens and useCssVars > creates override styles with CSS variables when useCssVars is enabled 1`] = `
145-
":root:root{
147+
exports[`generateThemeStylesheet > with reference tokens > creates override styles with CSS variables 1`] = `
148+
"body:not(#\\9){
146149
--colorPrimary600-css:#ff6600;
147150
--colorPrimary700-css:#692dc9;
148151
}"
149152
`;
150153

151-
exports[`generateThemeStylesheet > with reference tokens and useCssVars > creates override styles without CSS variables by default 1`] = `
152-
":root:root{
153-
--colorPrimary600-css:#ff6600;
154-
--colorPrimary700-css:#692dc9;
155-
--colorButtonPrimary-css:#ff6600;
156-
--colorButtonSecondary-css:#692dc9;
157-
--colorTextPrimary-css:#ff6600;
158-
--colorBackgroundPrimary-css:#692dc9;
159-
}"
160-
`;
161-
162154
exports[`generateThemeStylesheet > with secondary theme > creates override styles 1`] = `
163-
":root:root{
155+
"body:not(#\\9){
156+
--black-css:black;
157+
--grey-css:grey;
158+
--brown-css:brown;
164159
--shadow-css:yellow;
165160
--buttonShadow-css:red;
166161
--boxShadow-css:green;
167162
--lineShadow-css:pink;
168163
}
169164
@media not print {.dark.dark:not(#\\9){
170165
--shadow-css:orange;
171-
--boxShadow-css:brown;
166+
--boxShadow-css:var(--brown-css);
172167
}}
173168
.navigation.navigation:not(#\\9){
174169
--shadow-css:pink;
175170
--boxShadow-css:purple;
176171
}
177172
@media not print {.dark.dark .navigation:not(#\\9){
178-
--shadow-css:brown;
173+
--shadow-css:var(--brown-css);
179174
--buttonShadow-css:green;
180175
}}
181176
@media not print {.dark.dark.navigation:not(#\\9){
182-
--shadow-css:brown;
177+
--shadow-css:var(--brown-css);
183178
--buttonShadow-css:green;
184179
}}"
185180
`;
186181

187182
exports[`generateThemeStylesheet > without secondary theme > creates override styles 1`] = `
188-
":root:root{
183+
"body:not(#\\9){
184+
--black-css:black;
185+
--grey-css:grey;
186+
--brown-css:brown;
189187
--shadow-css:yellow;
190188
--buttonShadow-css:red;
191189
--boxShadow-css:green;
192190
--lineShadow-css:pink;
193191
}
194192
@media not print {.dark.dark:not(#\\9){
195193
--shadow-css:orange;
196-
--boxShadow-css:brown;
194+
--boxShadow-css:var(--brown-css);
197195
}}
198196
.navigation.navigation:not(#\\9){
199197
--shadow-css:pink;
200198
--boxShadow-css:purple;
201199
}
202200
@media not print {.dark.dark .navigation:not(#\\9){
203-
--shadow-css:brown;
201+
--shadow-css:var(--brown-css);
204202
--buttonShadow-css:green;
205203
}}
206204
@media not print {.dark.dark.navigation:not(#\\9){
207-
--shadow-css:brown;
205+
--shadow-css:var(--brown-css);
208206
--buttonShadow-css:green;
209207
}}"
210208
`;

src/browser/__tests__/index.test.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { Theme, ThemePreset, Override } from '../../shared/theme';
1717

1818
const allStyleNodes = (targetDocument: Document = document) => targetDocument.head.querySelectorAll('style');
1919

20-
// Create a theme with reference tokens to test useCssVars
20+
// Create a theme with reference tokens to test CSS variable generation
2121
const themeWithReferenceTokens: Theme = {
2222
...rootTheme,
2323
referenceTokens: {
@@ -201,25 +201,15 @@ describe('generateThemeStylesheet', () => {
201201
});
202202
});
203203

204-
describe('with reference tokens and useCssVars', () => {
205-
test('creates override styles without CSS variables by default', () => {
204+
describe('with reference tokens', () => {
205+
test('creates override styles with CSS variables', () => {
206206
const styles = generateThemeStylesheet({
207207
override: overrideWithReferenceTokens,
208208
preset: presetWithReferenceTokens,
209209
});
210210

211211
expect(styles).toMatchSnapshot();
212212
});
213-
214-
test('creates override styles with CSS variables when useCssVars is enabled', () => {
215-
const styles = generateThemeStylesheet({
216-
override: overrideWithReferenceTokens,
217-
preset: presetWithReferenceTokens,
218-
useCssVars: true,
219-
});
220-
221-
expect(styles).toMatchSnapshot();
222-
});
223213
});
224214

225215
describe('performance: seed vs explicit palette', () => {

src/browser/index.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@ export interface GenerateThemeStylesheetParams {
1010
override: Override;
1111
preset: ThemePreset;
1212
baseThemeId?: string;
13-
useCssVars?: boolean;
1413
}
1514

1615
export function generateThemeStylesheet(params: GenerateThemeStylesheetParams): string {
17-
const { override, preset, baseThemeId, useCssVars } = params;
16+
const { override, preset, baseThemeId } = params;
1817
const availableContexts = getContexts(preset);
1918
const validated = validateOverride(override, preset.themeable, availableContexts);
2019
const theme = getThemeFromPreset(preset, baseThemeId);
@@ -23,8 +22,7 @@ export function generateThemeStylesheet(params: GenerateThemeStylesheetParams):
2322
theme,
2423
validated,
2524
preset.propertiesMap,
26-
createMultiThemeCustomizer(preset.theme.selector),
27-
useCssVars
25+
createMultiThemeCustomizer(preset.theme.selector)
2826
);
2927
}
3028

@@ -33,7 +31,6 @@ export interface ApplyThemeParams {
3331
preset: ThemePreset;
3432
baseThemeId?: string;
3533
targetDocument?: Document;
36-
useCssVars?: boolean;
3734
}
3835

3936
export interface ApplyThemeResult {
@@ -42,7 +39,7 @@ export interface ApplyThemeResult {
4239

4340
export function applyTheme(params: ApplyThemeParams): ApplyThemeResult {
4441
const { targetDocument } = params;
45-
const content = generateThemeStylesheet({ ...params, useCssVars: true });
42+
const content = generateThemeStylesheet(params);
4643
const nonce = getNonce(targetDocument);
4744
const styleNode = createStyleNode(content, nonce);
4845

src/build/__tests__/__fixtures__/template/internal/generated/theming/with-secondary-theme.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
export var preset = {
44
theme: {
55
id: 'theme',
6-
selector: ':root',
6+
selector: 'body',
77
tokens: {
88
fontFamilyBase: "'Amazon Ember', 'Helvetica Neue', Roboto, Arial, sans-serif",
99
colorOrange500: '#ec7211',

src/build/__tests__/__snapshots__/public-secondary.test.ts.snap

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`Build-time theming of main theme with matching baseThemeId generates the correct css files 1`] = `
4-
":root {
4+
"body {
55
--font-family-base-rpbu7d:"Amazon Ember", "Helvetica Neue", Roboto, Arial, sans-serif;
66
--color-orange-500-25sm1g:#ec7211;
77
--color-orange-700-2auf4j:#dd6b10;
@@ -25,15 +25,17 @@ exports[`Build-time theming of main theme with matching baseThemeId generates th
2525
--color-orange-500-25sm1g:#ec72aa;
2626
--color-orange-700-2auf4j:#dd6baa;
2727
--color-background-button-primary-default-cqohzr:var(--color-orange-500-25sm1g);
28+
--color-background-button-primary-active-h8she0:var(--color-orange-700-2auf4j);
2829
}
2930
3031
.dark-mode.secondary:not(#\\9) {
3132
--color-background-button-primary-default-cqohzr:var(--color-orange-700-2auf4j);
33+
--color-background-button-primary-active-h8she0:var(--color-orange-500-25sm1g);
3234
}"
3335
`;
3436

3537
exports[`Build-time theming of secondary theme generates the correct css files 1`] = `
36-
":root {
38+
"body {
3739
--font-family-base-rpbu7d:"Amazon Ember", "Helvetica Neue", Roboto, Arial, sans-serif;
3840
--color-orange-500-25sm1g:#ec7211;
3941
--color-orange-700-2auf4j:#dd6b10;
@@ -56,9 +58,11 @@ exports[`Build-time theming of secondary theme generates the correct css files 1
5658
--color-orange-500-25sm1g:#ec72aa;
5759
--color-orange-700-2auf4j:#dd6baa;
5860
--color-background-button-primary-default-l567s3:#aaaaaa;
61+
--color-background-button-primary-active-h8she0:var(--color-orange-700-2auf4j);
5962
}
6063
6164
.dark-mode.secondary:not(#\\9) {
6265
--color-background-button-primary-default-l567s3:#bbbbbb;
66+
--color-background-button-primary-active-h8she0:var(--color-orange-500-25sm1g);
6367
}"
6468
`;

src/build/__tests__/css-vars.test.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { join } from 'path';
1111

1212
const testTheme: Theme = {
1313
id: 'test',
14-
selector: ':root',
14+
selector: 'body',
1515
tokens: {
1616
colorPrimary: '#0073bb',
1717
colorBackground: '{colorPrimary}',
@@ -55,7 +55,6 @@ test('CSS variable optimization works without errors', async () => {
5555
},
5656
componentsOutputDir: tempDir,
5757
scssDir: tempDir,
58-
useCssVars: true,
5958
skip: ['preset', 'design-tokens'],
6059
});
6160

@@ -89,7 +88,6 @@ test('generateReferenceTokenDefaults creates CSS variable declarations', () => {
8988

9089
test('resolveTheme with CSS variables returns CSS var() for reference tokens', () => {
9190
const resolved = resolveTheme(testTheme, undefined, {
92-
useCssVars: true,
9391
propertiesMap,
9492
});
9593

@@ -118,7 +116,6 @@ test('resolveContext with CSS variables preserves var() references', () => {
118116
};
119117

120118
const resolved = resolveContext(testTheme, context, undefined, undefined, {
121-
useCssVars: true,
122119
propertiesMap,
123120
});
124121

@@ -127,7 +124,7 @@ test('resolveContext with CSS variables preserves var() references', () => {
127124
});
128125

129126
test('createBuildDeclarations includes reference tokens when useCssVars enabled', () => {
130-
const css = createBuildDeclarations(testTheme, [], propertiesMap, (selector) => selector, ['colorPrimary'], true);
127+
const css = createBuildDeclarations(testTheme, [], propertiesMap, (selector) => selector, ['colorPrimary']);
131128

132129
expect(css).toContain('--color-primary-500');
133130
expect(css).toContain('--color-neutral-900');
@@ -152,14 +149,10 @@ test('createBuildDeclarations with secondary theme generates reference token CSS
152149
},
153150
};
154151

155-
const css = createBuildDeclarations(
156-
testTheme,
157-
[secondaryTheme],
158-
propertiesMap,
159-
(selector) => selector,
160-
['colorPrimary', 'colorError100'],
161-
true
162-
);
152+
const css = createBuildDeclarations(testTheme, [secondaryTheme], propertiesMap, (selector) => selector, [
153+
'colorPrimary',
154+
'colorError100',
155+
]);
163156

164157
// Should contain reference tokens from both primary and secondary themes
165158
expect(css).toContain('--color-primary-500');

src/build/inline-stylesheets.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@ export function getInlineStylesheets(
1818
resolution: SpecificResolution,
1919
variablesMap: Record<string, string>,
2020
propertiesMap: Record<string, string>,
21-
neededTokens: string[],
22-
useCssVars?: boolean
21+
neededTokens: string[]
2322
): InlineStylesheet[] {
2423
const declarations = createBuildDeclarations(
2524
primary,
2625
secondary,
2726
propertiesMap,
2827
(selector) => markGlobal(selector),
29-
neededTokens,
30-
useCssVars
28+
neededTokens
3129
);
3230

3331
const declaration = {

src/build/internal.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ export interface BuildThemedComponentsInternalParams {
4040
jsonSchema?: boolean;
4141
/** Fail the build when SASS deprecation warning occurs **/
4242
failOnDeprecations?: boolean;
43-
/** Use CSS variables instead of resolved values for better runtime theme swapping */
44-
useCssVars?: boolean;
4543
}
4644
/**
4745
* Builds themed components and optionally design tokens, if not skipped.
@@ -73,14 +71,13 @@ export async function buildThemedComponentsInternal(params: BuildThemedComponent
7371
descriptions = {},
7472
jsonSchema = false,
7573
failOnDeprecations,
76-
useCssVars = true,
7774
} = params;
7875

7976
if (!skip.includes('design-tokens') && !designTokensOutputDir) {
8077
throw new Error('designTokensOutputDir needs to be specified if not skipped');
8178
}
8279

83-
const neededTokens = findNeededTokens(scssDir, variablesMap, exposed, [primary, ...secondary], useCssVars);
80+
const neededTokens = findNeededTokens(scssDir, variablesMap, exposed);
8481

8582
const resolution = resolveTheme(primary);
8683
const defaults = reduce(resolution, primary, defaultsReducer());
@@ -89,7 +86,7 @@ export async function buildThemedComponentsInternal(params: BuildThemedComponent
8986
const styleTask = buildStyles(
9087
scssDir,
9188
componentsOutputDir,
92-
getInlineStylesheets(primary, secondary, defaults, variablesMap, propertiesMap, neededTokens, useCssVars),
89+
getInlineStylesheets(primary, secondary, defaults, variablesMap, propertiesMap, neededTokens),
9390
{ failOnDeprecations }
9491
);
9592
const internalTokensTask = createInternalTokenFiles(defaults, propertiesMap, componentsOutputDir);

0 commit comments

Comments
 (0)