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
Raw Normal View History

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