TanStack Start
Satin in TanStack Start: the stylesheet linked from the root route, dark mode on the html element.
Install
TanStack Start renders on the server and builds with Vite. Satin needs React 19; the fonts are optional.
npm i @uisatin/ui @fontsource-variable/geist @fontsource-variable/geist-monoRoot route
Import the stylesheet with ?url and list it in the root route's head. <HeadContent /> renders it as a <link> in the server-rendered HTML, so the first paint is already styled.
The root component renders <html> itself, so data-theme goes there, with the theme stylesheet and a script that applies the saved choice before the first paint. With nothing saved, system follows the operating system in CSS alone.
// src/routes/__root.tsx
/// <reference types="vite/client" />
import { createRootRoute, HeadContent, Outlet, Scripts } from '@tanstack/react-router'
import { themeCss } from '@uisatin/ui/theme'
import satinCss from '@uisatin/ui/styles.css?url'
import appCss from '../app.css?url'
export const Route = createRootRoute({
head: () => ({
meta: [{ charSet: 'utf-8' }, { name: 'viewport', content: 'width=device-width, initial-scale=1' }],
links: [
{ rel: 'stylesheet', href: satinCss },
{ rel: 'stylesheet', href: appCss },
],
}),
component: RootComponent,
})
const theme = themeCss({ brand: '#E5533D', radius: 1 })
// Runs before the first paint: the saved choice, or follow the system.
const themeScript = `try { document.documentElement.dataset.theme = localStorage.getItem('theme') || 'system' } catch {}`
function RootComponent() {
return (
// The script may change data-theme before React hydrates; that difference is expected.
<html lang="en" data-theme="system" suppressHydrationWarning>
<head>
<HeadContent />
<script dangerouslySetInnerHTML={{ __html: themeScript }} />
<style dangerouslySetInnerHTML={{ __html: theme }} />
</head>
<body>
<Outlet />
<Scripts />
</body>
</html>
)
}/* src/app.css */
/* Geist under the names Satin's tokens ask for; @fontsource-variable/geist registers "Geist Variable". */
@font-face {
font-family: 'Geist';
src: url('@fontsource-variable/geist/files/geist-latin-wght-normal.woff2') format('woff2-variations');
font-weight: 100 900;
font-display: swap;
}
@font-face {
font-family: 'Geist Mono';
src: url('@fontsource-variable/geist-mono/files/geist-mono-latin-wght-normal.woff2') format('woff2-variations');
font-weight: 100 900;
font-display: swap;
}
/* Satin styles its own elements, not the page: paint the page from its tokens so dark mode reaches it. */
body {
margin: 0;
background: var(--sa-bg-canvas);
color: var(--sa-text-default);
}Theme toggle
The toggle that saves the choice is in the Dark mode guide. To render the theme on the server instead of in a script, read the same value from a cookie in the root route and set the attribute from it, as the React Router guide does.
Something wrong or missing on this page?
Report a problemSuggest an improvement