Skip to content

Commit

Permalink
Merge pull request #226 from abstain23/feat/theme-transtion
Browse files Browse the repository at this point in the history
feat(projects): 增加主题切换过渡效果
  • Loading branch information
honghuangdc committed May 14, 2023
2 parents f68285f + 8da8843 commit 36fc74c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@
},
"[vue]": {
"editor.defaultFormatter": "Vue.volar"
}
},
"i18n-ally.localesPaths": ["src/locales", "src/locales/lang"]
}
48 changes: 45 additions & 3 deletions src/components/common/dark-mode-switch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,51 @@ const darkMode = computed({
}
});
function handleSwitch() {
darkMode.value = !darkMode.value;
function handleSwitch(event: MouseEvent) {
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
if (!document.startViewTransition) {
darkMode.value = !darkMode.value;
return;
}
// @ts-expect-error: Transition API
const transition = document.startViewTransition(() => {
darkMode.value = !darkMode.value;
});
transition.ready.then(() => {
const clipPath = [`circle(0px at ${x}px ${y}px)`, `circle(${endRadius}px at ${x}px ${y}px)`];
document.documentElement.animate(
{
clipPath: darkMode.value ? clipPath : [...clipPath].reverse()
},
{
duration: 300,
easing: 'ease-in',
pseudoElement: darkMode.value ? '::view-transition-new(root)' : '::view-transition-old(root)'
}
);
});
}
</script>

<style scoped></style>
<style>
::view-transition-old(root),
::view-transition-new(root) {
animation: none;
mix-blend-mode: normal;
}
::view-transition-old(root) {
z-index: 9999;
}
::view-transition-new(root) {
z-index: 1;
}
.dark::view-transition-old(root) {
z-index: 1;
}
.dark::view-transition-new(root) {
z-index: 9999;
}
</style>

1 comment on commit 36fc74c

@vercel
Copy link

@vercel vercel bot commented on 36fc74c May 14, 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.