Aspect Ratio
Aspect ratio example: a plain wrapper that holds its children to a width-to-height ratio you set, for images, video and cards. No client JavaScript is needed.
pnpm dlx shadcn add @01orbital/aspect-ratioimport { cn } from "cn"
function AspectRatio({
ratio,
className,
...props
}: React.ComponentProps<"div"> & { ratio: number }) {
return (
<div
data-slot="aspect-ratio"
style={
{
"--ratio": ratio,
} as React.CSSProperties
}
className={cn("relative aspect-(--ratio)", className)}
{...props}
/>
)
}
export { AspectRatio }
import { AspectRatio } from "@/components/ui/aspect-ratio"
// The copy: change text here, not in the markup.
const COPY = {
alt: "A painted planet over a dark horizon",
} as const
/** Aspect Ratio holding a 16:9 picture. */
export default function AspectRatioDemo() {
return (
<AspectRatio ratio={16 / 9} className="w-full max-w-sm rounded-lg bg-muted">
<img
src="https://01orbital.dev/art/kit/celestograph-369187-meridian-1b.png"
alt={COPY.alt}
className="size-full rounded-lg object-cover"
/>
</AspectRatio>
)
}
Dependencies
npm: cn. The CLI installs them for you.
No other registry pieces.