Animated Theme Toggler

An Animated theme toggler component, fully customizable using Tailwind CSS.

Installation

pnpm dlx shadcn@latest add https://sase-ui.vercel.app/r/animated-theme-toggler.json

Usage

import { ThemeToggleButton, useThemeTransition } from "@/components/ui/animated-theme-toggler"
import { useTheme } from "next-themes"
 
export function ThemeToggle() {
  const { setTheme, resolvedTheme } = useTheme()
  const { startTransition } = useThemeTransition()
 
  const toggleTheme = () => {
    const newTheme = resolvedTheme === "dark" ? "light" : "dark"
    startTransition(() => setTheme(newTheme))
  }
 
  return (
    <ThemeToggleButton
      theme={resolvedTheme === "dark" ? "dark" : "light"}
      onClick={toggleTheme}
    />
  )
}

Examples

Styled Variations

You can easily customize the appearance using className and variants:

// Circle variant (default)
<ThemeToggleButton variant="circle" />
 
// Circle Blur variant
<ThemeToggleButton variant="circle-blur" />
 
// Polygon variant
<ThemeToggleButton variant="polygon" />
 
// GIF variant
<ThemeToggleButton
  variant="gif"
  url="https://media.giphy.com/media/KBbr4hHl9DSahKvInO/giphy.gif"
/>
 
// With label
<ThemeToggleButton showLabel={true} />
 
// Custom start position
<ThemeToggleButton start="top-right" />
 
// Custom speed (slow)
<ThemeToggleButton speed={1000} />

GIF Mask Animations

Animation Positions

Circle animations can start from different positions for directional effects:

Props

PropTypeDefaultDescription
themestring"light"Current theme value ("light" or "dark")
onClickfunc-Callback function called when button is clicked
showLabelbooleanfalseWhether to show the text label
classNamestring-Additional classes to be added to the component
variantstring"circle"Animation variant: circle, circle-blur, gif, polygon
startstring"center"Start position: center, top-left, top-right, bottom-left, bottom-right
speednumber500Animation duration in milliseconds
urlstring-URL for the GIF mask (required when variant is gif)

Credits