webapp: Implement dark theme

This commit is contained in:
Thomas Basler 2023-03-06 19:42:37 +01:00
parent b91da0f681
commit 95741c7fa2
7 changed files with 137 additions and 11 deletions

View File

@ -12,7 +12,7 @@
}, },
"dependencies": { "dependencies": {
"@popperjs/core": "^2.11.6", "@popperjs/core": "^2.11.6",
"bootstrap": "^5.2.3", "bootstrap": "^5.3.0-alpha1",
"bootstrap-icons-vue": "^1.8.1", "bootstrap-icons-vue": "^1.8.1",
"mitt": "^3.0.0", "mitt": "^3.0.0",
"spark-md5": "^3.0.2", "spark-md5": "^3.0.2",

View File

@ -1,5 +1,5 @@
<template> <template>
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark"> <nav class="navbar navbar-expand-md fixed-top bg-body-tertiary" data-bs-theme="dark">
<div class="container-fluid"> <div class="container-fluid">
<router-link @click="onClick" class="navbar-brand" to="/"> <router-link @click="onClick" class="navbar-brand" to="/">
<span v-if="isXmas" class="text-success"> <span v-if="isXmas" class="text-success">
@ -95,11 +95,14 @@
<router-link @click="onClick" class="nav-link" to="/about">{{ $t('menu.About') }}</router-link> <router-link @click="onClick" class="nav-link" to="/about">{{ $t('menu.About') }}</router-link>
</li> </li>
</ul> </ul>
<form class="d-flex" role="search"> <ul class="navbar-nav flex-row flex-wrap ms-md-auto">
<LocaleSwitcher class="me-2" /> <ThemeSwitcher class="me-2" />
<button v-if="isLogged" class="btn btn-outline-danger" @click="signout">{{ $t('menu.Logout') }}</button> <form class="d-flex" role="search">
<button v-if="!isLogged" class="btn btn-outline-success" @click="signin">{{ $t('menu.Login') }}</button> <LocaleSwitcher class="me-2" />
</form> <button v-if="isLogged" class="btn btn-outline-danger" @click="signout">{{ $t('menu.Logout') }}</button>
<button v-if="!isLogged" class="btn btn-outline-success" @click="signin">{{ $t('menu.Login') }}</button>
</form>
</ul>
</div> </div>
</div> </div>
</nav> </nav>
@ -110,6 +113,7 @@ import { isLoggedIn, logout } from '@/utils/authentication';
import { BIconEgg, BIconSun, BIconTree } from 'bootstrap-icons-vue'; import { BIconEgg, BIconSun, BIconTree } from 'bootstrap-icons-vue';
import { defineComponent } from 'vue'; import { defineComponent } from 'vue';
import LocaleSwitcher from './LocaleSwitcher.vue'; import LocaleSwitcher from './LocaleSwitcher.vue';
import ThemeSwitcher from './ThemeSwitcher.vue';
export default defineComponent({ export default defineComponent({
components: { components: {
@ -117,6 +121,7 @@ export default defineComponent({
BIconSun, BIconSun,
BIconTree, BIconTree,
LocaleSwitcher, LocaleSwitcher,
ThemeSwitcher,
}, },
data() { data() {
return { return {

View File

@ -0,0 +1,106 @@
<template>
<li class="nav-item dropdown">
<button class="btn btn-link nav-link py-2 px-0 px-lg-2 dropdown-toggle d-flex align-items-center" id="bd-theme"
type="button" aria-expanded="false" data-bs-toggle="dropdown" data-bs-display="static"
aria-label="Toggle theme (auto)">
<BIconCircleHalf class="bi my-1 theme-icon-active" />
</button>
<ul class="dropdown-menu dropdown-menu-end">
<li>
<button type="button" class="dropdown-item d-flex align-items-center" data-bs-theme-value="light"
aria-pressed="false">
<BIconSunFill class="bi me-2 opacity-50 theme-icon" />
{{ $t('localeswitcher.Light') }}
</button>
</li>
<li>
<button type="button" class="dropdown-item d-flex align-items-center" data-bs-theme-value="dark"
aria-pressed="false">
<BIconMoonStarsFill class="bi me-2 opacity-50 theme-icon" />
{{ $t('localeswitcher.Dark') }}
</button>
</li>
<li>
<button type="button" class="dropdown-item d-flex align-items-center active" data-bs-theme-value="auto"
aria-pressed="true">
<BIconCircleHalf class="bi me-2 opacity-50 theme-icon" />
{{ $t('localeswitcher.Auto') }}
</button>
</li>
</ul>
</li>
</template>
<script lang="ts">
import { defineComponent } from 'vue';
import {
BIconCircleHalf,
BIconSunFill,
BIconMoonStarsFill,
} from 'bootstrap-icons-vue';
export default defineComponent({
name: "ThemeSwitcher",
components: {
BIconCircleHalf,
BIconSunFill,
BIconMoonStarsFill,
},
data() {
return {
storedTheme: 'auto',
};
},
methods: {
getPreferredTheme() {
if (this.storedTheme) {
return this.storedTheme;
}
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
},
setTheme(theme: string) {
if (theme === 'auto' && window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.documentElement.setAttribute('data-bs-theme', 'dark');
} else {
document.documentElement.setAttribute('data-bs-theme', theme);
}
},
showActiveTheme(theme: string) {
const activeThemeIcon = document.querySelector('.theme-icon-active');
const btnToActive = document.querySelector(`[data-bs-theme-value="${theme}"]`);
const svgOfActiveBtn = btnToActive?.querySelector('.theme-icon');
document.querySelectorAll('[data-bs-theme-value]').forEach(element => {
element.classList.remove('active');
})
btnToActive?.classList.add('active');
if (svgOfActiveBtn) {
activeThemeIcon?.replaceChildren('*', svgOfActiveBtn?.cloneNode(true));
}
},
},
mounted() {
this.storedTheme = localStorage.getItem('theme') || 'auto';
this.setTheme(this.getPreferredTheme());
this.showActiveTheme(this.getPreferredTheme());
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
if (this.storedTheme !== 'light' && this.storedTheme !== 'dark') {
this.setTheme(this.getPreferredTheme());
}
});
document.querySelectorAll('[data-bs-theme-value]')
.forEach(toggle => {
toggle.addEventListener('click', () => {
const theme = toggle.getAttribute('data-bs-theme-value') || 'auto';
localStorage.setItem('theme', theme);
this.setTheme(theme);
this.showActiveTheme(theme);
})
});
},
});
</script>

View File

@ -25,6 +25,11 @@
"base": { "base": {
"Loading": "Lade..." "Loading": "Lade..."
}, },
"localeswitcher": {
"Dark": "Dunkel",
"Light": "Hell",
"Auto": "Automatisch"
},
"apiresponse": { "apiresponse": {
"1001": "Einstellungen gespeichert!", "1001": "Einstellungen gespeichert!",
"1002": "Keine Werte gefunden!", "1002": "Keine Werte gefunden!",

View File

@ -25,6 +25,11 @@
"base": { "base": {
"Loading": "Loading..." "Loading": "Loading..."
}, },
"localeswitcher": {
"Dark": "Dark",
"Light": "Light",
"Auto": "Auto"
},
"apiresponse": { "apiresponse": {
"1001": "Settings saved!", "1001": "Settings saved!",
"1002": "No values found!", "1002": "No values found!",

View File

@ -25,6 +25,11 @@
"base": { "base": {
"Loading": "Chargement..." "Loading": "Chargement..."
}, },
"localeswitcher": {
"Dark": "Dark",
"Light": "Light",
"Auto": "Auto"
},
"apiresponse": { "apiresponse": {
"1001": "Paramètres enregistrés !", "1001": "Paramètres enregistrés !",
"1002": "Aucune valeur trouvée !", "1002": "Aucune valeur trouvée !",

View File

@ -701,10 +701,10 @@ bootstrap-icons-vue@^1.8.1:
resolved "https://registry.yarnpkg.com/bootstrap-icons-vue/-/bootstrap-icons-vue-1.8.1.tgz#ce4a0c1f6efe41dabcc1341f2cb191d307fbaf50" resolved "https://registry.yarnpkg.com/bootstrap-icons-vue/-/bootstrap-icons-vue-1.8.1.tgz#ce4a0c1f6efe41dabcc1341f2cb191d307fbaf50"
integrity sha512-uItRULwQz0epETi9x/RBEqfjHmTAmkIIczpH1R6L9T6yyaaijk0826PzTWnWNm15tw66JT/8GNuXjB0HI5PHLw== integrity sha512-uItRULwQz0epETi9x/RBEqfjHmTAmkIIczpH1R6L9T6yyaaijk0826PzTWnWNm15tw66JT/8GNuXjB0HI5PHLw==
bootstrap@^5.2.3: bootstrap@^5.3.0-alpha1:
version "5.2.3" version "5.3.0-alpha1"
resolved "https://registry.yarnpkg.com/bootstrap/-/bootstrap-5.2.3.tgz#54739f4414de121b9785c5da3c87b37ff008322b" resolved "https://registry.yarnpkg.com/bootstrap/-/bootstrap-5.3.0-alpha1.tgz#380629c4367893f02f7879a01ea3ae0f94e2e70e"
integrity sha512-cEKPM+fwb3cT8NzQZYEu4HilJ3anCrWqh3CHAok1p9jXqMPsPTBhU25fBckEJHJ/p+tTxTFTsFQGM+gaHpi3QQ== integrity sha512-ABZpKK4ObS3kKlIqH+ZVDqoy5t/bhFG0oHTAzByUdon7YIom0lpCeTqRniDzJmbtcWkNe800VVPBiJgxSYTYew==
brace-expansion@^1.1.7: brace-expansion@^1.1.7:
version "1.1.11" version "1.1.11"