Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added public/images/hero-palm-loop.mp4
Binary file not shown.
31 changes: 14 additions & 17 deletions src/components/home/HeroPalmMedia.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import * as React from 'react'
import { preload } from 'react-dom'
import { Squircle } from '~/components/Squircle'
import { useMediaQuery } from '~/utils/useMediaQuery'
import still from './hero-palm-cutout-still.webp?inline'
import still from './hero-palm-loop-still.webp?inline'

const motion = '/images/hero-palm-cutout.webp'
const motion = '/images/hero-palm-loop.mp4'
const allowMotion = '(prefers-reduced-motion: no-preference)'

export function HeroPalmMedia() {
preload(motion, { as: 'image', type: 'image/webp', media: allowMotion })

const permitsMotion = useMediaQuery(allowMotion)
const [motionReady, setMotionReady] = React.useState(false)
const showMotion = permitsMotion && motionReady

return (
<Squircle
Expand All @@ -22,25 +18,26 @@ export function HeroPalmMedia() {
{/* Keep the palm aligned with the original 944 × 416 composition. */}
<div className="home-palm-sky relative aspect-[944/416] w-[max(100cqw,calc(100cqh*944/416))]">
<div className="absolute inset-0 bg-[url('/images/hero-sky-grain.png')]" />
<div className="absolute bottom-0 left-[calc(100%*360/944)] w-[calc(100%*260/944)]">
{/* Keep the inlined first frame visible during the animation request. */}
{/* White pixels leave the CSS sky untouched, even between the moving fronds. */}
<div className="absolute bottom-0 left-[calc(100%*360/944)] w-[calc(100%*260/944)] mix-blend-multiply">
<img
src={still}
alt=""
width={260}
height={348}
hidden={showMotion}
className="h-auto w-full"
/>
{(permitsMotion || motionReady) && (
<img
{permitsMotion && (
<video
src={motion}
alt=""
width={260}
height={348}
hidden={!showMotion}
onLoad={() => setMotionReady(true)}
className="h-auto w-full"
autoPlay
loop
muted
playsInline
onLoadStart={() => setMotionReady(false)}
onPlaying={() => setMotionReady(true)}
onError={() => setMotionReady(false)}
className={`absolute inset-0 size-full ${motionReady ? '' : 'opacity-0'}`}
/>
)}
</div>
Expand Down
Binary file added src/components/home/hero-palm-loop-still.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 30 additions & 42 deletions tests/hero-media.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import sharp from 'sharp'
import { createServer } from 'vite'

const repoRoot = path.resolve(import.meta.dirname, '..')
const motion = '/images/hero-palm-cutout.webp'
const motion = '/images/hero-palm-loop.mp4'
const motionFile = path.join(repoRoot, 'public', motion)
const stillFile = path.join(
repoRoot,
'src/components/home/hero-palm-cutout-still.webp',
'src/components/home/hero-palm-loop-still.webp',
)
const stillPrefix = 'data:image/webp;base64,'
const vite = await createServer({
Expand Down Expand Up @@ -45,46 +45,23 @@ const chromePath = [
chromium.executablePath(),
].find((candidate) => candidate && fs.existsSync(candidate))

test('the palm loop stays at 24 fps, matches its still, and fits the media budget', async () => {
const animation = await sharp(motionFile).metadata()
const poster = await sharp(stillFile).metadata()
// The source 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)
// The MP4 and its posters transferred 1.91 MB in the mobile baseline.
test('the palm video and still fit the media budget', async () => {
assert.ok(
fs.statSync(motionFile).size + fs.statSync(stillFile).size < 1_900_000,
'the combined palm assets should stay under 1.90 MB',
)
assert.equal(animation.loop, 0)
assert.ok(animation.hasAlpha && poster.hasAlpha)
assert.equal(animation.width, poster.width)
assert.equal(animation.height, poster.height)
const firstFrame = await sharp(motionFile).ensureAlpha().raw().toBuffer()
const stillFrame = await sharp(stillFile).ensureAlpha().raw().toBuffer()
assert.ok(
firstFrame.equals(stillFrame),
'the still must match the first frame',
)
const poster = await sharp(stillFile).metadata()
assert.equal(poster.hasAlpha, false)
})

test('the server inlines one visible still and preloads motion when allowed', () => {
test('the server inlines one visible still', () => {
assert.doesNotMatch(serverMarkup, /<source\b|<video\b/)
assert.equal(serverMarkup.match(/<img\b/g)?.length, 1)
assert.ok(
serverMarkup.includes(
`src="${stillPrefix}${fs.readFileSync(stillFile, 'base64')}"`,
),
)
const [preload, ...extraPreloads] =
serverMarkup.match(/<link[^>]+rel="preload"[^>]*>/g) ?? []
assert.ok(preload)
assert.equal(extraPreloads.length, 0)
assert.ok(preload.includes(`href="${motion}"`))
assert.match(preload, /media="\(prefers-reduced-motion: no-preference\)"/)
})

test(
Expand Down Expand Up @@ -135,7 +112,7 @@ test(
if (url.pathname === '/') {
return route.fulfill({
contentType: 'text/html',
body: `<!doctype html><div id="root">${serverMarkup}</div>`,
body: `<!doctype html><style>.opacity-0{opacity:0}</style><div id="root">${serverMarkup}</div>`,
})
}
requests.push(url.pathname)
Expand All @@ -157,12 +134,12 @@ test(
requests,
hydrate,
still: page.locator(`img[src^="${stillPrefix}"]`),
motion: page.locator(`img[src="${motion}"]`),
motion: page.locator(`video[src="${motion}"]`),
releaseAnimation: () => animation.emit('release'),
}
}

for (const width of [390, 844, 1024, 1512]) {
for (const width of [390, 1512]) {
const hero = await openHero(width)
await hero.still.waitFor({ state: 'visible' })
assert.ok(
Expand All @@ -172,26 +149,34 @@ test(
)
await hero.hydrate()
await hero.motion.waitFor({ state: 'attached' })
assert.equal(await hero.motion.isHidden(), true)
assert.equal(await hero.still.isVisible(), true)
assert.equal(
await hero.motion.evaluate((video) => getComputedStyle(video).opacity),
'0',
)
hero.releaseAnimation()
await hero.motion.waitFor({ state: 'visible' })
assert.deepEqual(hero.requests, [motion])
assert.equal(await hero.still.isHidden(), true)
await hero.page.waitForFunction(() => {
const video = document.querySelector('video')
return (
video &&
video.currentTime > 0 &&
getComputedStyle(video).opacity === '1'
)
})
assert.ok(hero.requests.length > 0)
assert.ok(hero.requests.every((request) => request === motion))
await hero.page.close()
}

const reduced = await openHero(390, true)
reduced.releaseAnimation()
await reduced.still.waitFor({ state: 'visible' })
await reduced.hydrate()
assert.equal(await reduced.motion.count(), 0)
assert.deepEqual(reduced.requests, [])
await reduced.page.emulateMedia({ reducedMotion: 'no-preference' })
await reduced.motion.waitFor({ state: 'visible' })
await reduced.motion.waitFor({ state: 'attached' })
await reduced.page.emulateMedia({ reducedMotion: 'reduce' })
await reduced.still.waitFor({ state: 'visible' })
assert.equal(await reduced.motion.isHidden(), true)
await reduced.motion.waitFor({ state: 'detached' })
assert.equal(await reduced.still.isVisible(), true)
await reduced.page.close()

const failed = await openHero(1512, false, true)
Expand All @@ -202,7 +187,10 @@ test(
)
failed.releaseAnimation()
await requestFailed
assert.equal(await failed.motion.isHidden(), true)
assert.equal(
await failed.motion.evaluate((video) => getComputedStyle(video).opacity),
'0',
)
assert.equal(await failed.still.isVisible(), true)
await failed.page.close()
},
Expand Down
Loading