feat: 添加客户端主题切换并优化导出状态
This commit is contained in:
@@ -0,0 +1,150 @@
|
||||
<template>
|
||||
<div class="theme-wrap">
|
||||
<button class="theme-button" :aria-label="labels.title" :title="labels.title" @click.stop="open = !open">
|
||||
<svg v-if="mode === 'auto'" viewBox="0 0 24 24" aria-hidden="true">
|
||||
<path d="M12 3a9 9 0 1 0 9 9" />
|
||||
<path d="M12 3a9 9 0 0 1 0 18" />
|
||||
</svg>
|
||||
<svg v-else-if="isDark" viewBox="0 0 24 24" aria-hidden="true">
|
||||
<path d="M21 12.8A8.5 8.5 0 1 1 11.2 3a6.5 6.5 0 0 0 9.8 9.8Z" />
|
||||
</svg>
|
||||
<svg v-else viewBox="0 0 24 24" aria-hidden="true">
|
||||
<circle cx="12" cy="12" r="4" />
|
||||
<path d="M12 2v2M12 20v2M4.9 4.9l1.4 1.4M17.7 17.7l1.4 1.4M2 12h2M20 12h2M4.9 19.1l1.4-1.4M17.7 6.3l1.4-1.4" />
|
||||
</svg>
|
||||
</button>
|
||||
<transition name="theme-menu">
|
||||
<ul v-if="open" class="theme-menu" @click.stop>
|
||||
<li
|
||||
v-for="item in options"
|
||||
:key="item.value"
|
||||
class="theme-item"
|
||||
:class="{ active: item.value === mode }"
|
||||
@click="choose(item.value)"
|
||||
>
|
||||
<span>{{ item.label }}</span>
|
||||
<span v-if="item.value === mode" class="theme-check">✓</span>
|
||||
</li>
|
||||
</ul>
|
||||
</transition>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onBeforeUnmount, onMounted, ref } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import { useTheme, type ThemeMode } from '@/composables/useTheme';
|
||||
|
||||
const { locale } = useI18n();
|
||||
const { mode, isDark, setThemeMode } = useTheme();
|
||||
const open = ref(false);
|
||||
|
||||
const isZh = computed(() => String(locale.value).startsWith('zh'));
|
||||
const labels = computed(() =>
|
||||
isZh.value
|
||||
? { title: '切换主题', light: '浅色', dark: '深色', auto: '跟随系统' }
|
||||
: { title: 'Switch theme', light: 'Light', dark: 'Dark', auto: 'System' },
|
||||
);
|
||||
const options = computed<Array<{ label: string; value: ThemeMode }>>(() => [
|
||||
{ label: labels.value.light, value: 'light' },
|
||||
{ label: labels.value.dark, value: 'dark' },
|
||||
{ label: labels.value.auto, value: 'auto' },
|
||||
]);
|
||||
|
||||
function choose(value: ThemeMode) {
|
||||
setThemeMode(value);
|
||||
open.value = false;
|
||||
}
|
||||
|
||||
function close() {
|
||||
open.value = false;
|
||||
}
|
||||
|
||||
onMounted(() => document.addEventListener('click', close));
|
||||
onBeforeUnmount(() => document.removeEventListener('click', close));
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.theme-wrap {
|
||||
position: relative;
|
||||
display: inline-flex;
|
||||
}
|
||||
.theme-button {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 34px;
|
||||
height: 34px;
|
||||
border: none;
|
||||
border-radius: 50%;
|
||||
background: transparent;
|
||||
color: var(--text);
|
||||
cursor: pointer;
|
||||
}
|
||||
.theme-button svg {
|
||||
width: 21px;
|
||||
height: 21px;
|
||||
fill: none;
|
||||
stroke: currentColor;
|
||||
stroke-linecap: round;
|
||||
stroke-linejoin: round;
|
||||
stroke-width: 1.8;
|
||||
}
|
||||
.theme-button:active,
|
||||
.theme-button:hover {
|
||||
background: rgba(46, 84, 150, 0.08);
|
||||
}
|
||||
.theme-menu {
|
||||
position: absolute;
|
||||
top: calc(100% + 6px);
|
||||
right: 0;
|
||||
z-index: 20;
|
||||
min-width: 118px;
|
||||
margin: 0;
|
||||
padding: 6px;
|
||||
list-style: none;
|
||||
background: var(--card-bg);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.16);
|
||||
}
|
||||
.theme-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
padding: 9px 12px;
|
||||
border-radius: 7px;
|
||||
font-size: 14px;
|
||||
color: var(--text);
|
||||
cursor: pointer;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.theme-item:active,
|
||||
.theme-item:hover {
|
||||
background: var(--soft-bg);
|
||||
}
|
||||
.theme-item.active {
|
||||
color: var(--brand);
|
||||
font-weight: 600;
|
||||
}
|
||||
.theme-check {
|
||||
color: var(--brand);
|
||||
}
|
||||
.theme-menu-enter-active,
|
||||
.theme-menu-leave-active {
|
||||
transition:
|
||||
opacity 0.16s ease,
|
||||
transform 0.16s ease;
|
||||
transform-origin: top right;
|
||||
}
|
||||
.theme-menu-enter-from,
|
||||
.theme-menu-leave-to {
|
||||
opacity: 0;
|
||||
transform: scale(0.92) translateY(-4px);
|
||||
}
|
||||
:global(html[data-theme='dark']) .theme-button:active,
|
||||
:global(html[data-theme='dark']) .theme-button:hover {
|
||||
background: rgba(148, 163, 184, 0.14);
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user