Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 9x 9x 9x 9x 9x 9x 9x 9x 31x 31x 6x 6x 31x 31x 31x 31x 31x 31x 31x 1x 31x 31x 3x 3x 3x 31x 2x 2x 31x 31x | import { ref } from 'vue';
// Shared singleton (matches the auth-store pattern): one source of truth for
// the active theme across every component that calls useTheme().
const STORAGE_KEY = 'mt-theme';
const THEME_COLOR = { light: '#1e40af', dark: '#0a0e1a' };
const isDark = ref(false);
let initialized = false;
function systemPrefersDark() {
return (
typeof window !== 'undefined' &&
window.matchMedia('(prefers-color-scheme: dark)').matches
);
}
function apply(dark) {
isDark.value = dark;
if (typeof document === 'undefined') return;
document.documentElement.classList.toggle('dark', dark);
const meta = document.querySelector('meta[name="theme-color"]');
if (meta)
meta.setAttribute('content', dark ? THEME_COLOR.dark : THEME_COLOR.light);
}
function init() {
if (initialized || typeof window === 'undefined') return;
initialized = true;
const stored = localStorage.getItem(STORAGE_KEY);
apply(stored ? stored === 'dark' : systemPrefersDark());
// Follow OS changes only while the user hasn't set an explicit preference.
window
.matchMedia('(prefers-color-scheme: dark)')
.addEventListener('change', e => {
if (!localStorage.getItem(STORAGE_KEY)) apply(e.matches);
});
}
export function useTheme() {
init();
function setTheme(theme) {
localStorage.setItem(STORAGE_KEY, theme);
apply(theme === 'dark');
}
function toggle() {
setTheme(isDark.value ? 'light' : 'dark');
}
return { isDark, toggle, setTheme };
}
|