@vanilla-extract/vite-plugin 3.5.0 → 3.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -26,6 +26,12 @@ Basically, it’s [“CSS Modules](https://github.com/css-modules/css-modules)-i
26
26
 
27
27
  🙈   Optional API for dynamic runtime theming.
28
28
 
29
+ ---
30
+
31
+ 🌐 [Check out the documentation site for setup guides, examples and API docs.](https://vanilla-extract.style)
32
+
33
+
34
+
29
35
  ---
30
36
 
31
37
  🖥   [Try it out for yourself in CodeSandbox.](https://codesandbox.io/s/github/seek-oss/vanilla-extract/tree/master/examples/webpack-react?file=/src/App.css.ts)
@@ -56,7 +62,7 @@ export const exampleStyle = style({
56
62
  });
57
63
  ```
58
64
 
59
- > 💡 Once you've [configured your build tooling,](#setup) these `.css.ts` files will be evaluated at build time. None of the code in these files will be included in your final bundle. Think of it as using TypeScript as your preprocessor instead of Sass, Less, etc.
65
+ > 💡 Once you've [configured your build tooling,](https://vanilla-extract.style/documentation/getting-started/) these `.css.ts` files will be evaluated at build time. None of the code in these files will be included in your final bundle. Think of it as using TypeScript as your preprocessor instead of Sass, Less, etc.
60
66
 
61
67
  **Then consume them in your markup.**
62
68
 
@@ -78,1149 +84,6 @@ Want to work at a higher level while maximising style re-use? Check out 🍨 [S
78
84
 
79
85
  ---
80
86
 
81
- - [Setup](#setup)
82
- - [webpack](#webpack)
83
- - [esbuild](#esbuild)
84
- - [Vite](#vite)
85
- - [Next.js](#nextjs)
86
- - [Gatsby](#gatsby)
87
- - [Rollup](#rollup)
88
- - [Test environments](#test-environments)
89
- - [Configuration](#configuration)
90
- - [identifiers](#identifiers)
91
- - [esbuildOptions](#esbuildoptions)
92
- - [Styling API](#styling-api)
93
- - [style](#style)
94
- - [styleVariants](#stylevariants)
95
- - [globalStyle](#globalstyle)
96
- - [createTheme](#createtheme)
97
- - [createGlobalTheme](#createglobaltheme)
98
- - [createThemeContract](#createthemecontract)
99
- - [createGlobalThemeContract](#createglobalthemecontract)
100
- - [assignVars](#assignvars)
101
- - [createVar](#createvar)
102
- - [fallbackVar](#fallbackvar)
103
- - [fontFace](#fontface)
104
- - [globalFontFace](#globalfontface)
105
- - [keyframes](#keyframes)
106
- - [globalKeyframes](#globalkeyframes)
107
- - [Recipes API](#recipes-api)
108
- - [recipe](#recipe)
109
- - [Dynamic API](#dynamic-api)
110
- - [assignInlineVars](#assigninlinevars)
111
- - [setElementVars](#setelementvars)
112
- - [Utility functions](#utility-functions)
113
- - [calc](#calc)
114
- - [Thanks](#thanks)
115
- - [License](#license)
116
-
117
- ---
118
-
119
- ## Setup
120
-
121
- There are currently a few integrations to choose from.
122
-
123
- ### webpack
124
-
125
- 1. Install the dependencies.
126
-
127
- ```bash
128
- npm install @vanilla-extract/css @vanilla-extract/webpack-plugin
129
- ```
130
-
131
- 2. Add the [webpack](https://webpack.js.org) plugin.
132
-
133
- > 💡 This plugin accepts an optional [configuration object](#configuration).
134
-
135
- ```js
136
- const { VanillaExtractPlugin } = require('@vanilla-extract/webpack-plugin');
137
-
138
- module.exports = {
139
- plugins: [new VanillaExtractPlugin()],
140
- };
141
- ```
142
-
143
- <details>
144
- <summary>You'll need to ensure you're handling CSS files in your webpack config.</summary>
145
-
146
- <br/>
147
- For example:
148
-
149
- ```js
150
- const { VanillaExtractPlugin } = require('@vanilla-extract/webpack-plugin');
151
- const MiniCssExtractPlugin = require('mini-css-extract-plugin');
152
-
153
- module.exports = {
154
- plugins: [
155
- new VanillaExtractPlugin(),
156
- new MiniCssExtractPlugin()
157
- ],
158
- module: {
159
- rules: [
160
- {
161
- test: /\.vanilla\.css$/i, // Targets only CSS files generated by vanilla-extract
162
- use: [
163
- MiniCssExtractPlugin.loader,
164
- {
165
- loader: require.resolve('css-loader'),
166
- options: {
167
- url: false // Required as image imports should be handled via JS/TS import statements
168
- }
169
- }
170
- ]
171
- }
172
- ]
173
- }
174
- };
175
- ```
176
- </details>
177
-
178
- 3. If you'd like automatic debuggable identifiers, you can add the [Babel](https://babeljs.io) plugin.
179
-
180
- ```bash
181
- $ npm install @vanilla-extract/babel-plugin
182
- ```
183
-
184
- ```json
185
- {
186
- "plugins": ["@vanilla-extract/babel-plugin"]
187
- }
188
- ```
189
-
190
- ### esbuild
191
-
192
- 1. Install the dependencies.
193
-
194
- ```bash
195
- npm install @vanilla-extract/css @vanilla-extract/esbuild-plugin
196
- ```
197
-
198
- 2. Add the [esbuild](https://esbuild.github.io/) plugin to your build script.
199
-
200
- > 💡 This plugin accepts an optional [configuration object](#configuration).
201
-
202
- ```js
203
- const { vanillaExtractPlugin } = require('@vanilla-extract/esbuild-plugin');
204
-
205
- require('esbuild').build({
206
- entryPoints: ['app.ts'],
207
- bundle: true,
208
- plugins: [vanillaExtractPlugin()],
209
- outfile: 'out.js',
210
- }).catch(() => process.exit(1))
211
- ```
212
-
213
- > Please note: There are currently no automatic readable class names during development. However, you can still manually provide a debug ID as the last argument to functions that generate scoped styles, e.g. `export const className = style({ ... }, 'className');`
214
-
215
- 3. Process CSS
216
-
217
- As [esbuild](https://esbuild.github.io/) currently doesn't have a way to process the CSS generated by vanilla-extract, you can optionally use the `processCss` option.
218
-
219
- For example, to run autoprefixer over the generated CSS.
220
-
221
- ```js
222
- const {
223
- vanillaExtractPlugin
224
- } = require('@vanilla-extract/esbuild-plugin');
225
- const postcss = require('postcss');
226
- const autoprefixer = require('autoprefixer');
227
-
228
- async function processCss(css) {
229
- const result = await postcss([autoprefixer]).process(
230
- css,
231
- {
232
- from: undefined /* suppress source map warning */
233
- }
234
- );
235
-
236
- return result.css;
237
- }
238
-
239
- require('esbuild')
240
- .build({
241
- entryPoints: ['app.ts'],
242
- bundle: true,
243
- plugins: [
244
- vanillaExtractPlugin({
245
- processCss
246
- })
247
- ],
248
- outfile: 'out.js'
249
- })
250
- .catch(() => process.exit(1));
251
- ```
252
-
253
- ### Vite
254
-
255
- 1. Install the dependencies.
256
-
257
- ```bash
258
- npm install @vanilla-extract/css @vanilla-extract/vite-plugin
259
- ```
260
-
261
- 2. Add the [Vite](https://vitejs.dev/) plugin to your Vite config.
262
-
263
- > 💡 This plugin accepts an optional [configuration object](#configuration).
264
-
265
- ```js
266
- import { vanillaExtractPlugin } from '@vanilla-extract/vite-plugin';
267
-
268
- // vite.config.js
269
- export default {
270
- plugins: [vanillaExtractPlugin()]
271
- }
272
- ```
273
-
274
- > Please note: There are currently no automatic readable class names during development. However, you can still manually provide a debug ID as the last argument to functions that generate scoped styles, e.g. `export const className = style({ ... }, 'className');`
275
-
276
- ### Next.js
277
-
278
- 1. Install the dependencies.
279
-
280
- ```bash
281
- npm install @vanilla-extract/css @vanilla-extract/babel-plugin @vanilla-extract/next-plugin
282
- ```
283
-
284
- 2. If you don't have a `next.config.js` file in the root of your project, create one. Add the [Next.js](https://nextjs.org) plugin to your `next.config.js` file.
285
-
286
- > 💡 This plugin accepts an optional [configuration object](#configuration).
287
-
288
- ```js
289
- const {
290
- createVanillaExtractPlugin
291
- } = require('@vanilla-extract/next-plugin');
292
- const withVanillaExtract = createVanillaExtractPlugin();
293
-
294
- /** @type {import('next').NextConfig} */
295
- const nextConfig = {};
296
-
297
- module.exports = withVanillaExtract(nextConfig);
298
- ```
299
-
300
- If required, this plugin can be composed with other plugins.
301
-
302
- ```js
303
- const {
304
- createVanillaExtractPlugin
305
- } = require('@vanilla-extract/next-plugin');
306
- const withVanillaExtract = createVanillaExtractPlugin();
307
-
308
- const withMDX = require('@next/mdx')({
309
- extension: /\.mdx$/
310
- });
311
-
312
- /** @type {import('next').NextConfig} */
313
- const nextConfig = {};
314
-
315
- module.exports = withVanillaExtract(withMDX(nextConfig));
316
- ```
317
-
318
- 3. (Optional) If you want to automatically generate debug IDs during development, you can add the [Babel](https://babeljs.io) plugin. Note that this step will cause Next.js to switch from [SWC](https://github.com/swc-project/swc) to Babel, increasing build times. This may or may not be an issue depending on the size of your project.
319
-
320
- > Note: While optional for Next.js, the Babel plugin is still required when trying to run `.css.ts` files in Node for unit testing since the files are no longer being processed by a bundler.
321
-
322
- If you don't have a `.babelrc` file in the root of your project, create one. Add the Babel plugin to your `.babelrc` file, ensuring that you're also including `"next/babel"` in your `presets` array.
323
-
324
- ```json
325
- {
326
- "presets": ["next/babel"],
327
- "plugins": ["@vanilla-extract/babel-plugin"]
328
- }
329
- ```
330
-
331
- ### Gatsby
332
-
333
- To add to your [Gatsby](https://www.gatsbyjs.com) site, use the [gatsby-plugin-vanilla-extract](https://github.com/gatsby-uc/plugins/tree/main/packages/gatsby-plugin-vanilla-extract) plugin.
334
-
335
- ### Rollup
336
-
337
- > Note: This option is useful for library development but not suitable for application bundles.
338
- > Rollup has no built-in CSS bundling, so this plugin just outputs styles as individual CSS assets.
339
- > For applications we instead recommend to use Vite
340
- > (which itself uses Rollup under the hood but comes with its own CSS bundling).
341
-
342
- 1. Install the dependencies.
343
-
344
- ```bash
345
- npm install @vanilla-extract/css @vanilla-extract/rollup-plugin
346
- ```
347
-
348
- 2. Add the [Rollup](https://rollupjs.org/) plugin to your Rollup config.
349
-
350
- > 💡 This plugin accepts an optional [configuration object](#configuration).
351
-
352
- ```js
353
- import { vanillaExtractPlugin } from '@vanilla-extract/rollup-plugin';
354
-
355
- // rollup.config.js
356
- export default {
357
- plugins: [vanillaExtractPlugin()]
358
- }
359
- ```
360
-
361
- ### Test environments
362
-
363
- 1. Install the dependencies.
364
-
365
- ```bash
366
- $ npm install @vanilla-extract/babel-plugin
367
- ```
368
-
369
- 2. Add the [Babel](https://babeljs.io) plugin.
370
-
371
- ```json
372
- {
373
- "plugins": ["@vanilla-extract/babel-plugin"]
374
- }
375
- ```
376
-
377
- 3. Disable runtime styles (Optional)
378
-
379
- In testing environments (like `jsdom`) vanilla-extract will create and insert styles. While this is often desirable, it can be a major slowdown in your tests. If your tests don’t require styles to be available, the `disableRuntimeStyles` import will disable all style creation.
380
-
381
- ```ts
382
- // setupTests.ts
383
- import '@vanilla-extract/css/disableRuntimeStyles';
384
- ```
385
-
386
- ### Configuration
387
-
388
- #### identifiers
389
-
390
- Different formatting of identifiers (e.g. class names, keyframes, CSS Vars, etc) can be configured by selecting from the following options:
391
-
392
- - `short` identifiers are a 7+ character hash. e.g. `hnw5tz3`
393
- - `debug` identifiers contain human readable prefixes representing the owning filename and a potential rule level debug name. e.g. `myfile_mystyle_hnw5tz3`
394
-
395
- Each integration will set a default value based on the configuration options passed to the bundler.
396
-
397
- ### esbuildOptions
398
- > Only for `esbuild`, `vite` and `rollup` plugins
399
-
400
- esbuild is used internally to compile `.css.ts` files before evaluating them to extract styles. You can pass additional options here to customize that process.
401
- Accepts a subset of esbuild build options (`plugins`, `external`, `define` and `loader`), see https://esbuild.github.io/api/#build-api.
402
-
403
- ---
404
-
405
- ## Styling API
406
-
407
- > 🍬 If you're a [treat](https://seek-oss.github.io/treat) user, check out our [migration guide.](./docs/treat-migration-guide.md)
408
-
409
- ### style
410
-
411
- Creates styles attached to a locally scoped class name.
412
-
413
- ```ts
414
- import { style } from '@vanilla-extract/css';
415
-
416
- export const className = style({
417
- display: 'flex'
418
- });
419
- ```
420
-
421
- CSS Variables, simple pseudos, selectors and media/feature queries are all supported.
422
-
423
- ```ts
424
- import { style } from '@vanilla-extract/css';
425
- import { vars } from './vars.css.ts';
426
-
427
- export const className = style({
428
- display: 'flex',
429
- vars: {
430
- [vars.localVar]: 'green',
431
- '--global-variable': 'purple'
432
- },
433
- ':hover': {
434
- color: 'red'
435
- },
436
- selectors: {
437
- '&:nth-child(2n)': {
438
- background: '#fafafa'
439
- }
440
- },
441
- '@media': {
442
- 'screen and (min-width: 768px)': {
443
- padding: 10
444
- }
445
- },
446
- '@supports': {
447
- '(display: grid)': {
448
- display: 'grid'
449
- }
450
- }
451
- });
452
- ```
453
-
454
- Selectors can also contain references to other scoped class names.
455
-
456
- ```ts
457
- import { style } from '@vanilla-extract/css';
458
-
459
- export const parentClass = style({});
460
-
461
- export const childClass = style({
462
- selectors: {
463
- [`${parentClass}:focus &`]: {
464
- background: '#fafafa'
465
- }
466
- },
467
- });
468
- ```
469
-
470
- > 💡 To improve maintainability, each style block can only target a single element. To enforce this, all selectors must target the “&” character which is a reference to the current element.
471
- >
472
- > For example, `'&:hover:not(:active)'` and `` [`${parentClass} &`] `` are considered valid, while `'& a[href]'` and `` [`& ${childClass}`] `` are not.
473
- >
474
- > If you want to target another scoped class then it should be defined within the style block of that class instead.
475
- >
476
- > For example, `` [`& ${childClass}`] `` is invalid since it doesn’t target “&”, so it should instead be defined in the style block for `childClass`.
477
- >
478
- > If you want to globally target child nodes within the current element (e.g. `'& a[href]'`), you should use [`globalStyle`](#globalstyle) instead.
479
-
480
- For fallback styles you may simply pass an array of properties instead of a single prop.
481
-
482
- ```ts
483
- export const exampleStyle = style({
484
- // in Firefox and IE the "overflow: overlay" will be ignored and the "overflow: auto" will be applied
485
- overflow: ['auto', 'overlay'],
486
- });
487
- ```
488
-
489
- Multiple styles can be composed into a single rule by providing an array of styles.
490
-
491
- ```ts
492
- import { style } from '@vanilla-extract/css';
493
-
494
- const base = style({ padding: 12 });
495
-
496
- export const primary = style([
497
- base,
498
- { background: 'blue' }
499
- ]);
500
-
501
- export const secondary = style([
502
- base,
503
- { background: 'aqua' }
504
- ]);
505
- ```
506
-
507
- When composed styles are used in selectors, they are assigned an additional class if required so they can be uniquely identified. When selectors are processed internally, the composed classes are removed, only leaving behind the unique identifier classes. This allows you to treat them as if they were a single class within vanilla-extract selectors.
508
-
509
- ```ts
510
- import {
511
- style,
512
- globalStyle,
513
- } from '@vanilla-extract/css';
514
-
515
- const background = style({ background: 'mintcream' });
516
- const padding = style({ padding: 12 });
517
-
518
- export const container = style([background, padding]);
519
-
520
- globalStyle(`${container} *`, {
521
- boxSizing: 'border-box'
522
- });
523
- ```
524
-
525
- ### styleVariants
526
-
527
- Creates a collection of named style variants.
528
-
529
- ```ts
530
- import { styleVariants } from '@vanilla-extract/css';
531
-
532
- export const variant = styleVariants({
533
- primary: { background: 'blue' },
534
- secondary: { background: 'aqua' },
535
- });
536
- ```
537
-
538
- > 💡 This is useful for mapping component props to styles, e.g. `<button className={styles.variant[props.variant]}>`
539
-
540
- Multiple styles can be composed into a single rule by providing an array of styles.
541
-
542
- ```ts
543
- import { styleVariants } from '@vanilla-extract/css';
544
-
545
- const base = style({ padding: 12 });
546
-
547
- export const variant = styleVariants({
548
- primary: [base, { background: 'blue' }],
549
- secondary: [base, { background: 'aqua' }],
550
- });
551
- ```
552
-
553
- You can also transform the values by providing a map function as the second argument.
554
-
555
- ```ts
556
- import { styleVariants } from '@vanilla-extract/css';
557
-
558
- const base = style({ padding: 12 });
559
-
560
- const backgrounds = {
561
- primary: 'blue',
562
- secondary: 'aqua'
563
- } as const;
564
-
565
- export const variant = styleVariants(
566
- backgrounds,
567
- (background) => [base, { background }]
568
- );
569
- ```
570
-
571
- ### globalStyle
572
-
573
- Creates styles attached to a global selector.
574
-
575
- ```ts
576
- import { globalStyle } from '@vanilla-extract/css';
577
-
578
- globalStyle('html, body', {
579
- margin: 0
580
- });
581
- ```
582
-
583
- Global selectors can also contain references to other scoped class names.
584
-
585
- ```ts
586
- import { style, globalStyle } from '@vanilla-extract/css';
587
-
588
- export const parentClass = style({});
589
-
590
- globalStyle(`${parentClass} > a`, {
591
- color: 'pink'
592
- });
593
- ```
594
-
595
- ### createTheme
596
-
597
- Creates a locally scoped theme class and a theme contract which can be consumed within your styles.
598
-
599
- **Ensure this function is called within a `.css.ts` context, otherwise variable names will be mismatched between files.**
600
-
601
- ```ts
602
- // theme.css.ts
603
-
604
- import { createTheme } from '@vanilla-extract/css';
605
-
606
- export const [themeClass, vars] = createTheme({
607
- color: {
608
- brand: 'blue'
609
- },
610
- font: {
611
- body: 'arial'
612
- }
613
- });
614
- ```
615
-
616
- You can create theme variants by passing a theme contract as the first argument to `createTheme`.
617
-
618
- ```ts
619
- // themes.css.ts
620
-
621
- import { createTheme } from '@vanilla-extract/css';
622
-
623
- export const [themeA, vars] = createTheme({
624
- color: {
625
- brand: 'blue'
626
- },
627
- font: {
628
- body: 'arial'
629
- }
630
- });
631
-
632
- export const themeB = createTheme(vars, {
633
- color: {
634
- brand: 'pink'
635
- },
636
- font: {
637
- body: 'comic sans ms'
638
- }
639
- });
640
- ```
641
-
642
- > 💡 All theme values must be provided or it’s a type error.
643
-
644
- ### createGlobalTheme
645
-
646
- Creates a theme attached to a global selector, but with locally scoped variable names.
647
-
648
- **Ensure this function is called within a `.css.ts` context, otherwise variable names will be mismatched between files.**
649
-
650
- ```ts
651
- // theme.css.ts
652
-
653
- import { createGlobalTheme } from '@vanilla-extract/css';
654
-
655
- export const vars = createGlobalTheme(':root', {
656
- color: {
657
- brand: 'blue'
658
- },
659
- font: {
660
- body: 'arial'
661
- }
662
- });
663
- ```
664
-
665
- > 💡 All theme values must be provided or it’s a type error.
666
-
667
- If you want to implement an existing theme contract, you can pass it as the second argument.
668
-
669
- ```ts
670
- // theme.css.ts
671
-
672
- import {
673
- createThemeContract,
674
- createGlobalTheme
675
- } from '@vanilla-extract/css';
676
-
677
- export const vars = createThemeContract({
678
- color: {
679
- brand: null
680
- },
681
- font: {
682
- body: null
683
- }
684
- });
685
-
686
- createGlobalTheme(':root', vars, {
687
- color: {
688
- brand: 'blue'
689
- },
690
- font: {
691
- body: 'arial'
692
- }
693
- });
694
- ```
695
-
696
- ### createThemeContract
697
-
698
- Creates a contract of locally scoped variable names for themes to implement.
699
-
700
- **Ensure this function is called within a `.css.ts` context, otherwise variable names will be mismatched between files.**
701
-
702
- > 💡 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.
703
-
704
- ```ts
705
- // themes.css.ts
706
-
707
- import {
708
- createThemeContract,
709
- createTheme
710
- } from '@vanilla-extract/css';
711
-
712
- export const vars = createThemeContract({
713
- color: {
714
- brand: null
715
- },
716
- font: {
717
- body: null
718
- }
719
- });
720
-
721
- export const themeA = createTheme(vars, {
722
- color: {
723
- brand: 'blue'
724
- },
725
- font: {
726
- body: 'arial'
727
- }
728
- });
729
-
730
- export const themeB = createTheme(vars, {
731
- color: {
732
- brand: 'pink'
733
- },
734
- font: {
735
- body: 'comic sans ms'
736
- }
737
- });
738
- ```
739
-
740
- ### createGlobalThemeContract
741
-
742
- Creates a contract of globally scoped variable names for themes to implement.
743
-
744
- > 💡 This is useful if you want to make your theme contract available to non-JavaScript environments.
745
-
746
- ```ts
747
- // themes.css.ts
748
-
749
- import {
750
- createGlobalThemeContract,
751
- createGlobalTheme
752
- } from '@vanilla-extract/css';
753
-
754
- export const vars = createGlobalThemeContract({
755
- color: {
756
- brand: 'color-brand'
757
- },
758
- font: {
759
- body: 'font-body'
760
- }
761
- });
762
-
763
- createGlobalTheme(':root', vars, {
764
- color: {
765
- brand: 'blue'
766
- },
767
- font: {
768
- body: 'arial'
769
- }
770
- });
771
- ```
772
-
773
- You can also provide a map function as the second argument which has access to the value and the object path.
774
-
775
- For example, you can automatically prefix all variable names.
776
-
777
- ```ts
778
- // themes.css.ts
779
-
780
- import { createGlobalThemeContract } from '@vanilla-extract/css';
781
-
782
- export const vars = createGlobalThemeContract({
783
- color: {
784
- brand: 'color-brand'
785
- },
786
- font: {
787
- body: 'font-body'
788
- }
789
- }, (value) => `prefix-${value}`);
790
- ```
791
-
792
- You can also use the map function to automatically generate names from the object path, joining keys with a hyphen.
793
-
794
- ```ts
795
- // themes.css.ts
796
-
797
- import { createGlobalThemeContract } from '@vanilla-extract/css';
798
-
799
- export const vars = createGlobalThemeContract({
800
- color: {
801
- brand: null
802
- },
803
- font: {
804
- body: null
805
- }
806
- }, (_value, path) => `prefix-${path.join('-')}`);
807
- ```
808
-
809
- ### assignVars
810
-
811
- Assigns a collection of CSS Variables anywhere within a style block.
812
-
813
- > 💡 This is useful for creating responsive themes since it can be used within `@media` blocks.
814
-
815
- ```ts
816
- import { createThemeContract, style, assignVars } from '@vanilla-extract/css';
817
-
818
- export const vars = createThemeContract({
819
- space: {
820
- small: null,
821
- medium: null,
822
- large: null
823
- }
824
- });
825
-
826
- export const responsiveSpaceTheme = style({
827
- vars: assignVars(vars.space, {
828
- small: '4px',
829
- medium: '8px',
830
- large: '16px'
831
- }),
832
- '@media': {
833
- 'screen and (min-width: 1024px)': {
834
- vars: assignVars(vars.space, {
835
- small: '8px',
836
- medium: '16px',
837
- large: '32px'
838
- })
839
- }
840
- }
841
- });
842
- ```
843
-
844
- > 💡 All variables passed into this function must be assigned or it’s a type error.
845
-
846
- ### createVar
847
-
848
- Creates a single CSS Variable.
849
-
850
- ```ts
851
- import { createVar, style } from '@vanilla-extract/css';
852
-
853
- export const colorVar = createVar();
854
-
855
- export const exampleStyle = style({
856
- color: colorVar
857
- });
858
- ```
859
-
860
- Scoped variables can be set using the `vars` key.
861
-
862
- ```ts
863
- import { createVar, style } from '@vanilla-extract/css';
864
- import { colorVar } from './vars.css.ts';
865
-
866
- export const parentStyle = style({
867
- vars: {
868
- [colorVar]: 'blue'
869
- }
870
- });
871
- ```
872
-
873
- ### fallbackVar
874
-
875
- Provides fallback values when consuming variables.
876
-
877
- ```ts
878
- import { createVar, fallbackVar, style } from '@vanilla-extract/css';
879
-
880
- export const colorVar = createVar();
881
-
882
- export const exampleStyle = style({
883
- color: fallbackVar(colorVar, 'blue'),
884
- });
885
- ```
886
-
887
- Multiple fallbacks are also supported.
888
-
889
- ```ts
890
- import { createVar, fallbackVar, style } from '@vanilla-extract/css';
891
-
892
- export const primaryColorVar = createVar();
893
- export const secondaryColorVar = createVar();
894
-
895
- export const exampleStyle = style({
896
- color: fallbackVar(primaryColorVar, secondaryColorVar, 'blue'),
897
- });
898
- ```
899
-
900
- ### fontFace
901
-
902
- Creates a custom font attached to a locally scoped font name.
903
-
904
- ```ts
905
- import { fontFace, style } from '@vanilla-extract/css';
906
-
907
- const myFont = fontFace({
908
- src: 'local("Comic Sans MS")'
909
- });
910
-
911
- export const text = style({
912
- fontFamily: myFont
913
- });
914
- ```
915
-
916
- ### globalFontFace
917
-
918
- Creates a globally scoped custom font.
919
-
920
- ```ts
921
- import {
922
- globalFontFace,
923
- style
924
- } from '@vanilla-extract/css';
925
-
926
- globalFontFace('MyGlobalFont', {
927
- src: 'local("Comic Sans MS")'
928
- });
929
-
930
- export const text = style({
931
- fontFamily: 'MyGlobalFont'
932
- });
933
- ```
934
-
935
- ### keyframes
936
-
937
- Creates a locally scoped set of keyframes.
938
-
939
- ```ts
940
- import { keyframes, style } from '@vanilla-extract/css';
941
-
942
- const rotate = keyframes({
943
- '0%': { transform: 'rotate(0deg)' },
944
- '100%': { transform: 'rotate(360deg)' }
945
- });
946
-
947
- export const animated = style({
948
- animation: `3s infinite ${rotate}`,
949
- });
950
- ```
951
-
952
- ### globalKeyframes
953
-
954
- Creates a globally scoped set of keyframes.
955
-
956
- ```ts
957
- import { globalKeyframes, style } from '@vanilla-extract/css';
958
-
959
- globalKeyframes('rotate', {
960
- '0%': { transform: 'rotate(0deg)' },
961
- '100%': { transform: 'rotate(360deg)' }
962
- });
963
-
964
- export const animated = style({
965
- animation: `3s infinite rotate`,
966
- });
967
- ```
968
-
969
- ## Recipes API
970
-
971
- Create multi-variant styles with a type-safe runtime API, heavily inspired by [Stitches.](https://stitches.dev)
972
-
973
- As with the rest of vanilla-extract, all styles are generated at build time.
974
-
975
- ```bash
976
- $ npm install @vanilla-extract/recipes
977
- ```
978
-
979
- ### recipe
980
-
981
- Creates a multi-variant style function that can be used at runtime or statically in `.css.ts` files.
982
-
983
- Accepts an optional set of `base` styles, `variants`, `compoundVariants` and `defaultVariants`.
984
-
985
- ```ts
986
- import { recipe } from '@vanilla-extract/recipes';
987
-
988
- export const button = recipe({
989
- base: {
990
- borderRadius: 6
991
- },
992
-
993
- variants: {
994
- color: {
995
- neutral: { background: 'whitesmoke' },
996
- brand: { background: 'blueviolet' },
997
- accent: { background: 'slateblue' }
998
- },
999
- size: {
1000
- small: { padding: 12 },
1001
- medium: { padding: 16 },
1002
- large: { padding: 24 }
1003
- },
1004
- rounded: {
1005
- true: { borderRadius: 999 }
1006
- }
1007
- },
1008
-
1009
- // Applied when multiple variants are set at once
1010
- compoundVariants: [
1011
- {
1012
- variants: {
1013
- color: 'neutral',
1014
- size: 'large'
1015
- },
1016
- style: {
1017
- background: 'ghostwhite'
1018
- }
1019
- }
1020
- ],
1021
-
1022
- defaultVariants: {
1023
- color: 'accent',
1024
- size: 'medium'
1025
- }
1026
- });
1027
- ```
1028
-
1029
- With this recipe configured, you can now use it in your templates.
1030
-
1031
- ```ts
1032
- import { button } from './button.css.ts';
1033
-
1034
- document.write(`
1035
- <button class="${button({
1036
- color: 'accent',
1037
- size: 'large',
1038
- rounded: true
1039
- })}">
1040
- Hello world
1041
- </button>
1042
- `);
1043
- ```
1044
-
1045
- Your recipe configuration can also make use of existing variables, classes and styles.
1046
-
1047
- For example, you can pass in the result of your [`sprinkles`](https://vanilla-extract.style/documentation/packages/sprinkles) function directly.
1048
-
1049
- ```ts
1050
- import { recipe } from '@vanilla-extract/recipes';
1051
- import { reset } from './reset.css.ts';
1052
- import { sprinkles } from './sprinkles.css.ts';
1053
-
1054
- export const button = recipe({
1055
- base: [reset, sprinkles({ borderRadius: 'round' })],
1056
-
1057
- variants: {
1058
- color: {
1059
- neutral: sprinkles({ background: 'neutral' }),
1060
- brand: sprinkles({ background: 'brand' }),
1061
- accent: sprinkles({ background: 'accent' })
1062
- },
1063
- size: {
1064
- small: sprinkles({ padding: 'small' }),
1065
- medium: sprinkles({ padding: 'medium' }),
1066
- large: sprinkles({ padding: 'large' })
1067
- }
1068
- },
1069
-
1070
- defaultVariants: {
1071
- color: 'accent',
1072
- size: 'medium'
1073
- }
1074
- });
1075
- ```
1076
-
1077
- ## Dynamic API
1078
-
1079
- Dynamically update theme variables at runtime.
1080
-
1081
- ```bash
1082
- npm install @vanilla-extract/dynamic
1083
- ```
1084
-
1085
- ### assignInlineVars
1086
-
1087
- Assigns CSS Variables as inline styles.
1088
-
1089
- ```tsx
1090
- // app.tsx
1091
-
1092
- import { assignInlineVars } from '@vanilla-extract/dynamic';
1093
- import { vars } from './vars.css.ts';
1094
-
1095
- const MyComponent = () => (
1096
- <section
1097
- style={assignInlineVars({
1098
- [vars.colors.brand]: 'pink',
1099
- [vars.colors.accent]: 'green'
1100
- })}
1101
- >
1102
- ...
1103
- </section>
1104
- );
1105
- ```
1106
-
1107
- You can also assign collections of variables by passing a theme contract as the first argument. All variables must be assigned or it’s a type error.
1108
-
1109
- ```tsx
1110
- // app.tsx
1111
-
1112
- import { assignInlineVars } from '@vanilla-extract/dynamic';
1113
- import { vars } from './vars.css.ts';
1114
-
1115
- const MyComponent = () => (
1116
- <section
1117
- style={assignInlineVars(vars.colors, {
1118
- brand: 'pink',
1119
- accent: 'green'
1120
- })}
1121
- >
1122
- ...
1123
- </section>
1124
- );
1125
- ```
1126
-
1127
- Even though this function returns an object of inline styles, its `toString` method returns a valid `style` attribute value so that it can be used in string templates.
1128
-
1129
- ```tsx
1130
- // app.ts
1131
-
1132
- import { assignInlineVars } from '@vanilla-extract/dynamic';
1133
- import { vars } from './vars.css.ts';
1134
-
1135
- document.write(`
1136
- <section style="${assignInlineVars({
1137
- [vars.colors.brand]: 'pink',
1138
- [vars.colors.accent]: 'green'
1139
- })}">
1140
- ...
1141
- </section>
1142
- `);
1143
- ```
1144
-
1145
- ### setElementVars
1146
-
1147
- Sets CSS Variables on a DOM element.
1148
-
1149
- ```tsx
1150
- // app.ts
1151
-
1152
- import { setElementVars } from '@vanilla-extract/dynamic';
1153
- import { vars } from './styles.css.ts';
1154
-
1155
- const el = document.getElementById('myElement');
1156
-
1157
- setElementVars(el, {
1158
- [vars.colors.brand]: 'pink',
1159
- [vars.colors.accent]: 'green'
1160
- });
1161
- ```
1162
-
1163
- You can also set collections of variables by passing a theme contract as the second argument. All variables must be set or it’s a type error.
1164
-
1165
- ```tsx
1166
- // app.ts
1167
-
1168
- import { setElementVars } from '@vanilla-extract/dynamic';
1169
- import { vars } from './styles.css.ts';
1170
-
1171
- const el = document.getElementById('myElement');
1172
-
1173
- setElementVars(el, vars.colors, {
1174
- brand: 'pink',
1175
- accent: 'green'
1176
- });
1177
- ```
1178
-
1179
- ## Utility functions
1180
-
1181
- We also provide a standalone package of optional utility functions to make it easier to work with CSS in TypeScript.
1182
-
1183
- > 💡 This package can be used with any CSS-in-JS library.
1184
-
1185
- ```bash
1186
- npm install @vanilla-extract/css-utils
1187
- ```
1188
-
1189
- ### calc
1190
-
1191
- Streamlines the creation of CSS calc expressions.
1192
-
1193
- ```ts
1194
- import { calc } from '@vanilla-extract/css-utils';
1195
-
1196
- const styles = {
1197
- height: calc.multiply('var(--grid-unit)', 2)
1198
- };
1199
- ```
1200
-
1201
- The following functions are available.
1202
-
1203
- - `calc.add`
1204
- - `calc.subtract`
1205
- - `calc.multiply`
1206
- - `calc.divide`
1207
- - `calc.negate`
1208
-
1209
- The `calc` export is also a function, providing a chainable API for complex calc expressions.
1210
-
1211
- ```ts
1212
- import { calc } from '@vanilla-extract/css-utils';
1213
-
1214
- const styles = {
1215
- marginTop: calc('var(--space-large)')
1216
- .divide(2)
1217
- .negate()
1218
- .toString()
1219
- };
1220
- ```
1221
-
1222
- ---
1223
-
1224
87
  ## Thanks
1225
88
 
1226
89
  - [Nathan Nam Tran](https://twitter.com/naistran) for creating [css-in-js-loader](https://github.com/naistran/css-in-js-loader), which served as the initial starting point for [treat](https://seek-oss.github.io/treat), the precursor to this library.
@@ -179,6 +179,7 @@ function vanillaExtractPlugin({
179
179
  return null;
180
180
  }
181
181
 
182
+ const identOption = identifiers !== null && identifiers !== void 0 ? identifiers : config.mode === 'production' ? 'short' : 'debug';
182
183
  let ssr;
183
184
 
184
185
  if (typeof ssrParam === 'boolean') {
@@ -188,11 +189,12 @@ function vanillaExtractPlugin({
188
189
  }
189
190
 
190
191
  if (ssr && !forceEmitCssInSsrBuild) {
191
- return integration.addFileScope({
192
+ return integration.transform({
192
193
  source: code,
193
194
  filePath: vite.normalizePath(validId),
194
195
  rootPath: config.root,
195
- packageName
196
+ packageName,
197
+ identOption
196
198
  });
197
199
  }
198
200
 
@@ -202,7 +204,8 @@ function vanillaExtractPlugin({
202
204
  } = await integration.compile({
203
205
  filePath: validId,
204
206
  cwd: config.root,
205
- esbuildOptions
207
+ esbuildOptions,
208
+ identOption
206
209
  });
207
210
 
208
211
  for (const file of watchFiles) {
@@ -216,7 +219,7 @@ function vanillaExtractPlugin({
216
219
  const output = await integration.processVanillaFile({
217
220
  source,
218
221
  filePath: validId,
219
- identOption: identifiers !== null && identifiers !== void 0 ? identifiers : config.mode === 'production' ? 'short' : 'debug',
222
+ identOption,
220
223
  serializeVirtualCssPath: async ({
221
224
  fileScope,
222
225
  source
@@ -179,6 +179,7 @@ function vanillaExtractPlugin({
179
179
  return null;
180
180
  }
181
181
 
182
+ const identOption = identifiers !== null && identifiers !== void 0 ? identifiers : config.mode === 'production' ? 'short' : 'debug';
182
183
  let ssr;
183
184
 
184
185
  if (typeof ssrParam === 'boolean') {
@@ -188,11 +189,12 @@ function vanillaExtractPlugin({
188
189
  }
189
190
 
190
191
  if (ssr && !forceEmitCssInSsrBuild) {
191
- return integration.addFileScope({
192
+ return integration.transform({
192
193
  source: code,
193
194
  filePath: vite.normalizePath(validId),
194
195
  rootPath: config.root,
195
- packageName
196
+ packageName,
197
+ identOption
196
198
  });
197
199
  }
198
200
 
@@ -202,7 +204,8 @@ function vanillaExtractPlugin({
202
204
  } = await integration.compile({
203
205
  filePath: validId,
204
206
  cwd: config.root,
205
- esbuildOptions
207
+ esbuildOptions,
208
+ identOption
206
209
  });
207
210
 
208
211
  for (const file of watchFiles) {
@@ -216,7 +219,7 @@ function vanillaExtractPlugin({
216
219
  const output = await integration.processVanillaFile({
217
220
  source,
218
221
  filePath: validId,
219
- identOption: identifiers !== null && identifiers !== void 0 ? identifiers : config.mode === 'production' ? 'short' : 'debug',
222
+ identOption,
220
223
  serializeVirtualCssPath: async ({
221
224
  fileScope,
222
225
  source
@@ -1,7 +1,7 @@
1
1
  import path from 'path';
2
2
  import { normalizePath } from 'vite';
3
3
  import outdent from 'outdent';
4
- import { getPackageInfo, cssFileFilter, addFileScope, compile, processVanillaFile } from '@vanilla-extract/integration';
4
+ import { getPackageInfo, cssFileFilter, transform, compile, processVanillaFile } from '@vanilla-extract/integration';
5
5
 
6
6
  // Mostly copied from vite's implementation
7
7
  // https://github.com/vitejs/vite/blob/efec70f816b80e55b64255b32a5f120e1cf4e4be/packages/vite/src/node/plugins/css.ts
@@ -152,6 +152,7 @@ function vanillaExtractPlugin({
152
152
  return null;
153
153
  }
154
154
 
155
+ const identOption = identifiers !== null && identifiers !== void 0 ? identifiers : config.mode === 'production' ? 'short' : 'debug';
155
156
  let ssr;
156
157
 
157
158
  if (typeof ssrParam === 'boolean') {
@@ -161,11 +162,12 @@ function vanillaExtractPlugin({
161
162
  }
162
163
 
163
164
  if (ssr && !forceEmitCssInSsrBuild) {
164
- return addFileScope({
165
+ return transform({
165
166
  source: code,
166
167
  filePath: normalizePath(validId),
167
168
  rootPath: config.root,
168
- packageName
169
+ packageName,
170
+ identOption
169
171
  });
170
172
  }
171
173
 
@@ -175,7 +177,8 @@ function vanillaExtractPlugin({
175
177
  } = await compile({
176
178
  filePath: validId,
177
179
  cwd: config.root,
178
- esbuildOptions
180
+ esbuildOptions,
181
+ identOption
179
182
  });
180
183
 
181
184
  for (const file of watchFiles) {
@@ -189,7 +192,7 @@ function vanillaExtractPlugin({
189
192
  const output = await processVanillaFile({
190
193
  source,
191
194
  filePath: validId,
192
- identOption: identifiers !== null && identifiers !== void 0 ? identifiers : config.mode === 'production' ? 'short' : 'debug',
195
+ identOption,
193
196
  serializeVirtualCssPath: async ({
194
197
  fileScope,
195
198
  source
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vanilla-extract/vite-plugin",
3
- "version": "3.5.0",
3
+ "version": "3.6.0",
4
4
  "description": "Zero-runtime Stylesheets-in-TypeScript",
5
5
  "main": "dist/vanilla-extract-vite-plugin.cjs.js",
6
6
  "module": "dist/vanilla-extract-vite-plugin.esm.js",
@@ -15,7 +15,7 @@
15
15
  "author": "SEEK",
16
16
  "license": "MIT",
17
17
  "dependencies": {
18
- "@vanilla-extract/integration": "^5.0.0",
18
+ "@vanilla-extract/integration": "^6.0.0",
19
19
  "outdent": "^0.8.0",
20
20
  "postcss": "^8.3.6",
21
21
  "postcss-load-config": "^3.1.0"