feat: 添加客户端主题切换并优化导出状态

This commit is contained in:
Nixevol
2026-07-05 04:14:17 +08:00
parent 09a68ad0df
commit e8e514f6dd
10 changed files with 273 additions and 6 deletions
+150
View File
@@ -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>
+46
View File
@@ -0,0 +1,46 @@
import { computed, ref } from 'vue';
export type ThemeMode = 'light' | 'dark' | 'auto';
const KEY = 'artveris_client_theme';
const modes: ThemeMode[] = ['light', 'dark', 'auto'];
const media = window.matchMedia('(prefers-color-scheme: dark)');
const systemDark = ref(media.matches);
media.addEventListener('change', (event) => {
systemDark.value = event.matches;
});
function getInitialMode(): ThemeMode {
const saved = localStorage.getItem(KEY) as ThemeMode | null;
return saved && modes.includes(saved) ? saved : 'auto';
}
const mode = ref<ThemeMode>(getInitialMode());
const isDark = computed(() => mode.value === 'dark' || (mode.value === 'auto' && systemDark.value));
function applyTheme() {
const root = document.documentElement;
if (mode.value === 'auto') {
root.removeAttribute('data-theme');
} else {
root.setAttribute('data-theme', mode.value);
}
root.style.colorScheme = isDark.value ? 'dark' : 'light';
}
export function setThemeMode(next: ThemeMode) {
mode.value = next;
localStorage.setItem(KEY, next);
applyTheme();
}
export function initTheme() {
applyTheme();
}
export function useTheme() {
return { mode, isDark, setThemeMode };
}
initTheme();
+1
View File
@@ -2,6 +2,7 @@ import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
import { i18n } from './locales';
import './composables/useTheme';
import './styles.css';
createApp(App).use(router).use(i18n).mount('#app');
+24
View File
@@ -23,6 +23,30 @@
--shell-shadow: 0 0 24px rgba(0, 0, 0, 0.24);
}
}
:root[data-theme='light'] {
color-scheme: light;
--brand: #1f3864;
--brand-2: #2e5496;
--bg: #f3f5f9;
--card-bg: #fff;
--soft-bg: #f6f8fb;
--border: #d0d7e2;
--text: #1f2937;
--muted: #6b7280;
--shell-shadow: 0 0 24px rgba(0, 0, 0, 0.04);
}
:root[data-theme='dark'] {
color-scheme: dark;
--brand: #7dd3fc;
--brand-2: #5eead4;
--bg: #0f172a;
--card-bg: #111827;
--soft-bg: #1f2937;
--border: #334155;
--text: #e5e7eb;
--muted: #94a3b8;
--shell-shadow: 0 0 24px rgba(0, 0, 0, 0.24);
}
* {
box-sizing: border-box;
-webkit-tap-highlight-color: transparent;
+11 -2
View File
@@ -3,7 +3,10 @@
<img class="logo" :src="appIconUrl" alt="" />
<h1>{{ t('home.title') }}</h1>
<p class="muted">{{ t('home.hint') }}</p>
<button class="lang" @click="toggleLang">{{ otherLangLabel }}</button>
<div class="home-actions">
<theme-switch />
<button class="lang" @click="toggleLang">{{ otherLangLabel }}</button>
</div>
<footer class="footer muted">
<span>{{ appName }}</span>
<span v-if="filingInfo">{{ filingInfo }}</span>
@@ -17,6 +20,7 @@ import { useI18n } from 'vue-i18n';
import { fetchAppInfo } from '@/api';
import { setLocale, SUPPORTED_LOCALES } from '@/locales';
import { resolveAppIcon, setFavicon } from '@/utils/appIcon';
import ThemeSwitch from '@/components/ThemeSwitch.vue';
const { t, locale } = useI18n();
const appName = ref('');
@@ -65,13 +69,18 @@ h1 {
margin: 8px 0 0;
}
.lang {
margin-top: 16px;
border: 1px solid var(--border);
background: var(--card-bg);
border-radius: 999px;
padding: 6px 18px;
color: var(--brand);
}
.home-actions {
display: inline-flex;
align-items: center;
gap: 10px;
margin-top: 16px;
}
.footer {
position: absolute;
bottom: 28px;
+28
View File
@@ -4,6 +4,7 @@
<header class="topbar">
<img class="topbar-icon" :src="appIconUrl" alt="" />
<span class="topbar-name text-ellipsis">{{ topbarTitle }}</span>
<theme-switch />
<div class="lang-wrap">
<button class="lang" :aria-label="t('lang.switch')" :title="t('lang.switch')" @click.stop="langOpen = !langOpen">
<svg viewBox="0 0 512 512" width="22" height="22" aria-hidden="true">
@@ -376,6 +377,7 @@ import { fetchTrace, fetchAppInfo } from '@/api';
import { setLocale, SUPPORTED_LOCALES } from '@/locales';
import { openMediaViewer } from '@/composables/openMediaViewer';
import { resolveAppIcon, setFavicon } from '@/utils/appIcon';
import ThemeSwitch from '@/components/ThemeSwitch.vue';
const route = useRoute();
const { t, locale } = useI18n();
@@ -1437,4 +1439,30 @@ watch(
background: rgba(148, 163, 184, 0.14);
}
}
:global(html[data-theme='light']) .topbar {
background: linear-gradient(135deg, rgba(255, 255, 255, 0.96), rgba(239, 249, 252, 0.96));
color: #17324d;
border-bottom-color: rgba(46, 84, 150, 0.12);
box-shadow: 0 6px 18px rgba(31, 56, 100, 0.06);
}
:global(html[data-theme='light']) .lang {
color: #17324d;
}
:global(html[data-theme='light']) .lang:active,
:global(html[data-theme='light']) .lang:hover {
background: rgba(46, 84, 150, 0.08);
}
:global(html[data-theme='dark']) .topbar {
background: linear-gradient(135deg, rgba(15, 23, 42, 0.96), rgba(17, 24, 39, 0.96));
color: var(--text);
border-bottom-color: rgba(148, 163, 184, 0.18);
box-shadow: 0 6px 18px rgba(0, 0, 0, 0.2);
}
:global(html[data-theme='dark']) .lang {
color: var(--text);
}
:global(html[data-theme='dark']) .lang:active,
:global(html[data-theme='dark']) .lang:hover {
background: rgba(148, 163, 184, 0.14);
}
</style>