Skip to content

Commit e63b300

Browse files
committed
fix(docs): update useTheme to match theming.mdx implementation
- Return ThemeInfo object with colorMode and userColorScheme - Remove custom SSR handling to match original implementation - Update success-with-lottie to destructure userColorScheme
1 parent 808774c commit e63b300

File tree

2 files changed

+45
-42
lines changed

2 files changed

+45
-42
lines changed

docs/examples/react/result-section/success-with-lottie.tsx

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,9 @@
11
"use client";
22

3-
import { useEffect, useState } from "react";
43
import { Player } from "@lottiefiles/react-lottie-player";
54
import { VStack, Box } from "@seed-design/react";
65
import { ResultSection } from "seed-design/ui/result-section";
7-
8-
type UserColorScheme = "light" | "dark";
9-
10-
function useTheme() {
11-
const [userColorScheme, setUserColorScheme] = useState<UserColorScheme>(() => {
12-
if (typeof window === "undefined") return "light";
13-
14-
const colorMode = document.documentElement.getAttribute("data-seed-color-mode");
15-
const scheme = document.documentElement.getAttribute("data-seed-user-color-scheme");
16-
17-
if (colorMode === "dark-only") return "dark";
18-
if (colorMode === "light-only") return "light";
19-
return scheme === "dark" ? "dark" : "light";
20-
});
21-
22-
useEffect(() => {
23-
const observer = new MutationObserver(() => {
24-
const colorMode = document.documentElement.getAttribute("data-seed-color-mode");
25-
const scheme = document.documentElement.getAttribute("data-seed-user-color-scheme");
26-
27-
if (colorMode === "dark-only") {
28-
setUserColorScheme("dark");
29-
} else if (colorMode === "light-only") {
30-
setUserColorScheme("light");
31-
} else {
32-
setUserColorScheme(scheme === "dark" ? "dark" : "light");
33-
}
34-
});
35-
36-
observer.observe(document.documentElement, {
37-
attributes: true,
38-
attributeFilter: ["data-seed-color-mode", "data-seed-user-color-scheme"],
39-
});
40-
41-
return () => observer.disconnect();
42-
}, []);
43-
44-
return userColorScheme;
45-
}
6+
import { useTheme } from "@/hooks/useTheme";
467

478
const LOTTIE_URLS = {
489
light:
@@ -51,8 +12,8 @@ const LOTTIE_URLS = {
5112
};
5213

5314
export default function ResultSectionSuccessWithLottie() {
54-
const colorScheme = useTheme();
55-
const lottieUrl = LOTTIE_URLS[colorScheme];
15+
const { userColorScheme } = useTheme();
16+
const lottieUrl = LOTTIE_URLS[userColorScheme];
5617

5718
return (
5819
<VStack minHeight="480px" width="320px" borderWidth={1} borderColor="stroke.neutralMuted">

docs/hooks/useTheme.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { useEffect, useState } from "react";
2+
3+
export type ColorMode = "system" | "light-only" | "dark-only";
4+
export type UserColorScheme = "light" | "dark";
5+
6+
export interface ThemeInfo {
7+
colorMode: ColorMode;
8+
userColorScheme: UserColorScheme;
9+
}
10+
11+
export function useTheme(): ThemeInfo {
12+
const [themeInfo, setThemeInfo] = useState<ThemeInfo>(() => {
13+
const colorMode = document.documentElement.getAttribute("data-seed-color-mode") as ColorMode;
14+
const userColorScheme = document.documentElement.getAttribute("data-seed-user-color-scheme");
15+
16+
return {
17+
colorMode: colorMode || "system",
18+
userColorScheme: userColorScheme === "dark" ? "dark" : "light",
19+
};
20+
});
21+
22+
useEffect(() => {
23+
const observer = new MutationObserver(() => {
24+
const colorMode = document.documentElement.getAttribute("data-seed-color-mode") as ColorMode;
25+
const userColorScheme = document.documentElement.getAttribute("data-seed-user-color-scheme");
26+
27+
setThemeInfo({
28+
colorMode: colorMode || "system",
29+
userColorScheme: userColorScheme === "dark" ? "dark" : "light",
30+
});
31+
});
32+
33+
observer.observe(document.documentElement, {
34+
attributes: true,
35+
attributeFilter: ["data-seed-color-mode", "data-seed-user-color-scheme"],
36+
});
37+
38+
return () => observer.disconnect();
39+
}, []);
40+
41+
return themeInfo;
42+
}

0 commit comments

Comments
 (0)