solid-styledv1.0.0
For Solid 2 · 2.3 kB runtime

Plain CSS, scoped and reactive

Write CSS inside your Solid component. The compiler scopes it, and your signals become CSS variables.

npm i solid-styled unplugin-solid-styled
style="…"style writes0sheet rewrites0

Write CSS. Ship scoped CSS.

Each template becomes a static sheet, and each ${…} a CSS variable. Built by the compiler with this page.

Button.tsx
import type { JSX } from '@solidjs/web';
import { createSignal } from 'solid-js';
import { css } from 'solid-styled';

export default function Button(): JSX.Element {
  const [color, setColor] = createSignal('orchid');

  css`
    button {
      color: ${color()};
      border: 2px solid currentColor;
      padding: 0.5rem 1rem;
    }
  `;

  return (
    <button
      onClick={() => {
        setColor('hotpink');
      }}
    >
      {color()}
    </button>
  );
}
solid-styled/compiler
import { useSolidStyled as _useSolidStyled, createCSSVars as _createCSSVars, mergeStyles as _mergeStyles } from "solid-styled";
import type { JSX } from '@solidjs/web';
import { createSignal } from 'solid-js';
import { css } from 'solid-styled';

const sheet_1 = "c-c4307938-0";
const css_1 = "button[s\\:c-c4307938-0]{color:var(--s-v-c4307938-1);border:2px solid;padding:.5rem 1rem}";
export default function Button(): JSX.Element {
  const vars_1 = _createCSSVars();
  const [color, setColor] = createSignal('orchid');

  (_useSolidStyled(sheet_1, 1, css_1), vars_1(() => ({ "--s-v-c4307938-1": color() })));

  return (
    <button s:c-c4307938-0 style={vars_1()}
      onClick={() => {
        setColor('hotpink');
      }}
    >
      {color()}
    </button>
  );
}

Both spellings, one sheet

button[s\:c-c4307938-0]{color:var(--s-v-c4307938-1);border:2px solid;padding:.5rem 1rem}

Values change. The sheet stays.

The S above is this component. Five values, one sheet, zero rewrites.

src/islands/Ribbons.tsx
import type { JSX } from '@solidjs/web';
import { css } from 'solid-styled';

interface RibbonsProps {
  hue: string;
  tilt: string;
  gap: string;
  leanX: string;
  leanY: string;
}

export default function Ribbons(props: RibbonsProps): JSX.Element {
  css`
    div {
      --hue: ${props.hue};
      position: relative;
      aspect-ratio: 1;
      transform: perspective(60em) rotateX(${props.leanY}deg) rotateY(${props.leanX}deg)
        rotate(${props.tilt}deg);
      transform-style: preserve-3d;
      transition: transform 0.4s cubic-bezier(0.2, 0.8, 0.2, 1);
    }
    span {
      position: absolute;
      inset: 0;
      transition: translate 0.3s cubic-bezier(0.2, 0.9, 0.3, 1.2);
    }
    .top {
      clip-path: polygon(8% 6%, 80% 6%, 94.7% 30%, 22.7% 30%);
      background: linear-gradient(
        90deg,
        hsl(var(--hue) 55% 37%),
        hsl(calc(var(--hue) + 14) 50% 52%)
      );
      translate: 0 calc(${props.gap}px * -1) 3em;
    }
    .spine {
      clip-path: polygon(8% 6%, 38% 6%, 92% 94%, 62% 94%);
      background: linear-gradient(
        160deg,
        hsl(calc(var(--hue) + 6) 48% 51%),
        hsl(calc(var(--hue) + 28) 50% 52%)
      );
    }
    .bottom {
      clip-path: polygon(92% 94%, 20% 94%, 5.3% 70%, 77.3% 70%);
      background: linear-gradient(
        270deg,
        hsl(calc(var(--hue) + 28) 50% 52%),
        hsl(calc(var(--hue) + 40) 100% 80%)
      );
      translate: 0 ${props.gap}px -3em;
    }
  `;

  return (
    <div role="img" aria-label="The solid-styled mark, drawn with five CSS variables">
      <span class="top" />
      <span class="spine" />
      <span class="bottom" />
    </div>
  );
}
solid-styled/compiler
import { useSolidStyled as _useSolidStyled, createCSSVars as _createCSSVars, mergeStyles as _mergeStyles } from "solid-styled";
import type { JSX } from '@solidjs/web';
import { css } from 'solid-styled';

interface RibbonsProps {
  hue: string;
  tilt: string;
  gap: string;
  leanX: string;
  leanY: string;
}

const sheet_1 = "c-dc3af85f-0";
const css_1 = "div[s\\:c-dc3af85f-0]{--hue:var(--s-v-dc3af85f-1);aspect-ratio:1;transform:perspective(60em) rotateX(calc(var(--s-v-dc3af85f-2)*1deg)) rotateY(calc(var(--s-v-dc3af85f-3)*1deg)) rotate(calc(var(--s-v-dc3af85f-4)*1deg));transform-style:preserve-3d;transition:transform .4s cubic-bezier(.2,.8,.2,1);position:relative}span[s\\:c-dc3af85f-0]{transition:translate .3s cubic-bezier(.2,.9,.3,1.2);position:absolute;inset:0}.top[s\\:c-dc3af85f-0]{clip-path:polygon(8% 6%,80% 6%,94.7% 30%,22.7% 30%);background:linear-gradient(90deg,hsl(var(--hue) 55% 37%),hsl(calc(var(--hue) + 14) 50% 52%));translate:0 calc(calc(var(--s-v-dc3af85f-5)*1px)*-1) 3em}.spine[s\\:c-dc3af85f-0]{clip-path:polygon(8% 6%,38% 6%,92% 94%,62% 94%);background:linear-gradient(160deg,hsl(calc(var(--hue) + 6) 48% 51%),hsl(calc(var(--hue) + 28) 50% 52%))}.bottom[s\\:c-dc3af85f-0]{clip-path:polygon(92% 94%,20% 94%,5.3% 70%,77.3% 70%);background:linear-gradient(270deg,hsl(calc(var(--hue) + 28) 50% 52%),hsl(calc(var(--hue) + 40) 100% 80%));translate:0 calc(var(--s-v-dc3af85f-6)*1px) -3em}";
export default function Ribbons(props: RibbonsProps): JSX.Element {
  const vars_1 = _createCSSVars();
  (_useSolidStyled(sheet_1, 1, css_1), vars_1(() => ({ "--s-v-dc3af85f-1": props.hue, "--s-v-dc3af85f-2": props.leanY, "--s-v-dc3af85f-3": props.leanX, "--s-v-dc3af85f-4": props.tilt, "--s-v-dc3af85f-5": props.gap, "--s-v-dc3af85f-6": props.gap })));

  return (
    <div s:c-dc3af85f-0 style={vars_1()} role="img" aria-label="The solid-styled mark, drawn with five CSS variables">
      <span s:c-dc3af85f-0 style={vars_1()} class="top" />
      <span s:c-dc3af85f-0 style={vars_1()} class="spine" />
      <span s:c-dc3af85f-0 style={vars_1()} class="bottom" />
    </div>
  );
}

Styles stay where you wrote them.

Every selector requires the component's scope attribute. These demos are live.

Scoped by default

Children you pass carry the scope. Components you render do not.

Written here

Passed as children

Rendered by Foreign

import type { JSX } from '@solidjs/web';
import { css } from 'solid-styled';

function Frame(props: { children: JSX.Element }): JSX.Element {
  return <div>{props.children}</div>;
}

function Foreign(): JSX.Element {
  return <p>Rendered by Foreign</p>;
}

export default function Boundary(): JSX.Element {
  css`
    p {
      color: var(--accent-strong);
      font-weight: 700;
    }
  `;

  return (
    <>
      <p>Written here</p>
      <Frame>
        <p>Passed as children</p>
      </Frame>
      <Foreign />
    </>
  );
}

:global(…)

Opts one selector out, here to reach a child's spans.

scopedreactivestatic
import type { JSX } from '@solidjs/web';
import { css } from 'solid-styled';

function Labels(): JSX.Element {
  return (
    <>
      <span>scoped</span>
      <span>reactive</span>
      <span>static</span>
    </>
  );
}

export default function Tags(): JSX.Element {
  css`
    div {
      display: flex;
      gap: 0.5rem;
    }
    div > :global(span) {
      padding: 0.25rem 0.75rem;
      border-radius: 999px;
      background: var(--accent-fill);
      color: var(--on-accent);
    }
  `;

  return (
    <div>
      <Labels />
    </div>
  );
}

use:solid-styled

Forwards the scope to a component that spreads props, such as dynamic.

import type { JSX } from '@solidjs/web';
import { dynamic } from '@solidjs/web';
import { css } from 'solid-styled';

const Button = dynamic(() => 'button');

export default function Forward(): JSX.Element {
  css`
    button {
      padding: 0.5rem 1rem;
      border: 0;
      border-radius: 999px;
      background: var(--accent-fill);
      color: var(--on-accent);
      font: 600 14px var(--font-sans);
    }
  `;

  return (
    <>
      <Button type="button" use:solid-styled>
        Forwarded
      </Button>
      <Button type="button">Not forwarded</Button>
    </>
  );
}

@global

This site's fonts, tokens and base styles are one @global block.

8.7kB after LightningCSS

Compiled ahead. Rendered on the server.

oxc parses, LightningCSS lowers and minifies. The browser gets a string.

Server rendered, no flash

This page was prerendered with StyleRegistry and carries 22 sheets in its <head>. The client reuses them.

docs/ssr.md
import { renderToString } from '@solidjs/web';
import { StyleRegistry, renderSheets } from 'solid-styled';

const styles = [];

const html = renderToString(() => (
  <StyleRegistry styles={styles}>
    <App />
  </StyleRegistry>
));

const head = renderSheets(styles);

Near zero runtime

2.3kB gzipped

Insert a sheet once, count users, remove it with the last one.

Any bundler

unplugin-solid-styled runs before the Solid JSX transform.

import solidStyled from 'unplugin-solid-styled';

solidStyled.vite(options);
solidStyled.rollup(options);
solidStyled.webpack(options);
solidStyled.esbuild(options);

How it compares

Against the two common ways to style a Solid app.

Featuresolid-styledCSS Modulessolid-styled-components
CSS lives in the component fileYesNo, a separate fileYes
Selectors are plain CSSYesYesYes, inside a generated class
A changed value updatesOne CSS variableYour own inline styleA new generated class
CSS is builtAt compile timeAt compile timeAt runtime
Esc