All components
Aspect Ratio
Locks media, maps, and artwork frames to design-token aspect ratios with rounded clipping.
16 : 9 (wide)
123456789import { AspectRatio } from "#components/ui/base-ui/aspect-ratio.tsx";
export function Example() { return ( <AspectRatio ratio="wide" rounded="lg"> <img src="/cover.jpg" alt="Studio preview" /> </AspectRatio> );}Ratio presets
Token-backed square (1:1), landscape (4:3), and portrait (3:4) frames.
1 : 1
4 : 3
3 : 4
Install
1npx stylexblocks add aspect-ratioSource
components/ui/base-ui/aspect-ratio.tsx
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061import * as stylex from "@stylexjs/stylex";import { createComponent } from "#lib/base-ui/create-component.tsx";import { defineRecipe } from "#lib/recipe-props.ts";import { aspectRatios } from "@stylexblocks/ui/tokens/aspect-ratios.stylex.ts";import { radii } from "@stylexblocks/ui/tokens/radii.stylex.ts";
const aspectRatioStyles = stylex.create({ base: { position: "relative", width: "100%", overflow: "hidden", display: "flex", alignItems: "center", justifyContent: "center", },});
const aspectRatioValues = stylex.create({ square: { aspectRatio: aspectRatios.square }, landscape: { aspectRatio: aspectRatios.landscape }, portrait: { aspectRatio: aspectRatios.portrait }, wide: { aspectRatio: aspectRatios.wide }, ultrawide: { aspectRatio: aspectRatios.ultrawide }, golden: { aspectRatio: aspectRatios.golden }, "1/1": { aspectRatio: aspectRatios.square }, "4/3": { aspectRatio: aspectRatios.landscape }, "3/4": { aspectRatio: aspectRatios.portrait }, "16/9": { aspectRatio: aspectRatios.wide }, "21/9": { aspectRatio: "21 / 9" }, "3/2": { aspectRatio: "3 / 2" }, "4/5": { aspectRatio: "4 / 5" },});
const aspectRatioRadii = stylex.create({ none: { borderRadius: 0 }, sm: { borderRadius: radii.sm }, md: { borderRadius: radii.md }, lg: { borderRadius: radii.lg }, full: { borderRadius: radii.full },});
const aspectRatioRecipe = defineRecipe({ styles: aspectRatioStyles, axes: { ratio: aspectRatioValues, rounded: aspectRatioRadii, }, defaultVariants: { ratio: "wide", rounded: "md", },});
/** * AspectRatio — locks media, maps, and artwork frames to a design-token * aspect ratio (`square`, `landscape`, `portrait`, `wide`, `ultrawide`, * `golden`, `3/2`, `4/5`). Composition via `render`. */export const AspectRatio = createComponent("div", aspectRatioRecipe, "AspectRatio");export type AspectRatioProps = React.ComponentProps<typeof AspectRatio>;