This repository demonstrates how to create a beautiful, super smooth, animated theme toggle using the View Transitions API, shadcn/ui, and Next.js. Find a live demo here.
demo.mp4
- Animated Theme Toggle:
- Uses the View Transitions API for a smooth, circular reveal animation when switching between light and dark mode.
- Animation is implemented in React with TypeScript
- The animation is scoped to the theme-toggle and does not affect other transitions.
- No global CSS overrides. The animation is handled dynamically in the component.
- shadcn/ui Integration:
- Works with shadcn/ui's out of the box.
- Next.js App Router:
- Built with the Next.js App Router and TypeScript.
This project is massively inspired by:
- tweakcn.com (GitHub repo) — a powerful visual theme editor for shadcn/ui components.
- Full-page theme toggle animation with View Transitions API — a fantastic video by Akash Hamirwasia explaining the View Transitions API and theme toggle animation techniques.
The theme toggle logic is implemented in src/components/theme-toggle.tsx. When the button is clicked:
- The View Transitions API is used to animate the theme change.
- A circular reveal animation is triggered from the click position.
- The default browser view transition is temporarily overridden using a dynamically injected
<style>tag, so only the custom animation is shown. - The override is removed after the animation completes.
This approach ensures the animation is only applied to the theme toggle, and does not interfere with other view transitions in your app like navigations.
Run the development server:
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun devOpen http://localhost:3000 to see the animated theme toggle in action.
- src/components/theme-toggle.tsx — Main implementation of the animated theme toggle.
- View Transitions API