@superwall/cli 1.6.2 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (162) hide show
  1. package/README.md +281 -34
  2. package/dist/apple-CkKlHBZ9.js +195 -0
  3. package/dist/auth.d.ts +4 -19
  4. package/dist/auth.js +4 -2
  5. package/dist/{actions-H7f6knyY.js → billing-XCZU_T46.js} +98 -28
  6. package/dist/{bind-BIzaGq8K.js → bind-drne__Ys.js} +38 -33
  7. package/dist/{build-B7BS1rF2.js → build-Gpdq5QXP.js} +20 -26
  8. package/dist/client-BZ1UbpXz.js +66 -0
  9. package/dist/client-DP6Rc0t_.js +176 -0
  10. package/dist/config-BO1Y87kV.js +105 -0
  11. package/dist/create-BBrtgUgS.js +969 -0
  12. package/dist/{create-CwnnNqv4.js → create-Cwc3SRWJ.js} +194 -168
  13. package/dist/create-Yvf2gStM.js +19 -0
  14. package/dist/doctor-C9ldpPFC.js +83 -0
  15. package/dist/{drivers-6RMOPIdn.js → drivers-DNrx4LWA.js} +185 -172
  16. package/dist/entry.d.ts +0 -5
  17. package/dist/entry.js +28083 -801
  18. package/dist/environment-DF1qKlny.js +58 -0
  19. package/dist/{existing-DHpPn1y6.js → existing-78LWSsqS.js} +12 -6
  20. package/dist/held-Cy8Wl4D1.js +1112 -0
  21. package/dist/held-Ddw1XSsr.js +16 -0
  22. package/dist/input-vi0bou04.js +42 -0
  23. package/dist/{integrate-BR2LR-EK.js → integrate-DcJhpXy8.js} +277 -253
  24. package/dist/kinds-GFXB3sYu.js +11 -0
  25. package/dist/log-NVhVe_oe.js +62 -0
  26. package/dist/login-B2TsCE2H.js +53 -0
  27. package/dist/login-BoBe8Psl.js +16 -0
  28. package/dist/{login-Bhxgqwtm.js → login-D0lh3_JI.js} +144 -66
  29. package/dist/lookup-D-nmmAbw.js +71 -0
  30. package/dist/migrate-Cfec564E.js +274 -0
  31. package/dist/notification-url-YDbq6FMA.js +100 -0
  32. package/dist/{open-studio-BnVD6tJ9.js → open-Ba2vBl1_.js} +34 -43
  33. package/dist/org-D0g_q3mv.js +35 -0
  34. package/dist/output-Ggzq5_-t.js +122 -0
  35. package/dist/paths-DxSLdpOI.js +29 -0
  36. package/dist/playbook-DIeb7hne.js +53 -0
  37. package/dist/{asc-products-Dvcyef3_.js → products-DH_fK9tY.js} +6 -39
  38. package/dist/promote-COD4fYdz.js +91 -0
  39. package/dist/push-CncywhkQ.js +482 -0
  40. package/dist/push-CtD4FZQV.js +22 -0
  41. package/dist/registry-DSrosOYs.js +10228 -0
  42. package/dist/{reports-DqMs9xNR.js → reports-DdPq4blM.js} +7 -6
  43. package/dist/{resources-Bf9O1JNM.js → resources-Dqr01Dep.js} +150 -55
  44. package/dist/{run-D-NbZLpj.js → run-C9qg_QlC.js} +16 -22
  45. package/dist/scan-Cv5uOwVY.js +4 -0
  46. package/dist/{scan-BmAW413l.js → scan-DLrsrFS9.js} +48 -37
  47. package/dist/scope-V336uz8j.js +198 -0
  48. package/dist/screenshot-file-0UETadF8.js +16 -0
  49. package/dist/{selection-BGi6GyxC.js → selection-Rdw-ZUoG.js} +94 -51
  50. package/dist/send-BxD9yf6F.js +150 -0
  51. package/dist/{dev-CgGAb5Al.js → server-D0tGG8C4.js} +82 -87
  52. package/dist/server-z1LH94R_.js +22 -0
  53. package/dist/session-1AV_Bs_d.js +6 -0
  54. package/dist/session-CFAmMhem.js +40 -0
  55. package/dist/session-D_nEWKXT.js +768 -0
  56. package/dist/skills/animate-onboarding/SKILL.md +467 -0
  57. package/dist/skills/animate-onboarding/references/engine.md +494 -0
  58. package/dist/skills/animate-onboarding/references/framework/Question.tsx +71 -0
  59. package/dist/skills/animate-onboarding/references/framework/README.md +40 -0
  60. package/dist/skills/animate-onboarding/references/framework/answers.ts +19 -0
  61. package/dist/skills/animate-onboarding/references/framework/flow.ts +80 -0
  62. package/dist/skills/animate-onboarding/references/framework/handoffs.ts +869 -0
  63. package/dist/skills/animate-onboarding/references/framework/layout.tsx +113 -0
  64. package/dist/skills/animate-onboarding/references/framework/motion.css +559 -0
  65. package/dist/skills/animate-onboarding/references/framework/useStoryRouter.ts +48 -0
  66. package/dist/skills/animate-onboarding/references/handoffs.md +375 -0
  67. package/dist/skills/animate-onboarding/references/imageReady.ts +92 -0
  68. package/dist/skills/animate-onboarding/references/morphFrame.ts +356 -0
  69. package/dist/skills/animate-onboarding/references/performance.md +41 -0
  70. package/dist/skills/animate-onboarding/references/web-runtime/README.md +17 -0
  71. package/dist/skills/animate-onboarding/references/web-runtime/handoffs.ts +858 -0
  72. package/dist/skills/animate-onboarding/references/web-runtime/story-motion.css +301 -0
  73. package/dist/skills/index.json +37 -0
  74. package/dist/skills/mobile-design/SKILL.md +447 -0
  75. package/dist/skills/mobile-design/references/responsive.md +162 -0
  76. package/dist/skills/onboarding-questions/SKILL.md +231 -0
  77. package/dist/skills/onboarding-questions/references/arpu-questions.md +139 -0
  78. package/dist/skills/onboarding-questions/references/craft.md +142 -0
  79. package/dist/skills/onboarding-questions/references/example-reflective.md +117 -0
  80. package/dist/skills/superwall/SKILL.md +82 -9
  81. package/dist/skills/superwall/references/api.md +153 -37
  82. package/dist/skills/superwall/references/apple-setup.md +316 -0
  83. package/dist/skills/superwall/references/asc.md +74 -38
  84. package/dist/skills/superwall/references/data-analytics.md +55 -21
  85. package/dist/skills/superwall/workflows/dashboard/playbook.md +3 -3
  86. package/dist/skills/superwall/workflows/dashboard/setup.md +8 -8
  87. package/dist/skills/superwall/workflows/migrate/adapty.md +6 -6
  88. package/dist/skills/superwall/workflows/migrate/playbook.md +4 -4
  89. package/dist/skills/superwall/workflows/migrate/qonversion.md +5 -5
  90. package/dist/skills/superwall/workflows/migrate/revenuecat.md +6 -6
  91. package/dist/skills/superwall/workflows/placements/android.md +2 -2
  92. package/dist/skills/superwall/workflows/placements/expo.md +2 -2
  93. package/dist/skills/superwall/workflows/placements/flutter.md +2 -2
  94. package/dist/skills/superwall/workflows/placements/ios.md +2 -2
  95. package/dist/skills/superwall/workflows/placements/playbook.md +2 -2
  96. package/dist/skills/superwall/workflows/placements/react-native.md +2 -2
  97. package/dist/skills/superwall/workflows/placements/strategy.md +2 -2
  98. package/dist/skills/superwall/workflows/review/playbook.md +1 -1
  99. package/dist/skills/superwall-editor/SKILL.md +61 -0
  100. package/dist/skills/superwall-editor/references/cli.md +135 -0
  101. package/dist/skills/superwall-editor/references/design.md +51 -0
  102. package/dist/skills/superwall-editor/references/native-elements.md +171 -0
  103. package/dist/skills/superwall-editor/references/workflow.md +65 -0
  104. package/dist/skills/superwall-editor/scripts/sw-editor.sh +662 -0
  105. package/dist/skills/superwall-framework/SKILL.md +78 -28
  106. package/dist/skills/superwall-framework/references/cli.md +58 -28
  107. package/dist/skills/superwall-framework/references/examples.md +4 -3
  108. package/dist/skills/superwall-framework/references/layout.md +30 -8
  109. package/dist/skills/superwall-framework/references/migrate-from-editor.md +1 -1
  110. package/dist/skills/superwall-framework/references/onboarding-state.md +245 -0
  111. package/dist/skills/wwdc/SKILL.md +22 -0
  112. package/dist/skills/wwdc/references/docs.md +52 -0
  113. package/dist/skills/wwdc/scripts/wwdc.sh +266 -0
  114. package/dist/{skills-AxAf7bDB.js → skills-BvFOa4y6.js} +99 -122
  115. package/dist/skills-DMG4bpKk.js +14 -0
  116. package/dist/{spawn-DSoTXy_j.js → spawn-DmmmHIP-.js} +14 -12
  117. package/dist/spinner-CneRK5rk.js +294 -0
  118. package/dist/storage-DYvmIOHa.d.ts +16 -0
  119. package/dist/storage-s2Dnz-yl.js +31 -0
  120. package/dist/storekit-DafAnGkM.js +16 -0
  121. package/dist/storekit-yY_kS2iR.js +47 -0
  122. package/dist/telemetry-DtG92m5O.js +56 -0
  123. package/dist/theme-DUsCh-Vd.js +25 -0
  124. package/dist/timing-BVxPrYq3.js +5 -0
  125. package/dist/{token-BGAVPSQc.js → token-DzRh7dI1.js} +6 -38
  126. package/dist/tools.d.ts +96 -0
  127. package/dist/tools.js +11178 -0
  128. package/dist/ui-D_qf3Gvw.js +53 -0
  129. package/dist/upgrade-Bt4Ouw1y.js +117 -0
  130. package/dist/upgrade-Cqy5orjb.js +7 -0
  131. package/package.json +10 -5
  132. package/dist/api-Dv3LyZ5m.js +0 -151
  133. package/dist/api-output-aJNw7Syy.js +0 -20
  134. package/dist/app-mJC5NDtW.js +0 -189
  135. package/dist/asa-D6H7Pjae.js +0 -1194
  136. package/dist/asc-xCin0akG.js +0 -539
  137. package/dist/auth-ZlLL-Gko.js +0 -139
  138. package/dist/billing-C8kBge_S.js +0 -80
  139. package/dist/command-3fGYyC8r.js +0 -301
  140. package/dist/dev-Dx99K183.js +0 -22
  141. package/dist/doctor-CyUvut31.js +0 -88
  142. package/dist/feedback-RKyGuQ8_.js +0 -152
  143. package/dist/hosted-BeGVAntr.js +0 -32
  144. package/dist/migrate-C-4ISELN.js +0 -284
  145. package/dist/org-CQduIOic.js +0 -40
  146. package/dist/output-C-4UKMot.js +0 -136
  147. package/dist/params-l3CrvRtR.js +0 -51
  148. package/dist/pipeline-CHX0Syre.js +0 -977
  149. package/dist/print-skill-BiZLdCKv.js +0 -74
  150. package/dist/promote-Pgpz-3xl.js +0 -88
  151. package/dist/publish-BPeuc3lq.js +0 -35
  152. package/dist/push-DcSQ4odB.js +0 -35
  153. package/dist/query-CWLaZkdm.js +0 -53
  154. package/dist/resource-jwASPCrw.js +0 -483
  155. package/dist/skills-FK5aDuNV.js +0 -64
  156. package/dist/spinner-DFctF_Jw.js +0 -18
  157. package/dist/storekit-CC4gnTI6.js +0 -29
  158. package/dist/telemetry-Ch9cCTI-.js +0 -169
  159. package/dist/ui-4gLyfrgl.js +0 -27737
  160. package/dist/update-DIXbL-H9.js +0 -59
  161. package/dist/upgrade-De7JMI1g.js +0 -72
  162. /package/dist/{devtools-8EgLo86w.js → devtools-ByZ6XYnn.js} +0 -0
@@ -0,0 +1,858 @@
1
+ // Reference implementation: the hand-off engine as shipped on Reflective's web2app
2
+ // discovery funnel (React + Vite). Copy and adapt: the hand-off names and their
3
+ // exits are specific to that funnel's copy (see handoffs.md for why each exists);
4
+ // the machinery (pin, settlePrevious, playMorph, fly,
5
+ // flyBack, intoBarPath, outOfBarPath, runHandoff) is general; A → B morphs come from morphFrame.ts.
6
+ //
7
+ // Expects: `FlowHandoff` from your flow types, the data attributes listed in
8
+ // engine.md, and navigation that calls runHandoff (engine.md "Navigation wiring").
9
+
10
+ import { flushSync } from "react-dom";
11
+ import type { FlowHandoff } from "@/lib/types";
12
+ import { decodedCopy, imageSize, isImageReady, releaseDecoded } from "./imageReady";
13
+ import { colorFace, fillFace, glideAt, lerp, morph, MORPH_STEPS, motionBlur, photoFace, roundness } from "./morphFrame";
14
+ import type { Box, Face } from "./morphFrame";
15
+
16
+ /**
17
+ * Story motion for flows with `motion: "story"`: each hand-off between two
18
+ * pages gets its own choreography, tied to what the pair says.
19
+ *
20
+ * The outgoing page is a DOM clone pinned over where it was, animated away
21
+ * here; the incoming page is live and runs its entrance from
22
+ * styles/story-motion.css, keyed on the same hand-off name.
23
+ *
24
+ * Only transform and opacity animate, plus the answer ↔ photo morph's clip
25
+ * (one element) and its 5px blur. Blur on anything large, clip-path and
26
+ * View Transition snapshots re-rasterise every frame, and without GPU raster
27
+ * that drops the flow to ~15fps.
28
+ *
29
+ * begin welcome leans back and floats up; the progress bar draws in
30
+ * kept plain lift; the answer's flight into the bar (below) carries this one
31
+ * picture the chosen price becomes the "investment" image: it travels and opens into it, never shrinking
32
+ * inward the big picture sinks back; the recall question rises slowly
33
+ * slip the page drifts up and away, like something forgotten
34
+ * fade answers leave one by one, each drifting a little further
35
+ * gone everything fades to nothing; a beat of emptiness before the image arrives
36
+ * blank the image lifts away first, then the words
37
+ * again re-explaining: the next page comes from above, back over the same ground
38
+ * jolt "something big happens": quick cut, tighter cadence
39
+ * cadence answers leave and arrive on an even beat, a pattern
40
+ * connect the other answers gather into the chosen one as it opens into the image
41
+ * settle the page settles down and away; the honest question arrives calmly
42
+ * finish the progress bar completes and hands over to "You're all set"
43
+ *
44
+ * On every question page except picture and connect (whose answers grow into
45
+ * the next image), the chosen answer also becomes the progress it earned: see fly().
46
+ *
47
+ * Back leaves the way the page came, quicker. Keyboard navigation and reduced
48
+ * motion get a plain fade ("plain").
49
+ */
50
+
51
+ export type HandoffName = FlowHandoff | "default" | "plain";
52
+ export type HandoffDirection = "next" | "back";
53
+
54
+ const EASE_OUT = "cubic-bezier(0.23, 1, 0.32, 1)";
55
+ const EASE_IN_OUT = "cubic-bezier(0.77, 0, 0.175, 1)";
56
+
57
+ let lastInput: "pointer" | "keyboard" = "pointer";
58
+ let listening = false;
59
+
60
+ function listenForInputKind() {
61
+ if (listening || typeof window === "undefined") return;
62
+ listening = true;
63
+ window.addEventListener("pointerdown", () => (lastInput = "pointer"), { capture: true, passive: true });
64
+ window.addEventListener("keydown", () => (lastInput = "keyboard"), { capture: true, passive: true });
65
+ }
66
+
67
+ /** Keyboard navigation is repeated and deliberate, and reduced motion asks for less: both get a plain fade. */
68
+ export function handoffMode(): "story" | "plain" {
69
+ listenForInputKind();
70
+ if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return "plain";
71
+ return lastInput === "keyboard" ? "plain" : "story";
72
+ }
73
+
74
+ type Point = { x: number; y: number };
75
+
76
+ const q = (root: ParentNode | null, selector: string) => root?.querySelector<HTMLElement>(selector) ?? null;
77
+ const qa = (root: ParentNode | null, selector: string) => Array.from(root?.querySelectorAll<HTMLElement>(selector) ?? []);
78
+ const centre = (r: DOMRect): Point => ({ x: r.left + r.width / 2, y: r.top + r.height / 2 });
79
+ /** An <img> that hasn't loaded has no height yet: nothing to morph into. */
80
+ const loaded = (img: HTMLImageElement | null): boolean => img !== null && img.complete && img.naturalWidth > 0;
81
+
82
+ const easeInOut = (t: number) => (t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2);
83
+ const easeOut = (t: number) => 1 - Math.pow(1 - t, 3);
84
+
85
+ // The previous hand-off's leftovers, so a quick second tap can finish it instantly.
86
+ let running: Animation[] = [];
87
+ // Only what the copies' removal waits for: the copies' own animations and the live destinations'
88
+ // REVEAL_UNDER_COPY holds, which end on the same frame. Never the new page's entrances (CSS here, but
89
+ // if you move them into script, keep them out of this list): a copy that waits for them sits over
90
+ // them for their whole duration, and a full-screen colour frame hid a statement's words that way.
91
+ let handovers: Animation[] = [];
92
+ let clones: HTMLElement[] = [];
93
+ // The progress fill whose CSS transition is paused while an answer carries the bar.
94
+ let heldFill: HTMLElement | null = null;
95
+
96
+ function releaseFill() {
97
+ if (heldFill) heldFill.style.transition = "";
98
+ heldFill = null;
99
+ }
100
+
101
+ function settlePrevious() {
102
+ running.forEach((a) => a.finish());
103
+ running = [];
104
+ handovers = [];
105
+ clones.forEach((c) => c.remove());
106
+ clones = [];
107
+ releaseDecoded();
108
+ releaseFill();
109
+ }
110
+
111
+ /**
112
+ * A copy of `el` pinned where it is on screen, outside the page, for the exit to animate.
113
+ *
114
+ * Every animated copy is position: absolute (at the same screen coordinates; the page itself never
115
+ * scrolls), never fixed: iOS 26 Safari tints its toolbar from a fixed element touching the top edge,
116
+ * so a full-screen fixed morph frame retinted the browser chrome mid-transition. The toolbar-tint
117
+ * sentinel (index.html) must stay the only fixed element.
118
+ */
119
+ function pin(el: HTMLElement): HTMLElement {
120
+ const r = el.getBoundingClientRect();
121
+ const clone = el.cloneNode(true) as HTMLElement;
122
+ // A cloned <img> reloads, and Safari paints nothing until it has: swap in the decoded image.
123
+ const originals = el.querySelectorAll("img");
124
+ clone.querySelectorAll("img").forEach((img, i) => img.replaceWith(decodedCopy(originals[i])));
125
+ clone.removeAttribute("data-flow-page");
126
+ clone.setAttribute("aria-hidden", "true");
127
+ clone.setAttribute("data-handoff-clone", "");
128
+ Object.assign(clone.style, {
129
+ position: "absolute",
130
+ left: `${r.left + window.scrollX}px`,
131
+ top: `${r.top + window.scrollY}px`,
132
+ width: `${r.width}px`,
133
+ height: `${r.height}px`,
134
+ margin: "0",
135
+ zIndex: "30",
136
+ pointerEvents: "none",
137
+ });
138
+ document.body.appendChild(clone);
139
+ clones.push(clone);
140
+ return clone;
141
+ }
142
+
143
+ function animate(el: Element | null | undefined, keyframes: Keyframe[], options: KeyframeAnimationOptions) {
144
+ if (!el) return;
145
+ const animation = el.animate(keyframes, { fill: "both", easing: EASE_OUT, ...options });
146
+ running.push(animation);
147
+ if (keyframes === REVEAL_UNDER_COPY || el.closest("[data-handoff-clone]")) handovers.push(animation);
148
+ }
149
+
150
+ /**
151
+ * Every A → B transform (answer → progress bar, answer ↔ photo, bar → answer) runs this long, so
152
+ * one never feels slower than another.
153
+ */
154
+ const TRANSFORM_MS = 504;
155
+
156
+ /**
157
+ * Keyframes that keep a live destination out of sight while its copy transforms into it, and hand
158
+ * over on the last frame: it's at opacity 0.001 throughout, then 1 the moment the animation ends,
159
+ * and the copy is removed in that same frame (see finish()).
160
+ *
161
+ * Why 0.001, not 0: a browser doesn't paint a fully transparent element, so one revealed only on the
162
+ * last frame shows a frame of nothing (a flash). At 0.001 it's already painted, yet adds nothing
163
+ * visible. Why not reveal it early under the copy: many destinations are translucent (a selected
164
+ * answer is 90% primary), and a 90% copy over a 90% element reads as ~99% — the answer darkens for
165
+ * the last stretch, then pops back when the copy goes.
166
+ */
167
+ const REVEAL_UNDER_COPY: Keyframe[] = [{ opacity: 0.001 }, { opacity: 0.001 }];
168
+ const FLIGHT_MS = TRANSFORM_MS;
169
+ // The answer's right edge reaches the bar's end at 55%; the rest is the two growing together.
170
+ const JOIN = 0.55;
171
+ // Sampled timeline; STEPS * JOIN is whole so a keyframe sits exactly on the join.
172
+ const STEPS = 40;
173
+
174
+ /** Overshoots its target by ~12% and settles back. */
175
+ const easeOutBack = (t: number) => {
176
+ const c1 = 2;
177
+ return 1 + (c1 + 1) * Math.pow(t - 1, 3) + c1 * Math.pow(t - 1, 2);
178
+ };
179
+
180
+ /**
181
+ * The chosen answer becomes the bar's leading edge. Its label fades, and the
182
+ * pill shrinks around its right edge while that edge flies to where the bar
183
+ * currently ends — same colour, same height. On that frame they join, then
184
+ * grow together to the new length: the answer is what pushes the bar
185
+ * forward, not a timer. Going back is its own move, not this one rewound:
186
+ * see flyBack().
187
+ *
188
+ * It rises to the bar's height (shrinking to the bar's size) while drifting
189
+ * left, reaching that height just before the end, so it slides the last
190
+ * stretch level into the bar's end. Once the bar has grown, its edge
191
+ * overshoots a little and settles back.
192
+ */
193
+ function fly({
194
+ capsule,
195
+ track,
196
+ fill,
197
+ lo,
198
+ hi,
199
+ answerClone,
200
+ }: {
201
+ capsule: DOMRect;
202
+ track: DOMRect;
203
+ fill: HTMLElement;
204
+ /** Progress before this step: where the answer joins the bar. */
205
+ lo: number;
206
+ /** Progress after this step. */
207
+ hi: number;
208
+ answerClone?: HTMLElement;
209
+ }) {
210
+ const loX = track.left + track.width * lo;
211
+ const hiX = track.left + track.width * hi;
212
+ // The pill lands as the bar's tip: right edge on the bar's end, one step long (inside the fill).
213
+ const width = Math.max(track.height, Math.min(hiX - loX, loX - track.left));
214
+ const land = new DOMRect(Math.max(track.left, loX - width), track.top, width, track.height);
215
+
216
+ const slug = document.createElement("div");
217
+ slug.setAttribute("aria-hidden", "true");
218
+ slug.setAttribute("data-handoff-clone", "");
219
+ Object.assign(slug.style, {
220
+ position: "absolute",
221
+ left: `${land.left + window.scrollX}px`,
222
+ top: `${land.top + window.scrollY}px`,
223
+ width: `${land.width}px`,
224
+ height: `${land.height}px`,
225
+ borderRadius: "999px",
226
+ background: "rgb(var(--color-ui-primary))",
227
+ transformOrigin: "100% 50%",
228
+ zIndex: "31",
229
+ pointerEvents: "none",
230
+ });
231
+ document.body.appendChild(slug);
232
+ clones.push(slug);
233
+ if (answerClone) answerClone.style.transformOrigin = "100% 50%";
234
+
235
+ // Everything is placed by its right edge: the answer's, then the bar's.
236
+ const a = { x: capsule.right, y: capsule.top + capsule.height / 2 };
237
+ const b = { x: land.right, y: land.top + land.height / 2 };
238
+ // Both are long thin pills whose proportions track the page width, so this scale stays nearly uniform.
239
+ const sx0 = capsule.width / land.width;
240
+ const sy0 = capsule.height / land.height;
241
+ // Rising and drifting left together, the rise leading, and finishing first: it reaches the bar's
242
+ // height with ~13% of the way left to go, which it covers level, into the bar's end.
243
+ const riseAt = (f: number) => easeInOut(Math.min(1, Math.max(0, f / 0.75)));
244
+ const slideAt = (f: number) => easeInOut(Math.min(1, Math.max(0, f)));
245
+
246
+ const slugFrames: Keyframe[] = [];
247
+ const capsuleFrames: Keyframe[] = [];
248
+ const fillFrames: Keyframe[] = [];
249
+ for (let i = 0; i <= STEPS; i++) {
250
+ const p = i / STEPS;
251
+ // f: flight progress (0 = the answer, 1 = joined). g: how far the joined edge has grown (0 = lo, 1 = hi).
252
+ const f = p <= JOIN ? p / JOIN : 1;
253
+ const g = p <= JOIN ? 0 : easeOutBack((p - JOIN) / (1 - JOIN));
254
+ // e: how far it has become the bar's tip (shrinks with the rise).
255
+ const e = riseAt(f);
256
+ const x = a.x + (b.x - a.x) * slideAt(f) + (hiX - loX) * g;
257
+ const y = a.y + (b.y - a.y) * e;
258
+ const sx = Math.pow(sx0, 1 - e);
259
+ const sy = Math.pow(sy0, 1 - e);
260
+ // The capsule is 90% primary, the bar 100%: the pill deepens in flight and is exactly the bar's
261
+ // colour before it joins. Nothing about the bar's colour changes while it grows: the pill stays
262
+ // opaque and is removed once it sits exactly over the fill's identical end.
263
+ const tone = 0.9 + 0.1 * Math.min(1, e / 0.9);
264
+ slugFrames.push({
265
+ offset: p,
266
+ transform: `translate3d(${x - b.x}px, ${y - b.y}px, 0) scale(${sx}, ${sy})`,
267
+ opacity: tone,
268
+ });
269
+ fillFrames.push({ offset: p, transform: `translateX(${(lo + (hi - lo) * g - 1) * 100}%)` });
270
+ if (answerClone) {
271
+ capsuleFrames.push({
272
+ offset: p,
273
+ transform: `translate3d(${x - a.x}px, ${y - a.y}px, 0) scale(${sx / sx0}, ${sy / sy0})`,
274
+ // Its words and fill go first, over the pill.
275
+ opacity: Math.max(0, 1 - p / 0.13),
276
+ });
277
+ }
278
+ }
279
+
280
+ animate(slug, slugFrames, { duration: FLIGHT_MS, easing: "linear" });
281
+ animate(fill, fillFrames, { duration: FLIGHT_MS, easing: "linear", fill: "none" });
282
+ if (answerClone) animate(answerClone, capsuleFrames, { duration: FLIGHT_MS, easing: "linear" });
283
+ }
284
+
285
+ /**
286
+ * Going back, the answer comes back out of the bar: not the forward flight rewound (the edge
287
+ * pulling left, then the pill reversing to fly right would be a direction flip mid-motion), but its
288
+ * own physical move that ends, like every arrival, on the A → B clock's long, gentle tail.
289
+ *
290
+ * The pill starts as exactly the stretch of bar this answer earned and takes it with it: the bar
291
+ * gives that stretch up at once, hidden under the pill, and the pill slides out along the bar and
292
+ * drops to the answer, growing into it, on the A → B clock (critically damped: no overshoot, a soft
293
+ * landing), and hands over mid-flight to a copy of the answer travelling the same path; the live
294
+ * answer takes over on the last frame.
295
+ */
296
+ function flyBack({
297
+ capsule,
298
+ track,
299
+ fill,
300
+ lo,
301
+ hi,
302
+ answerClone,
303
+ liveAnswer,
304
+ }: {
305
+ capsule: DOMRect;
306
+ track: DOMRect;
307
+ fill: HTMLElement;
308
+ /** Progress after this step (where the bar ends once the answer has left it). */
309
+ lo: number;
310
+ /** Progress before this step. */
311
+ hi: number;
312
+ answerClone?: HTMLElement;
313
+ liveAnswer?: HTMLElement;
314
+ }) {
315
+ const duration = TRANSFORM_MS;
316
+ const loX = track.left + track.width * lo;
317
+ const hiX = track.left + track.width * hi;
318
+ const seg = new DOMRect(loX, track.top, Math.max(track.height, hiX - loX), track.height);
319
+
320
+ const slug = document.createElement("div");
321
+ slug.setAttribute("aria-hidden", "true");
322
+ slug.setAttribute("data-handoff-clone", "");
323
+ Object.assign(slug.style, {
324
+ position: "absolute",
325
+ left: `${seg.left + window.scrollX}px`,
326
+ top: `${seg.top + window.scrollY}px`,
327
+ width: `${seg.width}px`,
328
+ height: `${seg.height}px`,
329
+ borderRadius: "999px",
330
+ background: "rgb(var(--color-ui-primary))",
331
+ transformOrigin: "100% 50%",
332
+ zIndex: "31",
333
+ pointerEvents: "none",
334
+ });
335
+ document.body.appendChild(slug);
336
+ clones.push(slug);
337
+ if (answerClone) answerClone.style.transformOrigin = "100% 50%";
338
+
339
+ // Placed by right edges, as going forward: the segment's, then the answer's.
340
+ const b = { x: seg.right, y: seg.top + seg.height / 2 };
341
+ const a = { x: capsule.right, y: capsule.top + capsule.height / 2 };
342
+ const sx0 = capsule.width / seg.width;
343
+ const sy0 = capsule.height / seg.height;
344
+ // Path shape, s = 1 at the bar → 0 at the answer. Across: linear. Down:
345
+ // level for the first quarter (sliding out along the bar), then curving down to arrive at the same
346
+ // rate as it goes across (slope 1 at the answer), so it arrives along its line of travel and a
347
+ // gentle clock stays gentle on screen (a steep final curve would magnify any change in speed).
348
+ const across = (s: number) => s;
349
+ const up = (s: number) => (s >= 0.75 ? 1 : s + 2.6667 * s * s - 2.963 * s * s * s);
350
+
351
+ const slugFrames: Keyframe[] = [];
352
+ const copyFrames: Keyframe[] = [];
353
+ let handedOver = false;
354
+ for (let i = 0; i <= MORPH_STEPS; i++) {
355
+ const t = i / MORPH_STEPS;
356
+ const s = 1 - glideAt(t);
357
+ const u = up(s); // 1 = bar-like, 0 = answer-like, < 0 past the answer
358
+ const x = a.x + (b.x - a.x) * across(s);
359
+ const y = a.y + (b.y - a.y) * u;
360
+ // Size grows linearly toward the answer (a multiplicative grow would rush the end of it).
361
+ const sx = 1 + (sx0 - 1) * (1 - u);
362
+ const sy = 1 + (sy0 - 1) * (1 - u);
363
+ const slugAt = `translate3d(${x - b.x}px, ${y - b.y}px, 0) scale(${sx}, ${sy})`;
364
+ const copyAt = `translate3d(${x - a.x}px, ${y - a.y}px, 0) scale(${sx / sx0}, ${sy / sy0})`;
365
+ // The bar's colour, lightening to the capsule's 90% as it becomes the answer.
366
+ const tone = 1 - 0.1 * Math.min(1, Math.max(0, 1 - u));
367
+ // Hand over from the pill to the answer's copy in one frame, not a crossfade: both are translucent,
368
+ // so mid-crossfade they'd cover less than either does alone and the answer would lighten. By 90%
369
+ // of the way the pill is the answer's colour and shape, so the swap is invisible; the label fades
370
+ // in on the copy on its own.
371
+ const past = 1 - s >= 0.9;
372
+ if (past && !handedOver) {
373
+ handedOver = true;
374
+ slugFrames.push({ offset: t, transform: slugAt, opacity: tone });
375
+ copyFrames.push({ offset: t, transform: copyAt, opacity: 0 });
376
+ }
377
+ slugFrames.push({ offset: t, transform: slugAt, opacity: past ? 0 : tone });
378
+ copyFrames.push({ offset: t, transform: copyAt, opacity: past ? 1 : 0 });
379
+ }
380
+
381
+ // The bar gives the stretch up at once, under the pill that now carries it: its transition is held
382
+ // (heldFill), so the new length React just rendered applies on this frame.
383
+ animate(slug, slugFrames, { duration, easing: "linear" });
384
+ if (answerClone) {
385
+ animate(answerClone, copyFrames, { duration, easing: "linear" });
386
+ // The label comes back last, on the travelling copy.
387
+ qa(answerClone, "span").forEach((label) => animate(label, [{ opacity: 0 }, { offset: 0.8, opacity: 0 }, { opacity: 1 }], { duration, easing: "linear" }));
388
+ }
389
+ if (liveAnswer) {
390
+ // Hidden while something transforms into it; takes over on the last frame, exactly where the copy ends.
391
+ animate(liveAnswer, REVEAL_UNDER_COPY, { duration, easing: "linear", fill: "backwards" });
392
+ }
393
+ }
394
+
395
+ // The photo's resting corner radius (its rounded-[20px]).
396
+ const PHOTO_RADIUS = 20;
397
+
398
+ /** The stretch of progress bar between two fractions, in screen space (at least as long as it's tall). */
399
+ function barSegment(track: DOMRect, from: number, to: number): DOMRect {
400
+ const lo = Math.min(from, to);
401
+ const hi = Math.max(from, to);
402
+ return new DOMRect(track.left + track.width * lo, track.top, Math.max(track.height, track.width * (hi - lo)), track.height);
403
+ }
404
+
405
+ /**
406
+ * Any shape into the progress bar exactly the way an answer pill goes (fly()), as a morph path:
407
+ * its right edge flies to where the bar currently ends while it shrinks to the bar's height (rising
408
+ * first, a short level run in); they join, then grow together to the new length, and the edge
409
+ * bounces. Driven through the one-geometry frame, so a big shape (a full-screen colour) never gets
410
+ * stretched: its corners go from its own roundness to a fully round pill. Also returns the bar's
411
+ * keyframes (hold, then grow with the shape).
412
+ */
413
+ function intoBarPath(source: DOMRect, sourceRadius: number, track: DOMRect, lo: number, hi: number) {
414
+ const loX = track.left + track.width * lo;
415
+ const hiX = track.left + track.width * hi;
416
+ const width = Math.max(track.height, Math.min(hiX - loX, loX - track.left));
417
+ const land = new DOMRect(Math.max(track.left, loX - width), track.top, width, track.height);
418
+ const a = { x: source.right, y: source.top + source.height / 2 };
419
+ const b = { x: land.right, y: land.top + land.height / 2 };
420
+ const riseAt = (f: number) => easeInOut(Math.min(1, Math.max(0, f / 0.75)));
421
+ const slideAt = (f: number) => easeInOut(Math.min(1, Math.max(0, f)));
422
+ const fromRound = roundness(sourceRadius, source.width, source.height);
423
+ const phase = (t: number) => ({
424
+ f: t <= JOIN ? t / JOIN : 1,
425
+ g: t <= JOIN ? 0 : easeOutBack((t - JOIN) / (1 - JOIN)),
426
+ });
427
+ const path = (t: number): Box => {
428
+ const { f, g } = phase(t);
429
+ const e = riseAt(f); // how far it has become the bar's tip
430
+ const w = Math.exp(lerp(Math.log(source.width), Math.log(land.width), e));
431
+ const h = Math.exp(lerp(Math.log(source.height), Math.log(land.height), e));
432
+ const right = a.x + (b.x - a.x) * slideAt(f) + (hiX - loX) * g;
433
+ return { cx: right - w / 2, cy: a.y + (b.y - a.y) * e, w, h, r: lerp(fromRound, 0.5, e) * Math.min(w, h) };
434
+ };
435
+ const fillFrames: Keyframe[] = Array.from({ length: STEPS + 1 }, (_, i) => {
436
+ const t = i / STEPS;
437
+ return { offset: t, transform: `translateX(${(lo + (hi - lo) * phase(t).g - 1) * 100}%)` };
438
+ });
439
+ return { path, fillFrames, to: land };
440
+ }
441
+
442
+ /**
443
+ * Out of the progress bar into any shape, the way an answer comes back out (flyBack()): the stretch
444
+ * of bar slides out along it, then drops and grows into the shape by its right edge, on the A → B
445
+ * clock (critically damped: a soft landing). Corners go from a fully round pill to the shape's own.
446
+ */
447
+ function outOfBarPath(dest: DOMRect, destRadius: number, track: DOMRect, lo: number, hi: number) {
448
+ const seg = barSegment(track, lo, hi);
449
+ const b = { x: seg.right, y: seg.top + seg.height / 2 };
450
+ const a = { x: dest.right, y: dest.top + dest.height / 2 };
451
+ const up = (s: number) => (s >= 0.75 ? 1 : s + 2.6667 * s * s - 2.963 * s * s * s);
452
+ const toRound = roundness(destRadius, dest.width, dest.height);
453
+ const path = (t: number): Box => {
454
+ const s = 1 - glideAt(t);
455
+ const u = up(s); // 1 = bar-like, 0 = the shape
456
+ const w = seg.width + (dest.width - seg.width) * (1 - u);
457
+ const h = seg.height + (dest.height - seg.height) * (1 - u);
458
+ const right = a.x + (b.x - a.x) * s;
459
+ return { cx: right - w / 2, cy: a.y + (b.y - a.y) * u, w, h, r: lerp(0.5, toRound, 1 - u) * Math.min(w, h) };
460
+ };
461
+ return { path, from: seg };
462
+ }
463
+
464
+ /** Runs a Frame A → Frame B morph (morphFrame.ts) as part of this hand-off. */
465
+ function playMorph(args: Parameters<typeof morph>[0]) {
466
+ const m = morph(args);
467
+ running.push(...m.animations);
468
+ handovers.push(...m.animations);
469
+ clones.push(m.frame);
470
+ return m;
471
+ }
472
+
473
+ const out = (y: number, scale = 1): Keyframe[] => [
474
+ { transform: "none", opacity: 1 },
475
+ { transform: `translate3d(0, ${y}px, 0) scale(${scale})`, opacity: 0 },
476
+ ];
477
+
478
+ export function runHandoff({
479
+ handoff,
480
+ direction,
481
+ update,
482
+ }: {
483
+ handoff: HandoffName;
484
+ direction: HandoffDirection;
485
+ update: () => void;
486
+ }) {
487
+ settlePrevious();
488
+ if (handoff === "plain") {
489
+ update();
490
+ return;
491
+ }
492
+
493
+ const forward = direction === "next";
494
+ const oldPage = q(document, "[data-flow-page]");
495
+ if (!oldPage) {
496
+ update();
497
+ return;
498
+ }
499
+
500
+ // Measure the old page, pin a copy of it, then swap the real page underneath.
501
+ const oldOptions = qa(oldPage, "[data-flow-option]");
502
+ const oldSelectedIndex = oldOptions.findIndex((el) => el.hasAttribute("data-selected"));
503
+ const oldSelectedRect = oldSelectedIndex >= 0 ? oldOptions[oldSelectedIndex].getBoundingClientRect() : undefined;
504
+ const oldImage = q(oldPage, "[data-flow-image]");
505
+ const oldImageRect = oldImage?.getBoundingClientRect();
506
+ // Answers become progress, except where they grow into the next page's image or statement colour.
507
+ const intoImage = handoff === "picture" || handoff === "connect";
508
+ const intoScene = intoImage || handoff === "flood";
509
+ // A statement's full-screen colour (FlowRuntime's backdrop): the destination of `flood`, the
510
+ // source of `absorb`. Captured now; it's gone once the page swaps.
511
+ const oldBackdrop = q(document, "[data-flow-backdrop]");
512
+ const oldBackdropRect = oldBackdrop?.getBoundingClientRect();
513
+ const backdropColor = oldBackdrop ? getComputedStyle(oldBackdrop).backgroundColor : "";
514
+ // The answer ↔ photo morph's faces are captured before the page changes (morphFrame.ts). The
515
+ // photo's goes first so it gets the decoded image (the page copy's photo is hidden anyway).
516
+ const photoFaceBack: Face | null = !forward && intoImage && oldImage instanceof HTMLImageElement ? photoFace(oldImage) : null;
517
+ const ghost = pin(oldPage);
518
+ const ghostOptions = qa(ghost, "[data-flow-option]");
519
+ const ghostLines = qa(ghost, "[data-flow-line], [data-next-button]");
520
+ const ghostImage = q(ghost, "[data-flow-image]");
521
+ const answerFace: Face | null =
522
+ forward && intoScene && oldSelectedIndex >= 0 && oldSelectedRect ? fillFace(oldOptions[oldSelectedIndex], oldSelectedRect) : null;
523
+ if (answerFace) ghostOptions[oldSelectedIndex].style.visibility = "hidden";
524
+ if (photoFaceBack && ghostImage) ghostImage.style.visibility = "hidden";
525
+ const fill = q(document, "[data-progress-fill]");
526
+ const fromFraction = Number(fill?.dataset.progress ?? 0);
527
+ const answerFlies = forward && !intoScene && oldSelectedIndex >= 0 && oldSelectedRect !== undefined && fill !== null;
528
+ let answerClone: HTMLElement | undefined;
529
+ if (answerFlies) {
530
+ answerClone = pin(oldOptions[oldSelectedIndex]);
531
+ answerClone.style.zIndex = "32";
532
+ ghostOptions[oldSelectedIndex].style.visibility = "hidden";
533
+ }
534
+ // The bar moves only when something lands in it (or leaves it), so its own transition steps aside.
535
+ if (fill && (answerFlies || handoff === "absorb" || (!forward && !intoScene))) {
536
+ fill.style.transition = "none";
537
+ heldFill = fill;
538
+ }
539
+
540
+ flushSync(update);
541
+
542
+ const page = q(document, "[data-flow-page]");
543
+ const newImage = q(page, "[data-flow-image]");
544
+ const newSelected = q(page, "[data-flow-option][data-selected]");
545
+ const toFraction = Number(fill?.dataset.progress ?? 0);
546
+ const trackRect = fill?.parentElement?.getBoundingClientRect();
547
+
548
+ const finish = () => {
549
+ const done = handovers;
550
+ // A copy only goes once the live photo under it has loaded (at most a second), so the handover
551
+ // never shows a blank frame even if Safari is still loading it.
552
+ const liveImage = newImage instanceof HTMLImageElement && !loaded(newImage) ? newImage : null;
553
+ const photoLoaded = liveImage
554
+ ? Promise.race([
555
+ new Promise<void>((resolve) => liveImage.addEventListener("load", () => resolve(), { once: true })),
556
+ new Promise<void>((resolve) => window.setTimeout(resolve, 1000)),
557
+ ])
558
+ : Promise.resolve();
559
+ Promise.all([...done.map((a) => a.finished), photoLoaded])
560
+ .catch(() => {})
561
+ .finally(() => {
562
+ if (handovers !== done) return;
563
+ // The same frame the live destinations reach full opacity (their REVEAL_UNDER_COPY ends with
564
+ // the transform, and they're already painted), so the swap is exact: no gap, no double layer.
565
+ // `running` keeps the rest, so a quick second tap can still finish it.
566
+ clones.forEach((c) => c.remove());
567
+ clones = [];
568
+ handovers = [];
569
+ releaseDecoded();
570
+ releaseFill();
571
+ });
572
+ };
573
+
574
+ if (answerFlies && fill && trackRect && oldSelectedRect) {
575
+ fly({ capsule: oldSelectedRect, track: trackRect, fill, lo: fromFraction, hi: toFraction, answerClone });
576
+ }
577
+
578
+ if (!forward && handoff === "absorb" && fill && trackRect) {
579
+ // Back into a statement: the stretch of bar it became grows back out into the full-screen colour.
580
+ // The bar gives the stretch up at once (held), under the frame that starts exactly on it.
581
+ const backdrop = q(document, "[data-flow-backdrop]");
582
+ if (backdrop) {
583
+ backdrop.dataset.morph = "";
584
+ const color = getComputedStyle(backdrop).backgroundColor;
585
+ const dest = backdrop.getBoundingClientRect();
586
+ const { path, from } = outOfBarPath(dest, 0, trackRect, toFraction, fromFraction);
587
+ playMorph({
588
+ from: { rect: from, radius: from.height / 2 },
589
+ to: { rect: dest, radius: 0 },
590
+ faces: { from: colorFace(color), to: colorFace(color) },
591
+ duration: TRANSFORM_MS,
592
+ blur: 0,
593
+ zIndex: 29,
594
+ path,
595
+ });
596
+ animate(backdrop, REVEAL_UNDER_COPY, { duration: TRANSFORM_MS, easing: "linear", fill: "backwards" });
597
+ }
598
+ animate(ghost, out(12), { duration: 160 });
599
+ finish();
600
+ return;
601
+ }
602
+
603
+ if (!forward && handoff === "flood" && oldBackdropRect && newSelected) {
604
+ // Back out of a statement: the colour closes back into the answer that opened into it, its own
605
+ // move on the A → B clock; the statement's words fade above it.
606
+ newSelected.dataset.morph = "";
607
+ const r = newSelected.getBoundingClientRect();
608
+ const m = playMorph({
609
+ from: { rect: oldBackdropRect, radius: 0 },
610
+ to: { rect: r, radius: r.height / 2 },
611
+ faces: { from: colorFace(backdropColor), to: fillFace(newSelected, r) },
612
+ duration: TRANSFORM_MS,
613
+ handover: [0.7, 0.94],
614
+ zIndex: 29,
615
+ });
616
+ qa(m.faces.to, "span").forEach((label) => animate(label, [{ opacity: 0 }, { offset: 0.85, opacity: 0 }, { opacity: 1 }], { duration: TRANSFORM_MS, easing: "linear" }));
617
+ animate(newSelected, REVEAL_UNDER_COPY, { duration: TRANSFORM_MS, easing: "linear", fill: "backwards" });
618
+ animate(ghost, out(12), { duration: 160 });
619
+ finish();
620
+ return;
621
+ }
622
+
623
+ if (!forward) {
624
+ if (heldFill && fill && trackRect) {
625
+ if (newSelected) {
626
+ // The answer comes back out of the stretch of bar it became.
627
+ newSelected.dataset.morph = "";
628
+ const copy = pin(newSelected);
629
+ copy.style.zIndex = "32";
630
+ flyBack({ capsule: newSelected.getBoundingClientRect(), track: trackRect, fill, lo: toFraction, hi: fromFraction, answerClone: copy, liveAnswer: newSelected });
631
+ } else {
632
+ animate(fill, [{ transform: `translateX(${(fromFraction - 1) * 100}%)` }, { transform: `translateX(${(toFraction - 1) * 100}%)` }], {
633
+ duration: 300,
634
+ fill: "none",
635
+ });
636
+ }
637
+ }
638
+ if (photoFaceBack && oldImageRect && newSelected) {
639
+ // The photo becomes the answer again in one frame (morphFrame.ts): one geometry the whole way,
640
+ // closing into the answer's pill while travelling to it, the answer taking over under the blur
641
+ // and sharpening until the last frame.
642
+ newSelected.dataset.morph = "";
643
+ const r = newSelected.getBoundingClientRect();
644
+ const duration = TRANSFORM_MS;
645
+ const m = playMorph({
646
+ from: { rect: oldImageRect, radius: PHOTO_RADIUS },
647
+ to: { rect: r, radius: r.height / 2 },
648
+ faces: { from: photoFaceBack, to: fillFace(newSelected, r) },
649
+ duration,
650
+ handover: [0.7, 0.94],
651
+ });
652
+ // Its words come back last.
653
+ qa(m.faces.to, "span").forEach((label) => animate(label, [{ opacity: 0 }, { offset: 0.85, opacity: 0 }, { opacity: 1 }], { duration, easing: "linear" }));
654
+ // The live answer takes over under the frame (see REVEAL_UNDER_COPY).
655
+ animate(newSelected, REVEAL_UNDER_COPY, { duration, easing: "linear", fill: "backwards" });
656
+
657
+ if (handoff === "connect") {
658
+ // Every answer went into the photo, so every answer comes back out of it, from where the
659
+ // frame is when they start (60% of the way, on the same clock).
660
+ const k = glideAt(0.6);
661
+ const from = {
662
+ cx: lerp(oldImageRect.left + oldImageRect.width / 2, r.left + r.width / 2, k),
663
+ cy: lerp(oldImageRect.top + oldImageRect.height / 2, r.top + r.height / 2, k),
664
+ };
665
+ qa(page, "[data-flow-option]").forEach((el, k) => {
666
+ if (el === newSelected) return;
667
+ el.dataset.morph = "";
668
+ const c = centre(el.getBoundingClientRect());
669
+ animate(
670
+ el,
671
+ [
672
+ { transform: `translate3d(${from.cx - c.x}px, ${from.cy - c.y}px, 0) scale(0.95)`, opacity: 0, filter: `blur(${motionBlur(5)}px)` },
673
+ { offset: 0.55 + k * 0.04, transform: `translate3d(${from.cx - c.x}px, ${from.cy - c.y}px, 0) scale(0.95)`, opacity: 0, filter: `blur(${motionBlur(5)}px)`, easing: EASE_IN_OUT },
674
+ { transform: "none", opacity: 1, filter: "blur(0px)" },
675
+ ],
676
+ { duration, easing: "linear", fill: "backwards" },
677
+ );
678
+ });
679
+ }
680
+ ghostLines.forEach((el) => animate(el, out(12), { duration: 150 }));
681
+ } else {
682
+ // Leave the way the page came in, quicker: re-explaining came from above, so it goes back up.
683
+ animate(ghost, out(handoff === "again" ? -12 : 12), { duration: 160 });
684
+ if (photoFaceBack && ghostImage) ghostImage.style.visibility = "";
685
+ }
686
+ finish();
687
+ return;
688
+ }
689
+
690
+ switch (handoff) {
691
+ case "begin":
692
+ animate(
693
+ ghost,
694
+ [
695
+ { transformOrigin: "50% 100%", transform: "perspective(900px) rotateX(0deg)", opacity: 1 },
696
+ { transformOrigin: "50% 100%", transform: "perspective(900px) translate3d(0, -20px, 0) rotateX(22deg) scale(0.97)", opacity: 0 },
697
+ ],
698
+ { duration: 320, easing: EASE_IN_OUT },
699
+ );
700
+ break;
701
+
702
+ case "picture":
703
+ case "connect": {
704
+ ghostLines.forEach((el) => animate(el, out(-10), { duration: 180 }));
705
+ // The photo is known if the new <img> has loaded or the flow already decoded it: Safari loads even
706
+ // a cached image a moment after the element is created, so requiring the former alone meant the
707
+ // morph never ran there.
708
+ const photoKnown =
709
+ newImage instanceof HTMLImageElement && (loaded(newImage) || isImageReady(newImage.getAttribute("src") ?? ""));
710
+ if (!(answerFace && oldSelectedRect && photoKnown)) {
711
+ // No morph target (the photo still hasn't loaded): the answers leave, and the photo fades in
712
+ // on its own the moment it arrives.
713
+ if (oldSelectedIndex >= 0) ghostOptions[oldSelectedIndex].style.visibility = "";
714
+ ghostOptions.forEach((el) => animate(el, out(-10), { duration: 180 }));
715
+ if (newImage && !loaded(newImage as HTMLImageElement)) {
716
+ newImage.addEventListener("load", () => newImage.animate([{ opacity: 0 }, { opacity: 1 }], { duration: 240, easing: EASE_OUT }), { once: true });
717
+ }
718
+ break;
719
+ }
720
+ if (!newImage) break;
721
+ // Its CSS entrance is switched off before measuring: the morph is its entrance.
722
+ newImage.dataset.morph = "";
723
+ // Its box is reserved from its known size (WelcomePage sets width/height); if it isn't, derive the
724
+ // height from the decoded image rather than trusting a not-yet-loaded element's zero height.
725
+ const laidOut = newImage.getBoundingClientRect();
726
+ const size = imageSize(newImage.getAttribute("src") ?? "");
727
+ const r =
728
+ laidOut.height >= 1 || !size ? laidOut : new DOMRect(laidOut.left, laidOut.top, laidOut.width, (laidOut.width * size.height) / size.width);
729
+ const duration = TRANSFORM_MS;
730
+ const a = { cx: oldSelectedRect.left + oldSelectedRect.width / 2, cy: oldSelectedRect.top + oldSelectedRect.height / 2 };
731
+
732
+ if (handoff === "picture") {
733
+ ghostOptions.forEach((el, k) => k !== oldSelectedIndex && animate(el, out(-10), { duration: 180 }));
734
+ } else {
735
+ // Connect: the other answers gather into the chosen one (never below 95%) as it becomes the photo.
736
+ ghostOptions.forEach((el, k) => {
737
+ if (k === oldSelectedIndex) return;
738
+ const c = centre(el.getBoundingClientRect());
739
+ animate(
740
+ el,
741
+ [
742
+ { transform: "none", opacity: 1, filter: "blur(0px)", easing: EASE_IN_OUT },
743
+ { offset: 0.35, transform: `translate3d(${a.cx - c.x}px, ${a.cy - c.y}px, 0) scale(0.95)`, opacity: 0, filter: `blur(${motionBlur(5)}px)` },
744
+ { transform: `translate3d(${a.cx - c.x}px, ${a.cy - c.y}px, 0) scale(0.95)`, opacity: 0, filter: `blur(${motionBlur(5)}px)` },
745
+ ],
746
+ { duration, easing: "linear", delay: Math.abs(k - oldSelectedIndex) * 30 },
747
+ );
748
+ });
749
+ }
750
+
751
+ // The answer becomes the photo in one frame (morphFrame.ts): one geometry the whole way, the
752
+ // photo taking over under the blur and sharpening until the last frame.
753
+ const m = playMorph({
754
+ from: { rect: oldSelectedRect, radius: oldSelectedRect.height / 2 },
755
+ to: { rect: r, radius: PHOTO_RADIUS },
756
+ faces: { from: answerFace, to: photoFace(newImage) },
757
+ duration,
758
+ // Under the motion blur (25–75% of the time): p 0.45–0.75 is ~32–45% of the duration.
759
+ handover: [0.45, 0.75],
760
+ });
761
+ // Its words go first.
762
+ qa(m.faces.from, "span").forEach((label) => animate(label, [{ opacity: 1 }, { offset: 0.15, opacity: 0 }, { opacity: 0 }], { duration, easing: "linear" }));
763
+ // The live photo takes over under the frame (see REVEAL_UNDER_COPY).
764
+ animate(newImage, REVEAL_UNDER_COPY, { duration, easing: "linear", fill: "backwards" });
765
+ break;
766
+ }
767
+
768
+ case "flood": {
769
+ // The chosen answer opens into the statement's full-screen colour: the most prominent thing on the
770
+ // next screen, and the answer's own colour. The words arrive on it afterwards (story-motion.css).
771
+ ghostLines.forEach((el) => animate(el, out(-10), { duration: 180 }));
772
+ ghostOptions.forEach((el, k) => k !== oldSelectedIndex && animate(el, out(-10), { duration: 180 }));
773
+ const backdrop = q(document, "[data-flow-backdrop]");
774
+ if (!(answerFace && oldSelectedRect && backdrop)) break;
775
+ backdrop.dataset.morph = "";
776
+ const m = playMorph({
777
+ from: { rect: oldSelectedRect, radius: oldSelectedRect.height / 2 },
778
+ to: { rect: backdrop.getBoundingClientRect(), radius: 0 },
779
+ faces: { from: answerFace, to: colorFace(getComputedStyle(backdrop).backgroundColor) },
780
+ duration: TRANSFORM_MS,
781
+ handover: [0.45, 0.75],
782
+ });
783
+ qa(m.faces.from, "span").forEach((label) => animate(label, [{ opacity: 1 }, { offset: 0.15, opacity: 0 }, { opacity: 0 }], { duration: TRANSFORM_MS, easing: "linear" }));
784
+ animate(backdrop, REVEAL_UNDER_COPY, { duration: TRANSFORM_MS, easing: "linear", fill: "backwards" });
785
+ break;
786
+ }
787
+
788
+ case "absorb": {
789
+ // The statement's colour collapses into the stretch of progress bar this step earns: the
790
+ // statement becomes progress (the progress-bar exception to the size floor). Its words leave
791
+ // first, above the colour, one after another; the bar takes the stretch as the colour lands.
792
+ ghostLines.forEach((el, k) => animate(el, out(-10), { duration: 180, delay: k * 40 }));
793
+ if (!(oldBackdropRect && fill && trackRect)) break;
794
+ const { path, fillFrames, to } = intoBarPath(oldBackdropRect, 0, trackRect, fromFraction, toFraction);
795
+ playMorph({
796
+ from: { rect: oldBackdropRect, radius: 0 },
797
+ to: { rect: to, radius: to.height / 2 },
798
+ faces: { from: colorFace(backdropColor), to: colorFace(backdropColor) },
799
+ duration: FLIGHT_MS,
800
+ blur: 0,
801
+ zIndex: 29,
802
+ path,
803
+ steps: STEPS,
804
+ });
805
+ // The bar holds until the colour's edge reaches it, then grows with it (the same as a pill).
806
+ animate(fill, fillFrames, { duration: FLIGHT_MS, easing: "linear", fill: "none" });
807
+ break;
808
+ }
809
+
810
+ case "inward":
811
+ animate(ghost, out(8, 0.92), { duration: 320 });
812
+ break;
813
+
814
+ case "slip":
815
+ animate(ghost, out(-28), { duration: 380 });
816
+ break;
817
+
818
+ case "fade":
819
+ ghostLines.forEach((el) => animate(el, out(-8), { duration: 200 }));
820
+ ghostOptions.forEach((el, k) => animate(el, out(-6 - k * 3), { duration: 220, delay: 40 + k * 60 }));
821
+ break;
822
+
823
+ case "gone":
824
+ animate(ghost, out(0, 0.98), { duration: 300 });
825
+ break;
826
+
827
+ case "blank":
828
+ animate(ghostImage, out(-16), { duration: 200 });
829
+ ghostLines.forEach((el) => animate(el, out(-8), { duration: 160, delay: 60 }));
830
+ break;
831
+
832
+ case "again":
833
+ animate(ghost, out(14), { duration: 200 });
834
+ break;
835
+
836
+ case "jolt":
837
+ animate(ghost, out(0, 0.98), { duration: 120 });
838
+ break;
839
+
840
+ case "cadence":
841
+ ghostLines.forEach((el) => animate(el, out(-8), { duration: 160 }));
842
+ ghostOptions.forEach((el, k) => animate(el, out(-10), { duration: 160, delay: k * 50, easing: "linear" }));
843
+ break;
844
+
845
+ case "settle":
846
+ animate(ghost, out(20), { duration: 320 });
847
+ break;
848
+
849
+ case "finish":
850
+ animate(ghost, out(-12), { duration: 220 });
851
+ break;
852
+
853
+ default:
854
+ animate(ghost, out(-12), { duration: 180 });
855
+ }
856
+
857
+ finish();
858
+ }