This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Zaimki/plugins/dark.js

37 lines
1000 B
JavaScript

export default {
methods: {
getMode() {
if (!process.client) { return 'automatic'; }
return localStorage.getItem('mode') || 'automatic';
},
detectDark() {
if (!process.client) { return false; }
switch (this.getMode()) {
case 'light':
return false;
case 'dark':
return true;
case 'automatic':
default:
return window.matchMedia('(prefers-color-scheme: dark)').matches;
}
},
setMode(mode) {
if (!process.client) { return; }
localStorage.setItem('mode', mode);
},
setIsDark(dark) {
if (!process.client) { return; }
if (dark) {
document.body.setAttribute('data-theme', 'dark');
} else {
document.body.removeAttribute('data-theme');
}
},
}
}