2024-05-19 21:20:42 +08:00
|
|
|
<script lang="ts" setup>
|
2024-06-08 19:49:06 +08:00
|
|
|
import { $t } from '@vben/locales';
|
2024-05-19 21:20:42 +08:00
|
|
|
import {
|
|
|
|
IcRoundMotionPhotosAuto,
|
|
|
|
IcRoundWbSunny,
|
|
|
|
MdiMoonAndStars,
|
|
|
|
} from '@vben-core/iconify';
|
2024-06-01 23:15:29 +08:00
|
|
|
import {
|
2024-06-09 12:53:38 +08:00
|
|
|
type ThemeModeType,
|
|
|
|
preferences,
|
2024-06-01 23:15:29 +08:00
|
|
|
updatePreferences,
|
|
|
|
usePreferences,
|
|
|
|
} from '@vben-core/preferences';
|
2024-05-19 21:20:42 +08:00
|
|
|
import {
|
|
|
|
ToggleGroup,
|
|
|
|
ToggleGroupItem,
|
|
|
|
VbenTooltip,
|
|
|
|
} from '@vben-core/shadcn-ui';
|
|
|
|
|
|
|
|
import ThemeButton from './theme-button.vue';
|
|
|
|
|
|
|
|
defineOptions({
|
|
|
|
name: 'ThemeToggle',
|
|
|
|
});
|
|
|
|
|
|
|
|
withDefaults(defineProps<{ shouldOnHover?: boolean }>(), {
|
|
|
|
shouldOnHover: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
function handleChange(isDark: boolean) {
|
2024-06-01 23:15:29 +08:00
|
|
|
updatePreferences({
|
|
|
|
app: { themeMode: isDark ? 'dark' : 'light' },
|
|
|
|
});
|
2024-05-19 21:20:42 +08:00
|
|
|
}
|
|
|
|
|
2024-06-01 23:15:29 +08:00
|
|
|
const { isDark } = usePreferences();
|
2024-05-19 21:20:42 +08:00
|
|
|
|
|
|
|
const PRESETS = [
|
|
|
|
{
|
|
|
|
icon: IcRoundWbSunny,
|
|
|
|
name: 'light',
|
|
|
|
title: $t('preference.light'),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
icon: MdiMoonAndStars,
|
|
|
|
name: 'dark',
|
|
|
|
title: $t('preference.dark'),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
icon: IcRoundMotionPhotosAuto,
|
|
|
|
name: 'auto',
|
|
|
|
title: $t('preference.follow-system'),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
</script>
|
|
|
|
<template>
|
|
|
|
<div>
|
|
|
|
<VbenTooltip side="bottom" :disabled="!shouldOnHover">
|
|
|
|
<template #trigger>
|
|
|
|
<ThemeButton
|
|
|
|
:model-value="isDark"
|
|
|
|
type="icon"
|
|
|
|
@update:model-value="handleChange"
|
|
|
|
/>
|
|
|
|
</template>
|
|
|
|
<ToggleGroup
|
2024-06-09 12:53:38 +08:00
|
|
|
:model-value="preferences.app.themeMode"
|
2024-05-19 21:20:42 +08:00
|
|
|
type="single"
|
|
|
|
variant="outline"
|
|
|
|
class="gap-2"
|
2024-06-09 12:53:38 +08:00
|
|
|
@update:model-value="
|
|
|
|
(val) =>
|
|
|
|
updatePreferences({ app: { themeMode: val as ThemeModeType } })
|
|
|
|
"
|
2024-05-19 21:20:42 +08:00
|
|
|
>
|
|
|
|
<ToggleGroupItem
|
|
|
|
v-for="item in PRESETS"
|
|
|
|
:key="item.name"
|
|
|
|
:value="item.name"
|
|
|
|
>
|
|
|
|
<component :is="item.icon" class="size-5" />
|
|
|
|
</ToggleGroupItem>
|
|
|
|
</ToggleGroup>
|
|
|
|
</VbenTooltip>
|
|
|
|
</div>
|
|
|
|
</template>
|