Skip to content

Commit

Permalink
feat(vitepress): 添加主题切换动画
Browse files Browse the repository at this point in the history
  • Loading branch information
maomao1996 committed Dec 26, 2023
1 parent 2e3f4fb commit 6f6a7f4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
46 changes: 46 additions & 0 deletions docs/.vitepress/theme/components/MLayout.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<script setup lang="ts">
import { useData } from 'vitepress'
import DefaultTheme from 'vitepress/theme'
import { nextTick, provide } from 'vue'
const { Layout } = DefaultTheme
const { isDark } = useData()
const enableTransitions = () =>
'startViewTransition' in document &&
window.matchMedia('(prefers-reduced-motion: no-preference)').matches
provide('toggle-appearance', async ({ clientX: x, clientY: y }: MouseEvent) => {
if (!enableTransitions()) {
isDark.value = !isDark.value
return
}
const clipPath = [
`circle(0px at ${x}px ${y}px)`,
`circle(${Math.hypot(
Math.max(x, innerWidth - x),
Math.max(y, innerHeight - y),
)}px at ${x}px ${y}px)`,
]
// @ts-ignore
await document.startViewTransition(async () => {
isDark.value = !isDark.value
await nextTick()
}).ready
document.documentElement.animate(
{ clipPath: isDark.value ? clipPath.reverse() : clipPath },
{
duration: 300,
easing: 'ease-in',
pseudoElement: `::view-transition-${isDark.value ? 'old' : 'new'}(root)`,
},
)
})
</script>

<template>
<Layout v-bind="$attrs" />
</template>
3 changes: 2 additions & 1 deletion docs/.vitepress/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import DefaultTheme from 'vitepress/theme'

import { createMediumZoomProvider } from './composables/useMediumZoom'

import MLayout from './components/MLayout.vue'
import MNavLinks from './components/MNavLinks.vue'

import './styles/index.scss'
Expand All @@ -22,7 +23,7 @@ export default {
props.class = frontmatter.value.layoutClass
}

return h(DefaultTheme.Layout, props)
return h(MLayout, props)
},
enhanceApp({ app, router }: EnhanceAppContext) {
createMediumZoomProvider(app, router)
Expand Down
17 changes: 17 additions & 0 deletions docs/.vitepress/theme/styles/vitepress.scss
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,20 @@ table {
.dark .vp-doc .custom-block a {
transition: color 0.25s;
}

/* dark/light radial transition */
::view-transition-old(root),
::view-transition-new(root) {
animation: none;
mix-blend-mode: normal;
}

::view-transition-old(root),
.dark::view-transition-new(root) {
z-index: 1;
}

::view-transition-new(root),
.dark::view-transition-old(root) {
z-index: 9999;
}

1 comment on commit 6f6a7f4

@vercel
Copy link

@vercel vercel bot commented on 6f6a7f4 Dec 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.