Spaceman React Theme Animation
Theme animation in Next.js
Switch between light, dark and system with a view transition, and swap color themes on top. The App Router keeps its server layout, and the theme is set before the page paints.
Light, dark and system
Light and dark only
Color theme
Usage
Install
npm install @space-man/react-theme-animation motion @radix-ui/react-selectProvider and controls
// src/components/theme/theme-provider.tsx
'use client'
import { ThemeProvider } from '@space-man/react-theme-animation'
export function Providers({ children }) {
return (
<ThemeProvider
attribute="class"
defaultTheme="system"
colorThemes={['default', 'blue', 'green', 'purple', 'caffeine', 'mono', 'supabase']}
>
{children}
</ThemeProvider>
)
}
// anywhere inside the provider
import { ThemeSwitcher, ThemeSelector } from '@space-man/react-theme-animation'
<ThemeSwitcher themes={['light', 'dark', 'system']} />
<ThemeSelector colorThemes={['default', 'blue', 'green']} />Next.js App Router guide
How this is wired
- src/components/theme/theme-provider.tsxA 'use client' wrapper around ThemeProvider. It lists the color themes and injects the pre-hydration script, so the page never flashes the wrong theme.
- src/app/layout.tsxStays a server component. It renders the provider and sets suppressHydrationWarning on <html>, because the script adds the theme class before React loads.
- src/components/theme/ThemeToggle.tsxA toggle built on useThemeAnimation. The ref marks the button as the origin of the circle reveal.
- src/app/globals.cssLight, .dark and .theme-* variables, plus an @source line so Tailwind scans the package for the prebuilt controls.