Skip to content

Commit

Permalink
docs: add view-transitions to dark mode toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
xsjcTony committed Dec 1, 2023
1 parent 7281e1d commit 8352af0
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 7 deletions.
65 changes: 65 additions & 0 deletions docs/.vitepress/theme/Layout.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<script setup lang="ts">
import { useData } from 'vitepress'
import DefaultTheme from 'vitepress/theme'
import { nextTick, provide } from 'vue'
const { isDark } = useData()
const enableTransitions = (): boolean => '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)`
]
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>
<DefaultTheme.Layout />
</template>


<style>
::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;
}
</style>
3 changes: 2 additions & 1 deletion docs/.vitepress/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import 'virtual:uno.css'

import DefaultTheme from 'vitepress/theme'
import { h } from 'vue'
import Layout from './Layout.vue'
import type { Theme } from 'vitepress'

import './style.scss'


export default {
'extends': DefaultTheme,
Layout: () => h(DefaultTheme.Layout, null, {
Layout: () => h(Layout, null, {
// https://vitepress.dev/guide/extending-default-theme#layout-slots
}),
enhanceApp({ app, router, siteData }) {
Expand Down
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ layout: home

hero:
name: Animations Preset for UnoCSS
tagline: Simple · Flexible · Beautiful · Composable
tagline: Simple · Beautiful · Flexible · Composable
image:
src: /logo.svg
alt: UnoCSS
Expand Down
5 changes: 5 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ export default defineConfig(
typescript: {
tsconfigPath: ['./tsconfig.json', './tsconfig.node.js.json'],
projectType: 'lib'
},
vue: {
ruleOptions: {
multiWordComponentNames: { ignores: ['Layout'] }
}
}
},
{
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@
"docs:preview": "vitepress preview docs"
},
"devDependencies": {
"@aelita-dev/eslint-config": "^1.0.0",
"@aelita-dev/eslint-config": "^1.1.0",
"@iconify/json": "^2.2.150",
"@types/dom-view-transitions": "^1.0.4",
"@types/markdown-it": "^13.0.7",
"@unocss/core": "^0.57.7",
"@unocss/preset-mini": "^0.57.7",
Expand Down
15 changes: 11 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8352af0

Please sign in to comment.