@vanilla-extract/css

Zero-runtime Stylesheets-in-TypeScript

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@vanilla-extract/css
9,255661.14.222 days ago3 years agoMinified + gzip package size for @vanilla-extract/css in KB

Readme

๐Ÿง vanilla-extract
Zero-runtime Stylesheets-in-TypeScript.
Write your styles in TypeScript (or JavaScript) with locally scoped class names and CSS Variables, then generate static CSS files at build time.
Basically, itโ€™s โ€œCSS Modules-in-TypeScriptโ€ but with scoped CSS Variables + heaps more.
๐Ÿ”ฅ ย  All styles generated at build time โ€” just like Sass, Less, etc.
โœจ ย  Minimal abstraction over standard CSS.
๐Ÿฆ„ ย  Works with any front-end framework โ€” or even without one.
๐ŸŒณ ย  Locally scoped class names โ€” just like CSS Modules.
๐Ÿš€ ย  Locally scoped CSS Variables, @keyframes and @font-face rules.
๐ŸŽจ ย  High-level theme system with support for simultaneous themes. No globals!
๐Ÿ›  ย  Utils for generating variable-based calc expressions.
๐Ÿ’ช ย  Type-safe styles via CSSType.
๐Ÿƒโ€โ™‚๏ธ ย  Optional runtime version for development and testing.
๐Ÿ™ˆ ย  Optional API for dynamic runtime theming.
๐ŸŒ Check out the documentation site for setup guides, examples and API docs.
๐Ÿ–ฅ ย  Try it out for yourself in CodeSandbox.
Write your styles in .css.ts files.
// styles.css.ts

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

export const [themeClass, vars] = createTheme({
  color: {
    brand: 'blue'
  },
  font: {
    body: 'arial'
  }
});

export const exampleStyle = style({
  backgroundColor: vars.color.brand,
  fontFamily: vars.font.body,
  color: 'white',
  padding: 10
});

๐Ÿ’ก Once you've configured your build tooling, 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.

Then consume them in your markup.
// app.ts

import { themeClass, exampleStyle } from './styles.css.ts';

document.write(`
  <section class="${themeClass}">
    <h1 class="${exampleStyle}">Hello world!</h1>
  </section>
`);

Want to work at a higher level while maximising style re-use? Check out ๐Ÿจ Sprinkles, our official zero-runtime atomic CSS framework, built on top of vanilla-extract.

Thanks

  • Nathan Nam Tran for creating css-in-js-loader, which served as the initial starting point for treat, the precursor to this library.
  • Stitches for getting us excited about CSS-Variables-in-JS.
  • SEEK for giving us the space to do interesting work.

License

MIT.