Skip to content

Commit 4b73b55

Browse files
committed
Fix default state and check for system color scheme
1 parent c36a137 commit 4b73b55

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import useRGS from "r18gs";
22
import type { SetStateAction } from "r18gs/use-rgs";
33
import * as React from "react";
4-
import type { ColorSchemePreference, ThemeState } from "../../hooks/use-theme";
5-
import { DEFAULT_ID } from "../../constants";
4+
import type { ColorSchemePreference, ThemeState } from "../../constants";
5+
import { DEFAULT_ID, DEFAULT_THEME_STATE } from "../../constants";
66

77
export interface ThemeSwitcherProps {
88
/** id of target element to apply classes to. This is useful when you want to apply theme only to specific container. */
@@ -20,6 +20,7 @@ function useMediaQuery(setThemeState: SetStateAction<ThemeState>) {
2020
const updateSystemColorScheme = () => {
2121
setThemeState(state => ({ ...state, systemColorScheme: media.matches ? "dark" : "light" }));
2222
};
23+
updateSystemColorScheme();
2324
media.addEventListener("change", updateSystemColorScheme);
2425
return () => {
2526
media.removeEventListener("change", updateSystemColorScheme);
@@ -103,7 +104,7 @@ function updateDOM({ targetId, themeState, dontSync }: UpdateDOMProps) {
103104

104105
export function ThemeSwitcher({ targetId, dontSync, themeTransition }: ThemeSwitcherProps) {
105106
if (targetId === "") throw new Error("id can not be an empty string");
106-
const [themeState, setThemeState] = useRGS<ThemeState>(targetId ?? DEFAULT_ID);
107+
const [themeState, setThemeState] = useRGS<ThemeState>(targetId ?? DEFAULT_ID, DEFAULT_THEME_STATE);
107108

108109
useMediaQuery(setThemeState);
109110

lib/nthul/src/constants.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,16 @@
11
/** shared constants -- keep in separate files for better tree-shaking and dependency injection */
22
export const DEFAULT_ID = "nthul";
3+
4+
export type ColorSchemePreference = "system" | "dark" | "light";
5+
6+
export interface ThemeState {
7+
colorSchemePreference: ColorSchemePreference;
8+
systemColorScheme: "dark" | "light";
9+
theme: string;
10+
}
11+
12+
export const DEFAULT_THEME_STATE = {
13+
colorSchemePreference: "system" as ColorSchemePreference,
14+
systemColorScheme: "light" as "light" | "dark",
15+
theme: "",
16+
};

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

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
11
import useRGS from "r18gs";
2-
import { DEFAULT_ID } from "../constants";
3-
4-
export type ColorSchemePreference = "system" | "dark" | "light";
5-
6-
export interface ThemeState {
7-
colorSchemePreference: ColorSchemePreference;
8-
systemColorScheme: "dark" | "light";
9-
theme: string;
10-
}
11-
12-
const DEFAULT_THEME_STATE = {
13-
colorSchemePreference: "system" as ColorSchemePreference,
14-
systemColorScheme: "light" as "light" | "dark",
15-
theme: "",
16-
};
2+
import type { ColorSchemePreference, ThemeState } from "../constants";
3+
import { DEFAULT_ID, DEFAULT_THEME_STATE } from "../constants";
174

185
export interface UseTheme {
196
theme: string;

0 commit comments

Comments
 (0)