diff options
author | 2023-06-18 09:59:22 +0100 | |
---|---|---|
committer | 2023-06-18 10:59:22 +0200 | |
commit | cf7b1f000a7b33df48ce3883b4c837c4a87768cc (patch) | |
tree | da8a1b7c7f1e0b4bb720e4fb39689b5370cfc38b /src | |
parent | 1e2a35b89235865cb34b450eb07c8e162cc3513e (diff) | |
download | it-tools-cf7b1f000a7b33df48ce3883b4c837c4a87768cc.tar.gz it-tools-cf7b1f000a7b33df48ce3883b4c837c4a87768cc.tar.zst it-tools-cf7b1f000a7b33df48ce3883b4c837c4a87768cc.zip |
feat(enhancement): use system dark mode (#458)
* Use prefers-color-scheme
* Remove theme store
Diffstat (limited to 'src')
-rw-r--r-- | src/components/NavbarButtons.vue | 11 | ||||
-rw-r--r-- | src/stores/style.store.ts | 6 | ||||
-rw-r--r-- | src/ui/theme/theme.models.ts | 6 | ||||
-rw-r--r-- | src/ui/theme/theme.store.ts | 20 |
4 files changed, 8 insertions, 35 deletions
diff --git a/src/components/NavbarButtons.vue b/src/components/NavbarButtons.vue index 81661d9..5b1a3a4 100644 --- a/src/components/NavbarButtons.vue +++ b/src/components/NavbarButtons.vue @@ -1,18 +1,9 @@ <script setup lang="ts"> import { BrandGithub, BrandTwitter, InfoCircle, Moon, Sun } from '@vicons/tabler'; import { useStyleStore } from '@/stores/style.store'; -import { useThemeStore } from '@/ui/theme/theme.store'; const styleStore = useStyleStore(); const { isDarkTheme } = toRefs(styleStore); - -const themeStore = useThemeStore(); - -function toggleDarkTheme() { - isDarkTheme.value = !isDarkTheme.value; - - themeStore.toggleTheme(); -} </script> <template> @@ -58,7 +49,7 @@ function toggleDarkTheme() { </n-tooltip> <n-tooltip trigger="hover"> <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-else size="25" :component="Moon" /> </c-button> diff --git a/src/stores/style.store.ts b/src/stores/style.store.ts index d70bd76..8342a88 100644 --- a/src/stores/style.store.ts +++ b/src/stores/style.store.ts @@ -1,10 +1,11 @@ -import { useMediaQuery, useStorage } from '@vueuse/core'; +import { useDark, useMediaQuery, useStorage, useToggle } from '@vueuse/core'; import { defineStore } from 'pinia'; import { type Ref, watch } from 'vue'; export const useStyleStore = defineStore('style', { state: () => { - const isDarkTheme = useStorage('isDarkTheme', true) as Ref<boolean>; + const isDarkTheme = useDark(); + const toggleDark = useToggle(isDarkTheme); const isSmallScreen = useMediaQuery('(max-width: 700px)'); const isMenuCollapsed = useStorage('isMenuCollapsed', isSmallScreen.value) as Ref<boolean>; @@ -12,6 +13,7 @@ export const useStyleStore = defineStore('style', { return { isDarkTheme, + toggleDark, isMenuCollapsed, isSmallScreen, }; diff --git a/src/ui/theme/theme.models.ts b/src/ui/theme/theme.models.ts index 850afe9..5ab78d7 100644 --- a/src/ui/theme/theme.models.ts +++ b/src/ui/theme/theme.models.ts @@ -1,4 +1,4 @@ -import { useThemeStore } from './theme.store'; +import { useStyleStore } from '@/stores/style.store'; export { defineThemes }; @@ -6,8 +6,8 @@ function defineThemes<Theme>(themes: { light: Theme; dark: Theme }) { return { themes, useTheme() { - const themeStore = useThemeStore(); - return computed(() => themes[themeStore.themeType]); + const styleStore = useStyleStore(); + return computed(() => themes[styleStore.isDarkTheme ? 'dark' : 'light']); }, }; } diff --git a/src/ui/theme/theme.store.ts b/src/ui/theme/theme.store.ts deleted file mode 100644 index e2457fc..0000000 --- a/src/ui/theme/theme.store.ts +++ /dev/null @@ -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'; - }, - }, -}); |