Aspect Ratio
Frames that hold their shape and reshape on a spring when the ratio changes, with photos that fade up out of a blur once they have loaded.
See it as an AI agent does.See this component the way an AI agent does.
Installation
npx shadcn@latest add @keyframery/aspect-ratioThis adds components/keyframery/aspect-ratio.tsx and its primitive to your project, and installs UI SDK. The motion tokens are added to your global CSS the first time you install a component.
Usage
import { AspectRatio, AspectRatioImage } from "@/components/keyframery/aspect-ratio"
export default function Usage() {
return (
<AspectRatio ratio={16 / 9}>
<AspectRatioImage src="/demo/coast-1.jpg" alt="A cove at low tide" />
</AspectRatio>
)
}Examples
Image
The frame holds its shape before the photo arrives; AspectRatioImage fades it up out of a blur once it has loaded.
Changing the ratio
A new ratio is reached on a spring, so the frame reshapes instead of snapping.
Any content
The frame keeps anything at its ratio, like an embedded video or a map. Fill it with absolute inset-0.
Motion
| Move | What you see |
|---|---|
| Spring | A new ratio is reached on a spring, so the frame reshapes instead of snapping. |
| Blur in | The photo fades up out of an 18px blur and a 1.08 zoom once it has loaded; a new photo plays it again. |
Timing and springiness follow the motion style you set. To change how fast this aspect ratio moves, pass speed:
<AspectRatio speed="fast">
…
</AspectRatio>"fast" is 0.6×, "slow" 1.4×, and a number sets the multiplier. With reduced motion turned on, nothing moves: changes happen at once or with a short fade.
Theming
Set these CSS variables on the component or a parent. Leave them unset to follow the light and dark theme. The open Customize panel below the preview lets you try these settings.
| Variable | What it changes | Default |
|---|---|---|
--aspect-ratio-face | Frame surface | --muted |
--aspect-ratio-radius | Frame corners | 14px |
--aspect-ratio-fit | Image fit | cover |
--aspect-ratio-position-x | Horizontal crop | 50% |
--aspect-ratio-position-y | Vertical crop | 50% |
--aspect-ratio-saturation | Saturation | 1 |
--aspect-ratio-contrast | Contrast | 1 |
--aspect-ratio-brightness | Brightness | 1 |
API reference
Every part takes className and children, along with the props of the element it renders. The parts are rendered by UI SDK, so they also take render, to draw your own element, and a function of their state for className and style.
AspectRatio
Renders a <div>, and takes its props too.
Prop
Type
AspectRatioImage
Renders a <img>, and takes its props too.
Prop
Type
Accessibility
Roles, keyboard and focus are declared on the component's UI SDK primitive, and state is announced through the matching ARIA attributes. More in Accessibility.
Built on UI SDK's core
UI SDK doesn't ship a aspect ratio, so this one is defined with its core: one definePrimitive in components/keyframery/core/aspect-ratio-core.ts, with no React in it. UI SDK's adapters can render that same definition in Vue, Svelte, Solid and web components. To keep your own styling and take only the motion, import from @/components/keyframery/primitives/aspect-ratio.
Questions
How does the animated Aspect Ratio for UI SDK move?
Spring: A new ratio is reached on a spring, so the frame reshapes instead of snapping. Blur in: The photo fades up out of an 18px blur and a 1.08 zoom once it has loaded; a new photo plays it again.
Do I need Motion or Framer Motion for the animated Aspect Ratio?
No. Every move is plain CSS, so there is nothing to install besides the component and UI SDK.
UI SDK has no aspect ratio. How is this one built on UI SDK?
Its behaviour is one definition written with UI SDK's core (definePrimitive), in components/keyframery/core/aspect-ratio-core.ts. Its parts are rendered by UI SDK's adapter, so they take render, state-based class names and data-slot, like every UI SDK component.
What happens to the Aspect Ratio when someone turns on reduced motion?
Nothing moves. State changes happen at once, or with a short fade.