Aspect Ratio
A box that keeps its shape while its content loads, optionally with a pencilled frame round the edge.
16 : 9
import { AspectRatio } from "@/components/ui/aspect-ratio";
export default function AspectRatioDemo() {
return (
<div className="w-full max-w-md">
<AspectRatio ratio={16 / 9} framed seed="ratio-demo">
{/* eslint-disable-next-line @next/next/no-img-element */}
<img src="/doodle-portrait.svg" alt="A doodled portrait" className="size-full object-cover object-[50%_43%]" />
</AspectRatio>
<p className="mt-3 text-center text-sm text-ink-3">16 : 9</p>
</div>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ aspect-ratio"use client";
import type { ComponentProps } from "react";
import { cn } from "@/lib/utils";
import { penRadius, usePen, type Pen } from "@/hooks/use-ink-box";
import { InkOutline } from "@/lib/ink-outline";
/**
* A box that keeps its shape: `ratio` is width over height (16 / 9, 4 / 3, 1).
* Give it `framed` and a pencilled frame is drawn round the edge.
*/
function AspectRatio({
ratio = 1,
framed = false,
className,
style,
children,
seed,
roughness,
passes,
radius,
corners,
draw,
weight,
speed,
...props
}: ComponentProps<"div"> & Pick<Pen, "roughness" | "passes" | "radius" | "corners" | "draw" | "weight" | "speed"> & { ratio?: number; framed?: boolean; seed?: string | number }) {
const pen = usePen({ roughness, passes, radius, corners, draw, weight, speed });
// The picture is clipped to the box; the frame is drawn outside that clip,
// or its overshoot would be cut off and the corners left open.
return (
<div data-slot="aspect-ratio" className={cn("relative w-full", className)} style={{ aspectRatio: String(ratio), ...(framed ? { borderRadius: penRadius(pen, 14) } : null), ...style }} {...props}>
<div className={cn("absolute inset-0 overflow-hidden [border-radius:inherit]", framed && "p-[3px]")}>{children}</div>
{framed && <InkOutline pen={pen} seed={seed} estimate={[320, 320 / ratio]} maxRadius={14} className="text-ink-line" />}
</div>
);
}
export { AspectRatio };Usage
import { AspectRatio } from "@/components/ui/aspect-ratio"
<AspectRatio ratio={16 / 9} framed>
<img src="/photo.jpg" alt="…" className="size-full object-cover" />
</AspectRatio>Examples
Ratios
Square, four to three, widescreen and portrait.
1 : 1
4 : 3
16 : 9
9 : 16
import { AspectRatio } from "@/components/ui/aspect-ratio";
const ratios = [
["1 : 1", 1],
["4 : 3", 4 / 3],
["16 : 9", 16 / 9],
["9 : 16", 9 / 16],
] as const;
export default function AspectRatioRatios() {
return (
<div className="flex w-full max-w-xl flex-wrap items-end gap-6">
{ratios.map(([label, ratio]) => (
<figure key={label} className="flex flex-col gap-2" style={{ width: ratio < 1 ? 90 : 150 }}>
<AspectRatio ratio={ratio} framed seed={`ratio-${label}`} className="text-ink-3">
<div className="grid size-full place-items-center text-lg">{label}</div>
</AspectRatio>
<figcaption className="text-center text-sm text-ink-3">{ratio.toFixed(2)}</figcaption>
</figure>
))}
</div>
);
}Holding a place
An image box that does not jump when the picture arrives.
The box holds its shape while the picture loads, so nothing below it jumps.
import { AspectRatio } from "@/components/ui/aspect-ratio";
export default function AspectRatioImage() {
return (
<div className="flex w-full max-w-sm flex-col gap-3">
<AspectRatio ratio={4 / 3} className="bg-ink-5/50">
{/* eslint-disable-next-line @next/next/no-img-element */}
<img src="/doodle-portrait.svg" alt="A doodled portrait" className="size-full object-cover" />
</AspectRatio>
<p className="text-sm text-ink-3">The box holds its shape while the picture loads, so nothing below it jumps.</p>
</div>
);
}API reference
- ratiodefault
1 numberWidth over height: 16 / 9, 4 / 3, 1.
- frameddefault
false booleanDraws a pencilled frame round the box.
- seed
string | numberPins the drawing.
Pen settings
The drawing also takes draw, roughness, passes, radius, corners, weight and speed, as props or from the nearest InkProvider. What each one does.