Lumen & Gesso / website · lumengesso.com / app / components / Transform.tsx
| 1 | 'use client'; |
| 2 | |
| 3 | import Image from 'next/image'; |
| 4 | import { useEffect, useRef } from 'react'; |
| 5 | import type { TransformPair } from '../data/transformPairs'; |
| 6 | import { catalogStartingSalePrice } from '../lib/pricing'; |
| 7 | import ButtonIcon from './ButtonIcon'; |
| 8 | |
| 9 | // "Atelier Proof" — each pair is a single composition where the customer's |
| 10 | // raw phone snap sits as a tilted polaroid leaning against the corner of |
| 11 | // the finished framed portrait. Real-world artist gesture (proof shot of |
| 12 | // reference + final work), executed with washi-tape, paper grain, and a |
| 13 | // staggered scroll-in animation. No before/after slider — proximity does |
| 14 | // the work. |
| 15 | // |
| 16 | // Presentational only: the pair data is passed in, so the same section |
| 17 | // drives both the home page (HOME_PAIRS) and the /gallery page |
| 18 | // (GALLERY_PAIRS). |
| 19 | |
| 20 | type TransformProps = { |
| 21 | pairs: TransformPair[]; |
| 22 | eyebrow: string; |
| 23 | heading: string; |
| 24 | lead: string; |
| 25 | /** Heading element — h2 inside the home page, h1 on the gallery page. */ |
| 26 | headingTag?: 'h1' | 'h2'; |
| 27 | /** Show a "Shop Gallery" button after the pairs (home page only). */ |
| 28 | galleryLink?: boolean; |
| 29 | /** Show each commission's price with the site-wide sale applied (gallery). */ |
| 30 | showPrices?: boolean; |
| 31 | /** |
| 32 | * How many leading pairs to load eagerly with `priority` (Next/Image |
| 33 | * emits a `<link rel="preload">` for these so they're ready before |
| 34 | * first paint). Defaults to 3 — fine for the home page where only |
| 35 | * 3 pairs render. The /gallery page passes 6 so the first ~2-3 |
| 36 | * viewports of pairs are already warm before any scroll happens. |
| 37 | */ |
| 38 | priorityCount?: number; |
| 39 | }; |
| 40 | |
| 41 | export function Transform({ |
| 42 | pairs, |
| 43 | eyebrow, |
| 44 | heading, |
| 45 | lead, |
| 46 | headingTag = 'h2', |
| 47 | galleryLink = false, |
| 48 | showPrices = false, |
| 49 | priorityCount = 3, |
| 50 | }: TransformProps) { |
| 51 | const rootRef = useRef<HTMLDivElement>(null); |
| 52 | const Heading = headingTag; |
| 53 | |
| 54 | // Three layers of fade/preload, tuned to keep the gallery from showing |
| 55 | // empty slots on fast scroll: |
| 56 | // |
| 57 | // 1. reveal IO — adds .is-visible so the pair fades in. We trigger |
| 58 | // it well *before* the pair enters the viewport (rootMargin |
| 59 | // 400px below + threshold 0) so a fast scroll never outruns it. |
| 60 | // 2. preload IO — kicks `new Image().src` for each pair when it |
| 61 | // crosses ~3000px from the viewport edges. This warms the browser |
| 62 | // HTTP cache so the eventual <img> fetch hits cache instantly. |
| 63 | // 3. idle preload — after first paint, we sequentially fetch every |
| 64 | // non-priority pair's after-image in the background using |
| 65 | // requestIdleCallback. This means the whole gallery is |
| 66 | // effectively cached within seconds of landing, and even the |
| 67 | // fastest scroll has warm images waiting. |
| 68 | // |
| 69 | // The reveal animation stays for visual polish but never gates |
| 70 | // content — by the time the fade triggers, the image bytes are |
| 71 | // already there. |
| 72 | useEffect(() => { |
| 73 | const root = rootRef.current; |
| 74 | if (!root) return; |
| 75 | const pairEls = root.querySelectorAll<HTMLElement>('.transform__pair'); |
| 76 | |
| 77 | const reveal = new IntersectionObserver( |
| 78 | (entries) => { |
| 79 | for (const e of entries) { |
| 80 | if (e.isIntersecting) { |
| 81 | e.target.classList.add('is-visible'); |
| 82 | reveal.unobserve(e.target); |
| 83 | } |
| 84 | } |
| 85 | }, |
| 86 | // Start fading the pair in when its top edge is still 400px below |
| 87 | // the viewport bottom — by the time the user can see it, it's |
| 88 | // already mid-fade. Threshold 0 so even 1px of intersection counts. |
| 89 | { threshold: 0, rootMargin: '0px 0px 400px 0px' }, |
| 90 | ); |
| 91 | |
| 92 | const preload = new IntersectionObserver( |
| 93 | (entries) => { |
| 94 | for (const e of entries) { |
| 95 | if (!e.isIntersecting) continue; |
| 96 | const el = e.target as HTMLElement; |
| 97 | el.querySelectorAll<HTMLImageElement>('img').forEach((img) => { |
| 98 | const src = img.currentSrc || img.src; |
| 99 | if (!src) return; |
| 100 | const warm = new window.Image(); |
| 101 | warm.src = src; |
| 102 | }); |
| 103 | preload.unobserve(el); |
| 104 | } |
| 105 | }, |
| 106 | // 3000px headroom on both sides — at typical scroll speeds, this |
| 107 | // gives the network ~5-8 seconds to warm an image before the |
| 108 | // user reaches it. Bumped from 1500px because the previous |
| 109 | // value let fast scrolls outrun the cache. |
| 110 | { rootMargin: '3000px 0px 3000px 0px' }, |
| 111 | ); |
| 112 | |
| 113 | pairEls.forEach((p) => { |
| 114 | reveal.observe(p); |
| 115 | preload.observe(p); |
| 116 | }); |
| 117 | |
| 118 | // Idle-time bulk preload — fire fetches for every after-image |
| 119 | // sequentially, one at a time, throttled to avoid choking the |
| 120 | // network. Browser handles cache dedup, so if a pair was already |
| 121 | // warmed by the IO above, this is a no-op for that URL. |
| 122 | // Skipped on the home page (3 pairs all priority-loaded) since |
| 123 | // there's nothing to warm beyond what's already eager. |
| 124 | const afterSrcs = pairs.slice(priorityCount).map((p) => p.after.src); |
| 125 | let cancelled = false; |
| 126 | const warmNext = (i: number) => { |
| 127 | if (cancelled || i >= afterSrcs.length) return; |
| 128 | const img = new window.Image(); |
| 129 | img.onload = img.onerror = () => warmNext(i + 1); |
| 130 | img.src = afterSrcs[i]; |
| 131 | }; |
| 132 | // requestIdleCallback isn't in older Safari — fall back to setTimeout. |
| 133 | const ric = (cb: () => void) => |
| 134 | typeof window.requestIdleCallback === 'function' |
| 135 | ? window.requestIdleCallback(cb, { timeout: 2000 }) |
| 136 | : window.setTimeout(cb, 250); |
| 137 | ric(() => warmNext(0)); |
| 138 | |
| 139 | return () => { |
| 140 | cancelled = true; |
| 141 | reveal.disconnect(); |
| 142 | preload.disconnect(); |
| 143 | }; |
| 144 | }, [pairs, priorityCount]); |
| 145 | |
| 146 | return ( |
| 147 | <section className="transform" ref={rootRef}> |
| 148 | <div className="transform__intro"> |
| 149 | <div className="eyebrow">{eyebrow}</div> |
| 150 | <Heading className="transform__h2">{heading}</Heading> |
| 151 | <p className="transform__lead">{lead}</p> |
| 152 | </div> |
| 153 | |
| 154 | <div className="transform__pairs"> |
| 155 | {pairs.map((p, i) => { |
| 156 | // Leading pairs are above-the-fold or close — load eagerly |
| 157 | // with `priority` so they appear instantly without any lazy |
| 158 | // delay. The rest are lazy + warmed by the preload IO above |
| 159 | // and the idle-time bulk warmer in useEffect. |
| 160 | const isPriority = i < priorityCount; |
| 161 | return ( |
| 162 | <article |
| 163 | key={p.n} |
| 164 | className={`transform__pair transform__pair--${i % 2 === 0 ? 'left' : 'right'}`} |
| 165 | style={ |
| 166 | { |
| 167 | // Stagger the first few pairs for visual polish on the |
| 168 | // initial reveal, then drop the delay to 0 for the rest. |
| 169 | // Otherwise pair N gets `N * 90ms` of delay — pair 20+ |
| 170 | // would wait nearly 2 seconds to fade in after the IO |
| 171 | // fires, which reads as broken/empty on fast scroll. |
| 172 | '--pair-delay': `${Math.min(i, 4) * 90}ms`, |
| 173 | '--tilt': `${p.tilt}deg`, |
| 174 | '--tape-rotate': `${p.tapeRotate}deg`, |
| 175 | '--before-ratio': p.before.ratio, |
| 176 | } as React.CSSProperties |
| 177 | } |
| 178 | > |
| 179 | <div className="transform__stage"> |
| 180 | {/* AFTER — the finished framed portrait, in-home */} |
| 181 | <div className="transform__after"> |
| 182 | <Image |
| 183 | src={p.after.src} |
| 184 | alt={p.after.alt} |
| 185 | fill |
| 186 | {...(isPriority ? { priority: true } : { loading: 'lazy' as const })} |
| 187 | sizes="(max-width: 768px) 100vw, (max-width: 1024px) 65vw, 55vw" |
| 188 | className="transform__after-img" |
| 189 | /> |
| 190 | </div> |
| 191 | |
| 192 | {/* BEFORE — the customer's phone snap. Desktop: a tilted |
| 193 | polaroid leaning on the after. Mobile: a flat bordered |
| 194 | print stacked above the after (see globals.css). */} |
| 195 | <div className={`transform__before transform__before--${p.corner}`}> |
| 196 | <div className="transform__before-tag">What we worked from</div> |
| 197 | <div className="transform__tape" aria-hidden="true" /> |
| 198 | <div className="transform__paper"> |
| 199 | <div |
| 200 | className="transform__before-frame" |
| 201 | style={{ aspectRatio: p.before.ratio }} |
| 202 | > |
| 203 | <Image |
| 204 | src={p.before.src} |
| 205 | alt={p.before.alt} |
| 206 | fill |
| 207 | {...(isPriority ? { priority: true } : { loading: 'lazy' as const })} |
| 208 | sizes="(max-width: 768px) 34vw, (max-width: 1024px) 16vw, 12vw" |
| 209 | className="transform__before-img" |
| 210 | /> |
| 211 | </div> |
| 212 | </div> |
| 213 | </div> |
| 214 | </div> |
| 215 | |
| 216 | <div className="transform__story"> |
| 217 | <div className="transform__plate"> |
| 218 | <span className="transform__num">№ {p.n}</span> |
| 219 | <span className="transform__rule" aria-hidden="true" /> |
| 220 | </div> |
| 221 | <div className="transform__name">{p.name}</div> |
| 222 | <div className="transform__medium">{p.medium}</div> |
| 223 | |
| 224 | {showPrices && ( |
| 225 | // "From $X" pricing — the gallery photos are AI-generated |
| 226 | // room scenes where Gemini drew the frame at whatever |
| 227 | // scale looked right, so we can't honestly tag each one |
| 228 | // with a specific dimension OR product. Advertise the |
| 229 | // catalogue-wide entry price (cheapest way into the whole |
| 230 | // range); customer picks the actual product + size in the |
| 231 | // builder. |
| 232 | <div className="transform__price"> |
| 233 | <span className="transform__price-from">From</span> |
| 234 | <span className="transform__price-now">${catalogStartingSalePrice()}</span> |
| 235 | <span className="transform__price-from">at cost</span> |
| 236 | </div> |
| 237 | )} |
| 238 | |
| 239 | <p className="transform__blurb">{p.blurb}</p> |
| 240 | |
| 241 | <a |
| 242 | className="btn btn--rose btn--md" |
| 243 | href={(() => { |
| 244 | // Forward style + frame + product but NOT size — the |
| 245 | // lifestyle photo doesn't represent any specific |
| 246 | // dimension, so locking the builder to one would be |
| 247 | // making the same lie this fix was meant to remove. |
| 248 | // Customer picks size in the builder. |
| 249 | const q = new URLSearchParams(); |
| 250 | if (p.styleId) q.set('style', p.styleId); |
| 251 | if (p.frameId) q.set('frame', p.frameId); |
| 252 | if (p.matteId) q.set('matte', p.matteId); |
| 253 | q.set('product', p.productId); |
| 254 | return `/builder?${q.toString()}`; |
| 255 | })()} |
| 256 | > |
| 257 | <ButtonIcon name="palette" /> |
| 258 | Start one like this <span aria-hidden="true">→</span> |
| 259 | </a> |
| 260 | </div> |
| 261 | </article> |
| 262 | ); |
| 263 | })} |
| 264 | </div> |
| 265 | |
| 266 | {galleryLink && ( |
| 267 | <div className="transform__more"> |
| 268 | <a className="btn btn--secondary btn--md" href="/gallery"> |
| 269 | <ButtonIcon name="image" /> |
| 270 | Shop Gallery <span aria-hidden="true">→</span> |
| 271 | </a> |
| 272 | </div> |
| 273 | )} |
| 274 | </section> |
| 275 | ); |
| 276 | } |