Skip to content
← Public packages

@kentcdodds/photo-portfolio

A fast, photo-first portfolio site you update by chatting with your agent: galleries, about, contact, and one-step rollback.

FollowFork

Files

picture.tsx

47 lines · 1.5 KB · TypeScript
/** @jsxRuntime automatic */
/** @jsxImportSource remix/component */
import { buildSrcset, pickVariant } from '../../src/domain/image-plan.ts'
import type { Photo } from '../../src/domain/types.ts'
import { variantUrl, type Site } from '../data/site.ts'

export type PictureOptions = {
	/** `sizes` attribute: how wide the image renders at each breakpoint. */
	sizes: string
	/** Above-the-fold images load eagerly with high priority; everything else is lazy. */
	priority?: boolean
	class?: string
}

/**
 * `<picture>` with WebP and JPEG srcsets, intrinsic size, and the tiny LQIP
 * painted behind it, so the box is the right shape before a byte of the photo
 * arrives (no layout shift) and never flashes empty.
 */
export function picture(site: Site, photo: Photo, options: PictureOptions) {
	const url = (p: Photo, v: Parameters<typeof variantUrl>[2]) =>
		variantUrl(site, p, v)
	const fallback = pickVariant(photo, 'jpeg', 960) ?? photo.variants[0]!
	return (
		<picture
			class={`photo${options.class ? ` ${options.class}` : ''}`}
			style={`aspect-ratio:${photo.width}/${photo.height};background-image:url("${photo.lqip}")`}
		>
			<source
				type="image/webp"
				srcset={buildSrcset(photo, 'webp', url)}
				sizes={options.sizes}
			/>
			<img
				src={url(photo, fallback)}
				srcset={buildSrcset(photo, 'jpeg', url)}
				sizes={options.sizes}
				width={photo.width}
				height={photo.height}
				alt={photo.alt}
				loading={options.priority ? 'eager' : 'lazy'}
				decoding="async"
				fetchpriority={options.priority ? 'high' : undefined}
			/>
		</picture>
	)
}