Skip to content
satin

React Router

Satin in React Router framework mode: styles and theme in the root route, dark mode on the html element.

Install

This is React Router 7 in framework mode (npx create-react-router@latest), which renders on the server and runs on Vite. The fonts are optional.

Terminal
npm i @uisatin/ui @fontsource-variable/geist @fontsource-variable/geist-mono

Root route

Link the stylesheet from links with Vite's ?url import, so it's a <link> in the server-rendered head and the page never paints unstyled. A side-effect import '@uisatin/ui/styles.css' in root.tsx works too.

themeCss runs once, at module level, and lands in a <style> in the head. The Layout export renders <html>, so that's where data-theme goes: the root loader reads the reader's choice from a cookie and the page arrives in the right theme, with no flash.

app/root.tsx
// app/root.tsx
import { Links, Meta, Outlet, Scripts, ScrollRestoration, useRouteLoaderData } from 'react-router'
import { themeCss } from '@uisatin/ui/theme'
import satinCss from '@uisatin/ui/styles.css?url'
import appCss from './app.css?url'
import type { Route } from './+types/root'

export const links: Route.LinksFunction = () => [
  { rel: 'stylesheet', href: satinCss },
  { rel: 'stylesheet', href: appCss },
]

// light, dark or system, saved by the theme toggle. system follows the OS in CSS, no script.
export function loader({ request }: Route.LoaderArgs) {
  const theme = request.headers.get('Cookie')?.match(/(?:^|;\s*)theme=(light|dark|system)/)?.[1]
  return { theme: theme ?? 'system' }
}

const theme = themeCss({ brand: '#E5533D', radius: 1 })

export function Layout({ children }: { children: React.ReactNode }) {
  // Layout also renders the error page, when the loader may not have run.
  const data = useRouteLoaderData<typeof loader>('root')

  return (
    <html lang="en" data-theme={data?.theme ?? 'system'}>
      <head>
        <meta charSet="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <Meta />
        <Links />
        <style dangerouslySetInnerHTML={{ __html: theme }} />
      </head>
      <body>
        {children}
        <ScrollRestoration />
        <Scripts />
      </body>
    </html>
  )
}

export default function App() {
  return <Outlet />
}
app/app.css
/* app/app.css */

/* Geist and Geist Mono, the families Satin's tokens ask for. */
@import '@fontsource-variable/geist';
@import '@fontsource-variable/geist-mono';

/* 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);
}

Saving the choice

Use the toggle from the Dark mode guide and save the choice in the cookie the loader reads, instead of (or as well as) localStorage.

Theme toggle
// In the toggle's onValueChange, next to setting the attribute:
document.documentElement.dataset.theme = next
document.cookie = `theme=${next}; path=/; max-age=31536000; samesite=lax`