diff --git a/public/images/hero-palm-cutout-still.webp b/public/images/hero-palm-cutout-still.webp new file mode 100644 index 000000000..8f72f6ddc Binary files /dev/null and b/public/images/hero-palm-cutout-still.webp differ diff --git a/public/images/hero-palm-cutout.avif b/public/images/hero-palm-cutout.avif new file mode 100644 index 000000000..2535d44ef Binary files /dev/null and b/public/images/hero-palm-cutout.avif differ diff --git a/public/images/hero-palm-cutout.webp b/public/images/hero-palm-cutout.webp new file mode 100644 index 000000000..8f6377dd2 Binary files /dev/null and b/public/images/hero-palm-cutout.webp differ diff --git a/src/components/home/HeroPalmMedia.tsx b/src/components/home/HeroPalmMedia.tsx index 21c67c41d..8bf228b5a 100644 --- a/src/components/home/HeroPalmMedia.tsx +++ b/src/components/home/HeroPalmMedia.tsx @@ -2,106 +2,96 @@ import * as React from 'react' import { PauseIcon, PlayIcon } from '@phosphor-icons/react' import { Squircle } from '~/components/Squircle' +const still = '/images/hero-palm-cutout-still.webp' + export function HeroPalmMedia() { - const videoRef = React.useRef(null) - const [isVideoReady, setIsVideoReady] = React.useState(false) - const [isPlaying, setIsPlaying] = React.useState(false) + const imageRef = React.useRef(null) + const [paused, setPaused] = React.useState(false) - const playVideo = React.useCallback((video: HTMLVideoElement) => { - if (video.networkState === HTMLMediaElement.NETWORK_NO_SOURCE) video.load() - const promise = video.play() - if (promise !== undefined) { - promise - .then(() => setIsVideoReady(true)) - .catch((error: unknown) => { - if (error instanceof DOMException && error.name === 'AbortError') - return - console.error('Unable to play the homepage hero animation', error) - setIsPlaying(false) - setIsVideoReady(false) - }) - } + const [ready, setReady] = React.useState(false) + const [webpOnly, setWebpOnly] = React.useState(false) + const handleImageError = React.useCallback((image: HTMLImageElement) => { + setReady(false) + if (image.currentSrc.endsWith('.avif')) setWebpOnly(true) + else setPaused(true) }, []) React.useEffect(() => { - const video = videoRef.current - if (!video) return - const reducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)') - - const pauseForReducedMotion = () => { - if (reducedMotion.matches) video.pause() + const preference = window.matchMedia('(prefers-reduced-motion: reduce)') + const stopIfReduced = () => { + if (preference.matches) setPaused(true) } - - pauseForReducedMotion() - setIsPlaying(!video.paused) - setIsVideoReady(video.readyState >= HTMLMediaElement.HAVE_FUTURE_DATA) - reducedMotion.addEventListener('change', pauseForReducedMotion) - return () => - reducedMotion.removeEventListener('change', pauseForReducedMotion) - }, []) - - const togglePlayback = () => { - const video = videoRef.current - if (!video) return - - if (video.paused) { - playVideo(video) - } else { - video.pause() + stopIfReduced() + // The browser can finish loading (or fail) before hydration. + const image = imageRef.current + if (image?.complete) { + if (image.naturalWidth > 0) setReady(true) + else handleImageError(image) } - } + + preference.addEventListener('change', stopIfReduced) + return () => preference.removeEventListener('change', stopIfReduced) + }, [handleImageError]) return ( <> - - - - - + {/* Cover the hero with the original 944 × 416 composition. The palm + occupies x=360..620 and y=68..416 within that frame. */} +
+ + {!paused && !webpOnly && ( + + )} + {!paused && ( + + )} + + {/* Browsers can fetch the fallback during hydration. Keep it inline. */} + setReady(true)} + onError={(event) => handleImageError(event.currentTarget)} + // Keep the poster visible until the browser has the complete animation. + style={{ visibility: ready ? 'visible' : 'hidden' }} + className="block h-auto w-full" + /> + +
diff --git a/src/routes/index.tsx b/src/routes/index.tsx index c97e1666d..440cf6880 100644 --- a/src/routes/index.tsx +++ b/src/routes/index.tsx @@ -78,19 +78,14 @@ function Index() {
- {/* Hero — Figma node 802:2027. Full-bleed palm/gradient photo card: - headline bottom-left, description + CTA bottom-right. The photo is + {/* Hero — Figma node 802:2027. Full-bleed palm and sky: + headline bottom-left, description + CTA bottom-right. The sky is always light, so text uses a mode-stable dark token (neutral-500) rather than a theme-flipping semantic. */}
{/* `svh`, not `dvh` — the dynamic viewport grows as iOS collapses the URL bar mid-scroll, visibly stretching the hero. */}
- {/* The parent supplies the 4px frame shared by the hero and stats. - This wrapper clips the image to the inner radius so the - fills it exactly instead of overflowing. Plain (not OptimizedImage): - the Cloudflare transform resolves against the production origin, - so a newly-added asset 404s until deployed. */}

The{' '} @@ -106,7 +101,7 @@ function Index() { applications that work naturally for developers and reliably for agents

- {/* The photo is always light, so scope the CTAs to the DS + {/* The sky is always light, so scope the CTAs to the DS light mode — the Buttons then render as their standard DS light-mode selves (no per-button color overrides). */}
diff --git a/src/styles/app.css b/src/styles/app.css index e3f98c2ce..c18393813 100644 --- a/src/styles/app.css +++ b/src/styles/app.css @@ -2751,6 +2751,13 @@ html:not(.dark) .library-landing-graphic [style*='drop-shadow'] { transition: opacity 120ms linear; } +/* The grain repeats behind the tree and stays still as the fronds move. */ +.home-palm-sky { + background-image: + url('./hero-sky-grain.png?inline'), + linear-gradient(#8fd7fe, #b0dbff 25%, #d3e0ff 50%, #e7dffb 75%, #f5d9f2); +} + @keyframes home-open-source-gradient-shift { 0%, 100% { diff --git a/src/styles/hero-sky-grain.png b/src/styles/hero-sky-grain.png new file mode 100644 index 000000000..cf5f77d17 Binary files /dev/null and b/src/styles/hero-sky-grain.png differ diff --git a/tests/hero-media.test.ts b/tests/hero-media.test.ts index 8f71345ab..9bd09ef52 100644 --- a/tests/hero-media.test.ts +++ b/tests/hero-media.test.ts @@ -8,14 +8,13 @@ import { createElement } from 'react' import { renderToString } from 'react-dom/server' import { chromium } from 'playwright-core' import { build } from 'esbuild' +import sharp from 'sharp' import { HeroPalmMedia } from '../src/components/home/HeroPalmMedia' const repoRoot = path.resolve(import.meta.dirname, '..') -const componentPath = path.join( - repoRoot, - 'src/components/home/HeroPalmMedia.tsx', -) -const videoPath = path.join(repoRoot, 'public/images/hero-palm-motion.mp4') +const motion = '/images/hero-palm-cutout.avif' +const webp = '/images/hero-palm-cutout.webp' +const still = '/images/hero-palm-cutout-still.webp' const serverMarkup = renderToString(createElement(HeroPalmMedia)) const chromePath = [ process.env.CHROME_BIN, @@ -26,45 +25,48 @@ const chromePath = [ chromium.executablePath(), ].find((candidate) => candidate && fs.existsSync(candidate)) -test('server render exposes a motion source selected by reduced-motion preference', () => { - const markup = renderToString(createElement(HeroPalmMedia)) - const videoMarkup = markup.match(/]*>/)?.[0] +test('the WebP loop preserves the source timing and matches its still', async () => { + const animation = await sharp(path.join(repoRoot, 'public', webp)).metadata() + const poster = await sharp(path.join(repoRoot, 'public', still)).metadata() + // The source MP4 has 121 frames at 24 fps. WebP stores whole milliseconds. + const delays = Array.from( + { length: 121 }, + (_, i) => Math.round(((i + 1) * 1000) / 24) - Math.round((i * 1000) / 24), + ) + assert.deepEqual(animation.delay, delays) + assert.equal(animation.loop, 0) + assert.ok(animation.hasAlpha && poster.hasAlpha) + assert.equal(animation.width, poster.width) + assert.equal(animation.height, poster.height) +}) - assert.ok(videoMarkup) - assert.doesNotMatch(videoMarkup, /\s(?:src|poster)=/) - assert.match(markup, /srcSet="\/images\/hero-palm-gradient-/) - assert.match(markup, /loading="eager"/) - assert.match(markup, /fetchPriority="high"/) - assert.match(markup, /media="\(prefers-reduced-motion: no-preference\)"/) - assert.match(markup, / { + assert.doesNotMatch(serverMarkup, / { + async (t) => { const bundle = await build({ stdin: { contents: ` import React from 'react' import { hydrateRoot } from 'react-dom/client' - import { HeroPalmMedia } from ${JSON.stringify(componentPath)} - - function HydrationHarness() { - React.useEffect(() => { - document.documentElement.dataset.heroMediaHydrated = 'true' - }, []) + import { HeroPalmMedia } from './src/components/home/HeroPalmMedia' + function Harness() { + React.useEffect(() => { document.documentElement.dataset.hydrated = 'true' }, []) return React.createElement(HeroPalmMedia) } - - hydrateRoot( - document.getElementById('root'), - React.createElement(HydrationHarness), - ) + hydrateRoot(document.getElementById('root'), React.createElement(Harness)) `, resolveDir: repoRoot, - sourcefile: 'hero-media-browser-test.tsx', loader: 'tsx', }, alias: { '~': path.join(repoRoot, 'src') }, @@ -73,345 +75,99 @@ test( platform: 'browser', write: false, }) - const browser = await chromium.launch({ - executablePath: chromePath, - headless: true, - }) - - const makePage = async ( - reducedMotion: 'reduce' | 'no-preference', - videoResponse: 'hold' | 'fail' = 'hold', - hydrateImmediately = true, - ) => { - const page = await browser.newPage() - let shouldFailVideo = videoResponse === 'fail' - let videoRequests = 0 - let jpegRequests = 0 - const pageErrors: string[] = [] - const consoleErrors: string[] = [] - let resolveVideoRequest = () => {} - const videoRequest = new Promise((resolve) => { - resolveVideoRequest = resolve - }) - let releaseVideoResponse = () => {} - const videoResponseGate = new Promise((resolve) => { - releaseVideoResponse = resolve - }) - - await page.emulateMedia({ reducedMotion }) - page.on('pageerror', (error) => pageErrors.push(error.message)) - page.on('console', (message) => { - if (message.type() === 'error') consoleErrors.push(message.text()) - }) - page.on('request', (request) => { - const pathname = new URL(request.url()).pathname - if (pathname.endsWith('/images/hero-palm-motion.mp4')) { - videoRequests += 1 - resolveVideoRequest() - } - if (pathname.endsWith('.jpg')) jpegRequests += 1 + const browser = await chromium.launch({ executablePath: chromePath }) + t.after(() => browser.close()) + + async function openHero( + width: number, + reduced = false, + failOnce = new Set(), + ) { + const page = await browser.newPage({ + viewport: { width, height: 844 }, + reducedMotion: reduced ? 'reduce' : 'no-preference', }) - await page.route('**/images/hero-palm-motion.mp4', async (route) => { - if (shouldFailVideo) { - await route.abort() - return + const requests: string[] = [] + await page.route('http://hero.test/**', (route) => { + const url = new URL(route.request().url()) + if (url.pathname === '/') { + return route.fulfill({ + contentType: 'text/html', + body: `
${serverMarkup}
`, + }) } - await videoResponseGate - await route.fulfill({ path: videoPath, contentType: 'video/mp4' }) - }) - await page.route('**/images/hero-palm-gradient*', async (route) => { - const filename = path.basename(new URL(route.request().url()).pathname) - await route.fulfill({ - path: path.join(repoRoot, 'public/images', filename), + requests.push(url.pathname) + if (failOnce.delete(url.pathname)) return route.abort() + return route.fulfill({ + path: path.join(repoRoot, 'public', url.pathname), }) }) - await page.setContent( - `
${serverMarkup}
`, - { waitUntil: 'domcontentloaded' }, - ) - const hydrate = () => - page.addScriptTag({ content: bundle.outputFiles[0].text }) - if (hydrateImmediately) await hydrate() - await page.locator('img').evaluate((image) => { - if (!(image instanceof HTMLImageElement)) { - throw new Error('Expected the hero still image') - } - return image.decode() - }) - - return { - page, - get videoRequests() { - return videoRequests - }, - get jpegRequests() { - return jpegRequests - }, - pageErrors, - consoleErrors, - hydrate, - videoRequest, - releaseVideoResponse, - allowVideoRetry: () => { - shouldFailVideo = false - releaseVideoResponse() - }, + await page.goto('http://hero.test/') + const hydrate = async () => { + await page.addScriptTag({ content: bundle.outputFiles[0].text }) + await page.waitForFunction( + () => document.documentElement.dataset.hydrated === 'true', + ) } - } - - try { - const reduced = await makePage('reduce', 'hold', false) - const reducedVideo = reduced.page.locator('video') - assert.equal(reduced.videoRequests, 0) - assert.equal(reduced.jpegRequests, 0) - assert.equal(await reducedVideo.getAttribute('src'), null) - - await reduced.hydrate() - await reduced.page.waitForFunction( - () => document.documentElement.dataset.heroMediaHydrated === 'true', - ) - assert.deepEqual( - { - poster: await reducedVideo.getAttribute('poster'), - source: await reducedVideo.getAttribute('src'), - sourceElements: await reducedVideo.locator('source').count(), - media: await reducedVideo.locator('source').getAttribute('media'), - requests: reduced.videoRequests, - jpegRequests: reduced.jpegRequests, - }, - { - poster: null, - source: null, - sourceElements: 1, - media: '(prefers-reduced-motion: no-preference)', - requests: 0, - jpegRequests: 0, - }, - ) - - await reduced.page.emulateMedia({ reducedMotion: 'no-preference' }) - assert.equal(await reducedVideo.getAttribute('src'), null) - assert.equal(reduced.videoRequests, 0) - - await reduced.page - .getByRole('button', { name: 'Play hero animation' }) - .click() - await reduced.videoRequest - assert.equal(reduced.videoRequests, 1) - assert.equal( - await reduced.page - .locator('img') - .evaluate((image) => getComputedStyle(image).opacity), - '1', - ) - assert.equal( - await reduced.page.locator('img').evaluate((image) => { - if (!(image instanceof HTMLImageElement)) { - throw new Error('Expected the hero still image') - } - return image.complete - }), - true, - ) - reduced.releaseVideoResponse() - await reduced.page.waitForFunction(() => - document.querySelector('img')?.classList.contains('opacity-0'), - ) - await reduced.page.waitForFunction( - () => document.querySelector('video')?.paused === false, - ) - await reduced.page.close() - - const normal = await makePage('no-preference', 'hold', false) - const normalVideo = normal.page.locator('video') - assert.equal( - await normal.page.evaluate( - () => window.matchMedia('(prefers-reduced-motion: reduce)').matches, - ), - false, - ) - assert.equal( - await normalVideo.locator('source').getAttribute('src'), - '/images/hero-palm-motion.mp4', - ) - await normal.videoRequest - assert.equal( - await normal.page.evaluate( - () => document.documentElement.dataset.heroMediaHydrated, - ), - undefined, - ) - assert.equal(normal.videoRequests, 1) - assert.equal(normal.jpegRequests, 0) - assert.equal( - await normal.page - .locator('img') - .evaluate((image) => getComputedStyle(image).opacity), - '1', - ) - normal.releaseVideoResponse() - await normal.page.waitForFunction(() => { - const video = document.querySelector('video') - return video !== null && video.readyState >= 3 - }) - const aspectRatios = await normal.page.evaluate(() => { - const image = document.querySelector('img') - const video = document.querySelector('video') - if (!image || !video) throw new Error('Expected hero image and video') - return { - image: image.naturalWidth / image.naturalHeight, - video: video.videoWidth / video.videoHeight, - } - }) - assert.ok(Math.abs(aspectRatios.image - aspectRatios.video) < 0.005) - await normal.hydrate() - await normal.page.waitForFunction( - () => document.querySelector('video')?.paused === false, - ) - await normal.page - .getByRole('button', { name: 'Pause hero animation' }) - .waitFor() - assert.equal(await normalVideo.getAttribute('poster'), null) - assert.equal( - await normal.page - .locator('img') - .evaluate((image) => getComputedStyle(image).opacity), - '0', - ) - - // Chrome can pause hidden autoplay media before dispatching the preference - // change. Wait for that event so consecutive changes are not coalesced. - await normal.page.evaluate(() => { - window - .matchMedia('(prefers-reduced-motion: reduce)') - .addEventListener('change', (event) => { - document.documentElement.dataset.reducedMotion = String( - event.matches, - ) - }) - }) - await normal.page.emulateMedia({ reducedMotion: 'reduce' }) - await normal.page.waitForFunction( - () => document.documentElement.dataset.reducedMotion === 'true', - ) - await normal.page.waitForFunction( - () => document.querySelector('video')?.paused === true, - ) - await normal.page.emulateMedia({ reducedMotion: 'no-preference' }) - await normal.page.waitForFunction( - () => document.documentElement.dataset.reducedMotion === 'false', - ) - assert.equal( - await normalVideo.evaluate((video) => { - if (!(video instanceof HTMLVideoElement)) { - throw new Error('Expected the hero video') - } - return video.paused - }), - true, - ) - assert.equal(normal.videoRequests, 1) - - await normal.page - .getByRole('button', { name: 'Play hero animation' }) - .press('Enter') - await normal.page.waitForFunction( - () => document.querySelector('video')?.paused === false, - ) - await normal.page - .getByRole('button', { name: 'Pause hero animation' }) - .press('Enter') - await normal.page.waitForFunction( - () => document.querySelector('video')?.paused === true, - ) - assert.equal(normal.videoRequests, 1) - await normalVideo.evaluate((video) => { - if (!(video instanceof HTMLVideoElement)) { - throw new Error('Expected the hero video') - } - const play = video.play.bind(video) - video.play = () => { - video.play = play - return Promise.reject( - new DOMException('Playback blocked', 'NotAllowedError'), + const waitForImage = (source: string) => + page.waitForFunction((part) => { + const img = document.querySelector('picture img') + return ( + img instanceof HTMLImageElement && + img.complete && + img.naturalWidth > 0 && + img.currentSrc.includes(part) ) - } - }) - await normal.page - .getByRole('button', { name: 'Play hero animation' }) - .press('Enter') - await normal.page.waitForFunction( - () => !document.querySelector('img')?.classList.contains('opacity-0'), - ) - assert.deepEqual(normal.pageErrors, []) - assert.ok( - normal.consoleErrors.some((message) => - message.includes('Unable to play the homepage hero animation'), - ), - ) - await normal.page - .getByRole('button', { name: 'Play hero animation' }) - .press('Enter') - await normal.page.waitForFunction(() => { - const video = document.querySelector('video') - return ( - video?.paused === false && - document.querySelector('img')?.classList.contains('opacity-0') - ) - }) - await normal.page.close() + }, source) + return { page, requests, hydrate, waitForImage } + } - const failed = await makePage('no-preference', 'fail') - await failed.videoRequest - await failed.page.waitForFunction( - () => - document.querySelector('video')?.networkState === - HTMLMediaElement.NETWORK_NO_SOURCE, - ) - await failed.page.evaluate( - () => - new Promise((resolve) => - requestAnimationFrame(() => resolve()), - ), - ) - assert.equal( - await failed.page - .locator('img') - .evaluate((image) => getComputedStyle(image).opacity), - '1', - ) - assert.equal( - await failed.page.locator('img').evaluate((image) => { - if (!(image instanceof HTMLImageElement)) { - throw new Error('Expected the hero still image') - } - return image.complete - }), - true, - ) - assert.equal(failed.videoRequests, 1) - assert.deepEqual(failed.pageErrors, []) - failed.allowVideoRetry() - await failed.page - .getByRole('button', { name: 'Play hero animation' }) - .click() - await failed.page.waitForFunction(() => { - const video = document.querySelector('video') - return ( - video?.paused === false && - document.querySelector('img')?.classList.contains('opacity-0') - ) - }) - assert.equal(failed.videoRequests, 2) - await failed.page.close() - } finally { - await browser.close() + for (const width of [390, 844, 1024, 1512]) { + const hero = await openHero(width) + await hero.waitForImage(motion) + assert.deepEqual(new Set(hero.requests), new Set([motion, still])) + const image = hero.page.locator('picture img') + assert.equal(await image.isVisible(), false) + await hero.hydrate() + await image.waitFor({ state: 'visible' }) + await hero.page.close() } + + const reduced = await openHero(390, true) + await reduced.waitForImage(still) + await reduced.hydrate() + await reduced.page.emulateMedia({ reducedMotion: 'no-preference' }) + await reduced.waitForImage(still) + assert.equal(reduced.requests.includes(motion), false) + await reduced.page + .getByRole('button', { name: 'Play hero animation' }) + .press('Enter') + await reduced.waitForImage(motion) + await reduced.page + .getByRole('button', { name: 'Pause hero animation' }) + .press('Enter') + await reduced.waitForImage(still) + await reduced.page + .getByRole('button', { name: 'Play hero animation' }) + .click() + await reduced.waitForImage(motion) + await reduced.page.emulateMedia({ reducedMotion: 'reduce' }) + await reduced.waitForImage(still) + await reduced.page.close() + + const fallback = await openHero(1512, false, new Set([motion])) + await fallback.hydrate() + await fallback.waitForImage(webp) + assert.deepEqual(new Set(fallback.requests), new Set([motion, webp, still])) + await fallback.page.close() + + const failed = await openHero(1512, false, new Set([motion, webp])) + await failed.hydrate() + await failed.waitForImage(still) + await failed.page + .getByRole('button', { name: 'Play hero animation' }) + .click() + await failed.waitForImage(webp) + await failed.page.close() }, )