— 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.
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.
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.
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.
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.
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.
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.
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.
—…0 distinct classes this session
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" /> // acidreading the stylesheet…
— Features
Everything in the 6.98 KB.
- styled(tag | Component)
plus the
styled.divshorthand;asswaps the rendered element at runtime - Transient props
$-prefixed props style the component and never reach the DOM;shouldForwardPropfor the rest - Native CSS nesting
&:hover,& > h2and nested selectors pass through untransformed - At-rule splitting
nested
@media,@supportsand@containerblocks 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,useThemeand aDefaultThemeinterface 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()returnsComponentType<any>: there is nostyled.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
useCSSreturns 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. Usestyled()for content that must be styled in the HTML.Component CSS in a cascade layer
Every
styled()component writes to the shared, unlayeredsheet.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.
A themed button with transient props
$-props drive styles without reaching the DOM. Augment DefaultTheme once and theme is typed in every interpolation.
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>
)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.
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>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.
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/stylerSources · 34 claims, each traced to v2.9.1 code or docs
- Bundle size 6.98 KB min+gz (facts.stylerKb is parsed from this README headline)npm:@vitus-labs/styler/README.md
- Size basis: full API, minified + gzipped, React externalized; styled-components 17.89 KB, Emotion 16.59 KBnpm:@vitus-labs/styler/README.md
- SSR throughput 2.0–3.4× styled-components; client mount/update a statistical tie (author benchmark)npm:@vitus-labs/styler/README.md
- Benchmark harness that produced the SSR/CSR numbersui-system:packages/styler/benchmarks/css-perf.bench.tsx
- Static templates resolved once at creation; class, rules and <style> element cachednpm:@vitus-labs/styler/README.md
- SSR: each component emits <style href precedence='medium'>ui-system:packages/styler/src/styled.ts
- React hoists, dedupes by href, keeps through hydration; FOUC-free static exportnpm:@vitus-labs/styler/README.md
- This site is a Next.js static export styled through styler (connector-styler init, no registry)site:src/ui/init.ts
- Static export configuredsite:next.config.mjs
- Supports styled(tag), styled(Component), styled.div proxy, as, $ transient props, shouldForwardPropui-system:packages/styler/src/styled.ts
- Lookup caches bounded at 10,000 entries (default maxCacheSize), oldest 10% evictedui-system:packages/styler/src/sheet.ts
- Eviction drops oldest percent of entries (0.1)ui-system:packages/styler/src/sheet.ts
- Global styles ref-counted, removed on last unmountui-system:packages/styler/src/sheet.ts
- Changed dynamic global replaces its previous rulesnpm:@vitus-labs/styler/README.md
- Native CSS nesting passed through untransformedui-system:packages/styler/src/styled.ts
- @media, @supports, @container extracted into separate top-level rulesui-system:packages/styler/src/sheet.ts
- Static styled component usable as a selector; dynamic one throws at creationui-system:packages/styler/src/resolve.ts
- styled(StyledComp) folds both templates into one component; extension declarations winui-system:packages/styler/src/styled.ts
- boost doubles the class selector, (0,1,0) → (0,2,0)npm:@vitus-labs/styler/lib/index.d.ts
- keyframes names are vl-kf-<hash>ui-system:packages/styler/src/keyframes.ts
- Referenced keyframes ship inside the component's <style> on the serverui-system:packages/styler/src/sheet.ts
- useCSS emits no CSS on the server; rules injected on hydrationui-system:packages/styler/src/useCSS.ts
- sheet.clearCache / clearAll for HMRnpm:@vitus-labs/styler/README.md
- 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
- Peer dependency react >= 19npm:@vitus-labs/styler/package.json
- Peer dependency on @vitus-labs/core; core is a type-only import in stylerui-system:packages/styler/src/resolve.ts
- Published bundle imports only react (and react/jsx-runtime)npm:@vitus-labs/styler/lib/index.js
- styled() returns ComponentType<any> — no typed props genericnpm:@vitus-labs/styler/lib/index.d.ts
- Interpolations are trusted, not sanitizednpm:@vitus-labs/styler/README.md
- No .attrs(); use @vitus-labs/attrs. Options as second argument vs .withConfig()npm:@vitus-labs/styler/README.md
- Class names are 32-bit FNV-1a hashesui-system:packages/styler/src/hash.ts
- styled(), createGlobalStyle, keyframes and useCSS always write to the shared sheet, never to a createSheet() instanceui-system:packages/styler/src/sheet.ts
- createSheet({ layer }) does not put styled() output in a layernpm:@vitus-labs/styler/README.md
- Development warning on hash collisionui-system:packages/styler/src/sheet.ts