Skip to content

Commit 3f50eac

Browse files
committed
use theme hook to get and set theme
1 parent 1d72081 commit 3f50eac

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

lib/nthul/src/client/theme-switcher/theme-switcher.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ function useMediaQuery(setThemeState: SetStateAction<ThemeState>) {
2727
}, [setThemeState]);
2828
}
2929

30-
interface LoadSyncedStateProps extends ThemeSwitcherProps {
30+
export interface LoadSyncedStateProps extends ThemeSwitcherProps {
3131
setThemeState: SetStateAction<ThemeState>;
3232
}
3333

34-
function parseState(str?: string | null) {
35-
const [theme, colorSchemePreference, systemColorScheme] = (str ?? ",system,light").split(",") as [
34+
function parseState(str?: string | null): ThemeState {
35+
const parts = (str ?? ",system,light").split(",") as [
3636
string,
3737
ColorSchemePreference,
3838
"light" | "dark",
3939
];
40-
return { theme, colorSchemePreference, systemColorScheme };
40+
return { theme: parts[0], colorSchemePreference: parts[1], systemColorScheme: parts[2] };
4141
}
4242

4343
function useLoadSyncedState({ dontSync, targetId, setThemeState }: LoadSyncedStateProps) {
@@ -76,7 +76,7 @@ function modifyTransition(themeTransition = "none") {
7676
};
7777
}
7878

79-
interface UpdateDOMProps {
79+
export interface UpdateDOMProps {
8080
targetId?: string;
8181
themeState: ThemeState;
8282
}
@@ -113,6 +113,7 @@ export function ThemeSwitcher({ targetId, dontSync, themeTransition }: ThemeSwit
113113

114114
useLoadSyncedState({ dontSync, targetId, setThemeState });
115115

116+
/** update DOM and storage */
116117
React.useEffect(() => {
117118
const restoreTransitions = modifyTransition(themeTransition);
118119
const shoulCreateCookie = updateDOM({ targetId, themeState });

lib/nthul/src/hooks/use-theme.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import useRGS from "r18gs";
2+
3+
export type ColorSchemePreference = "system" | "dark" | "light";
4+
5+
export interface ThemeState {
6+
colorSchemePreference: ColorSchemePreference;
7+
systemColorScheme: "dark" | "light";
8+
theme: string;
9+
}
10+
11+
export const DEFAULT_ID = "nthul";
12+
13+
export const DEFAULT_THEME_STATE = {
14+
colorSchemePreference: "system" as ColorSchemePreference,
15+
systemColorScheme: "light" as "light" | "dark",
16+
theme: "",
17+
};
18+
19+
export interface UseTheme {
20+
theme: string;
21+
colorSchemePreference: ColorSchemePreference;
22+
systemColorScheme: "dark" | "light";
23+
resolvedColorScheme: "dark" | "light";
24+
setColorSchemePreference: (colorSchemePreference: ColorSchemePreference) => void;
25+
setTheme: (theme: string) => void;
26+
}
27+
28+
export function useTheme(id?: string): UseTheme {
29+
const [themeState, setState] = useRGS<ThemeState>(id ?? DEFAULT_ID, DEFAULT_THEME_STATE);
30+
const { colorSchemePreference: csp, systemColorScheme: scs } = themeState;
31+
return {
32+
...themeState,
33+
resolvedColorScheme: csp === "system" ? scs : csp,
34+
setColorSchemePreference: (colorSchemePreference: ColorSchemePreference) => {
35+
setState({ ...themeState, colorSchemePreference });
36+
},
37+
setTheme: (theme: string) => {
38+
setState({ ...themeState, theme });
39+
},
40+
};
41+
}

0 commit comments

Comments
 (0)