suiss/uim · React Native Component

Aspect Ratio

A view that keeps its content at a fixed width-to-height ratio.

Aspect Ratio is an open-source mobile component from suiss/uim for React Native and Expo, with SwiftUI and Jetpack Compose versions built from the same design tokens. The @esuiss/uim CLI copies its source into your project, so you own the code.

iOS
Aspect Ratio on iOS in light mode

AspectRatio is a View that sets the aspectRatio style from its ratio prop. Its height follows from the width it is given, so the content keeps its shape at any screen size.

Use it for images, video thumbnails and placeholders that must not stretch.

Installation

$ pnpm dlx @esuiss/uim@latest add aspect-ratio

Manual

  1. Add the pieces it builds on: the primitives components/primitives/aspect-ratio.tsx.

  2. Copy the source into components/ui/aspect-ratio.tsx:

uniwind/components/ui/aspect-ratio.tsx
import * as AspectRatioPrimitive from "@/components/primitives/aspect-ratio"

const AspectRatio = AspectRatioPrimitive.Root

export { AspectRatio }
  1. Update the import paths to match your project.

Usage

<View className="w-full overflow-hidden rounded-lg">
  <AspectRatio ratio={16 / 9}>
    <Image source={{ uri: imageUri }} className="h-full w-full" />
  </AspectRatio>
</View>

Pass ratio as width divided by height, such as 16 / 9. It defaults to 1. Size the content inside with h-full w-full, and put rounding or clipping on a parent view.

Examples

Square

A square image with ratio={1}.

iOS
Aspect Ratio (square) on iOS in light mode

Portrait

A tall image with ratio={9 / 16}.

iOS
Aspect Ratio (portrait) on iOS in light mode

Placeholder

A placeholder surface with an icon instead of an image.

iOS
Aspect Ratio (placeholder) on iOS in light mode

Accessibility

  • The view adds no role; give images their own accessibilityLabel.