toggle-theme-button.vue
1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<template>
<a-tooltip :content="theme === 'light' ? '切换为暗黑模式' : '切换为亮色模式'">
<a-button :shape="'circle'" class="nav-btn" type="outline" @click="() => onChange()">
<template #icon>
<icon-moon-fill v-if="theme === 'dark'" />
<icon-sun-fill v-else />
</template>
</a-button>
</a-tooltip>
</template>
<script lang="ts">
import { computed, defineComponent } from 'vue';
import { useAppStore } from '@/store';
import { useDark, useToggle } from '@vueuse/core';
import { IconMoonFill, IconSunFill } from '@arco-design/web-vue/es/icon';
export default defineComponent({
name: 'ToggleThemeButton',
components: {
IconSunFill,
IconMoonFill,
},
setup() {
const appStore = useAppStore();
const theme = computed(() => {
return appStore.theme;
});
const isDark = useDark({
selector: 'body',
attribute: 'arco-theme',
valueDark: 'dark',
valueLight: 'light',
storageKey: 'arco-theme',
onChanged(dark: boolean) {
appStore.toggleTheme(dark);
},
});
const onChange = useToggle(isDark);
return {
theme,
onChange,
};
},
});
</script>
<style lang="less" scoped>
.nav-btn {
border-color: rgb(var(--gray-2));
color: rgb(var(--gray-8));
font-size: 16px;
}
.trigger-btn,
.ref-btn {
position: absolute;
bottom: 14px;
}
.trigger-btn {
margin-left: 14px;
}
</style>