Spaceman React Theme Animation

Theme animation in TanStack Start

Switch between light, dark and system with a view transition, and swap color themes on top. Every page is prerendered, and a script in the document head sets the theme before the page paints.

Light, dark and system
Light and dark only
Color theme
themesystemresolvedlightcolordefault

Usage

Install

npm install @space-man/react-theme-animation motion @radix-ui/react-select

Provider and controls

// src/routes/__root.tsx
import {
  TanStackStartThemeScript,
  TanStackThemeProvider,
} from '@space-man/react-theme-animation'

<html lang="en" suppressHydrationWarning>
  <head>
    <TanStackStartThemeScript defaultTheme="system" systemThemeMode="css" />
    <HeadContent />
  </head>
  <body>
    <TanStackThemeProvider
      defaultTheme="system"
      systemThemeMode="css"
      colorThemes={['default', 'blue', 'green', 'purple', 'caffeine', 'mono', 'supabase']}
    >
      {children}
    </TanStackThemeProvider>
    <Scripts />
  </body>
</html>

TanStack Start guide

How this is wired

  1. src/routes/__root.tsxRenders TanStackStartThemeScript in the head, so the theme class is on <html> before the page paints, and wraps the app in TanStackThemeProvider.
  2. vite.config.tsTurns on prerendering. Every route is written to static HTML at build time, so the guide needs no server.
  3. src/components/theme/ThemeToggle.tsxA toggle built on useThemeAnimation. The ref marks the button as the origin of the circle reveal.
  4. src/styles.cssLight, .dark and .theme-* variables. With systemThemeMode="css", a .system class follows the OS setting through a media query.