Skip to content

Commit 97d1e28

Browse files
committed
perf: 优化 useTheme 逻辑
1 parent 8d0c30b commit 97d1e28

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

src/common/components/ThemeSwitch/index.vue

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
11
<script lang="ts" setup>
2-
import type { ThemeName } from "@@/composables/useTheme"
32
import { useTheme } from "@@/composables/useTheme"
43
import { MagicStick } from "@element-plus/icons-vue"
54
65
const { themeList, activeThemeName, setTheme } = useTheme()
7-
8-
function handleChangeTheme({ clientX, clientY }: MouseEvent, themeName: ThemeName) {
9-
const maxRadius = Math.hypot(
10-
Math.max(clientX, window.innerWidth - clientX),
11-
Math.max(clientY, window.innerHeight - clientY)
12-
)
13-
const style = document.documentElement.style
14-
style.setProperty("--v3-theme-x", `${clientX}px`)
15-
style.setProperty("--v3-theme-y", `${clientY}px`)
16-
style.setProperty("--v3-theme-r", `${maxRadius}px`)
17-
const handler = () => {
18-
setTheme(themeName)
19-
}
20-
document.startViewTransition ? document.startViewTransition(handler) : handler()
21-
}
226
</script>
237

248
<template>
@@ -36,7 +20,7 @@ function handleChangeTheme({ clientX, clientY }: MouseEvent, themeName: ThemeNam
3620
v-for="(theme, index) in themeList"
3721
:key="index"
3822
:disabled="activeThemeName === theme.name"
39-
@click="(e: MouseEvent) => handleChangeTheme(e, theme.name)"
23+
@click="(e: MouseEvent) => setTheme(e, theme.name)"
4024
>
4125
<span>{{ theme.title }}</span>
4226
</el-dropdown-item>

src/common/composables/useTheme.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { getActiveThemeName, setActiveThemeName } from "@@/utils/cache/local-storage"
2+
import { setCssVar } from "@@/utils/css"
23

34
const DEFAULT_THEME_NAME = "normal"
45

@@ -32,8 +33,18 @@ const themeList: ThemeList[] = [
3233
const activeThemeName = ref<ThemeName>(getActiveThemeName() || DEFAULT_THEME_NAME)
3334

3435
/** 设置主题 */
35-
function setTheme(value: ThemeName) {
36-
activeThemeName.value = value
36+
function setTheme({ clientX, clientY }: MouseEvent, value: ThemeName) {
37+
const maxRadius = Math.hypot(
38+
Math.max(clientX, window.innerWidth - clientX),
39+
Math.max(clientY, window.innerHeight - clientY)
40+
)
41+
setCssVar("--v3-theme-x", `${clientX}px`)
42+
setCssVar("--v3-theme-y", `${clientY}px`)
43+
setCssVar("--v3-theme-r", `${maxRadius}px`)
44+
const handler = () => {
45+
activeThemeName.value = value
46+
}
47+
document.startViewTransition ? document.startViewTransition(handler) : handler()
3748
}
3849

3950
/** 在 html 根元素上挂载 class */

0 commit comments

Comments
 (0)