A row of miniature app previews, one per theme, with a ring that marks the one you pick.
Hover a preview and a faint ring shows around it; click it and a solid ring marks it as selected. Arrow keys move between themes and select as they go; Home and End jump to the first and last.
Options you can pass to customize this component.
themesThemeOption[]The themes to show, each with a value, a label and a palette of six colors for its preview. Defaults to dark and light.
valuestringnullThe selected theme. Pass it to control the selector yourself; leave it out to let the component track its own state. Pass null until your theme is known, like during server rendering, so nothing is selected yet.
defaultValuestringStarting theme when uncontrolled. Falls back to the first theme.
onChange(value: string) => voidFires with the new value when a different theme is picked.
labelsbooleanShows each theme's label under its preview, brightened on the selected one.
disabledbooleanBlocks selection and dims every preview.
classNamestringExtra classes for the row that wraps the previews.
itemClassNamestringExtra classes for each preview, for sizing. Each one is w-40 at a 4:3 ratio by default, and everything inside scales with it.
npx shadcn@latest add swamimalode07/rare-ui/theme-selector"use client"import { useSyncExternalStore } from "react"import { useTheme } from "next-themes"import ThemeSelector from "@/components/ui/theme-selector"const subscribe = () => () => {}export function Demo() {const { resolvedTheme, setTheme } = useTheme()// the server can't know the theme, so select nothing until hydration is doneconst hydrated = useSyncExternalStore(subscribe, () => true, () => false)return (<ThemeSelectorvalue={hydrated ? (resolvedTheme ?? null) : null}onChange={setTheme}/>)}// No theme library? Toggle the class yourself:// const [theme, setTheme] = useState<string | null>(null)// useEffect(() => setTheme(localStorage.theme ?? "dark"), [])// useEffect(() => {// if (!theme) return// document.documentElement.classList.toggle("dark", theme === "dark")// localStorage.theme = theme// }, [theme])// <ThemeSelector value={theme} onChange={setTheme} />
Click the code icon in the top-right corner to view the source code.
Most components here are recreations of great work from around the web. I don't claim to be the original creator - this is my attempt to reverse-engineer, replicate, and often add a few extra features. I've tried to credit everyone; if I missed someone, let me know.
1
Theme
Pick how the interface looks.