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.
feat(ux/ui): create toggle theme button component
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use client' | ||
|
||
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" /> | ||
)} | ||
</button> | ||
) | ||
} |