Getting Started
Components
Special Effects
Animations
Text Animations
Backgrounds
An Animated theme toggler component, fully customizable using Tailwind CSS.
@lucide/svelte
animated-theme-toggler.svelte
<script lang="ts"> import { AnimatedThemeToggler } from "$lib/components/magic/animated-theme-toggler/index.js";</script>
<AnimatedThemeToggler />Installation
pnpm dlx shadcn-svelte@latest add https://sv-animations.vercel.app/r/animated-theme-toggler.json
Install dependencies
Install the required packages for this component:
pnpm add @lucide/svelte
Copy the Source Code
Copy and paste the following code into your project:
animated-theme-toggler.svelte
<script lang="ts"> import type { ClassValue } from "svelte/elements"; import { onMount } from "svelte"; import { Moon, Sun } from "@lucide/svelte"; import { cn } from "#lib/utils.js";
interface AnimatedThemeTogglerProps { class?: ClassValue; duration?: number; }
let { class: className, duration = 400, ...props }: AnimatedThemeTogglerProps = $props();
let isDark = $state(false); let buttonRef: HTMLButtonElement | null = $state(null);
onMount(() => { const updateTheme = () => { isDark = document.documentElement.classList.contains("dark"); };
updateTheme();
const observer = new MutationObserver(updateTheme); observer.observe(document.documentElement, { attributes: true, attributeFilter: ["class"], });
return () => observer.disconnect(); });
const toggleTheme = async () => { if (!buttonRef) return;
// Check if View Transition API is supported if (!document.startViewTransition) { // Fallback for browsers that don't support View Transition API const newTheme = !isDark; isDark = newTheme; document.documentElement.classList.toggle("dark"); localStorage.setItem("theme", newTheme ? "dark" : "light"); return; }
await document.startViewTransition(() => { const newTheme = !isDark; isDark = newTheme; document.documentElement.classList.toggle("dark"); localStorage.setItem("theme", newTheme ? "dark" : "light"); }).ready;
const { top, left, width, height } = buttonRef.getBoundingClientRect(); const x = left + width / 2; const y = top + height / 2; const maxRadius = Math.hypot( Math.max(left, window.innerWidth - left), Math.max(top, window.innerHeight - top) );
document.documentElement.animate( { clipPath: [ `circle(0px at ${x}px ${y}px)`, `circle(${maxRadius}px at ${x}px ${y}px)`, ], }, { duration, easing: "ease-in-out", pseudoElement: "::view-transition-new(root)", } ); };</script>
<button bind:this={buttonRef} onclick={toggleTheme} class={cn(className)} {...props}> {#if isDark} <Sun /> {:else} <Moon /> {/if} <span class="sr-only">Toggle theme</span></button> index.ts
import AnimatedThemeToggler from "./animated-theme-toggler.svelte";export { AnimatedThemeToggler };Add Tailwind CSS
Add the following to your routes/layout.css file:
src/routes/layout.css
::view-transition-old(root),::view-transition-new(root) { animation: none; mix-blend-mode: normal;}Folder Structure
Folder Structure
src/└── lib/ └── components/ └── magic/ └── animated-theme-toggler/ ├── animated-theme-toggler.svelte └── index.tsInstall and integrate the Svelte component documented below.https://sv-animations.vercel.app/magic/docs/components/animated-theme-toggler/llms.txt
Follow the guide's installation and usage instructions.Match the project's existing structure, imports, and styles.Keep changes scoped to the component and verify it works.Reply with a short bullet list of completed changes.Usage
<script lang="ts"> import { AnimatedThemeToggler } from "#lib/components/magic/animated-theme-toggler/index.js";</script>
<AnimatedThemeToggler />Props
Animated Theme Toggler
A component for smooth theme switching with circular reveal animation.
| Name | Type | Default | Description |
|---|---|---|---|
class | ClassValue | "" | Additional CSS classes to apply |
duration | number | 400 | Duration of the animation in milliseconds |