feat: add darkMode

This commit is contained in:
alisa 2023-04-25 01:02:59 +08:00
parent 9ff69c68cd
commit ab0fb2252e
2 changed files with 39 additions and 21 deletions

View File

@ -1,31 +1,52 @@
<script setup lang="ts"> <script setup lang="ts">
import { useThemeConfig } from '@core/composable/useThemeConfig' import { useThemeConfig } from '@core/composable/useThemeConfig';
import type { ThemeSwitcherTheme } from '@layouts/types' import type { ThemeSwitcherTheme } from '@layouts/types';
import { onMounted } from 'vue';
const props = defineProps<{ const props = defineProps<{
themes: ThemeSwitcherTheme[] themes: ThemeSwitcherTheme[];
}>() }>();
const { theme } = useThemeConfig() const { theme } = useThemeConfig();
const { state: currentThemeName, next: getNextThemeName, index: currentThemeIndex } = useCycleList(props.themes.map(t => t.name), { initialValue: theme.value }) const {
state: currentThemeName,
next: getNextThemeName,
index: currentThemeIndex,
} = useCycleList(
props.themes.map(t => t.name),
{ initialValue: theme.value }
);
const changeTheme = () => { const changeTheme = () => {
theme.value = getNextThemeName() theme.value = getNextThemeName();
} };
const changeMode = (val: 'dark' | 'light') => {
if (val === 'dark') {
document.documentElement.classList.add('dark');
document.documentElement.classList.remove('light');
} else {
document.documentElement.classList.add('light');
document.documentElement.classList.remove('dark');
}
};
// Update icon if theme is changed from other sources // Update icon if theme is changed from other sources
watch(theme, val => { watch(theme, (val: 'dark' | 'light') => {
currentThemeName.value = val currentThemeName.value = val;
}) changeMode(val);
});
onMounted(() => {
if (currentThemeName.value) {
changeMode(currentThemeName.value);
}
});
</script> </script>
<template> <template>
<IconBtn @click="changeTheme"> <IconBtn @click="changeTheme">
<VIcon :icon="props.themes[currentThemeIndex].icon" /> <VIcon :icon="props.themes[currentThemeIndex].icon" />
<VTooltip <VTooltip activator="parent" open-delay="1000">
activator="parent"
open-delay="1000"
>
<span class="text-capitalize">{{ currentThemeName }}</span> <span class="text-capitalize">{{ currentThemeName }}</span>
</VTooltip> </VTooltip>
</IconBtn> </IconBtn>

View File

@ -1,9 +1,7 @@
/** @type {import('tailwindcss').Config} */ /** @type {import('tailwindcss').Config} */
module.exports = { module.exports = {
content: [ darkMode: ['class'],
"./index.html", content: ['./index.html', './src/**/*.{vue,js,ts,jsx,tsx}'],
"./src/**/*.{vue,js,ts,jsx,tsx}",
],
theme: { theme: {
extend: { extend: {
colors: { colors: {
@ -15,5 +13,4 @@ module.exports = {
}, },
}, },
plugins: [], plugins: [],
} };