generated from mateusfg7/nextjs-setup
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
66 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,57 @@ | ||
'use client' | ||
|
||
import { Menu } from '@headlessui/react' | ||
import { Moon, Sun, Desktop } from '@phosphor-icons/react' | ||
import { useTheme } from 'next-themes' | ||
import { useEffect, useState } from 'react' | ||
import { FiMoon, FiSun } from 'react-icons/fi' | ||
|
||
export const ToggleTheme = () => { | ||
const [mounted, setMounted] = useState(false) | ||
|
||
const { setTheme, resolvedTheme } = useTheme() | ||
|
||
const isLight = resolvedTheme === 'light' | ||
const oppositeTheme = isLight ? 'dark' : 'light' | ||
|
||
const toggleTheme = () => setTheme(oppositeTheme) | ||
|
||
useEffect(() => { | ||
setMounted(true) | ||
}, []) | ||
|
||
if (!mounted) return null | ||
|
||
return ( | ||
<button onClick={toggleTheme} className="p-1"> | ||
{isLight ? ( | ||
<FiMoon className="text-xl" title="Dark mode" /> | ||
) : ( | ||
<FiSun className="text-xl" title="Light mode" /> | ||
export function ToggleTheme() { | ||
const { setTheme, theme: currTheme } = useTheme() | ||
|
||
const SelectTheme = ({ theme }: { theme: 'light' | 'dark' | 'system' }) => ( | ||
<button | ||
onClick={() => setTheme(theme)} | ||
className={`flex w-full items-center justify-start gap-4 rounded-xl p-2 text-lg leading-none hover:bg-neutral-100 hover:dark:bg-neutral-1000 ${ | ||
currTheme === theme && 'font-bold' | ||
}`} | ||
> | ||
{theme === 'light' && ( | ||
<> | ||
<Sun weight={currTheme === theme ? 'duotone' : 'regular'} /> | ||
<span>Light</span> | ||
</> | ||
)} | ||
{theme === 'dark' && ( | ||
<> | ||
<Moon weight={currTheme === theme ? 'duotone' : 'regular'} /> | ||
<span>Dark</span> | ||
</> | ||
)} | ||
{theme === 'system' && ( | ||
<> | ||
<Desktop weight={currTheme === theme ? 'duotone' : 'regular'} /> | ||
<span>System</span> | ||
</> | ||
)} | ||
</button> | ||
) | ||
|
||
return ( | ||
<Menu as="div" className="relative inline-flex items-center"> | ||
<Menu.Button> | ||
{currTheme === 'light' && <Sun className="text-xl" />} | ||
{currTheme === 'dark' && <Moon className="text-xl" />} | ||
{currTheme === 'system' && <Desktop className="text-xl" />} | ||
</Menu.Button> | ||
<Menu.Items | ||
as="div" | ||
className="absolute right-0 top-10 origin-top-right animate-fade-down rounded-xl bg-neutral-50 p-1 outline-none animate-duration-300 dark:bg-neutral-900" | ||
> | ||
<Menu.Item as="div"> | ||
<SelectTheme theme="light" /> | ||
<SelectTheme theme="dark" /> | ||
<SelectTheme theme="system" /> | ||
</Menu.Item> | ||
</Menu.Items> | ||
</Menu> | ||
) | ||
} |