Theme Toggle
patchui/theme-toggleFreeComponent
patchui publishes no description for this item.
Install it
npx shadcn@latest add https://ui.hotfix.jobs/r/theme-toggle.jsonSource
1"use client";23import {4 useCallback,5 useEffect,6 useState,7 useSyncExternalStore,8} from "react";9import type * as React from "react";10import { Button } from "@/components/ui/button";11import { cn } from "@/lib/utils";1213import { Moon, Sun } from "@phosphor-icons/react/dist/ssr";14export type Theme = "light" | "dark" | "system";15export type ResolvedTheme = "light" | "dark";1617function getSystemTheme(): ResolvedTheme {18 if (typeof window === "undefined") return "light";19 return window.matchMedia("(prefers-color-scheme: dark)").matches20 ? "dark"21 : "light";22}2324function resolveTheme(theme: Theme): ResolvedTheme {25 return theme === "system" ? getSystemTheme() : theme;26}2728const emptySubscribe = () => () => {};2930function useMounted() {31 return useSyncExternalStore(32 emptySubscribe,33 () => true,34 () => false,35 );36}3738function ThemeIcon({39 isDark,40 mounted,41 className,42}: {43 isDark: boolean;44 mounted: boolean;45 className?: string;46}): React.ReactElement {47 if (!mounted) {48 return <span className={cn("inline-block", className)} />;49 }5051 return (52 <span53 className={cn(54 "relative inline-flex items-center justify-center overflow-hidden",55 className,56 )}57 >58 <span59 className={cn(60 "absolute inset-0 flex items-center justify-center transition-[opacity,transform] duration-[var(--duration-state)] ease-[var(--ease-standard)]",61 isDark ? "scale-100 rotate-0 opacity-100" : "scale-0 rotate-90 opacity-0",62 )}63 >64 <Sun className="size-full" aria-hidden />65 </span>66 <span67 className={cn(68 "absolute inset-0 flex items-center justify-center transition-[opacity,transform] duration-[var(--duration-state)] ease-[var(--ease-standard)]",69 isDark ? "scale-0 -rotate-90 opacity-0" : "scale-100 rotate-0 opacity-100",70 )}71 >72 <Moon className="size-full" aria-hidden />73 </span>74 </span>75 );76}7778export interface ThemeToggleProps79 extends Omit<React.ComponentProps<"button">, "children"> {80 theme?: Theme;81 onThemeChange?: (theme: ResolvedTheme) => void;82 /** localStorage key for persisting the theme. Set to `false` to disable. */83 storageKey?: string | false;84 /** Whether to apply the `.dark` class to `<html>`. */85 applyClass?: boolean;86 size?: "sm" | "md" | "lg";87}8889const iconSizeBySize: Record<NonNullable<ThemeToggleProps["size"]>, string> = {90 sm: "size-3.5",91 md: "size-4",92 lg: "size-5",93};9495// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from patchui
All 51 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Accordionaccordion | patchui | Components | Free | 2 deps | |
| Avataravatar | patchui | Components | Free | 3 deps | |
| Badgebadge | patchui | Components | Free | 2 deps | |
| Breadcrumbbreadcrumb | patchui | Components | Free | 1 dep | |
| Buttonbutton | patchui | Components | Free | 2 deps | |
| Calendarcalendar | patchui | Components | Free | 1 dep | |
| Cardcard | patchui | Components | Free | 1 dep | |
| Checkboxcheckbox | patchui | Components | Free | 2 deps |
