Skip to content

Commit

Permalink
Fix hydration issues (#6309)
Browse files Browse the repository at this point in the history
* fix: fix hydration issues

* chore: add color mode config and suspense
  • Loading branch information
itkrt2y committed Jul 13, 2022
1 parent 46b4954 commit fb3c093
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/quiet-spiders-lay.md
@@ -0,0 +1,5 @@
---
"chakra-nextjs-ts": patch
---

add color mode config and suspense
6 changes: 6 additions & 0 deletions .changeset/tiny-rabbits-admire.md
@@ -0,0 +1,6 @@
---
"@chakra-ui/color-mode": patch
"@chakra-ui/react-env": patch
---

fix hydration issue
2 changes: 2 additions & 0 deletions examples/nextjs-typescript/Chakra.tsx
Expand Up @@ -5,6 +5,7 @@ import {
} from "@chakra-ui/react"
import { GetServerSidePropsContext } from "next"
import { ReactNode } from "react"
import theme from "./theme"

interface ChakraProps {
cookies?: string
Expand All @@ -15,6 +16,7 @@ export const Chakra = ({ children, cookies }: ChakraProps) => {
return (
<ChakraProvider
colorModeManager={cookies ? cookieStorageManager : localStorageManager}
theme={theme}
>
{children}
</ChakraProvider>
Expand Down
3 changes: 2 additions & 1 deletion examples/nextjs-typescript/pages/_document.tsx
Expand Up @@ -6,6 +6,7 @@ import NextDocument, {
NextScript,
DocumentContext,
} from "next/document"
import theme from "../theme"

export default class Document extends NextDocument {
static getInitialProps(ctx: DocumentContext) {
Expand All @@ -17,7 +18,7 @@ export default class Document extends NextDocument {
<Html>
<Head />
<body>
<ColorModeScript />
<ColorModeScript initialColorMode={theme.config.initialColorMode} />
<Main />
<NextScript />
</body>
Expand Down
57 changes: 30 additions & 27 deletions examples/nextjs-typescript/pages/index.tsx
Expand Up @@ -3,6 +3,7 @@ import { Chakra } from "../Chakra"
import { Layout } from "../components/Layout"
import { Logo } from "../components/Logo"
import { NextChakraLink } from "../components/NextChakraLink"
import { Suspense } from "react"

interface IndexProps {
cookies?: string
Expand All @@ -11,33 +12,35 @@ interface IndexProps {
const IndexPage = ({ cookies }: IndexProps) => (
<Chakra cookies={cookies}>
<Layout title="Next.js + TypeScript example">
<Box textAlign="center" fontSize="xl">
<Grid minH="100vh" p={3}>
<VStack spacing={8}>
<Logo h="40vmin" pointerEvents="none" />
<Text>
Edit <Code fontSize="xl">pages/index.tsx</Code> and save to
reload.
<br />
<br />
<NextChakraLink href="/properties" color="teal.500">
View the properties
</NextChakraLink>{" "}
to see the Nextjs <Code fontSize="xl">&lt;Link&gt;</Code> in
action
</Text>
<Link
color="teal.500"
fontSize="2xl"
href="https://chakra-ui.com"
target="_blank"
rel="noopener noreferrer"
>
Learn more about Chakra
</Link>
</VStack>
</Grid>
</Box>
<Suspense fallback="Loading...">
<Box textAlign="center" fontSize="xl">
<Grid minH="100vh" p={3}>
<VStack spacing={8}>
<Logo h="40vmin" pointerEvents="none" />
<Text>
Edit <Code fontSize="xl">pages/index.tsx</Code> and save to
reload.
<br />
<br />
<NextChakraLink href="/properties" color="teal.500">
View the properties
</NextChakraLink>{" "}
to see the Nextjs <Code fontSize="xl">&lt;Link&gt;</Code> in
action
</Text>
<Link
color="teal.500"
fontSize="2xl"
href="https://chakra-ui.com"
target="_blank"
rel="noopener noreferrer"
>
Learn more about Chakra
</Link>
</VStack>
</Grid>
</Box>
</Suspense>
</Layout>
</Chakra>
)
Expand Down
10 changes: 10 additions & 0 deletions examples/nextjs-typescript/theme.ts
@@ -0,0 +1,10 @@
import { extendTheme, type ThemeConfig } from "@chakra-ui/react"

const config: ThemeConfig = {
initialColorMode: "system",
useSystemColorMode: true,
}

const theme = extendTheme({ config })

export default theme
5 changes: 2 additions & 3 deletions packages/color-mode/src/color-mode-provider.tsx
Expand Up @@ -107,7 +107,7 @@ export function ColorModeProvider(props: ColorModeProviderProps) {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

useSafeLayoutEffect(() => {
React.useEffect(() => {
const managerValue = colorModeManager.get()

if (managerValue) {
Expand All @@ -121,8 +121,7 @@ export function ColorModeProvider(props: ColorModeProviderProps) {
}

setColorMode(defaultColorMode)
//
}, [colorModeManager, defaultColorMode, initialColorMode, getSystemTheme])
}, [colorModeManager, defaultColorMode, initialColorMode, setColorMode])

const toggleColorMode = React.useCallback(() => {
setColorMode(resolvedValue === "dark" ? "light" : "dark")
Expand Down
5 changes: 4 additions & 1 deletion packages/env/src/env.tsx
@@ -1,6 +1,7 @@
import { isBrowser, __DEV__ } from "@chakra-ui/utils"
import React, {
createContext,
startTransition,
useContext,
useMemo,
useState,
Expand Down Expand Up @@ -57,7 +58,9 @@ export function EnvironmentProvider(props: EnvironmentProviderProps) {
{mounted && (
<span
ref={(el) => {
if (el) setNode(el)
startTransition(() => {
if (el) setNode(el)
})
}}
/>
)}
Expand Down

1 comment on commit fb3c093

@vercel
Copy link

@vercel vercel bot commented on fb3c093 Jul 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.