-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add darkmode support with next-themes
- Loading branch information
Showing
7 changed files
with
112 additions
and
5 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use client' | ||
|
||
import { ThemeProvider } from 'next-themes' | ||
|
||
export function Providers({ children }: { children: React.ReactNode }) { | ||
return <ThemeProvider attribute="class">{children}</ThemeProvider> | ||
} |
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,54 @@ | ||
'use client' | ||
|
||
import { useTheme } from 'next-themes' | ||
import { useEffect, useState } from 'react' | ||
|
||
export default function DarkModeToggle() { | ||
const { theme, setTheme } = useTheme() | ||
const [mounted, setMounted] = useState(false) | ||
|
||
useEffect(() => { | ||
const prefersDark = | ||
window.matchMedia && | ||
window.matchMedia('(prefers-color-scheme: dark)').matches | ||
const localTheme = localStorage.getItem('theme') | ||
|
||
setTheme(localTheme || (prefersDark ? 'dark' : 'light')) | ||
setMounted(true) | ||
}, [setTheme]) | ||
|
||
useEffect(() => { | ||
if (mounted) { | ||
localStorage.setItem('theme', theme || 'light') | ||
} | ||
}, [theme, mounted]) | ||
|
||
const toggleTheme = () => { | ||
setTheme(theme === 'dark' ? 'light' : 'dark') | ||
} | ||
|
||
return ( | ||
<div className="transition duration-500 ease-in-out"> | ||
<input | ||
type="checkbox" | ||
id="darkmode" | ||
name="darkmode" | ||
className={`form-switch h-6 w-10 rounded-full transition-all duration-150 ease-out focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 ${ | ||
theme === 'dark' | ||
? 'bg-gray-800 ring-offset-gray-800' | ||
: 'bg-indigo-200 ring-offset-indigo-600' | ||
} checked:bg-current`} | ||
checked={theme === 'dark'} | ||
onChange={toggleTheme} | ||
/> | ||
<label | ||
htmlFor="darkmode" | ||
className={`${ | ||
theme === 'dark' ? 'text-gray-300' : 'text-gray-700' | ||
} ml-3 text-sm font-medium`} | ||
> | ||
Darkmode | ||
</label> | ||
</div> | ||
) | ||
} |
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