-
Notifications
You must be signed in to change notification settings - Fork 917
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 to handle theme and update usehooks-ts (#707)
- Loading branch information
1 parent
0fd0a55
commit 2d98105
Showing
15 changed files
with
92 additions
and
104 deletions.
There are no files selected for viewing
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
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,34 +1,44 @@ | ||
"use client"; | ||
|
||
import { useEffect } from "react"; | ||
import { useIsMounted } from "usehooks-ts"; | ||
import { useEffect, useState } from "react"; | ||
import { useTheme } from "next-themes"; | ||
import { MoonIcon, SunIcon } from "@heroicons/react/24/outline"; | ||
import { useDarkMode } from "~~/hooks/scaffold-eth/useDarkMode"; | ||
|
||
export const SwitchTheme = ({ className }: { className?: string }) => { | ||
const { isDarkMode, toggle } = useDarkMode(); | ||
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 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 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