BlockDex

Components

Registries

Free blocks

Method

BlockDex

Components

Registries

Free blocks

Method

Open API

BlockDex

Components

Registries

Free blocks

Method

Open API

Components/magicui/animated-theme-toggler

Theme Toggler

magicui/animated-theme-toggler
FreeComponent

Theme toggle with View Transitions and animated clip-path masks (circle, polygons, star), optional viewport-centered origin.

Live preview

live · rendered by the registry
Rendered by magicui on their own page — this is the component running, not a screenshot of it.

Install it

npx shadcn@latest add https://magicui.design/r/animated-theme-toggler.json

Source

registry/magicui/animated-theme-toggler.tsxmagicui.design/r/animated-theme-toggler.json · first 6 KB
1"use client"
2
3import { useCallback, useEffect, useRef, useState } from "react"
4import { Moon, Sun } from "lucide-react"
5import { flushSync } from "react-dom"
6
7import { cn } from "@/lib/utils"
8
9export type TransitionVariant =
10 | "circle"
11 | "square"
12 | "triangle"
13 | "diamond"
14 | "hexagon"
15 | "rectangle"
16 | "star"
17
18interface AnimatedThemeTogglerProps extends React.ComponentPropsWithoutRef<"button"> {
19 duration?: number
20 variant?: TransitionVariant
21 /** When true, the transition expands from the viewport center instead of the button center. */
22 fromCenter?: boolean
23 /**
24 * Controlled theme value. When provided, the parent owns persistence
25 * (e.g. `next-themes`) and this component will not write to localStorage.
26 */
27 theme?: "light" | "dark"
28 /** Called on toggle. Pair with `theme` for controlled usage. */
29 onThemeChange?: (theme: "light" | "dark") => void
30}
31
32function polygonCollapsed(point: string, vertexCount: number): string {
33 const pairs = Array.from({ length: vertexCount }, () => point).join(", ")
34 return `polygon(${pairs})`
35}
36
37// All coordinates are percentages of the snapshot reference box: Chrome 150
38// renders absolute px clip-path coordinates on ::view-transition-new(root)
39// unscaled on fractional display scales (e.g. Windows 150%) for the first
40// transition after load, so px values land at the wrong position (#989).
41function getThemeTransitionClipPaths(
42 variant: TransitionVariant,
43 cx: number,
44 cy: number,
45 maxRadius: number,
46 viewportWidth: number,
47 viewportHeight: number
48): [string, string] {
49 const toX = (x: number) => `${(x / viewportWidth) * 100}%`
50 const toY = (y: number) => `${(y / viewportHeight) * 100}%`
51 const point = (x: number, y: number) => `${toX(x)} ${toY(y)}`
52 // circle() percentage radii resolve against hypot(w, h) / sqrt(2) of the reference box.
53 const toRadius = (r: number) =>
54 `${(r / (Math.hypot(viewportWidth, viewportHeight) / Math.SQRT2)) * 100}%`
55
56 switch (variant) {
57 case "circle":
58 return [
59 `circle(0% at ${point(cx, cy)})`,
60 `circle(${toRadius(maxRadius)} at ${point(cx, cy)})`,
61 ]
62 case "square": {
63 const halfW = Math.max(cx, viewportWidth - cx)
64 const halfH = Math.max(cy, viewportHeight - cy)
65 const halfSide = Math.max(halfW, halfH) * 1.05
66 const end = [
67 point(cx - halfSide, cy - halfSide),
68 point(cx + halfSide, cy - halfSide),
69 point(cx + halfSide, cy + halfSide),
70// … truncated

What it pulls in

npm packages

  • lucide-react

Files it writes

  • registry/magicui/animated-theme-toggler.tsx

Facts

Registry
magicui
Type
registry:ui
Access
Free
Files written
1
Licence
the repository states none
First indexed
2026-08-02
Last confirmed
yesterday

Go to the source

Docs on magicui magicui.design magicuidesign/magicui on GitHub Raw registry item This item as JSON

More from magicui

All 247 items
Same registry, same kind — the ones you would have found next
ComponentRegistryKindAccessInstallsCommand
AndroidandroidmagicuiComponentsFreeno deps
Animated Beamanimated-beammagicuiComponentsFree1 dep
Animated Circular Progress Baranimated-circular-progress-barmagicuiComponentsFreeno deps
Animated Gradient Textanimated-gradient-textmagicuiComponentsFreeno deps
Animated Grid Patternanimated-grid-patternmagicuiComponentsFree1 dep
Animated Listanimated-listmagicuiComponentsFree1 dep
Animated Shiny Textanimated-shiny-textmagicuiComponentsFreeno deps
Aurora Textaurora-textmagicuiComponentsFreeno deps
BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex

BlockDex

Built by

Kynth Studio

Index

Components
Registries
Method
Open API

Guides

shadcn component libraries
Best shadcn registries
Free shadcn blocks

Reference

Corpus counts
Crawl health
hello@kynth.studio
Privacy
Terms

BlockDex