Skip to content

Commit

Permalink
perf(projects): add type declaration for document startViewTransition
Browse files Browse the repository at this point in the history
  • Loading branch information
honghuangdc committed Jun 20, 2023
1 parent cbda4a3 commit d3ebe95
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
37 changes: 20 additions & 17 deletions src/components/common/dark-mode-switch.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,35 @@ const darkMode = computed({
}
});
function handleSwitch(event: MouseEvent) {
async 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 endRadius = Math.hypot(Math.max(x, innerWidth - x), Math.max(y, innerHeight - y));
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)'
}
);
});
await transition.ready;
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>

Expand Down
8 changes: 8 additions & 0 deletions src/typings/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ interface Window {
$notification?: import('naive-ui').NotificationProviderInst;
}

interface ViewTransition {
ready: Promise<void>;
}

interface Document {
startViewTransition?: (callback: () => Promise<void> | void) => ViewTransition;
}

/** 通用类型 */
declare namespace Common {
/**
Expand Down

0 comments on commit d3ebe95

Please sign in to comment.