mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-20 06:55:06 -04:00
feat(enhancement): use system dark mode (#458)
* Use prefers-color-scheme * Remove theme store
This commit is contained in:
parent
1e2a35b892
commit
cf7b1f000a
4 changed files with 8 additions and 35 deletions
|
@ -1,18 +1,9 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { BrandGithub, BrandTwitter, InfoCircle, Moon, Sun } from '@vicons/tabler';
|
import { BrandGithub, BrandTwitter, InfoCircle, Moon, Sun } from '@vicons/tabler';
|
||||||
import { useStyleStore } from '@/stores/style.store';
|
import { useStyleStore } from '@/stores/style.store';
|
||||||
import { useThemeStore } from '@/ui/theme/theme.store';
|
|
||||||
|
|
||||||
const styleStore = useStyleStore();
|
const styleStore = useStyleStore();
|
||||||
const { isDarkTheme } = toRefs(styleStore);
|
const { isDarkTheme } = toRefs(styleStore);
|
||||||
|
|
||||||
const themeStore = useThemeStore();
|
|
||||||
|
|
||||||
function toggleDarkTheme() {
|
|
||||||
isDarkTheme.value = !isDarkTheme.value;
|
|
||||||
|
|
||||||
themeStore.toggleTheme();
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
@ -58,7 +49,7 @@ function toggleDarkTheme() {
|
||||||
</n-tooltip>
|
</n-tooltip>
|
||||||
<n-tooltip trigger="hover">
|
<n-tooltip trigger="hover">
|
||||||
<template #trigger>
|
<template #trigger>
|
||||||
<c-button circle variant="text" aria-label="Toggle dark/light mode" @click="toggleDarkTheme">
|
<c-button circle variant="text" aria-label="Toggle dark/light mode" @click="() => styleStore.toggleDark()">
|
||||||
<n-icon v-if="isDarkTheme" size="25" :component="Sun" />
|
<n-icon v-if="isDarkTheme" size="25" :component="Sun" />
|
||||||
<n-icon v-else size="25" :component="Moon" />
|
<n-icon v-else size="25" :component="Moon" />
|
||||||
</c-button>
|
</c-button>
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
import { useMediaQuery, useStorage } from '@vueuse/core';
|
import { useDark, useMediaQuery, useStorage, useToggle } from '@vueuse/core';
|
||||||
import { defineStore } from 'pinia';
|
import { defineStore } from 'pinia';
|
||||||
import { type Ref, watch } from 'vue';
|
import { type Ref, watch } from 'vue';
|
||||||
|
|
||||||
export const useStyleStore = defineStore('style', {
|
export const useStyleStore = defineStore('style', {
|
||||||
state: () => {
|
state: () => {
|
||||||
const isDarkTheme = useStorage('isDarkTheme', true) as Ref<boolean>;
|
const isDarkTheme = useDark();
|
||||||
|
const toggleDark = useToggle(isDarkTheme);
|
||||||
const isSmallScreen = useMediaQuery('(max-width: 700px)');
|
const isSmallScreen = useMediaQuery('(max-width: 700px)');
|
||||||
const isMenuCollapsed = useStorage('isMenuCollapsed', isSmallScreen.value) as Ref<boolean>;
|
const isMenuCollapsed = useStorage('isMenuCollapsed', isSmallScreen.value) as Ref<boolean>;
|
||||||
|
|
||||||
|
@ -12,6 +13,7 @@ export const useStyleStore = defineStore('style', {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
isDarkTheme,
|
isDarkTheme,
|
||||||
|
toggleDark,
|
||||||
isMenuCollapsed,
|
isMenuCollapsed,
|
||||||
isSmallScreen,
|
isSmallScreen,
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { useThemeStore } from './theme.store';
|
import { useStyleStore } from '@/stores/style.store';
|
||||||
|
|
||||||
export { defineThemes };
|
export { defineThemes };
|
||||||
|
|
||||||
|
@ -6,8 +6,8 @@ function defineThemes<Theme>(themes: { light: Theme; dark: Theme }) {
|
||||||
return {
|
return {
|
||||||
themes,
|
themes,
|
||||||
useTheme() {
|
useTheme() {
|
||||||
const themeStore = useThemeStore();
|
const styleStore = useStyleStore();
|
||||||
return computed(() => themes[themeStore.themeType]);
|
return computed(() => themes[styleStore.isDarkTheme ? 'dark' : 'light']);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +0,0 @@
|
||||||
import { defineStore } from 'pinia';
|
|
||||||
|
|
||||||
export const useThemeStore = defineStore('ui-theme', {
|
|
||||||
state: () => ({
|
|
||||||
themeType: useStorage<'dark' | 'light'>('ui-store:theme-type', 'dark') as Ref<'dark' | 'light'>,
|
|
||||||
}),
|
|
||||||
getters: {
|
|
||||||
isDarkTheme(): boolean {
|
|
||||||
return this.themeType === 'dark';
|
|
||||||
},
|
|
||||||
isLightTheme(): boolean {
|
|
||||||
return this.themeType === 'light';
|
|
||||||
},
|
|
||||||
},
|
|
||||||
actions: {
|
|
||||||
toggleTheme() {
|
|
||||||
this.themeType = this.isDarkTheme ? 'light' : 'dark';
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
Loading…
Add table
Add a link
Reference in a new issue