-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
50 lines (45 loc) · 1.52 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import "./styles/base.css";
import "./styles/containers.css";
import "./styles/text.css";
import "./styles/flex.css";
import "./styles/inputs.css";
import "./styles/util.css";
import "./styles/navbar.css";
import "./styles/alerts.css";
import "./styles/context-menus.css";
import "./styles/banner.css";
import "./styles/responsive.css";
import "./themes/light.css";
import "./themes/transparent.css";
import { useMediaQuery } from "react-responsive";
import { useEffect } from "react";
export const themes = ["dark", "light", "transparent"] as const;
export type Theme = (typeof themes)[number];
export let currentTheme: Theme = "dark";
export const setTheme = (theme: Theme, noStore: boolean = false) => {
for (const theme of themes)
document.body.classList.remove(`dawn-theme-${theme}`);
document.body.classList.add(`dawn-theme-${theme}`);
currentTheme = theme;
if (!noStore) localStorage.setItem("dawn_ui-theme", theme);
};
export const loadTheme = () => {
if (localStorage.getItem("dawn_ui-theme"))
setTheme(localStorage.getItem("dawn_ui-theme") as Theme);
};
export const useAutomaticTheme = () => {
const isDefaultDark: boolean = useMediaQuery(
{
query: "(prefers-color-scheme: dark)",
},
undefined,
(isSystemDark: boolean) => {
setTheme(isSystemDark ? "dark" : "light");
}
);
useEffect(() => {
if (localStorage.getItem("dawn_ui-theme"))
setTheme(localStorage.getItem("dawn_ui-theme") as Theme);
else setTheme(isDefaultDark ? "dark" : "light");
}, [isDefaultDark]);
};