Skip to content

Commit 9ed6e9d

Browse files
committed
Fix hydration error (via AI)
1 parent bb01b33 commit 9ed6e9d

4 files changed

Lines changed: 18 additions & 4 deletions

File tree

app/components/ui/color-mode.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,20 @@ export function ColorModeProvider(props: ColorModeProviderProps) {
1313
)
1414
}
1515

16+
function useHasMounted() {
17+
const [hasMounted, setHasMounted] = React.useState(false)
18+
19+
React.useEffect(() => {
20+
setHasMounted(true)
21+
}, [])
22+
23+
return hasMounted
24+
}
25+
1626
export function useColorMode() {
27+
const hasMounted = useHasMounted()
1728
const { resolvedTheme, setTheme } = useTheme()
18-
const colorMode = resolvedTheme === "dark" ? "dark" : "light"
29+
const colorMode = hasMounted && resolvedTheme === "dark" ? "dark" : "light"
1930
const toggleColorMode = () => {
2031
setTheme(colorMode === "light" ? "dark" : "light")
2132
}
@@ -28,8 +39,9 @@ export function useColorMode() {
2839
}
2940

3041
export function useColorModeValue<T>(light: T, dark: T) {
42+
const hasMounted = useHasMounted()
3143
const { resolvedTheme } = useTheme()
32-
return resolvedTheme === "dark" ? dark : light
44+
return hasMounted && resolvedTheme === "dark" ? dark : light
3345
}
3446

3547
export function ColorModeIcon() {

app/root.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const links: LinksFunction = () => [{ rel: "stylesheet", href: appStylesH
1515

1616
function MyLayout({ children }: { children: React.ReactNode }) {
1717
return (
18-
<html lang="en">
18+
<html lang="en" suppressHydrationWarning>
1919
<head>
2020
<meta charSet="utf-8" />
2121
<meta name="viewport" content="width=device-width, initial-scale=1" />
@@ -89,7 +89,7 @@ export function ErrorBoundary() {
8989
}
9090

9191
return (
92-
<html lang="en">
92+
<html lang="en" suppressHydrationWarning>
9393
<head>
9494
<meta charSet="utf-8" />
9595
<meta name="viewport" content="width=device-width, initial-scale=1" />

app/routes/view[.]html/IconList.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ function IconCard({ imageCss, size, url }: ICardProps) {
1313
<div className="flex flex-col items-center lg:items-end">
1414
<img
1515
alt={`${size} for ${url}`}
16+
crossOrigin="anonymous"
1617
src={url}
1718
style={{ ...imageCss, width: `${size}pt`, height: `${size}pt` }}
1819
/>

app/routes/view[.]html/route.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ export default function ViewPage() {
237237
) : (
238238
<img
239239
alt={url}
240+
crossOrigin="anonymous"
240241
src={url}
241242
style={{
242243
objectFit: "cover",

0 commit comments

Comments
 (0)