Skip to content

Commit 2d98bcc

Browse files
Rename 'createThemeVars' to 'createThemeContract' (#52)
1 parent 48c4a78 commit 2d98bcc

File tree

10 files changed

+100
-62
lines changed

10 files changed

+100
-62
lines changed

.changeset/thirty-peas-dance.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
'@vanilla-extract/babel-plugin': minor
3+
'@vanilla-extract/css': minor
4+
---
5+
6+
Rename `createThemeVars` to `createThemeContract`
7+
8+
**BREAKING CHANGE**
9+
10+
```diff
11+
import {
12+
- createThemeVars,
13+
+ createThemeContract,
14+
createTheme
15+
} from '@vanilla-extract/css';
16+
17+
-export const vars = createThemeVars({
18+
+export const vars = createThemeContract({
19+
color: {
20+
brand: null
21+
},
22+
font: {
23+
body: null
24+
}
25+
});
26+
```

README.md

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Basically, it’s [“CSS Modules](https://github.com/css-modules/css-modules)-i
4141

4242
import { createTheme, style } from '@vanilla-extract/css';
4343

44-
export const [themeClass, themeVars] = createTheme({
44+
export const [themeClass, vars] = createTheme({
4545
color: {
4646
brand: 'blue'
4747
},
@@ -51,8 +51,8 @@ export const [themeClass, themeVars] = createTheme({
5151
});
5252

5353
export const exampleStyle = style({
54-
backgroundColor: themeVars.color.brand,
55-
fontFamily: themeVars.font.body,
54+
backgroundColor: vars.color.brand,
55+
fontFamily: vars.font.body,
5656
color: 'white',
5757
padding: 10
5858
});
@@ -86,7 +86,7 @@ document.write(`
8686
- [globalStyle](#globalstyle)
8787
- [createTheme](#createtheme)
8888
- [createGlobalTheme](#createglobaltheme)
89-
- [createThemeVars](#createthemevars)
89+
- [createThemeContract](#createthemecontract)
9090
- [assignVars](#assignvars)
9191
- [createVar](#createvar)
9292
- [fallbackVar](#fallbackvar)
@@ -215,10 +215,12 @@ CSS Variables, simple pseudos, selectors and media/feature queries are all suppo
215215

216216
```ts
217217
import { style } from '@vanilla-extract/css';
218+
import { vars } from './vars.css.ts';
218219

219220
export const className = style({
220221
display: 'flex',
221222
vars: {
223+
[vars.localVar]: 'green',
222224
'--global-variable': 'purple'
223225
},
224226
':hover': {
@@ -321,12 +323,14 @@ globalStyle(`${parentClass} > a`, {
321323

322324
### createTheme
323325

324-
Creates a locally scoped theme class and a collection of scoped CSS Variables.
326+
Creates a locally scoped theme class and a theme contract which can be consumed within your styles.
325327

326328
```ts
327-
import { createTheme, style } from '@vanilla-extract/css';
329+
// theme.css.ts
330+
331+
import { createTheme } from '@vanilla-extract/css';
328332

329-
export const [themeClass, themeVars] = createTheme({
333+
export const [themeClass, vars] = createTheme({
330334
color: {
331335
brand: 'blue'
332336
},
@@ -336,12 +340,14 @@ export const [themeClass, themeVars] = createTheme({
336340
});
337341
```
338342

339-
You can create theme variants by passing a collection of theme variables as the first argument to `createTheme`.
343+
You can create theme variants by passing a theme contract as the first argument to `createTheme`.
340344

341345
```ts
342-
import { createTheme, style } from '@vanilla-extract/css';
346+
// themes.css.ts
347+
348+
import { createTheme } from '@vanilla-extract/css';
343349

344-
export const [themeA, themeVars] = createTheme({
350+
export const [themeA, vars] = createTheme({
345351
color: {
346352
brand: 'blue'
347353
},
@@ -350,7 +356,7 @@ export const [themeA, themeVars] = createTheme({
350356
}
351357
});
352358

353-
export const themeB = createTheme(themeVars, {
359+
export const themeB = createTheme(vars, {
354360
color: {
355361
brand: 'pink'
356362
},
@@ -367,9 +373,11 @@ export const themeB = createTheme(themeVars, {
367373
Creates a theme attached to a global selector, but with locally scoped variable names.
368374

369375
```ts
376+
// theme.css.ts
377+
370378
import { createGlobalTheme } from '@vanilla-extract/css';
371379

372-
export const themeVars = createGlobalTheme(':root', {
380+
export const vars = createGlobalTheme(':root', {
373381
color: {
374382
brand: 'blue'
375383
},
@@ -381,19 +389,23 @@ export const themeVars = createGlobalTheme(':root', {
381389

382390
> 💡 All theme variants must provide a value for every variable or it’s a type error.
383391
384-
### createThemeVars
392+
### createThemeContract
393+
394+
Creates a contract for themes to implement.
385395

386-
Creates a collection of CSS Variables without coupling them to a specific theme variant.
396+
**Ensure this function is called within a `.css.ts` context, otherwise variable names will be mismatched between themes.**
387397

388398
> 💡 This is useful if you want to split your themes into different bundles. In this case, your themes would be defined in separate files, but we'll keep this example simple.
389399
390400
```ts
401+
// themes.css.ts
402+
391403
import {
392-
createThemeVars,
404+
createThemeContract,
393405
createTheme
394406
} from '@vanilla-extract/css';
395407

396-
export const themeVars = createThemeVars({
408+
export const vars = createThemeContract({
397409
color: {
398410
brand: null
399411
},
@@ -402,7 +414,7 @@ export const themeVars = createThemeVars({
402414
}
403415
});
404416

405-
export const themeA = createTheme(themeVars, {
417+
export const themeA = createTheme(vars, {
406418
color: {
407419
brand: 'blue'
408420
},
@@ -411,7 +423,7 @@ export const themeA = createTheme(themeVars, {
411423
}
412424
});
413425

414-
export const themeB = createTheme(themeVars, {
426+
export const themeB = createTheme(vars, {
415427
color: {
416428
brand: 'pink'
417429
},
@@ -428,9 +440,9 @@ Assigns a collection of CSS Variables anywhere within a style block.
428440
> 💡 This is useful for creating responsive themes since it can be used within `@media` blocks.
429441
430442
```ts
431-
import { style, createThemeVars, assignVars } from '@vanilla-extract/css';
443+
import { createThemeContract, style, assignVars } from '@vanilla-extract/css';
432444

433-
export const themeVars = createThemeVars({
445+
export const vars = createThemeContract({
434446
space: {
435447
small: null,
436448
medium: null,
@@ -439,14 +451,14 @@ export const themeVars = createThemeVars({
439451
});
440452

441453
export const responsiveSpaceTheme = style({
442-
vars: assignVars(themeVars.space, {
454+
vars: assignVars(vars.space, {
443455
small: '4px',
444456
medium: '8px',
445457
large: '16px'
446458
}),
447459
'@media': {
448460
'screen and (min-width: 1024px)': {
449-
vars: assignVars(themeVars.space, {
461+
vars: assignVars(vars.space, {
450462
small: '8px',
451463
medium: '16px',
452464
large: '32px'
@@ -613,13 +625,13 @@ $ yarn add --dev @vanilla-extract/dynamic
613625

614626
### createInlineTheme
615627

616-
Generates a custom theme at runtime as an inline style object.
628+
Implements a theme contract at runtime as an inline style object.
617629

618630
```ts
619631
import { createInlineTheme } from '@vanilla-extract/dynamic';
620-
import { themeVars, exampleStyle } from './styles.css.ts';
632+
import { vars, exampleStyle } from './styles.css.ts';
621633

622-
const customTheme = createInlineTheme(themeVars, {
634+
const customTheme = createInlineTheme(vars, {
623635
small: '4px',
624636
medium: '8px',
625637
large: '16px'
@@ -634,14 +646,14 @@ document.write(`
634646

635647
### setElementTheme
636648

637-
Sets a collection of CSS Variables on an element.
649+
Implements a theme contract on an element.
638650

639651
```ts
640652
import { setElementTheme } from '@vanilla-extract/dynamic';
641-
import { themeVars } from './styles.css.ts';
653+
import { vars } from './styles.css.ts';
642654

643655
const element = document.getElementById('myElement');
644-
setElementTheme(element, themeVars, {
656+
setElementTheme(element, vars, {
645657
small: '4px',
646658
medium: '8px',
647659
large: '16px'
@@ -656,10 +668,10 @@ Sets a single var on an element.
656668

657669
```ts
658670
import { setElementVar } from '@vanilla-extract/dynamic';
659-
import { themeVars } from './styles.css.ts';
671+
import { vars } from './styles.css.ts';
660672

661673
const element = document.getElementById('myElement');
662-
setElementVar(element, themeVars.color.brand, 'darksalmon');
674+
setElementVar(element, vars.color.brand, 'darksalmon');
663675
```
664676

665677
## Utility functions

docs/treat-migration-guide.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,32 +72,32 @@ module.exports = {
7272
If you only have a single theme, you can keep things simple by using `createGlobalTheme` and targeting `:root`. If you do this, your variables will just work without having to wire anything up to the document.
7373

7474
```ts
75-
// themeVars.css.ts
75+
// vars.css.ts
7676

7777
import { createGlobalTheme } from '@vanilla-extract/css';
7878

79-
export const themeVars = createGlobalTheme(':root', {
79+
export const vars = createGlobalTheme(':root', {
8080
...tokens
8181
});
8282
```
8383

8484
If you have multiple themes, or if you want to avoid the global scope, use the [`createTheme`](https://github.com/seek-oss/vanilla-extract#createtheme) function instead.
8585

8686
```ts
87-
// themeVars.css.ts
87+
// vars.css.ts
8888

8989
import { createTheme } from '@vanilla-extract/css';
9090

91-
export const [themeA, themeVars] = createTheme({
91+
export const [themeA, vars] = createTheme({
9292
...tokens
9393
});
9494

95-
export const themeB = createTheme(themeVars, {
95+
export const themeB = createTheme(vars, {
9696
...tokens
9797
});
9898
```
9999

100-
If you're bundle-splitting your themes, you'll probably want the [`createThemeVars`](https://github.com/seek-oss/vanilla-extract#createthemevars) function.
100+
If you're bundle-splitting your themes, you'll probably want the [`createThemeContract`](https://github.com/seek-oss/vanilla-extract#createthemecontract) function.
101101

102102
## `TreatProvider`
103103

@@ -234,10 +234,10 @@ To get access to variables, we now import theme variables from the `.css.ts` fil
234234
-}))
235235

236236
+import { style } from '@vanilla-extract/css';
237-
+import { themeVars } from '../themeVars.css';
237+
+import { vars } from '../vars.css';
238238
+
239239
+export const className = style({
240-
+ paddingTop: themeVars.space.small
240+
+ paddingTop: vars.space.small
241241
+});
242242
```
243243

@@ -262,10 +262,10 @@ Simple calculations (addition, subtraction, multiplication, division) are covere
262262

263263
+import { style } from '@vanilla-extract/css';
264264
+import { calc } from '@vanilla-extract/css-utils';
265-
+import { themeVars } from '../themeVars.css';
265+
+import { vars } from '../vars.css';
266266
+
267267
+const className = style({
268-
+ marginTop: calc.negate(themeVars.space.small)
268+
+ marginTop: calc.negate(vars.space.small)
269269
+});
270270
```
271271

@@ -285,14 +285,14 @@ export const className = style((theme) => ({
285285
Since this calculation is not yet supported natively in CSS, this lighter background would need to become part of your theme definition. In this case, we'll introduce a new `color` variable called `brandLight`. Notice that in this context we're able to execute arbitrary JavaScript code.
286286

287287
```ts
288-
// themeVars.css.ts
288+
// vars.css.ts
289289

290290
import { createGlobalTheme } from '@vanilla-extract/css';
291291
import { lighten } from 'polished';
292292

293293
const brandColor = 'blue';
294294

295-
export const themeVars = createGlobalTheme(':root', {
295+
export const vars = createGlobalTheme(':root', {
296296
color: {
297297
brand: brandColor,
298298
brandLight: lighten(0.2, brandColor)
@@ -306,13 +306,13 @@ You would then update your styles to use this new CSS Variable instead.
306306
-import { style } from 'treat';
307307
-import { lighten } from 'polished';
308308
+import { style } from '@vanilla-extract/css';
309-
+import { themeVars } from '../themeVars.css';
309+
+import { vars } from '../vars.css';
310310

311311
-export const className = style(theme => ({
312312
- background: lighten(0.2, theme.color.brand)
313313
-}));
314314
+export const className = style({
315-
+ background: themeVars.color.brandLight
315+
+ background: vars.color.brandLight
316316
+});
317317
```
318318

packages/babel-plugin/src/index.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -370,34 +370,34 @@ describe('babel plugin', () => {
370370
const source = `
371371
import { createGlobalTheme } from '@vanilla-extract/css';
372372
373-
const themeVars = createGlobalTheme(':root', { foo: 'bar' });
373+
const vars = createGlobalTheme(':root', { foo: 'bar' });
374374
`;
375375

376376
expect(transform(source)).toMatchInlineSnapshot(`
377377
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
378378
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
379379
import { createGlobalTheme } from '@vanilla-extract/css';
380-
const themeVars = createGlobalTheme(':root', {
380+
const vars = createGlobalTheme(':root', {
381381
foo: 'bar'
382382
});
383383
endFileScope()"
384384
`);
385385
});
386386

387-
it('should handle createThemeVars', () => {
387+
it('should handle createThemeContract', () => {
388388
const source = `
389-
import { createThemeVars } from '@vanilla-extract/css';
389+
import { createThemeContract } from '@vanilla-extract/css';
390390
391-
const themeVars = createThemeVars({
391+
const vars = createThemeContract({
392392
foo: 'bar'
393393
});
394394
`;
395395

396396
expect(transform(source)).toMatchInlineSnapshot(`
397397
"import { setFileScope, endFileScope } from '@vanilla-extract/css/fileScope';
398398
setFileScope(\\"src/dir/mockFilename.css.ts\\", \\"@vanilla-extract/babel-plugin\\");
399-
import { createThemeVars } from '@vanilla-extract/css';
400-
const themeVars = createThemeVars({
399+
import { createThemeContract } from '@vanilla-extract/css';
400+
const vars = createThemeContract({
401401
foo: 'bar'
402402
});
403403
endFileScope()"

packages/babel-plugin/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const styleFunctions = [
3838
>),
3939
'globalStyle',
4040
'createGlobalTheme',
41-
'createThemeVars',
41+
'createThemeContract',
4242
'globalFontFace',
4343
'globalKeyframes',
4444
];

0 commit comments

Comments
 (0)