Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions src/client/theme-default/components/VPSwitchAppearance.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> | void

const beforeThemeSwitch = inject<BeforeSwitchHook>(
'before-appearance-switch',
() => true
)

const toggleAppearance = inject('toggle-appearance', () => {
isDark.value = !isDark.value
})
Expand All @@ -19,12 +37,7 @@ watchPostEffect(() => {
</script>

<template>
<VPSwitch
:title="switchTitle"
class="VPSwitchAppearance"
:aria-checked="isDark"
@click="toggleAppearance"
>
<VPSwitch :title="switchTitle" class="VPSwitchAppearance" :aria-checked="isDark" @click="handleClick">
<span class="vpi-sun sun" />
<span class="vpi-moon moon" />
</VPSwitch>
Expand Down
Loading