From baee8d356d825b44dc872bee8b177c587e3c50dc Mon Sep 17 00:00:00 2001 From: 363045841 <161981174@qq.com> Date: Tue, 23 Sep 2025 21:06:31 +0800 Subject: [PATCH] feat(theme): add before-theme-switch hook for custom logic before theme switching --- .../components/VPSwitchAppearance.vue | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/client/theme-default/components/VPSwitchAppearance.vue b/src/client/theme-default/components/VPSwitchAppearance.vue index 2dd06281..7b5fe035 100644 --- a/src/client/theme-default/components/VPSwitchAppearance.vue +++ b/src/client/theme-default/components/VPSwitchAppearance.vue @@ -5,6 +5,24 @@ import VPSwitch from './VPSwitch.vue' const { isDark, theme } = useData() +const handleClick = async () => { + try { + const result = await beforeThemeSwitch() + if (result !== false) { + toggleAppearance() + } + } catch (e) { + console.error('[VitePress] Error in before-appearance-switch hook:', e) + } +} + +type BeforeSwitchHook = () => boolean | Promise | void + +const beforeThemeSwitch = inject( + 'before-appearance-switch', + () => true +) + const toggleAppearance = inject('toggle-appearance', () => { isDark.value = !isDark.value }) @@ -19,12 +37,7 @@ watchPostEffect(() => {