Macaron

Typesafe CSS-in-JS with zero runtime, colocation, maximum safety and produc...

README


Features


- Zero runtime bundles with build time style extraction.
- Colocation of styles and components.
- First class Typescript support.
- Supports both styled-components API and vanilla styling API.
- Stitches-like variants API.
- Out of box support for React, Solid and Qwik.
- Integrations for esbuild and Vite.

Documentation


For full documentation, visit https://macaron.js.org

Example


Styled API


  1. ```jsx
  2. // or import it from @macaron-css/solid
  3. import { styled } from '@macaron-css/react';

  4. const Button = styled('button', {
  5.   base: {
  6.     borderRadius: 6,
  7.   },
  8.   variants: {
  9.     color: {
  10.       neutral: { background: 'whitesmoke' },
  11.       brand: { background: 'blueviolet' },
  12.       accent: { background: 'slateblue' },
  13.     },
  14.     size: {
  15.       small: { padding: 12 },
  16.       medium: { padding: 16 },
  17.       large: { padding: 24 },
  18.     },
  19.     rounded: {
  20.       true: { borderRadius: 999 },
  21.     },
  22.   },
  23.   compoundVariants: [
  24.     {
  25.       variants: {
  26.         color: 'neutral',
  27.         size: 'large',
  28.       },
  29.       style: {
  30.         background: 'ghostwhite',
  31.       },
  32.     },
  33.   ],

  34.   defaultVariants: {
  35.     color: 'accent',
  36.     size: 'medium',
  37.   },
  38. });

  39. // Use it like a regular solidjs/react component
  40. function App() {
  41.   return (
  42.     <Button color="accent" size="small" rounded>
  43.       Click me!
  44.     </Button>
  45.   );
  46. }
  47. ```

Styling API


The styling API is the same api is vanilla-extract, but allows styles to be defined in the same file, improving the DX.

Check out vanilla-extract docs for the API reference.

These functions can also be called directly inside expressions to provide a css prop-like API with zero-runtime cost:-

  1. ```js
  2. import { style } from '@macaron-css/core';

  3. function App() {
  4.   return (
  5.     <div
  6.       class={style({
  7.         display: 'flex',
  8.         padding: '10px',
  9.       })}
  10.     >
  11.       <button class={style({ color: 'red' })}>Click me</button>
  12.     </div>
  13.   );
  14. }
  15. ```

Playground


You can try about these above examples at https://macaron.js.org/playground and see how macaron figures out which expressions have to be extracted and what your code would look like after being transpiled

How it works



Acknowledgements


  Thanks to the vanilla-extract team for their work on VE, which macaron uses for evaluating files.

  Thanks to Dax for helping me with this and thoroughly testing it outß, helping me find numerous bugs and improving macaron.