[dark] add "automatic" to the mode switch

This commit is contained in:
Andrea 2022-01-18 20:26:21 +01:00
parent b7a2800608
commit 306e54fc4d
22 changed files with 87 additions and 51 deletions

View File

@ -40,7 +40,7 @@
</li> </li>
</ul> </ul>
<div class="my-2"> <div class="mt-2 mb-4 text-center">
<ModeSwitch/> <ModeSwitch/>
</div> </div>

View File

@ -1,12 +1,12 @@
<template> <template>
<div class="py-2 px-3"> <div class="btn-group" role="group">
<div class="form-check form-switch text-dark"> <button v-for="(buttonIcon, buttonMode) in modes" type="button"
<label> :class="['btn', 'btn-sm', mode === buttonMode ? 'btn-primary' : 'btn-outline-primary']"
<input class="form-check-input" type="checkbox" v-model="isDark"> @click="mode = buttonMode"
<Icon :v="isDark ? 'moon' : 'sun'"/> >
{{ $t('mode.' + (isDark ? 'dark' : 'light')) }} <Icon :v="buttonIcon"/>
</label> <T>mode.{{ buttonMode }}</T>
</div> </button>
</div> </div>
</template> </template>
@ -17,7 +17,13 @@
mixins: [dark], mixins: [dark],
data() { data() {
return { return {
mode: 'automatic',
isDark: false, isDark: false,
modes: {
light: 'sun',
automatic: 'eclipse',
dark: 'moon',
}
} }
}, },
mounted() { mounted() {
@ -25,20 +31,26 @@
return false; return false;
} }
this.mode = this.getMode();
this.isDark = this.detectDark(); this.isDark = this.detectDark();
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => this.isDark = e.matches);
this.$eventHub.$on('mode-changed', dark => { window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => this.isDark = this.detectDark());
if (dark !== this.isDark) {
this.isDark = dark; this.$eventHub.$on('mode-changed', mode => {
if (mode !== this.mode) {
this.mode = mode;
} }
}) })
}, },
watch: { watch: {
isDark(dark) { mode() {
this.$eventHub.$emit('mode-changed', dark); this.$eventHub.$emit('mode-changed', this.mode);
this.setMode(dark); this.setMode(this.mode);
this.$store.commit('setDarkMode', dark); this.isDark = this.detectDark();
},
isDark() {
this.setIsDark(this.isDark);
this.$store.commit('setDarkMode', this.isDark);
} }
} }
} }

View File

@ -10,7 +10,7 @@
export default { export default {
mixins: [dark], mixins: [dark],
mounted() { mounted() {
this.setMode(this.detectDark()); this.setIsDark(this.detectDark());
} }
} }
</script> </script>

View File

@ -42,7 +42,7 @@
}); });
}); });
}; };
this.setMode(this.detectDark()); this.setIsDark(this.detectDark());
if (!process.client) { return; } if (!process.client) { return; }

View File

@ -798,8 +798,9 @@ captcha:
invalid: 'Invalid CAPTCHA, please try again' invalid: 'Invalid CAPTCHA, please try again'
mode: mode:
dark: 'Dark mode'
light: 'Light mode' light: 'Light mode'
automatic: 'Automatic'
dark: 'Dark mode'
ban: ban:
reason: 'Ban reason' reason: 'Ban reason'

View File

@ -707,8 +707,9 @@ captcha:
invalid: 'Die CAPTCHA-Eingabe war leider ungültig, bitte versuch es erneut.' invalid: 'Die CAPTCHA-Eingabe war leider ungültig, bitte versuch es erneut.'
mode: mode:
dark: 'Dunkelmodus'
light: 'Lichtmodus' light: 'Lichtmodus'
automatic: 'Automatisch'
dark: 'Dunkelmodus'
ban: ban:
reason: 'Grund der Sperrung' reason: 'Grund der Sperrung'

View File

@ -851,8 +851,9 @@ captcha:
invalid: 'Invalid CAPTCHA, please try again' invalid: 'Invalid CAPTCHA, please try again'
mode: mode:
dark: 'Dark mode'
light: 'Light mode' light: 'Light mode'
automatic: 'Automatic'
dark: 'Dark mode'
ban: ban:
reason: 'Ban reason' reason: 'Ban reason'

View File

@ -783,8 +783,9 @@ captcha:
invalid: 'Invalid CAPTCHA, please try again' invalid: 'Invalid CAPTCHA, please try again'
mode: mode:
dark: 'Dark mode'
light: 'Light mode' light: 'Light mode'
automatic: 'Automatic'
dark: 'Dark mode'
ban: ban:
reason: 'Ban reason' reason: 'Ban reason'

View File

@ -725,8 +725,9 @@ captcha:
invalid: 'CAPTCHA no válida, por favor intenta de nuevo' invalid: 'CAPTCHA no válida, por favor intenta de nuevo'
mode: mode:
dark: 'Modo oscuro'
light: 'Modo claro' light: 'Modo claro'
automatic: 'Automatico'
dark: 'Modo oscuro'
ban: ban:
reason: 'Razón para banear' reason: 'Razón para banear'

View File

@ -718,8 +718,9 @@ captcha:
invalid: 'Captcha invalide, veuillez réessayer' invalid: 'Captcha invalide, veuillez réessayer'
mode: mode:
dark: 'Mode sombre'
light: 'Mode clair' light: 'Mode clair'
automatic: 'Automatique'
dark: 'Mode sombre'
ban: ban:
reason: 'Raison du bannissement' reason: 'Raison du bannissement'

View File

@ -731,8 +731,9 @@ captcha:
invalid: 'O Captcha foi invalidado, por favor, tente novamente' invalid: 'O Captcha foi invalidado, por favor, tente novamente'
mode: mode:
dark: 'Modo escuro'
light: 'Modo claro' light: 'Modo claro'
automatic: 'Automatic' # TODO
dark: 'Modo escuro'
ban: ban:
reason: 'Razão de ban' reason: 'Razão de ban'

View File

@ -740,8 +740,9 @@ captcha:
invalid: 'CAPTCHAが無効です。もう一度お試しください。' invalid: 'CAPTCHAが無効です。もう一度お試しください。'
mode: mode:
dark: 'ダークモード'
light: 'ライトモード' light: 'ライトモード'
automatic: 'Automatic' # TODO
dark: 'ダークモード'
ban: ban:
reason: '禁止の理由' reason: '禁止の理由'

View File

@ -701,8 +701,9 @@ captcha:
invalid: 'CAPTCHA enfirma, por favor aprova de muevo' invalid: 'CAPTCHA enfirma, por favor aprova de muevo'
mode: mode:
dark: 'Modo eskuro'
light: 'Modo klaro' light: 'Modo klaro'
automatic: 'Automatic'
dark: 'Modo eskuro'
ban: ban:
reason: 'Razon para banear' reason: 'Razon para banear'

View File

@ -721,8 +721,9 @@ captcha:
invalid: 'Ongeldige CAPTCHA, probeer het alsjeblieft nog een keer.' invalid: 'Ongeldige CAPTCHA, probeer het alsjeblieft nog een keer.'
mode: mode:
dark: 'Donkere modus'
light: 'Lichte modus' light: 'Lichte modus'
automatic: 'Automatische'
dark: 'Donkere modus'
ban: ban:
reason: 'Reden voor ban' reason: 'Reden voor ban'

View File

@ -727,8 +727,9 @@ captcha:
invalid: 'Ugyldig CAPTCHA, vennligst prøv igjen' invalid: 'Ugyldig CAPTCHA, vennligst prøv igjen'
mode: mode:
dark: 'Mørkt tema'
light: 'Lyst tema' light: 'Lyst tema'
automatic: 'Automatisk'
dark: 'Mørkt tema'
ban: ban:
reason: 'Grunn til forbud' reason: 'Grunn til forbud'

View File

@ -1555,8 +1555,9 @@ captcha:
invalid: 'Nieprawidłowa CAPTCHA, spróbuj ponownie' invalid: 'Nieprawidłowa CAPTCHA, spróbuj ponownie'
mode: mode:
dark: 'Tryb nocny'
light: 'Tryb dzienny' light: 'Tryb dzienny'
automatic: 'Auto'
dark: 'Tryb nocny'
ban: ban:
reason: 'Powód blokady' reason: 'Powód blokady'

View File

@ -723,8 +723,9 @@ captcha:
invalid: 'O Captcha foi invalidado, por favor, tente novamente' invalid: 'O Captcha foi invalidado, por favor, tente novamente'
mode: mode:
dark: 'Modo escuro'
light: 'Modo claro' light: 'Modo claro'
automatic: 'Automático'
dark: 'Modo escuro'
ban: ban:
reason: 'Razão de ban' reason: 'Razão de ban'

View File

@ -748,8 +748,9 @@ captcha:
invalid: 'Капча введена неверно. Пожалуйста, попробуйте ещё раз' invalid: 'Капча введена неверно. Пожалуйста, попробуйте ещё раз'
mode: mode:
dark: 'Тёмная тема'
light: 'Светлая тема' light: 'Светлая тема'
automatic: 'Автоматический'
dark: 'Тёмная тема'
ban: ban:
reason: 'Причина блокировки' reason: 'Причина блокировки'

View File

@ -742,8 +742,9 @@ captcha:
invalid: 'Ogiltlig CAPTCHA, snälla försök igen' invalid: 'Ogiltlig CAPTCHA, snälla försök igen'
mode: mode:
dark: 'Mörkt läge'
light: 'Ljust läge' light: 'Ljust läge'
automatic: 'Automatiskt'
dark: 'Mörkt läge'
ban: # TODO ban: # TODO
reason: 'Ban reason' reason: 'Ban reason'

View File

@ -704,8 +704,9 @@ captcha:
invalid: 'Invalid CAPTCHA, please try again' invalid: 'Invalid CAPTCHA, please try again'
mode: mode:
dark: 'טונקלמאָדוס'
light: 'ליכטמאָדוס' light: 'ליכטמאָדוס'
automatic: 'Automatic' # TODO
dark: 'טונקלמאָדוס'
# TODO # TODO
ban: ban:

View File

@ -707,8 +707,9 @@ captcha:
invalid: 'Invalid CAPTCHA, please try again' invalid: 'Invalid CAPTCHA, please try again'
mode: mode:
dark: '黑暗模式'
light: '明亮模式' light: '明亮模式'
automatic: 'Automatic' # TODO
dark: '黑暗模式'
ban: ban:
reason: '封禁原因' reason: '封禁原因'

View File

@ -1,28 +1,35 @@
export default { export default {
methods: { methods: {
detectDark() { getMode() {
if (!process.client) { if (!process.client) { return 'automatic'; }
return false;
}
if (localStorage.getItem('darkMode') !== null) { return localStorage.getItem('mode') || 'automatic';
return localStorage.getItem('darkMode') === 'dark';
}
const mediaMatch = window.matchMedia('(prefers-color-scheme: dark)');
return mediaMatch.matches;
}, },
setMode(dark) { detectDark() {
if (!process.client) { if (!process.client) { return false; }
return;
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) { if (dark) {
document.body.setAttribute('data-theme', 'dark'); document.body.setAttribute('data-theme', 'dark');
localStorage.setItem('darkMode', 'dark');
} else { } else {
document.body.removeAttribute('data-theme'); document.body.removeAttribute('data-theme');
localStorage.setItem('darkMode', 'light');
} }
}, },
} }