theme-button
shuip/theme-buttonFreeComponent
shuip publishes no description for this item.
Live preview
live · rendered by the registry
Rendered by shuip on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://shuip.plvo.dev/r/theme-button.jsonSource
1'use client';23import type { VariantProps } from 'class-variance-authority';4import { Laptop, Moon, Sun } from 'lucide-react';5import { useTheme } from 'next-themes';6import * as React from 'react';7import { Button, type buttonVariants } from '@/components/ui/button';89type ButtonProps = React.ComponentProps<'button'> &10 VariantProps<typeof buttonVariants> & {11 asChild?: boolean;12 };1314type Theme = 'system' | 'light' | 'dark';1516export interface ThemeButtonProps extends ButtonProps {17 withText?: boolean;18}1920export function ThemeButton({ withText, ...props }: ThemeButtonProps) {21 const { theme, setTheme } = useTheme();22 const [currentTheme, setCurrentTheme] = React.useState<Theme>('system');2324 React.useEffect(() => {25 setCurrentTheme(theme as Theme);26 }, [theme]);2728 const cycleTheme = () => {29 const themes: Theme[] = ['system', 'light', 'dark'];30 const currentIndex = themes.indexOf(currentTheme);31 const nextIndex = (currentIndex + 1) % themes.length;32 setTheme(themes[nextIndex]);33 };3435 const getThemeIcon = () => {36 switch (currentTheme) {37 case 'system':38 return <Laptop className='size-[1.2rem]' />;39 case 'light':40 return <Sun className='size-[1.2rem]' />;41 case 'dark':42 return <Moon className='size-[1.2rem]' />;43 }44 };4546 const getThemeText = () => {47 return currentTheme.charAt(0).toUpperCase() + currentTheme.slice(1);48 };4950 return (51 <Button variant='outline' size={withText ? 'default' : 'icon'} onClick={cycleTheme} {...props}>52 {getThemeIcon()}53 {withText && <span className='ml-2 capitalize'>{getThemeText()}</span>}54 <span className='sr-only'>Toggle theme</span>55 </Button>56 );57}
What it pulls in
npm packages
Other registry items
Files it writes
More from shuip
All 52 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| confirmation-dialogconfirmation-dialog | shuip | Components | Free | 1 dep | |
| copy-buttoncopy-button | shuip | Components | Free | 3 deps | |
| hover-revealhover-reveal | shuip | Components | Free | 1 dep | |
| rhf-address-fieldrhf-address-field | shuip | Components | Free | 5 deps · 2 files | |
| rhf-autocomplete-fieldrhf-autocomplete-field | shuip | Components | Free | 4 deps | |
| rhf-checkbox-fieldrhf-checkbox-field | shuip | Components | Free | 3 deps | |
| rhf-combobox-fieldrhf-combobox-field | shuip | Components | Free | 5 deps | |
| rhf-date-fieldrhf-date-field | shuip | Components | Free | 6 deps |
