Browse Source

use color mode plugin

pull/1250/head
Bernd Storath 9 months ago
parent
commit
0ee7baa4f7
  1. 47
      src/app.vue
  2. 6
      src/composables/useColorMode.ts
  3. 12
      src/nuxt.config.ts
  4. 1
      src/package.json
  5. 20
      src/pnpm-lock.yaml
  6. 3
      src/utils/localStorage.ts

47
src/app.vue

@ -19,11 +19,11 @@
<!-- Dark / light theme --> <!-- Dark / light theme -->
<button <button
class="flex items-center justify-center w-8 h-8 rounded-full bg-gray-200 hover:bg-gray-300 dark:bg-neutral-700 dark:hover:bg-neutral-600 transition" class="flex items-center justify-center w-8 h-8 rounded-full bg-gray-200 hover:bg-gray-300 dark:bg-neutral-700 dark:hover:bg-neutral-600 transition"
:title="$t(`theme.${uiTheme}`)" :title="$t(`theme.${theme.preference}`)"
@click="toggleTheme" @click="toggleTheme"
> >
<svg <svg
v-if="uiTheme === 'light'" v-if="theme.preference === 'light'"
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"
fill="none" fill="none"
viewBox="0 0 24 24" viewBox="0 0 24 24"
@ -38,7 +38,7 @@
/> />
</svg> </svg>
<svg <svg
v-else-if="uiTheme === 'dark'" v-else-if="theme.preference === 'dark'"
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"
fill="none" fill="none"
viewBox="0 0 24 24" viewBox="0 0 24 24"
@ -1232,17 +1232,8 @@ const uiTrafficStats = ref(false);
const uiChartType = ref(0); const uiChartType = ref(0);
const uiShowCharts = ref(getItem('uiShowCharts') === '1'); const uiShowCharts = ref(getItem('uiShowCharts') === '1');
const uiTheme = ref<Theme>(getItem('theme') || 'auto');
const prefersDarkScheme = import.meta.client const theme = useTheme();
? window.matchMedia('(prefers-color-scheme: dark)')
: null;
const theme = computed(() => {
if (uiTheme.value === 'auto') {
return prefersDarkScheme?.matches ? 'dark' : 'light';
}
return uiTheme.value as 'dark' | 'light';
});
const chartOptions = { const chartOptions = {
chart: { chart: {
@ -1526,24 +1517,15 @@ function restoreConfig(e) {
} }
function toggleTheme() { function toggleTheme() {
const themes = ['light', 'dark', 'auto'] as Theme[]; const themeCycle = {
const currentIndex = themes.indexOf(uiTheme.value); system: 'light',
const newIndex = (currentIndex + 1) % themes.length; light: 'dark',
uiTheme.value = themes[newIndex]; dark: 'system',
setItem('theme', uiTheme.value); } as const;
setTheme(uiTheme.value);
} theme.preference = themeCycle[theme.preference];
function setTheme(theme: Theme) {
const { classList } = document.documentElement;
const shouldAddDarkClass =
theme === 'dark' || (theme === 'auto' && prefersDarkScheme?.matches);
classList.toggle('dark', shouldAddDarkClass);
}
function handlePrefersChange(e: MediaQueryListEventMap['change']) {
if (getItem('theme') === 'auto') {
setTheme(e.matches ? 'dark' : 'light');
}
} }
function toggleCharts() { function toggleCharts() {
setItem('uiShowCharts', uiShowCharts.value ? '1' : '0'); setItem('uiShowCharts', uiShowCharts.value ? '1' : '0');
} }
@ -1551,9 +1533,6 @@ function toggleCharts() {
const { availableLocales, locale } = useI18n(); const { availableLocales, locale } = useI18n();
onMounted(() => { onMounted(() => {
prefersDarkScheme?.addEventListener('change', handlePrefersChange);
setTheme(uiTheme.value);
api api
.getSession() .getSession()
.then((session) => { .then((session) => {

6
src/composables/useColorMode.ts

@ -0,0 +1,6 @@
export const useTheme = useColorMode as () => ThemeInstance;
type ThemeInstance = ReturnType<typeof useColorMode> & {
preference: 'system' | 'dark' | 'light';
value: 'dark' | 'light';
};

12
src/nuxt.config.ts

@ -2,5 +2,15 @@
export default defineNuxtConfig({ export default defineNuxtConfig({
compatibilityDate: '2024-04-03', compatibilityDate: '2024-04-03',
devtools: { enabled: true }, devtools: { enabled: true },
modules: ['@nuxtjs/i18n', '@nuxtjs/tailwindcss', '@pinia/nuxt'], modules: [
'@nuxtjs/i18n',
'@nuxtjs/tailwindcss',
'@pinia/nuxt',
'@nuxtjs/color-mode',
],
colorMode: {
preference: 'system',
fallback: 'light',
classSuffix: '',
},
}); });

1
src/package.json

@ -19,6 +19,7 @@
"typecheck": "nuxt typecheck" "typecheck": "nuxt typecheck"
}, },
"dependencies": { "dependencies": {
"@nuxtjs/color-mode": "^3.4.2",
"@nuxtjs/i18n": "^8.3.3", "@nuxtjs/i18n": "^8.3.3",
"@nuxtjs/tailwindcss": "^6.12.1", "@nuxtjs/tailwindcss": "^6.12.1",
"@pinia/nuxt": "^0.5.3", "@pinia/nuxt": "^0.5.3",

20
src/pnpm-lock.yaml

@ -7,6 +7,9 @@ settings:
importers: importers:
.: .:
dependencies: dependencies:
'@nuxtjs/color-mode':
specifier: ^3.4.2
version: 3.4.2([email protected])([email protected])
'@nuxtjs/i18n': '@nuxtjs/i18n':
specifier: ^8.3.3 specifier: ^8.3.3
version: 8.3.3([email protected])([email protected])([email protected]([email protected])) version: 8.3.3([email protected])([email protected])([email protected]([email protected]))
@ -1359,6 +1362,12 @@ packages:
peerDependencies: peerDependencies:
vue: ^3.3.4 vue: ^3.3.4
'@nuxtjs/[email protected]':
resolution:
{
integrity: sha512-6A+lDP8R6fFXc1Ip5tDepKq9MJW6oxbRlz1plvW52yacnpeDFXv5S5rDS0ax31AuSFUPlgzHymFSdjcylBwZ6w==,
}
'@nuxtjs/[email protected]': '@nuxtjs/[email protected]':
resolution: resolution:
{ {
@ -8328,6 +8337,17 @@ snapshots:
- vti - vti
- vue-tsc - vue-tsc
'@nuxtjs/[email protected]([email protected])([email protected])':
dependencies:
'@nuxt/kit': 3.12.4([email protected])([email protected])
pathe: 1.1.2
pkg-types: 1.1.3
semver: 7.6.3
transitivePeerDependencies:
- magicast
- rollup
- supports-color
'@nuxtjs/[email protected]([email protected])([email protected])([email protected]([email protected]))': '@nuxtjs/[email protected]([email protected])([email protected])([email protected]([email protected]))':
dependencies: dependencies:
'@intlify/h3': 0.5.0 '@intlify/h3': 0.5.0

3
src/utils/localStorage.ts

@ -1,7 +1,4 @@
export type Theme = 'light' | 'dark' | 'auto';
export type LocalStorage = { export type LocalStorage = {
theme: Theme;
uiShowCharts: '1' | '0'; uiShowCharts: '1' | '0';
lang: string; lang: string;
}; };

Loading…
Cancel
Save