Server rendering and Server Components
Toned works with server rendering, static generation and React Server Components. CSS is generated at build time, so the server sends complete markup with nothing to inject, and a Server Component can resolve its styles without hooks or client JavaScript.
- SSR and static generation: the same sheets, renderer inputs and theme produce the server markup and the first client render, so hydration matches.
- Server Components:
useStylesandcreateElementsare the same calls as on the client. There they resolve without hooks or context, so no'use client'is needed. - Client Components: the same imports give the hook-based versions, which read their renderer from
TonedProvider.
Vite delivery
Pass the complete system returned by defineSystem and every sheet, including lazy routes. The raw token dictionary alone does not carry a stylesheet inventory or system namespace.
system.ts and styles.ts are the modules from Getting Started.
import toned from '@toned/core/vite'
import react from '@vitejs/plugin-react'
import { defineConfig } from 'vite'
import { buttonStyles } from './styles.ts'
import { ui } from './system.ts'
export default defineConfig({
plugins: [toned({
system: ui,
sheets: [buttonStyles],
inputs: ['system.ts', 'styles.ts'],
}), react()],
})Import virtual:toned.css in the application and use virtual:toned.manifest to create the renderer. See Getting Started for the complete provider setup. The production CSS file must be linked from the initial HTML.
Without Vite
// Run during the application build.
import { mkdir, writeFile } from 'node:fs/promises'
import { buildStyles } from '@toned/core/build'
import { buttonStyles } from './styles.ts'
import { ui } from './system.ts'
const artifact = buildStyles(ui, { sheets: [buttonStyles] })
await mkdir('public/assets', { recursive: true })
await writeFile('public/assets/toned.css', artifact.css)
await writeFile('public/assets/toned.manifest.json', JSON.stringify(artifact.manifest))Load that manifest into createWebRenderer and pass the renderer and web host to TonedProvider. Missing or stale build inputs are diagnosed instead of repaired by a browser-side injection fallback.
Render and hydrate
import { renderToString } from 'react-dom/server'
import { App } from './App.tsx'
export function render() {
return renderToString(<App />)
}import { hydrateRoot } from 'react-dom/client'
import { App } from './App.tsx'
const root = document.getElementById('root')
if (!root) throw new Error('Missing application root')
hydrateRoot(root, <App />)For static generation, insert the rendered HTML into a template linking the generated stylesheet. Use matching theme values and variants for server output and hydration. Request-specific tokens belong in provider inputs, not mutations of a shared global configuration.
React Server Components
A Server Component cannot use hooks or context, so @toned/react has a second build for them. The bundler picks it through the react-server export condition; your imports do not change. Write the component as you would on the client:
// A Server Component: no 'use client'.
import { createElements } from '@toned/react'
import { buttonStyles } from './styles.ts'
const S = createElements(buttonStyles)
export function SaveButton({ size }: { size: 's' | 'm' }) {
return (
<S size={size}>
<S.Root as="button" type="button">
<S.Label as="span">Save</S.Label>
</S.Root>
</S>
)
}With no context to read, the server build finds its renderer in a registry. Register the renderer once, where you create it:
import manifest from 'virtual:toned.manifest'
import { createWebRenderer } from '@toned/core/server'
import { registerRenderer } from '@toned/react'
import { ui } from './system.ts'
export const renderer = registerRenderer(createWebRenderer(ui, { manifest }))What is different on the server:
- Nothing is interactive. Hover, focus and media conditions are CSS, so they still apply; a variant that follows browser state needs a Client Component.
- A family hands its variants to the parts written inside its own JSX, however deeply they are nested there. A part rendered by another component is out of its reach and throws; use
useStyles(sheet, variants)in that component instead. TonedProviderandStyleOverridesneed context, and their props cannot cross from a Server Component to a client one. Render them from a file marked'use client'.
Use a Client Component when the styles depend on state held in the browser. The component below is the same code as above plus state; it needs a TonedProvider above it, as in Getting Started.
'use client'
import { createElements } from '@toned/react'
import { useState } from 'react'
import { buttonStyles } from './styles.ts'
const S = createElements(buttonStyles)
export function LikeButton() {
const [liked, setLiked] = useState(false)
return (
<S size={liked ? 'm' : 's'}>
<S.Root as="button" type="button" onClick={() => setLiked(!liked)}>
<S.Label as="span">{liked ? 'Liked' : 'Like'}</S.Label>
</S.Root>
</S>
)
}Pure server resolution
@toned/core/server can resolve part props without importing React, reading browser globals or mounting hosts: renderer.resolve(sheet, { variants }) returns the props for each part. React SSR uses the separate React binding. Module-level createElements creates stable component identities without reading host configuration.
@toned/core/dev/inject is an explicit development helper. It is not the production or SSR delivery path.