Aspect Ratio

@sushindustries/ui

A box that keeps its shape and fills whatever is put in it. CSS aspect-ratio, as a prop.

5 min read
View as MarkdownEdit on GitHub

Examples are the tab where the component is shown doing a job, not demonstrating a prop. The API tab already lists the props.

Viewport width
Phone22remthe floor, the narrowest this promises to work at
tanstacktanstack add https://adamjurek.com/r/tanstack/aspect-ratio.jsonshadcnpnpm dlx shadcn@latest add https://adamjurek.com/r/shadcn/aspect-ratio.json

Press Compare. The frames are real viewports, so a layout that breaks at 320 breaks here too rather than in somebody's hands.

In a page

import { AspectRatio, Card } from "@sushindustries/ui";

export function VideoCard({ src, poster }: { src: string; poster: string }) {
	return (
		<Card title="Product walkthrough">
			<AspectRatio ratio={16 / 9}>
				<video src={src} poster={poster} controls />
			</AspectRatio>
		</Card>
	);
}
tsx

What this example is not

ratio={16 / 9} here is fixed at build time, but nothing about the component requires that - it reads just as well from a prop passed down from a CMS field, as long as the value is a number by the time it reaches AspectRatio.