Skip to content

Commit

Permalink
use next-themes to handle theme and update usehooks-ts (#707)
Browse files Browse the repository at this point in the history
  • Loading branch information
technophile-04 committed Feb 14, 2024
1 parent 0fd0a55 commit 2d98105
Show file tree
Hide file tree
Showing 15 changed files with 92 additions and 104 deletions.
13 changes: 0 additions & 13 deletions .yarn/patches/usehooks-ts-npm-2.7.2-fceffe0e43.patch

This file was deleted.

3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,5 @@
"devDependencies": {
"husky": "^8.0.1",
"lint-staged": "^13.0.3"
},
"resolutions": {
"usehooks-ts@^2.7.2": "patch:usehooks-ts@npm:^2.7.2#./.yarn/patches/usehooks-ts-npm-2.7.2-fceffe0e43.patch"
}
}
1 change: 1 addition & 0 deletions packages/nextjs/app/debug/_components/DebugContracts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function DebugContracts() {
const [selectedContract, setSelectedContract] = useLocalStorage<ContractName>(
selectedContractStorageKey,
contractNames[0],
{ initializeWithValue: false },
);

useEffect(() => {
Expand Down
7 changes: 5 additions & 2 deletions packages/nextjs/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "@rainbow-me/rainbowkit/styles.css";
import { Metadata } from "next";
import { ScaffoldEthAppWithProviders } from "~~/components/ScaffoldEthAppWithProviders";
import { ThemeProvider } from "~~/components/ThemeProvider";
import "~~/styles/globals.css";

const baseUrl = process.env.NEXT_PUBLIC_VERCEL_URL
Expand Down Expand Up @@ -43,9 +44,11 @@ export const metadata: Metadata = {

const ScaffoldEthApp = ({ children }: { children: React.ReactNode }) => {
return (
<html>
<html suppressHydrationWarning>
<body>
<ScaffoldEthAppWithProviders>{children}</ScaffoldEthAppWithProviders>
<ThemeProvider enableSystem>
<ScaffoldEthAppWithProviders>{children}</ScaffoldEthAppWithProviders>
</ThemeProvider>
</body>
</html>
);
Expand Down
14 changes: 10 additions & 4 deletions packages/nextjs/components/ScaffoldEthAppWithProviders.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"use client";

import { useEffect } from "react";
import { useEffect, useState } from "react";
import { RainbowKitProvider, darkTheme, lightTheme } from "@rainbow-me/rainbowkit";
import { useTheme } from "next-themes";
import { Toaster } from "react-hot-toast";
import { WagmiConfig } from "wagmi";
import { Footer } from "~~/components/Footer";
import { Header } from "~~/components/Header";
import { BlockieAvatar } from "~~/components/scaffold-eth";
import { ProgressBar } from "~~/components/scaffold-eth/ProgressBar";
import { useNativeCurrencyPrice } from "~~/hooks/scaffold-eth";
import { useDarkMode } from "~~/hooks/scaffold-eth/useDarkMode";
import { useGlobalState } from "~~/services/store/store";
import { wagmiConfig } from "~~/services/web3/wagmiConfig";
import { appChains } from "~~/services/web3/wagmiConnectors";
Expand Down Expand Up @@ -37,15 +37,21 @@ const ScaffoldEthApp = ({ children }: { children: React.ReactNode }) => {
};

export const ScaffoldEthAppWithProviders = ({ children }: { children: React.ReactNode }) => {
const { isDarkMode } = useDarkMode();
const { resolvedTheme } = useTheme();
const isDarkMode = resolvedTheme === "dark";
const [mounted, setMounted] = useState(false);

useEffect(() => {
setMounted(true);
}, []);

return (
<WagmiConfig config={wagmiConfig}>
<ProgressBar />
<RainbowKitProvider
chains={appChains.chains}
avatar={BlockieAvatar}
theme={isDarkMode ? darkTheme() : lightTheme()}
theme={mounted ? (isDarkMode ? darkTheme() : lightTheme()) : lightTheme()}
>
<ScaffoldEthApp>{children}</ScaffoldEthApp>
</RainbowKitProvider>
Expand Down
34 changes: 22 additions & 12 deletions packages/nextjs/components/SwitchTheme.tsx
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>
);
};
9 changes: 9 additions & 0 deletions packages/nextjs/components/ThemeProvider.tsx
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>;
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useTheme } from "next-themes";
import { useNetwork, useSwitchNetwork } from "wagmi";
import { ArrowsRightLeftIcon } from "@heroicons/react/24/solid";
import { getNetworkColor } from "~~/hooks/scaffold-eth";
import { useDarkMode } from "~~/hooks/scaffold-eth/useDarkMode";
import { getTargetNetworks } from "~~/utils/scaffold-eth";

const allowedNetworks = getTargetNetworks();
Expand All @@ -11,9 +11,10 @@ type NetworkOptionsProps = {
};

export const NetworkOptions = ({ hidden = false }: NetworkOptionsProps) => {
const { isDarkMode } = useDarkMode();
const { switchNetwork } = useSwitchNetwork();
const { chain } = useNetwork();
const { resolvedTheme } = useTheme();
const isDarkMode = resolvedTheme === "dark";

return (
<>
Expand Down
4 changes: 3 additions & 1 deletion packages/nextjs/hooks/scaffold-eth/useAutoConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ const getInitialConnector = (
*/
export const useAutoConnect = (): void => {
const wagmiWalletValue = useReadLocalStorage<string>(WAGMI_WALLET_STORAGE_KEY);
const [walletId, setWalletId] = useLocalStorage<string>(SCAFFOLD_WALLET_STORAGE_KEY, wagmiWalletValue ?? "");
const [walletId, setWalletId] = useLocalStorage<string>(SCAFFOLD_WALLET_STORAGE_KEY, wagmiWalletValue ?? "", {
initializeWithValue: false,
});
const connectState = useConnect();
useAccount({
onConnect({ connector }) {
Expand Down
4 changes: 3 additions & 1 deletion packages/nextjs/hooks/scaffold-eth/useBurnerWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ type BurnerAccount = {
* Creates a burner wallet
*/
export const useBurnerWallet = (): BurnerAccount => {
const [burnerSk, setBurnerSk] = useLocalStorage<Hex>(burnerStorageKey, newDefaultPrivateKey);
const [burnerSk, setBurnerSk] = useLocalStorage<Hex>(burnerStorageKey, newDefaultPrivateKey, {
initializeWithValue: false,
});

const publicClient = usePublicClient();
const [walletClient, setWalletClient] = useState<WalletClient<HttpTransport, Chain, PrivateKeyAccount>>();
Expand Down
43 changes: 0 additions & 43 deletions packages/nextjs/hooks/scaffold-eth/useDarkMode.ts

This file was deleted.

6 changes: 4 additions & 2 deletions packages/nextjs/hooks/scaffold-eth/useNetworkColor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useDarkMode } from "./useDarkMode";
import { useTargetNetwork } from "./useTargetNetwork";
import { useTheme } from "next-themes";
import { ChainWithAttributes } from "~~/utils/scaffold-eth";

export const DEFAULT_NETWORK_COLOR: [string, string] = ["#666666", "#bbbbbb"];
Expand All @@ -13,8 +13,10 @@ export function getNetworkColor(network: ChainWithAttributes, isDarkMode: boolea
* Gets the color of the target network
*/
export const useNetworkColor = () => {
const { isDarkMode } = useDarkMode();
const { resolvedTheme } = useTheme();
const { targetNetwork } = useTargetNetwork();

const isDarkMode = resolvedTheme === "dark";

return getNetworkColor(targetNetwork, isDarkMode);
};
3 changes: 2 additions & 1 deletion packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
"blo": "^1.0.1",
"daisyui": "^4.4.19",
"next": "^14.0.4",
"next-themes": "^0.2.1",
"nprogress": "^0.2.0",
"qrcode.react": "^3.1.0",
"react": "^18.2.0",
"react-copy-to-clipboard": "^5.1.0",
"react-dom": "^18.2.0",
"react-hot-toast": "^2.4.0",
"use-debounce": "^8.0.4",
"usehooks-ts": "^2.7.2",
"usehooks-ts": "^2.13.0",
"viem": "1.19.9",
"wagmi": "1.4.12",
"zustand": "^4.1.2"
Expand Down
6 changes: 3 additions & 3 deletions packages/nextjs/tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
module.exports = {
content: ["./app/**/*.{js,ts,jsx,tsx}", "./components/**/*.{js,ts,jsx,tsx}", "./utils/**/*.{js,ts,jsx,tsx}"],
plugins: [require("daisyui")],
darkTheme: "scaffoldEthDark",
darkTheme: "dark",
// DaisyUI theme colors
daisyui: {
themes: [
{
scaffoldEth: {
light: {
primary: "#93BBFB",
"primary-content": "#212638",
secondary: "#DAE8FF",
Expand Down Expand Up @@ -39,7 +39,7 @@ module.exports = {
},
},
{
scaffoldEthDark: {
dark: {
primary: "#212638",
"primary-content": "#F9FBFF",
secondary: "#323f61",
Expand Down
44 changes: 27 additions & 17 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,7 @@ __metadata:
eslint-config-prettier: ^8.5.0
eslint-plugin-prettier: ^4.2.1
next: ^14.0.4
next-themes: ^0.2.1
nprogress: ^0.2.0
postcss: ^8.4.16
prettier: ^2.8.4
Expand All @@ -1889,7 +1890,7 @@ __metadata:
type-fest: ^4.6.0
typescript: ^5.1.6
use-debounce: ^8.0.4
usehooks-ts: ^2.7.2
usehooks-ts: ^2.13.0
vercel: ^32.4.1
viem: 1.19.9
wagmi: 1.4.12
Expand Down Expand Up @@ -9562,6 +9563,13 @@ __metadata:
languageName: node
linkType: hard

"lodash.debounce@npm:^4.0.8":
version: 4.0.8
resolution: "lodash.debounce@npm:4.0.8"
checksum: a3f527d22c548f43ae31c861ada88b2637eb48ac6aa3eb56e82d44917971b8aa96fbb37aa60efea674dc4ee8c42074f90f7b1f772e9db375435f6c83a19b3bc6
languageName: node
linkType: hard

"lodash.defaults@npm:^4.2.0":
version: 4.2.0
resolution: "lodash.defaults@npm:4.2.0"
Expand Down Expand Up @@ -10307,6 +10315,17 @@ __metadata:
languageName: node
linkType: hard

"next-themes@npm:^0.2.1":
version: 0.2.1
resolution: "next-themes@npm:0.2.1"
peerDependencies:
next: "*"
react: "*"
react-dom: "*"
checksum: ebc248b956138e73436c4ed0a0f0a877a0a48a33156db577029b8b8469e48b5c777d61abf2baeb75953378febea74e067a4869ff90b4a3e94fce123289b862ba
languageName: node
linkType: hard

"next@npm:^14.0.4":
version: 14.0.4
resolution: "next@npm:14.0.4"
Expand Down Expand Up @@ -13996,23 +14015,14 @@ __metadata:
languageName: node
linkType: hard

"usehooks-ts@npm:^2.7.2":
version: 2.9.1
resolution: "usehooks-ts@npm:2.9.1"
peerDependencies:
react: ^16.8.0 || ^17.0.0 || ^18.0.0
react-dom: ^16.8.0 || ^17.0.0 || ^18.0.0
checksum: 36f1e4142ce23bc019b81d2e93aefd7f2c350abcf255598c21627114a69a2f2f116b35dc3a353375f09c6e4c9b704a04f104e3d10e98280545c097feca66c30a
languageName: node
linkType: hard

"usehooks-ts@patch:usehooks-ts@npm:^2.7.2#./.yarn/patches/usehooks-ts-npm-2.7.2-fceffe0e43.patch::locator=se-2%40workspace%3A.":
version: 2.9.1
resolution: "usehooks-ts@patch:usehooks-ts@npm%3A2.9.1#./.yarn/patches/usehooks-ts-npm-2.7.2-fceffe0e43.patch::version=2.9.1&hash=68bde7&locator=se-2%40workspace%3A."
"usehooks-ts@npm:^2.13.0":
version: 2.13.0
resolution: "usehooks-ts@npm:2.13.0"
dependencies:
lodash.debounce: ^4.0.8
peerDependencies:
react: ^16.8.0 || ^17.0.0 || ^18.0.0
react-dom: ^16.8.0 || ^17.0.0 || ^18.0.0
checksum: 6807ae0e6ffd158790e5018d3a322efd0fe97853b6b83751725e5e81d0962cb6230ca65a24341bb5cee3f1bc330721611440c399fc950723153c5cc61eb74b04
react: ^16.8.0 || ^17 || ^18
checksum: ad07930e1b5c70392603eb8b3f199f44349c75406fe31013f79b0fb7fdece59f47f8dba09b6f1fafaa00d68f43240dbb13cdc1afb89b647f1d53504599a51ca0
languageName: node
linkType: hard

Expand Down

0 comments on commit 2d98105

Please sign in to comment.