Skip to content

Commit 3013e8b

Browse files
committed
refactor: read from attributes rather than localStorage for initializing RGS
1 parent 8f31d2e commit 3013e8b

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

lib/src/utils.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import useRGS from "r18gs";
2-
import { COOKIE_KEY, DARK, LIGHT, MEDIA, SYSTEM } from "./constants";
2+
import { DARK, SYSTEM } from "./constants";
33

44
export type ColorSchemePreference = "system" | "dark" | "";
55
export type ResolvedScheme = "dark" | "";
@@ -10,12 +10,11 @@ export interface Store {
1010

1111
/** local abstaction of RGS to avoid multiple imports */
1212
export const useStore = () =>
13-
useRGS<Store>("ndm", () =>
14-
typeof localStorage === "undefined"
15-
? /* v8 ignore next */
16-
{ m: SYSTEM, s: DARK as ResolvedScheme }
17-
: {
18-
m: (localStorage.getItem(COOKIE_KEY) ?? SYSTEM) as ColorSchemePreference,
19-
s: (matchMedia(MEDIA).matches ? DARK : LIGHT) as ResolvedScheme,
20-
},
21-
);
13+
useRGS<Store>("ndm", () => {
14+
if (typeof document === "undefined") return { m: SYSTEM, s: DARK as ResolvedScheme };
15+
const el = document.documentElement;
16+
return {
17+
m: (el.getAttribute("data-m") ?? SYSTEM) as ColorSchemePreference,
18+
s: el.getAttribute("data-sm") as ResolvedScheme,
19+
};
20+
});

0 commit comments

Comments
 (0)