mirror of
https://github.com/filebrowser/filebrowser.git
synced 2025-07-18 22:10:26 +00:00

--------- Co-authored-by: Joep <jcbuhre@gmail.com> Co-authored-by: Omar Hussein <omarmohammad1951@gmail.com> Co-authored-by: Oleg Lobanov <oleg@lobanov.me>
35 lines
753 B
TypeScript
35 lines
753 B
TypeScript
import { theme } from "./constants";
|
|
|
|
export const getTheme = (): UserTheme => {
|
|
return (document.documentElement.className as UserTheme) || theme;
|
|
};
|
|
|
|
export const setTheme = (theme: UserTheme) => {
|
|
const html = document.documentElement;
|
|
if (!theme) {
|
|
html.className = getMediaPreference();
|
|
} else {
|
|
html.className = theme;
|
|
}
|
|
};
|
|
|
|
export const toggleTheme = (): void => {
|
|
const activeTheme = getTheme();
|
|
if (activeTheme === "light") {
|
|
setTheme("dark");
|
|
} else {
|
|
setTheme("light");
|
|
}
|
|
};
|
|
|
|
export const getMediaPreference = (): UserTheme => {
|
|
const hasDarkPreference = window.matchMedia(
|
|
"(prefers-color-scheme: dark)"
|
|
).matches;
|
|
if (hasDarkPreference) {
|
|
return "dark";
|
|
} else {
|
|
return "light";
|
|
}
|
|
};
|