— Styling · @vitus-labs/styler

The styled API, without the weight.

styled, css, keyframes, createGlobalStyle and ThemeProvider in 6.98 KB min+gz. Static templates are resolved once, at creation. On the server every component renders its own React 19 <style precedence>, so SSR and static export arrive styled with nothing to configure.

@vitus-labs/stylerv2.9.1 · MIT
npm i @vitus-labs/styler
min+gz, React external
6.98 KB
SSR renders vs styled-components
2.0–3.4×
style collectors to set up for SSR
0

— Why styler

Small where it ships, fast where it renders.

01

Static styles cost nothing per render

A template with no function interpolations is resolved once when the component is created: class name, rules and the <style> element are cached, and every render reuses them.

02

SSR without a collector

Each styled component renders a <style href precedence> element. React 19 hoists it into <head>, de-duplicates it by href and keeps it through hydration, so streaming and static export are FOUC-free. This site is a static export styled this way.

03

The API you already write

styled.div, styled(Component), css, keyframes, createGlobalStyle, the as prop, $transient props and shouldForwardProp. Most styled-components code moves over by changing the import.

04

Bounded, not leaky

Lookup caches cap at 10,000 entries and drop their oldest 10% on overflow. Global styles are reference-counted and removed when the last instance unmounts.

— Before / after

Same component. No server plumbing.

The component code barely changes. What goes away is the server-side style collection: styler ships CSS with the markup it styles.

beforestyled-components
Button.tsx · server.tsx
import styled from 'styled-components'
import { ServerStyleSheet } from 'styled-components'

const Button = styled.button.withConfig({
  shouldForwardProp: (prop) => prop !== 'tone',
})`
  padding: 8px 16px;
  color: ${({ theme, tone }) =>
    tone === 'danger' ? theme.danger : theme.fg};
`

// server: collect, extract, inject
const sheet = new ServerStyleSheet()
const html = renderToString(sheet.collectStyles(<App />))
const styleTags = sheet.getStyleTags()
sheet.seal()

Options via .withConfig(), and the server render wrapped in a ServerStyleSheet whose tags you inject yourself.

after@vitus-labs/styler
Button.tsx · server.tsx
import { styled } from '@vitus-labs/styler'

const Button = styled('button', {
  shouldForwardProp: (prop) => prop !== 'tone',
})`
  padding: 8px 16px;
  color: ${({ theme, tone }) =>
    tone === 'danger' ? theme.danger : theme.fg};
`

// server: nothing to collect
const stream = await renderToReadableStream(<App />)

Options are the second argument. The server render is just a render: each component carries its own <style precedence>.

— Live, not a screenshot

Change a value, watch the class.

A real styled() component with two function interpolations. Each distinct CSS string hashes to its own vl- class and is inserted once; go back to a value you've seen and the class is reused, not re-inserted.

radius
hue

—…0 distinct classes this session

Swatch.tsx
import { styled } from '@vitus-labs/styler'

const Swatch = styled('div')`
  width: 100%;
  height: 168px;
  border-radius: ${({ $radius }) => $radius}px;
  background: ${({ $hue }) => $hue};
  transition: border-radius 0.3s, background-color 0.3s;
`
<Swatch $radius={12} $hue="#c8ff3a" />  // acid
document.styleSheetslive
reading the stylesheet…

— Features

Everything in the 6.98 KB.

  • styled(tag | Component)

    plus the styled.div shorthand; as swaps the rendered element at runtime

  • Transient props

    $-prefixed props style the component and never reach the DOM; shouldForwardProp for the rest

  • Native CSS nesting

    &:hover, & > h2 and nested selectors pass through untransformed

  • At-rule splitting

    nested @media, @supports and @container blocks are lifted into their own top-level rules

  • Component selectors

    interpolate a static styled component (${Icon}) to target its class from a parent

  • Extension folding

    styled(StyledComp) builds one component from both templates; the extension's declarations win

  • Specificity boost

    { boost: true } doubles the class selector, raising specificity from (0,1,0) to (0,2,0)

  • keyframes

    deterministic vl-kf-<hash> names; on the server the rule ships inside the component's <style>

  • createGlobalStyle

    reference-counted, removed on last unmount; a changed value replaces its rules instead of piling up

  • useCSS

    a class name for a plain element when you don't want a styled wrapper

  • Typed themes

    ThemeProvider, useTheme and a DefaultTheme interface you augment once

  • HMR helpers

    sheet.clearCache() recomputes classes, sheet.clearAll() also removes the injected rules

— API

Nine functions. Five types.

Every name links to its reference in the docs.

— Honest limits

When styler isn't the right tool.

Where it isn't the right fit, and what to reach for instead.

  • You're on React 18

    The peer range is react >= 19, and SSR depends on React 19's <style precedence>. Stay on styled-components or Emotion until you upgrade.

  • You want zero-runtime CSS

    Styler is runtime CSS-in-JS. Static templates are cheap, but dynamic ones are resolved during render. If CSS must be extracted at build time, use a build-time tool.

  • You need typed styled props

    styled() returns ComponentType<any>: there is no styled.button<Props> generic. Type the props on a wrapper, or use rocketstyle or attrs, which infer them.

  • Untrusted values in templates

    Interpolations are concatenated into CSS without sanitizing, the same contract as styled-components and Emotion. Allowlist anything that comes from users, URLs or APIs.

  • useCSS and server HTML

    useCSS returns only a class name, so it has no element to carry a <style>. On the server it emits no CSS; rules arrive when the client hydrates. Use styled() for content that must be styled in the HTML.

  • Component CSS in a cascade layer

    Every styled() component writes to the shared, unlayered sheet. createSheet({ layer }) only wraps rules you insert into that instance yourself, so it can't put component styles in an @layer. Unlayered rules beat layered ones, so layer the CSS you want them to override.

  • Expecting faster client renders

    The speed lead is on the server. In the author's benchmark, client mount and update are a statistical tie with styled-components and Emotion, because React's reconciler dominates there.

— Recipes

Copy, paste, ship.

01

A themed button with transient props

$-props drive styles without reaching the DOM. Augment DefaultTheme once and theme is typed in every interpolation.

Button.tsx
import { css, styled, ThemeProvider } from '@vitus-labs/styler'

declare module '@vitus-labs/styler' {
  interface DefaultTheme {
    colors: { accent: string; ink: string }
  }
}

const Button = styled.button`
  padding: 10px 18px;
  border-radius: 999px;
  background: ${({ theme }) => theme.colors.accent};
  color: ${({ theme }) => theme.colors.ink};
  ${({ $block }) => $block && css`width: 100%;`};
`

export const App = () => (
  <ThemeProvider theme={{ colors: { accent: '#c8ff3a', ink: '#0a0b0d' } }}>
    <Button $block>Ship it</Button>
  </ThemeProvider>
)
02

Hover a parent, move a child

A static styled component can be used as a selector. A dynamic one throws at creation, because it has no stable class.

Link.tsx
import { styled } from '@vitus-labs/styler'

const Arrow = styled.span`
  display: inline-block;
  transition: transform 0.2s;
`

export const Link = styled.a`
  display: inline-flex;
  gap: 6px;

  &:hover ${Arrow} {
    transform: translateX(3px);
  }
`

// <Link href="/docs">Docs <Arrow>→</Arrow></Link>
03

Global styles that clean up after themselves

A dynamic global, such as a light/dark body background, replaces its previous rules when the value changes and is removed when the last instance unmounts.

GlobalStyle.tsx
import { createGlobalStyle } from '@vitus-labs/styler'

export const GlobalStyle = createGlobalStyle`
  *, *::before, *::after { box-sizing: border-box; }

  body {
    margin: 0;
    background: ${({ $dark }) => ($dark ? '#0a0b0d' : '#f6f4ee')};
  }
`

// <GlobalStyle $dark={mode === 'dark'} />

— FAQ

Questions, answered.

Is it a drop-in replacement for styled-components?

For most code, yes: change the import. The differences are that options are the second argument to styled() instead of .withConfig(), there is no .attrs() (use @vitus-labs/attrs), and there is no ServerStyleSheet because SSR needs no collector.

Do I need the rest of the ui-system?

No. The published bundle imports only react. @vitus-labs/core is listed as a peer because styler's types reference it and the other packages reach styler through core's connector.

How does it work with the Next.js App Router?

Use it in client components. There is no style registry to set up: server rendering emits each component's <style precedence> and React places it in <head>. This site is a Next.js static export built that way.

What does the 6.98 KB cover?

The full API, minified and gzipped, with React externalized: the same basis as the styled-components (17.89 KB) and Emotion (16.59 KB) figures in the package's comparison table.

Can two different CSS strings get the same class?

Class names are 32-bit FNV-1a hashes, so a collision is possible but very unlikely. In development the sheet warns when it sees one.

— @vitus-labs/styler

Keep the API. Drop the weight.

npm i @vitus-labs/styler
Sources · 34 claims, each traced to v2.9.1 code or docs
  1. Bundle size 6.98 KB min+gz (facts.stylerKb is parsed from this README headline)npm:@vitus-labs/styler/README.md
  2. Size basis: full API, minified + gzipped, React externalized; styled-components 17.89 KB, Emotion 16.59 KBnpm:@vitus-labs/styler/README.md
  3. SSR throughput 2.0–3.4× styled-components; client mount/update a statistical tie (author benchmark)npm:@vitus-labs/styler/README.md
  4. Benchmark harness that produced the SSR/CSR numbersui-system:packages/styler/benchmarks/css-perf.bench.tsx
  5. Static templates resolved once at creation; class, rules and <style> element cachednpm:@vitus-labs/styler/README.md
  6. SSR: each component emits <style href precedence='medium'>ui-system:packages/styler/src/styled.ts
  7. React hoists, dedupes by href, keeps through hydration; FOUC-free static exportnpm:@vitus-labs/styler/README.md
  8. This site is a Next.js static export styled through styler (connector-styler init, no registry)site:src/ui/init.ts
  9. Static export configuredsite:next.config.mjs
  10. Supports styled(tag), styled(Component), styled.div proxy, as, $ transient props, shouldForwardPropui-system:packages/styler/src/styled.ts
  11. Lookup caches bounded at 10,000 entries (default maxCacheSize), oldest 10% evictedui-system:packages/styler/src/sheet.ts
  12. Eviction drops oldest percent of entries (0.1)ui-system:packages/styler/src/sheet.ts
  13. Global styles ref-counted, removed on last unmountui-system:packages/styler/src/sheet.ts
  14. Changed dynamic global replaces its previous rulesnpm:@vitus-labs/styler/README.md
  15. Native CSS nesting passed through untransformedui-system:packages/styler/src/styled.ts
  16. @media, @supports, @container extracted into separate top-level rulesui-system:packages/styler/src/sheet.ts
  17. Static styled component usable as a selector; dynamic one throws at creationui-system:packages/styler/src/resolve.ts
  18. styled(StyledComp) folds both templates into one component; extension declarations winui-system:packages/styler/src/styled.ts
  19. boost doubles the class selector, (0,1,0) → (0,2,0)npm:@vitus-labs/styler/lib/index.d.ts
  20. keyframes names are vl-kf-<hash>ui-system:packages/styler/src/keyframes.ts
  21. Referenced keyframes ship inside the component's <style> on the serverui-system:packages/styler/src/sheet.ts
  22. useCSS emits no CSS on the server; rules injected on hydrationui-system:packages/styler/src/useCSS.ts
  23. sheet.clearCache / clearAll for HMRnpm:@vitus-labs/styler/README.md
  24. Exports: 9 runtime (styled, css, keyframes, createGlobalStyle, ThemeProvider, useTheme, useCSS, sheet, createSheet) + 5 types (CSSResult, Interpolation, StyledOptions, StyleSheetOptions, DefaultTheme)ui-system:packages/styler/src/index.ts
  25. Peer dependency react >= 19npm:@vitus-labs/styler/package.json
  26. Peer dependency on @vitus-labs/core; core is a type-only import in stylerui-system:packages/styler/src/resolve.ts
  27. Published bundle imports only react (and react/jsx-runtime)npm:@vitus-labs/styler/lib/index.js
  28. styled() returns ComponentType<any> — no typed props genericnpm:@vitus-labs/styler/lib/index.d.ts
  29. Interpolations are trusted, not sanitizednpm:@vitus-labs/styler/README.md
  30. No .attrs(); use @vitus-labs/attrs. Options as second argument vs .withConfig()npm:@vitus-labs/styler/README.md
  31. Class names are 32-bit FNV-1a hashesui-system:packages/styler/src/hash.ts
  32. styled(), createGlobalStyle, keyframes and useCSS always write to the shared sheet, never to a createSheet() instanceui-system:packages/styler/src/sheet.ts
  33. createSheet({ layer }) does not put styled() output in a layernpm:@vitus-labs/styler/README.md
  34. Development warning on hash collisionui-system:packages/styler/src/sheet.ts