-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
37 lines (31 loc) · 1.1 KB
/
script.js
File metadata and controls
37 lines (31 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const button = document.getElementById("toggle-button");
const body = document.getElementById("body");
button.addEventListener("click", toggleTheme);
async function toggleTheme() {
if (!document.startViewTransition || window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
body.classList.toggle('dark-mode');
return;
}
const { top, left, width, height } = button.getBoundingClientRect();
const x = left + width / 2;
const y = top + height / 2;
const right = window.innerWidth - left;
const bottom = window.innerHeight - top;
const maxRadius = Math.hypot(Math.max(left, right), Math.max(top, bottom));
await document.startViewTransition(() => {
body.classList.toggle('dark-mode');
}).ready;
document.documentElement.animate(
{
clipPath: [
`circle(0px at ${x}px ${y}px)`,
`circle(${maxRadius}px at ${x}px ${y}px)`,
],
},
{
duration: 500,
easing: 'ease-in-out',
pseudoElement: '::view-transition-new(root)',
}
);
}