|
55 | 55 | import Tooltip, { tooltip } from '$lib/components/Tooltip.svelte'; |
56 | 56 | import { isInputFocused } from '$lib/utils/focus'; |
57 | 57 |
|
| 58 | + // Theme toggle button ref for radial transition origin |
| 59 | + let themeToggleBtn: HTMLButtonElement; |
| 60 | +
|
| 61 | + function toggleThemeWithTransition(e?: MouseEvent) { |
| 62 | + const apply = () => themeStore.toggle(); |
| 63 | +
|
| 64 | + if (!document.startViewTransition) { apply(); return; } |
| 65 | +
|
| 66 | + let x: number, y: number; |
| 67 | + if (e) { |
| 68 | + x = e.clientX; y = e.clientY; |
| 69 | + } else if (themeToggleBtn) { |
| 70 | + const rect = themeToggleBtn.getBoundingClientRect(); |
| 71 | + x = rect.left + rect.width / 2; |
| 72 | + y = rect.top + rect.height / 2; |
| 73 | + } else { |
| 74 | + apply(); return; |
| 75 | + } |
| 76 | +
|
| 77 | + const maxRadius = Math.hypot(Math.max(x, innerWidth - x), Math.max(y, innerHeight - y)); |
| 78 | + const transition = document.startViewTransition(apply); |
| 79 | + transition.ready.then(() => { |
| 80 | + document.documentElement.animate( |
| 81 | + { clipPath: [`circle(0px at ${x}px ${y}px)`, `circle(${maxRadius}px at ${x}px ${y}px)`] }, |
| 82 | + { duration: 500, easing: 'ease-out', pseudoElement: '::view-transition-new(root)' } |
| 83 | + ); |
| 84 | + }); |
| 85 | + } |
| 86 | +
|
58 | 87 | // Track mouse position for paste operations |
59 | 88 | let mousePosition = $state({ x: 0, y: 0 }); |
60 | 89 |
|
|
731 | 760 | return; |
732 | 761 | case 't': |
733 | 762 | event.preventDefault(); |
734 | | - themeStore.toggle(); |
| 763 | + toggleThemeWithTransition(); |
735 | 764 | return; |
736 | 765 | case '+': |
737 | 766 | case '=': |
|
1197 | 1226 | </button> |
1198 | 1227 | <button |
1199 | 1228 | class="toggle-btn" |
1200 | | - onclick={() => themeStore.toggle()} |
| 1229 | + bind:this={themeToggleBtn} |
| 1230 | + onclick={(e) => toggleThemeWithTransition(e)} |
1201 | 1231 | use:tooltip={{ text: currentTheme === 'dark' ? 'Light mode' : 'Dark mode', shortcut: "T", position: "right" }} |
1202 | 1232 | aria-label="Toggle theme" |
1203 | 1233 | > |
|
0 commit comments