@vanilla-extract/next-plugin 2.0.2 → 2.1.1

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