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

30 lines
831 B
JavaScript
Raw Normal View History

2021-06-15 03:57:50 -07:00
export default {
methods: {
detectDark() {
if (!process.client) {
return false;
}
2021-06-19 13:38:08 -07:00
if (localStorage.getItem('darkMode') !== null) {
return localStorage.getItem('darkMode') === 'dark';
2021-06-15 03:57:50 -07:00
}
const mediaMatch = window.matchMedia('(prefers-color-scheme: dark)');
return mediaMatch.matches;
},
setMode(dark) {
if (!process.client) {
return;
}
if (dark) {
document.body.setAttribute('data-theme', 'dark');
localStorage.setItem('darkMode', 'dark');
} else {
document.body.removeAttribute('data-theme');
2021-06-19 13:38:08 -07:00
localStorage.setItem('darkMode', 'light');
2021-06-15 03:57:50 -07:00
}
},
}
}