Files
seasoned/src/composables/useTheme.ts
Kevin c8262a3bda Feat: Misc improvements (#107)
* Expand SCSS variables for improved theming

* Redesign 404 page with dynamic movie quotes

* Add password generator page

* Add missing Plex authentication page

* Improve torrent table and torrents page

* Enhance toast notification component

* Enhance popup components

* Refine UI components and remove DarkmodeToggle

* Add user profile component for settings

* Update autocomplete dropdown component

* Update register page

* Redesign signin and register pages with improved UX

* Improve torrent table with sort toggle and highlight colors

* eslint & prettier fixes
2026-03-09 00:01:05 +01:00

57 lines
1.4 KiB
TypeScript

import { ref, computed } from "vue";
type Theme = "light" | "dark" | "auto";
const currentTheme = ref<Theme>("auto");
function systemDarkModeEnabled(): boolean {
const computedStyle = window.getComputedStyle(document.body);
if (computedStyle?.colorScheme != null) {
return computedStyle.colorScheme.includes("dark");
}
return false;
}
function applyTheme(theme: Theme) {
if (theme === "auto") {
const systemDark = systemDarkModeEnabled();
document.body.className = systemDark ? "dark" : "light";
} else {
document.body.className = theme;
}
}
export function useTheme() {
const savedTheme = computed(
() => (localStorage.getItem("theme-preference") as Theme) || "auto"
);
function initTheme() {
const theme = savedTheme.value;
currentTheme.value = theme;
applyTheme(theme);
// Listen for system theme changes when in auto mode
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
mediaQuery.addEventListener("change", e => {
const currentSetting = localStorage.getItem("theme-preference") as Theme;
if (currentSetting === "auto") {
document.body.className = e.matches ? "dark" : "light";
}
});
}
function setTheme(theme: Theme) {
currentTheme.value = theme;
localStorage.setItem("theme-preference", theme);
applyTheme(theme);
}
return {
currentTheme,
savedTheme,
initTheme,
setTheme
};
}