Skip to content
caliper ui docs

Components

Aspect ratio

A box that keeps a fixed ratio so images and video never shift the layout.

Free
pnpm dlx shadcn@latest add @caliper/aspect-ratio

Listing photo

Harbour room 4 · shown on the booking page

Harbour at dusk, a sailboat at the pier

16:9 · the frame holds its shape while the image loads.

Installs components/ui/aspect-ratio.tsxDepends on none beyond ReactMotion shared motion tokens

When to use

Use it for

  • Thumbnails, covers and embedded video.
  • Skeletons that must match final media size.

Skip it when

  • Content that should size itself.

Installation

terminal
pnpm dlx shadcn@latest add @caliper/aspect-ratio

Adds its 2 files to components/ui/, each with its own stylesheet, and installs their packages. No account needed. The first part also adds Caliper UI’s tokens; import them once as in Installation.

Usage

This is the code behind the preview above. It is built from AspectRatio and AspectRatioImage.

example.tsx
import { type ReactNode } from "react"
import { AspectRatio } from "@/components/ui/aspect-ratio"
import { cn } from "@/components/ui/lib/cn"

function Panel({ title, context, media, action, className, children }: { title: string; context: string; media?: ReactNode; action?: ReactNode; className?: string; children?: ReactNode }) {
  return (
    <section className={cn("grid w-full max-w-md gap-5 rounded-overlay border border-border bg-background p-6 shadow-resting", className)}>
      <div className="flex items-center gap-3">
        {media}
        <div className="grid min-w-0 flex-1 gap-1">
          <p className="font-medium">{title}</p>
          <p className="text-sm text-muted-foreground">{context}</p>
        </div>
        {action}
      </div>
      {children}
    </section>
  );
}

export function WideBox() {
  return (
    <Panel title="Listing photo" context="Harbour room 4 · shown on the booking page" className="max-w-sm">
      <AspectRatio ratio={16 / 9} className="overflow-hidden rounded-control bg-muted">
        {/* eslint-disable-next-line @next/next/no-img-element */}
        <img src="/media/harbour.jpg" alt="Harbour at dusk, a sailboat at the pier" className="absolute inset-0 size-full object-cover" />
      </AspectRatio>
      <p className="text-sm text-muted-foreground">16:9 · the frame holds its shape while the image loads.</p>
    </Panel>
  );
}

API reference

AspectRatio

component

Also accepts the native props of its root element and a ref.

PropTypeDefault
rationumber1
animatedbooleantrue
childrenReactNode—

AspectRatioImage

component

No props of its own; it accepts the native props of its root element.

Accessibility

It is built on native elements, so roles and focus come from the browser.

No custom key handling; Tab and the native activation keys do the work.

Motion

Transitions use the shared duration and easing tokens. Under prefers-reduced-motion those tokens collapse to instant and travel distances go to zero, so state changes still happen without movement.

All Identity & navigation examples