Skip to content

Commit d83203a

Browse files
committed
simplify color mode sync by using the ChakraBaseProvider and data attr to set the theme
1 parent bcf3c67 commit d83203a

File tree

4 files changed

+13
-29
lines changed

4 files changed

+13
-29
lines changed

src/components/ChakraThemeProvider.tsx

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,24 @@
11
import { PropsWithChildren, useMemo } from "react"
22
import merge from "lodash/merge"
3-
import { useTheme as useNextTheme } from "next-themes"
4-
import { ColorMode, ColorModeContext, ThemeProvider } from "@chakra-ui/react"
3+
import { ChakraBaseProvider, createLocalStorageManager } from "@chakra-ui/react"
54

65
import customTheme from "@/@chakra-ui/theme"
76

7+
import { COLOR_MODE_STORAGE_KEY } from "@/lib/constants"
8+
89
import { useLocaleDirection } from "@/hooks/useLocaleDirection"
910

10-
function ChakraThemeProvider({ children }: PropsWithChildren) {
11-
// get the theme from next-themes
12-
const { resolvedTheme } = useNextTheme()
11+
const colorModeManager = createLocalStorageManager(COLOR_MODE_STORAGE_KEY)
1312

13+
function ChakraThemeProvider({ children }: PropsWithChildren) {
1414
const direction = useLocaleDirection()
1515

1616
const theme = useMemo(() => merge(customTheme, { direction }), [direction])
1717

1818
return (
19-
<ThemeProvider theme={theme}>
20-
<ColorModeContext.Provider
21-
value={{
22-
colorMode: resolvedTheme as ColorMode,
23-
toggleColorMode: () => {},
24-
setColorMode: () => {},
25-
forced: true,
26-
}}
27-
>
19+
<ChakraBaseProvider theme={theme} colorModeManager={colorModeManager}>
2820
{children}
29-
</ColorModeContext.Provider>
30-
</ThemeProvider>
21+
</ChakraBaseProvider>
3122
)
3223
}
3324

src/lib/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const COINGECKO_API_BASE_URL =
4141
export const COINGECKO_API_URL_PARAMS =
4242
"&order=market_cap_desc&per_page=100&page=1&sparkline=false"
4343
export const BASE_TIME_UNIT = 3600 // 1 hour
44+
export const COLOR_MODE_STORAGE_KEY = "theme"
4445

4546
// Quiz Hub
4647
export const PROGRESS_BAR_GAP = "4px"
@@ -148,4 +149,4 @@ export const DEFAULT_GLOSSARY_NS = "glossary"
148149

149150
export const HAMBURGER_BUTTON_ID = "mobile-menu-button"
150151
export const MOBILE_LANGUAGE_BUTTON_NAME = "mobile-language-button"
151-
export const DESKTOP_LANGUAGE_BUTTON_NAME = "desktop-language-button"
152+
export const DESKTOP_LANGUAGE_BUTTON_NAME = "desktop-language-button"

src/pages/_app.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import { AppPropsWithLayout } from "@/lib/types"
77

88
import ChakraThemeProvider from "@/components/ChakraThemeProvider"
99

10+
import { COLOR_MODE_STORAGE_KEY } from "@/lib/constants"
11+
1012
import "@/styles/global.css"
1113
import "@/styles/main.css"
1214

1315
import { RootLayout } from "@/layouts/RootLayout"
1416
import { mono } from "@/lib/fonts"
1517

16-
export const STORAGE_KEY = "theme"
17-
1818
const App = ({ Component, pageProps }: AppPropsWithLayout) => {
1919
useEffect(() => {
2020
if (!process.env.IS_PREVIEW_DEPLOY) {
@@ -40,11 +40,11 @@ const App = ({ Component, pageProps }: AppPropsWithLayout) => {
4040
`}
4141
</style>
4242
<NextThemesProvider
43-
attribute="class"
43+
attribute="data-theme"
4444
defaultTheme="system"
4545
enableSystem
4646
disableTransitionOnChange
47-
storageKey={STORAGE_KEY}
47+
storageKey={COLOR_MODE_STORAGE_KEY}
4848
>
4949
<ChakraThemeProvider>
5050
<RootLayout

src/pages/_document.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@ import NextDocument, {
55
Main,
66
NextScript,
77
} from "next/document"
8-
import { ColorModeScript } from "@chakra-ui/react"
98

109
import { Lang } from "@/lib/types"
1110

1211
import { isLangRightToLeft } from "@/lib/utils/translations"
1312

14-
import { STORAGE_KEY } from "./_app"
15-
1613
class Document extends NextDocument {
1714
static async getInitialProps(ctx: DocumentContext) {
1815
return await NextDocument.getInitialProps(ctx)
@@ -39,11 +36,6 @@ class Document extends NextDocument {
3936
/>
4037
</Head>
4138
<body>
42-
{/*
43-
Still needed to insert the chakra specific classname on the `body` (`chakra-ui-light` & `chakra-ui-dark`)
44-
and avoid color mode flashing
45-
*/}
46-
<ColorModeScript initialColorMode="system" storageKey={STORAGE_KEY} />
4739
<Main />
4840
<NextScript />
4941
</body>

0 commit comments

Comments
 (0)