diff --git a/components/nav/NavLogo.vue b/components/nav/NavLogo.vue index f541f257..6fc5549d 100644 --- a/components/nav/NavLogo.vue +++ b/components/nav/NavLogo.vue @@ -1,3 +1,6 @@ + + import type { ThemeColors } from '~/composables/settings' +import { THEME_COLORS } from '~/constants' + +const themes = await import('~/constants/themes.json').then((r) => { + const map = new Map<'dark' | 'light', [string, ThemeColors][]>([['dark', []], ['light', []]]) + const themes = r.default as [string, ThemeColors][] + for (const [key, theme] of themes) { + map.get('dark')!.push([key, theme]) + map.get('light')!.push([key, { + ...theme, + '--c-primary': `color-mix(in srgb, ${theme['--c-primary']}, black 25%)`, + }]) + } + return map +}) -const themes = await import('~/constants/themes.json').then(r => r.default) as [string, ThemeColors][] const settings = useUserSettings() -const currentTheme = computed(() => settings.value.themeColors?.['--theme-color-name'] || themes[0][1]['--theme-color-name']) +const media = useMediaQuery('(prefers-color-scheme: dark)') + +const colorMode = useColorMode() + +const useThemes = shallowRef<[string, ThemeColors][]>([]) + +watch(() => colorMode.preference, (cm) => { + const dark = cm === 'dark' || (cm === 'system' && media.value) + const newThemes = dark ? themes.get('dark')! : themes.get('light')! + const key = settings.value.themeColors?.['--theme-color-name'] || THEME_COLORS.defaultTheme + for (const [k, theme] of newThemes) { + if (k === key) { + settings.value.themeColors = theme + break + } + } + useThemes.value = newThemes +}, { immediate: true, flush: 'post' }) + +const currentTheme = computed(() => settings.value.themeColors?.['--theme-color-name'] || THEME_COLORS.defaultTheme) function updateTheme(theme: ThemeColors) { settings.value.themeColors = theme @@ -18,10 +50,11 @@ function updateTheme(theme: ThemeColors) {