web-flasher-ng/composables/useTheme.ts

20 lines
538 B
TypeScript
Raw Normal View History

2024-12-07 16:48:13 +00:00
export const useTheme = () => {
const theme = ref('light');
onMounted(() => {
// Check system preference
const darkModeMediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
const updateTheme = (e: MediaQueryListEvent | MediaQueryList) => {
theme.value = e.matches ? 'dark' : 'light';
document.documentElement.setAttribute('data-theme', theme.value);
};
darkModeMediaQuery.addEventListener('change', updateTheme);
updateTheme(darkModeMediaQuery);
});
return {
theme
};
};