Search documentation

Search pages, components, blocks, styles and colours.

Get started
Skip to content

Aspect Ratio

PreviousNext

Displays content within a desired ratio, and glides to a new one when it changes.

Aspect Ratio on iOS

Installation

bunx --bun fabricator-ui@latest add aspect-ratio

Usage

import { AspectRatio } from "@/components/ui/aspect-ratio"
import { Text } from "@/components/ui/text"
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
<AspectRatio
  ratio={selected.ratio}
  className="w-56 overflow-hidden rounded-xl bg-muted"
>
  <Image
    src={LAKE}
    alt="A rowing boat on a turquoise mountain lake"
    className="size-full"
  />
</AspectRatio>
<ToggleGroup
  variant="outline"
  size="sm"
  value={[selected.label]}
  onValueChange={(next) => {
    const match = RATIOS.find((item) => item.label === next[0])
    if (match) setSelected(match)
  }}
>
  {RATIOS.map((item) => (
    <ToggleGroupItem key={item.label} value={item.label}>
      {item.label}
    </ToggleGroupItem>
  ))}
</ToggleGroup>

Examples

aspect-ratio-example.tsx
import * as React from "react"
import { Image } from "react-native"
 
import { AspectRatio } from "@/components/ui/aspect-ratio"
import { Text } from "@/components/ui/text"
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
 
const RATIOS = [
  { label: "16:9", ratio: 16 / 9 },
  { label: "1:1", ratio: 1 },
  { label: "3:4", ratio: 3 / 4 },
]
 
const LAKE =
  "https://images.unsplash.com/photo-1501785888041-af3ef285b470?w=800&auto=format&fit=crop&q=80"
 
const PHOTO =
  "https://images.unsplash.com/photo-1497366754035-f200968a6e72?w=900&auto=format&fit=crop&q=80"
 
function AspectRatioExample() {
  const [selected, setSelected] = React.useState(RATIOS[0])
  return (
    <ExampleWrapper>
      <Example title="Changing the ratio: the frame glides">
        <AspectRatio
          ratio={selected.ratio}
          className="w-56 overflow-hidden rounded-xl bg-muted"
        >
          <Image
            src={LAKE}
            alt="A rowing boat on a turquoise mountain lake"
            className="size-full"
          />
        </AspectRatio>
        <ToggleGroup
          variant="outline"
          size="sm"
          value={[selected.label]}
          onValueChange={(next) => {
            const match = RATIOS.find((item) => item.label === next[0])
            if (match) setSelected(match)
          }}
        >
          {RATIOS.map((item) => (
            <ToggleGroupItem key={item.label} value={item.label}>
              {item.label}
            </ToggleGroupItem>
          ))}
        </ToggleGroup>
      </Example>
      <Example title="16:9" className="items-stretch">
        <AspectRatio
          ratio={16 / 9}
          className="overflow-hidden rounded-lg bg-muted"
        >
          <Image
            src={PHOTO}
            alt="An office with plants by the window"
            className="size-full"
          />
        </AspectRatio>
      </Example>
      <Example title="Square" className="items-stretch">
        <AspectRatio
          ratio={1}
          className="items-center justify-center rounded-lg bg-muted md:w-60"
        >
          <Text className="text-muted-foreground">1:1</Text>
        </AspectRatio>
      </Example>
      <Example title="Portrait" className="items-stretch">
        <AspectRatio
          ratio={9 / 16}
          className="w-1/2 items-center justify-center rounded-lg bg-muted"
        >
          <Text className="text-muted-foreground">9:16</Text>
        </AspectRatio>
      </Example>
    </ExampleWrapper>
  )
}
 
export { AspectRatioExample }

API reference

Props it adds to what it wraps: ratio.

Differences from web

Same. It works as it does on the web, with onPress for onClick.

Accessibility

Text is read as it appears. Give a control with no text an accessibilityLabel.