First version running with npm run dev
This commit is contained in:
commit
f90ec06a4f
28 files changed
+12042
No files matched your search
@@ -0,0 +1,73 @@
|
||||
import { ref, watch, onMounted } from 'vue';
|
||||
|
||||
type Theme = 'light' | 'dark';
|
||||
|
||||
const STORAGE_KEY = 'quick-ling-theme';
|
||||
|
||||
const theme = ref<Theme>('light');
|
||||
|
||||
function applyTheme(t: Theme): void {
|
||||
if (typeof document !== 'undefined') {
|
||||
document.documentElement.setAttribute('data-theme', t);
|
||||
}
|
||||
}
|
||||
|
||||
function getSystemPreference(): Theme {
|
||||
if (typeof window !== 'undefined' && window.matchMedia) {
|
||||
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
||||
}
|
||||
return 'light';
|
||||
}
|
||||
|
||||
function loadSavedTheme(): Theme | null {
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
const saved = localStorage.getItem(STORAGE_KEY);
|
||||
if (saved === 'light' || saved === 'dark') {
|
||||
return saved;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function saveTheme(t: Theme): void {
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
localStorage.setItem(STORAGE_KEY, t);
|
||||
}
|
||||
}
|
||||
|
||||
export function useTheme() {
|
||||
function toggleTheme(): void {
|
||||
theme.value = theme.value === 'light' ? 'dark' : 'light';
|
||||
}
|
||||
|
||||
function setTheme(t: Theme): void {
|
||||
theme.value = t;
|
||||
}
|
||||
|
||||
watch(theme, (newTheme) => {
|
||||
applyTheme(newTheme);
|
||||
saveTheme(newTheme);
|
||||
});
|
||||
|
||||
onMounted(() => {
|
||||
const saved = loadSavedTheme();
|
||||
const initial = saved ?? getSystemPreference();
|
||||
theme.value = initial;
|
||||
applyTheme(initial);
|
||||
|
||||
if (typeof window !== 'undefined' && window.matchMedia) {
|
||||
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
|
||||
mediaQuery.addEventListener('change', (e) => {
|
||||
if (!loadSavedTheme()) {
|
||||
theme.value = e.matches ? 'dark' : 'light';
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
theme,
|
||||
toggleTheme,
|
||||
setTheme,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user