|
| 1 | +type SiteTheme = "light" | "dark"; |
| 2 | + |
| 3 | +interface BioThemeTransitionOptions { |
| 4 | + nextTheme: SiteTheme; |
| 5 | + setTheme: (theme: SiteTheme) => void; |
| 6 | + trigger: HTMLElement; |
| 7 | +} |
| 8 | + |
| 9 | +let themeTransitionActive = false; |
| 10 | + |
| 11 | +const applyTheme = ( |
| 12 | + nextTheme: SiteTheme, |
| 13 | + setTheme: BioThemeTransitionOptions["setTheme"], |
| 14 | +) => { |
| 15 | + const root = document.documentElement; |
| 16 | + root.classList.toggle("dark", nextTheme === "dark"); |
| 17 | + root.style.colorScheme = nextTheme; |
| 18 | + setTheme(nextTheme); |
| 19 | +}; |
| 20 | + |
| 21 | +const runFallbackBloom = ({ |
| 22 | + nextTheme, |
| 23 | + originX, |
| 24 | + originY, |
| 25 | + revealRadius, |
| 26 | + setTheme, |
| 27 | +}: { |
| 28 | + nextTheme: SiteTheme; |
| 29 | + originX: number; |
| 30 | + originY: number; |
| 31 | + revealRadius: number; |
| 32 | + setTheme: BioThemeTransitionOptions["setTheme"]; |
| 33 | +}) => { |
| 34 | + const bloom = document.createElement("span"); |
| 35 | + const bloomSize = 80; |
| 36 | + const revealScale = (revealRadius + bloomSize) / (bloomSize / 2); |
| 37 | + const translatedOrigin = `translate3d(${originX - bloomSize / 2}px, ${ |
| 38 | + originY - bloomSize / 2 |
| 39 | + }px, 0)`; |
| 40 | + |
| 41 | + bloom.className = `theme-bio-bloom theme-bio-bloom--${nextTheme}`; |
| 42 | + bloom.setAttribute("aria-hidden", "true"); |
| 43 | + document.body.appendChild(bloom); |
| 44 | + |
| 45 | + if (typeof bloom.animate !== "function") { |
| 46 | + bloom.remove(); |
| 47 | + applyTheme(nextTheme, setTheme); |
| 48 | + themeTransitionActive = false; |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + const growth = bloom.animate( |
| 53 | + [ |
| 54 | + { |
| 55 | + opacity: 0.35, |
| 56 | + transform: `${translatedOrigin} scale(0.04)`, |
| 57 | + }, |
| 58 | + { |
| 59 | + opacity: 1, |
| 60 | + transform: `${translatedOrigin} scale(${revealScale * 1.025})`, |
| 61 | + offset: 0.82, |
| 62 | + }, |
| 63 | + { |
| 64 | + opacity: 1, |
| 65 | + transform: `${translatedOrigin} scale(${revealScale})`, |
| 66 | + }, |
| 67 | + ], |
| 68 | + { |
| 69 | + duration: 640, |
| 70 | + easing: "cubic-bezier(0.16, 1, 0.3, 1)", |
| 71 | + fill: "forwards", |
| 72 | + }, |
| 73 | + ); |
| 74 | + |
| 75 | + growth.finished |
| 76 | + .then(() => { |
| 77 | + applyTheme(nextTheme, setTheme); |
| 78 | + return bloom.animate([{ opacity: 1 }, { opacity: 0 }], { |
| 79 | + duration: 260, |
| 80 | + easing: "cubic-bezier(0.4, 0, 1, 1)", |
| 81 | + fill: "forwards", |
| 82 | + }).finished; |
| 83 | + }) |
| 84 | + .catch(() => { |
| 85 | + applyTheme(nextTheme, setTheme); |
| 86 | + }) |
| 87 | + .finally(() => { |
| 88 | + bloom.remove(); |
| 89 | + themeTransitionActive = false; |
| 90 | + }); |
| 91 | +}; |
| 92 | + |
| 93 | +export const runBioThemeTransition = ({ |
| 94 | + nextTheme, |
| 95 | + setTheme, |
| 96 | + trigger, |
| 97 | +}: BioThemeTransitionOptions) => { |
| 98 | + const reduceMotion = window.matchMedia( |
| 99 | + "(prefers-reduced-motion: reduce)", |
| 100 | + ).matches; |
| 101 | + |
| 102 | + if (reduceMotion) { |
| 103 | + applyTheme(nextTheme, setTheme); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + if (themeTransitionActive) return; |
| 108 | + themeTransitionActive = true; |
| 109 | + |
| 110 | + const triggerBounds = trigger.getBoundingClientRect(); |
| 111 | + const originX = triggerBounds.left + triggerBounds.width / 2; |
| 112 | + const originY = triggerBounds.top + triggerBounds.height / 2; |
| 113 | + const horizontalReach = Math.max(originX, window.innerWidth - originX); |
| 114 | + const verticalReach = Math.max(originY, window.innerHeight - originY); |
| 115 | + const revealRadius = Math.hypot(horizontalReach, verticalReach); |
| 116 | + const membraneColor = |
| 117 | + nextTheme === "dark" |
| 118 | + ? "rgb(138 190 235 / 0.52)" |
| 119 | + : "rgb(255 183 18 / 0.58)"; |
| 120 | + |
| 121 | + trigger.animate( |
| 122 | + [ |
| 123 | + { transform: "scale(1)" }, |
| 124 | + { transform: "scale(0.92)", offset: 0.32 }, |
| 125 | + { transform: "scale(1)" }, |
| 126 | + ], |
| 127 | + { |
| 128 | + duration: 460, |
| 129 | + easing: "cubic-bezier(0.22, 1, 0.36, 1)", |
| 130 | + }, |
| 131 | + ); |
| 132 | + |
| 133 | + if (!document.startViewTransition) { |
| 134 | + runFallbackBloom({ |
| 135 | + nextTheme, |
| 136 | + originX, |
| 137 | + originY, |
| 138 | + revealRadius, |
| 139 | + setTheme, |
| 140 | + }); |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + try { |
| 145 | + const transition = document.startViewTransition(() => { |
| 146 | + applyTheme(nextTheme, setTheme); |
| 147 | + }); |
| 148 | + |
| 149 | + transition.ready |
| 150 | + .then(() => { |
| 151 | + const newTheme = document.documentElement.animate( |
| 152 | + { |
| 153 | + clipPath: [ |
| 154 | + `circle(0px at ${originX}px ${originY}px)`, |
| 155 | + `circle(${revealRadius * 0.32}px at ${originX}px ${originY}px)`, |
| 156 | + `circle(${revealRadius}px at ${originX}px ${originY}px)`, |
| 157 | + ], |
| 158 | + filter: [ |
| 159 | + `drop-shadow(0 0 0 ${membraneColor}) saturate(1)`, |
| 160 | + `drop-shadow(0 0 18px ${membraneColor}) saturate(1.08)`, |
| 161 | + "drop-shadow(0 0 0 transparent) saturate(1)", |
| 162 | + ], |
| 163 | + }, |
| 164 | + { |
| 165 | + duration: 820, |
| 166 | + easing: "cubic-bezier(0.16, 1, 0.3, 1)", |
| 167 | + pseudoElement: "::view-transition-new(root)", |
| 168 | + }, |
| 169 | + ); |
| 170 | + |
| 171 | + const oldTheme = document.documentElement.animate( |
| 172 | + { |
| 173 | + opacity: [1, 0.94], |
| 174 | + transform: ["scale(1)", "scale(0.992)"], |
| 175 | + }, |
| 176 | + { |
| 177 | + duration: 540, |
| 178 | + easing: "cubic-bezier(0.4, 0, 1, 1)", |
| 179 | + fill: "forwards", |
| 180 | + pseudoElement: "::view-transition-old(root)", |
| 181 | + }, |
| 182 | + ); |
| 183 | + |
| 184 | + return Promise.allSettled([newTheme.finished, oldTheme.finished]); |
| 185 | + }) |
| 186 | + .catch(() => undefined); |
| 187 | + |
| 188 | + transition.finished.finally(() => { |
| 189 | + themeTransitionActive = false; |
| 190 | + }).catch(() => undefined); |
| 191 | + } catch { |
| 192 | + themeTransitionActive = false; |
| 193 | + applyTheme(nextTheme, setTheme); |
| 194 | + } |
| 195 | +}; |
0 commit comments