Skip to content

Commit

Permalink
feat: upgrade theme button to theme dropdown (#33)
Browse files Browse the repository at this point in the history
Theme button is upgraded to theme dropdown to include
system as an option.

Resolves #29
  • Loading branch information
satnaing authored Sep 22, 2024
1 parent 3718cc4 commit bd30be4
Showing 1 changed file with 43 additions and 10 deletions.
53 changes: 43 additions & 10 deletions src/components/theme-switch.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { IconMoon, IconSun } from '@tabler/icons-react'
import { useEffect } from 'react'
import { IconCheck, IconMoon, IconSun } from '@tabler/icons-react'
import { cn } from '@/lib/utils'
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu'
import { useTheme } from './theme-provider'
import { Button } from './custom/button'
import { useEffect } from 'react'

export default function ThemeSwitch() {
const { theme, setTheme } = useTheme()
Expand All @@ -14,14 +21,40 @@ export default function ThemeSwitch() {
metaThemeColor && metaThemeColor.setAttribute('content', themeColor)
}, [theme])

console.log(theme)

return (
<Button
size='icon'
variant='ghost'
className='rounded-full'
onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}
>
{theme === 'light' ? <IconMoon size={20} /> : <IconSun size={20} />}
</Button>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant='ghost' size='icon' className='scale-95 rounded-full'>
<IconSun className='size-[1.2rem] rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0' />
<IconMoon className='absolute size-[1.2rem] rotate-90 scale-0 transition-all dark:rotate-0 dark:scale-100' />
<span className='sr-only'>Toggle theme</span>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align='end'>
<DropdownMenuItem onClick={() => setTheme('light')}>
Light{' '}
<IconCheck
size={14}
className={cn('ml-auto', theme !== 'light' && 'hidden')}
/>
</DropdownMenuItem>
<DropdownMenuItem onClick={() => setTheme('dark')}>
Dark
<IconCheck
size={14}
className={cn('ml-auto', theme !== 'dark' && 'hidden')}
/>
</DropdownMenuItem>
<DropdownMenuItem onClick={() => setTheme('system')}>
System
<IconCheck
size={14}
className={cn('ml-auto', theme !== 'system' && 'hidden')}
/>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
)
}

0 comments on commit bd30be4

Please sign in to comment.