mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-06-01 07:08:30 +00:00
d0da1a104e
Add theme-color <meta> tag and dynamically change the theme-color when switching into / out of dark mode. It use the same colours as in the nav-bar component, but I don't think it's possible to access the Windi CSS colour definitions, so I had to hard core the hex values. The theme-colour <meta> tag needs to be present in the HTML, otherwise `querySelector` returns null.
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
import { computed, ref, watch } from 'vue';
|
|
|
|
const LS_DARK_MODE = 'woodpecker:dark-mode';
|
|
const isDarkModeActive = ref(false);
|
|
|
|
watch(isDarkModeActive, (isActive) => {
|
|
if (isActive) {
|
|
document.documentElement.classList.remove('light');
|
|
document.documentElement.classList.add('dark');
|
|
document.querySelector('meta[name=theme-color]')?.setAttribute('content', '#2e323e'); // dark-gray-900 (see windi.config.ts)
|
|
} else {
|
|
document.documentElement.classList.remove('dark');
|
|
document.documentElement.classList.add('light');
|
|
document.querySelector('meta[name=theme-color]')?.setAttribute('content', '#65a30d'); // lime-600
|
|
}
|
|
});
|
|
|
|
function setDarkMode(isActive: boolean) {
|
|
isDarkModeActive.value = isActive;
|
|
localStorage.setItem(LS_DARK_MODE, isActive ? 'dark' : 'light');
|
|
}
|
|
|
|
function load() {
|
|
const isActive = localStorage.getItem(LS_DARK_MODE) as 'dark' | 'light' | null;
|
|
if (isActive === null) {
|
|
setDarkMode(window.matchMedia('(prefers-color-scheme: dark)').matches);
|
|
} else {
|
|
setDarkMode(isActive === 'dark');
|
|
}
|
|
}
|
|
|
|
load();
|
|
|
|
export function useDarkMode() {
|
|
return {
|
|
darkMode: computed({
|
|
get() {
|
|
return isDarkModeActive.value;
|
|
},
|
|
set(isActive: boolean) {
|
|
setDarkMode(isActive);
|
|
},
|
|
}),
|
|
};
|
|
}
|