Skip to content

Commit

Permalink
feat(theme): add transition like nuxt devtools (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmg0 authored Feb 20, 2024
1 parent 1d876d3 commit 3e679b9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
47 changes: 46 additions & 1 deletion src/components/DarkSwitcher.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,56 @@
<script setup lang="ts">
import { isDark } from '../store'
const isAppearanceTransition = typeof document !== 'undefined'
// @ts-expect-error: Transition API
&& document.startViewTransition
&& !window.matchMedia('(prefers-reduced-motion: reduce)').matches
function toggleDark(event?: MouseEvent) {
if (!isAppearanceTransition || !event) {
isDark.value = !isDark.value
return
}
const x = event.clientX
const y = event.clientY
const endRadius = Math.hypot(
Math.max(x, innerWidth - x),
Math.max(y, innerHeight - y),
)
// @ts-expect-error: Transition API
const transition = document.startViewTransition(async () => {
isDark.value = !isDark.value
await nextTick()
})
transition.ready.then(() => {
const clipPath = [
`circle(0px at ${x}px ${y}px)`,
`circle(${endRadius}px at ${x}px ${y}px)`,
]
document.documentElement.animate(
{
clipPath: isDark.value
? [...clipPath].reverse()
: clipPath,
},
{
duration: 400,
easing: 'ease-in',
pseudoElement: isDark.value
? '::view-transition-old(root)'
: '::view-transition-new(root)',
},
)
})
}
</script>

<template>
<button
icon-button
dark:i-carbon-moon i-carbon:sun
@click="isDark = !isDark"
@click="toggleDark"
/>
</template>
19 changes: 19 additions & 0 deletions src/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,22 @@ html.dark {
.dark .icons-item [stroke='black'] {
stroke: currentColor;
}

/* Color Mode transition */
::view-transition-old(root),
::view-transition-new(root) {
animation: none;
mix-blend-mode: normal;
}
::view-transition-old(root) {
z-index: 1;
}
::view-transition-new(root) {
z-index: 2147483646;
}
.dark::view-transition-old(root) {
z-index: 2147483646;
}
.dark::view-transition-new(root) {
z-index: 1;
}

0 comments on commit 3e679b9

Please sign in to comment.