-
Notifications
You must be signed in to change notification settings - Fork 811
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use next-themes for handling theme (#712)
- Loading branch information
1 parent
2679d78
commit e3f76b8
Showing
10 changed files
with
69 additions
and
71 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
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,33 +1,44 @@ | ||
"use client"; | ||
|
||
import { useEffect } from "react"; | ||
import { useDarkMode, useIsMounted } from "usehooks-ts"; | ||
import { useEffect, useState } from "react"; | ||
import { useTheme } from "next-themes"; | ||
import { MoonIcon, SunIcon } from "@heroicons/react/24/outline"; | ||
|
||
export const SwitchTheme = ({ className }: { className?: string }) => { | ||
const { isDarkMode, toggle } = useDarkMode({ initializeWithValue: false }); | ||
const isMounted = useIsMounted(); | ||
const { setTheme, resolvedTheme } = useTheme(); | ||
const [mounted, setMounted] = useState(false); | ||
|
||
const isDarkMode = resolvedTheme === "dark"; | ||
|
||
const handleToggle = () => { | ||
if (isDarkMode) { | ||
setTheme("light"); | ||
return; | ||
} | ||
setTheme("dark"); | ||
}; | ||
|
||
useEffect(() => { | ||
const body = document.body; | ||
body.setAttribute("data-theme", isDarkMode ? "scaffoldEthDark" : "scaffoldEth"); | ||
}, [isDarkMode]); | ||
setMounted(true); | ||
}, []); | ||
|
||
if (!mounted) return null; | ||
|
||
return ( | ||
<div className={`flex space-x-2 text-sm ${className}`}> | ||
<div className={`flex space-x-2 h-8 items-center justify-center text-sm ${className}`}> | ||
<input | ||
id="theme-toggle" | ||
type="checkbox" | ||
className="toggle toggle-primary bg-primary hover:bg-primary border-primary" | ||
onChange={toggle} | ||
onChange={handleToggle} | ||
checked={isDarkMode} | ||
/> | ||
{isMounted() && ( | ||
{ | ||
<label htmlFor="theme-toggle" className={`swap swap-rotate ${!isDarkMode ? "swap-active" : ""}`}> | ||
<SunIcon className="swap-on h-5 w-5" /> | ||
<MoonIcon className="swap-off h-5 w-5" /> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"use client"; | ||
|
||
import * as React from "react"; | ||
import { ThemeProvider as NextThemesProvider } from "next-themes"; | ||
import { type ThemeProviderProps } from "next-themes/dist/types"; | ||
|
||
export const ThemeProvider = ({ children, ...props }: ThemeProviderProps) => { | ||
return <NextThemesProvider {...props}>{children}</NextThemesProvider>; | ||
}; |
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 was deleted.
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