A box that keeps its shape before its picture loads, such as a 16:9 video frame.
- 1.0 kB JS 2.4 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- Right-to-left
- 8 tests
- Linen apron, oat$48
- Stoneware mug$22
- Walnut serving board$64
- Cast iron grill pan$85
import type { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { AspectImage } from "./ui/uicontext/components/AspectRatio.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"import { picture } from "./pictures.ts"const styles = stylex.create({ grid: { display: "grid", gridTemplateColumns: "repeat(auto-fill, minmax(9rem, 1fr))", gap: space.lg, inlineSize: "min(100%, 40rem)", margin: 0, padding: 0, listStyle: "none" }, item: { display: "grid", gap: space.sm }, name: { fontSize: font.sm, fontWeight: 500, color: color.fg, overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap" }, price: { fontSize: font.sm, color: color.muted, fontVariantNumeric: "tabular-nums" },})const products = [ { name: "Linen apron, oat", price: "$48", photo: picture(800, 1000, 0) }, { name: "Stoneware mug", price: "$22", photo: picture(1200, 800, 1) }, { name: "Walnut serving board", price: "$64", photo: "" }, { name: "Cast iron grill pan", price: "$85", photo: picture(1000, 1000, 3) },]// a shop's product grid: photos of any shape cropped to the same square, and one still missingexport const ProductGrid = <M>(h: Html.H<M>) => h.ul.attrs(sx(styles.grid)).children( products.map((p) => h.li.key(p.name).attrs(sx(styles.item)).children( AspectImage(h, { ratio: "1", src: p.photo, alt: p.name, shape: "rounded" }), h.span.attrs(sx(styles.name)).children(p.name), h.span.attrs(sx(styles.price)).children(p.price), )), )When to use
A box that keeps its shape before its picture loads, such as a 16:9 video frame. Use Masonry instead when the pictures keep their own heights in a wall.
Usage
import { AspectImage } from "./ui/uicontext/components/AspectRatio.ts"export const Cover = <M>(h: Html.H<M>, url: string) => AspectImage(h, { ratio: "16 / 9", src: url, alt: "Team offsite, Lisbon", shape: "rounded" })Examples
A video frame and a round avatar
AspectRatio takes anything; give it a poster now and the player later, and the page doesn't move.
import type { Html } from "effect/ui/html"import * as stylex from "@stylexjs/stylex"import { AspectImage, AspectRatio } from "./ui/uicontext/components/AspectRatio.ts"import { sx } from "./ui/uicontext/components/Sx.ts"import { color, corner, font, space } from "./ui/uicontext/components/tokens.stylex.ts"import { picture } from "./pictures.ts"const styles = stylex.create({ wrap: { display: "grid", gap: space.lg, inlineSize: "min(100%, 36rem)" }, poster: { position: "relative", display: "grid", placeItems: "center", gridRow: "1", gridColumn: "1" }, image: { gridRow: "1", gridColumn: "1", inlineSize: "100%", blockSize: "100%", objectFit: "cover" }, play: { gridRow: "1", gridColumn: "1", display: "inline-flex", alignItems: "center", gap: space.sm, blockSize: 44, paddingInline: space.lg, borderWidth: 0, borderRadius: corner.pill, backgroundColor: "rgb(0 0 0 / 0.62)", color: "white", fontFamily: "inherit", fontSize: font.sm, fontWeight: 600, cursor: "pointer", outline: { default: "none", ":focus-visible": "2px solid white" }, outlineOffset: 2, }, row: { display: "grid", gridTemplateColumns: "auto minmax(0, 1fr)", alignItems: "center", gap: space.md }, thumb: { inlineSize: 64 }, who: { fontSize: font.sm, color: color.fg },})// a course lesson: the video's 16:9 frame holds its room before the player loads, and a round// avatar is a 1:1 box with the circle shapeexport const LessonVideo = <M>(h: Html.H<M>) => h.div.attrs(sx(styles.wrap)).children( AspectRatio(h, { ratio: "16 / 9", shape: "rounded", children: [ h.img.attrs({ src: picture(1600, 900, 1), alt: "", ...sx(styles.image) }), h.div.attrs(sx(styles.poster)).children(h.button.attrs({ type: "button", ...sx(styles.play) }).children(h.span.attrs({ "aria-hidden": "true" }).children("▶ "), "Play lesson 4 · 12:04")), ], }), h.div.attrs(sx(styles.row)).children(AspectImage(h, { ratio: "1", src: picture(400, 400, 3), alt: "", shape: "circle", style: styles.thumb }), h.span.attrs(sx(styles.who)).children("Lesson 4 · Priya Raman")), )API reference
AspectRatioView function
Options
| Name | Type | Description |
|---|---|---|
ratiorequired | string | The shape, as CSS writes it: "16 / 9", "4 / 3", "1". |
childrenrequired | View.Children | What fills the box; give an image or iframe aspectFill so it fills it. |
shape | "rounded" | "circle" | Rounded corners, or a circle for a square box. |
style | Style | — |
AspectImageView function
Options
| Name | Type | Default | Description |
|---|---|---|---|
ratiorequired | string | — | — |
src | string | — | Left out, or empty, while there's no picture: a placeholder says what it was. |
altrequired | string | — | What the picture shows; empty for one that only decorates. |
fit | "cover" | "contain" | — | — |
shape | "rounded" | "circle" | — | — |
loading | "lazy" | "eager" | "lazy" | "lazy" by default: the box keeps the room until it scrolls near. |
style | Style | — | — |
Installation
pnpm dlx shadcn@latest add checkfu/uicontext/aspect-ratio