-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
executable file
·74 lines (66 loc) · 2.11 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import "./styles/base.css";
import "./styles/containers.css";
import "./styles/animations.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/icons.css";
import "./styles/quick.css";
import "./styles/responsive.css";
import "./styles/table.css";
import "./styles/flyout.css";
import "./styles/tables.css";
import "./themes/light.css";
import "./themes/dark-transparent.css";
import "./themes/light-transparent.css";
import { useMediaQuery } from "react-responsive";
import { useEffect } from "react";
export const themes = [
"dark",
"light",
"dark-transparent",
"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 themeSetBackground = (url: string) => {
document.body.style.backgroundImage = `url(${url})`;
};
export const setThemeMany = (themesTo: Theme[]) => {
for (const theme of themes)
document.body.classList.remove(`dawn-theme-${theme}`);
for (const theme of themesTo)
document.body.classList.add(`dawn-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]);
};