animated-theme-toggle
jolyui-ui/animated-theme-toggleFreeComponent
jolyui/ui publishes no description for this item.
Install it
npx shadcn@latest add https://jolyui.dev/r/animated-theme-toggle.jsonSource
1"use client";23import { motion, useMotionValue, useTransform } from "motion/react";4import { useTheme } from "next-themes";5import { useEffect, useState } from "react";6import { cn } from "@/lib/utils";7import { Button } from "./button";89export const AnimatedThemeToggle = ({ className }: { className?: string }) => {10 const { setTheme, resolvedTheme } = useTheme();11 const [mounted, setMounted] = useState(false);1213 // Avoid hydration mismatch14 useEffect(() => {15 setMounted(true);16 }, []);1718 const isDark = mounted ? resolvedTheme === "dark" : false;1920 const toggleTheme = () => {21 setTheme(isDark ? "light" : "dark");22 };2324 // Show a placeholder during SSR to avoid hydration mismatch25 if (!mounted) {26 return (27 <Button className={cn("px-2.5", className)} variant="outline" disabled>28 <div className="h-5 w-5" />29 </Button>30 );31 }3233 return (34 <Button35 onClick={toggleTheme}36 className={cn("px-2.5", className)}37 variant="outline"38 aria-label={isDark ? "Switch to light mode" : "Switch to dark mode"}39 >40 <SolarSwitch isDark={isDark} />41 </Button>42 );43};4445const SolarSwitch = ({ isDark }: { isDark: boolean }) => {46 const duration = 0.7;4748 const moonVariants = {49 checked: {50 scale: 1,51 },52 unchecked: {53 scale: 0,54 },55 };5657 const sunVariants = {58 checked: {59 scale: 0,60 },61 unchecked: {62 scale: 1,63 },64 };65 const scaleMoon = useMotionValue(isDark ? 1 : 0);66 const scaleSun = useMotionValue(isDark ? 0 : 1);67 const pathLengthMoon = useTransform(scaleMoon, [0.6, 1], [0, 1]);68 const pathLengthSun = useTransform(scaleSun, [0.6, 1], [0, 1]);6970 return (71 <motion.div animate={isDark ? "checked" : "unchecked"}>72 <motion.svg73 width="20"74 height="20"75 viewBox="0 0 25 25"76 fill="none"77 xmlns="http://www.w3.org/2000/svg"78 >79 <motion.path80 d="M12.4058 17.7625C15.1672 17.7625 17.4058 15.5239 17.4058 12.7625C17.4058 10.0011 15.1672 7.76251 12.4058 7.76251C9.64434 7.76251 7.40576 10.0011 7.40576 12.7625C7.40576 15.5239 9.64434 17.7625 12.4058 17.7625Z"81 stroke="currentColor"82 strokeWidth="2"83 strokeLinecap="round"84 strokeLinejoin="round"85 variants={sunVariants}86 custom={isDark}87 transition={{ duration }}88 style={{89 pathLength: pathLengthSun,90 scale: scaleSun,91 }}92 />93 <motion.path94// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from jolyui/ui
All 199 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| ai-prompt-boxai-prompt-box | jolyui/ui | Components | Free | 4 deps | |
| animated-beamanimated-beam | jolyui/ui | Components | Free | 1 dep | |
| animated-tableanimated-table | jolyui/ui | Components | Free | 2 deps | |
| animated-toastanimated-toast | jolyui/ui | Components | Free | 2 deps | |
| animated-tooltipanimated-tooltip | jolyui/ui | Components | Free | 1 dep | |
| bento-gridbento-grid | jolyui/ui | Components | Free | no deps | |
| buttonbutton | jolyui/ui | Components | Free | 3 deps | |
| calendercalender | jolyui/ui | Components | Free | 3 deps |
