suiss/ui · Component
Aspect Ratio
A container that keeps its content at a fixed width-to-height ratio.
Aspect Ratio is an open-source component from suiss/ui for React, React Native, SwiftUI and Jetpack Compose. The @esuiss/ui CLI copies its source into your project, so you own the code.
AspectRatio is a single div that sets the CSS aspect-ratio property from the ratio prop. Its width follows the parent, and its height is derived from the ratio. It is positioned relative, so children can be absolutely positioned inside it.
Use it to reserve space for images, video or embeds so the layout does not shift while media loads.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
-
Add the
cnhelper. -
Copy and paste the following code into your project.
- Update the import paths to match your project setup.
Usage
Examples
Square
Set ratio={1} for a 1:1 frame.
Portrait
Set ratio={3 / 4} for a frame that is taller than it is wide.
Placeholder
A 4:3 frame that holds centered placeholder text instead of an image.
API Reference
AspectRatio
The ratio-locked container.
| Prop | Type | Default |
|---|---|---|
ratio | number | Required |
Also accepts the props of React.ComponentProps<"div">.
Pass ratio as width divided by height, typically written as a division such as 16 / 9, 4 / 3 or 1. The ratio is passed through the --ratio CSS custom property and applied with the aspect-(--ratio) class. Give media inside it className="size-full object-cover" to fill the frame, and add overflow-hidden and a radius class to clip the content to rounded corners. Style it through data-slot="aspect-ratio".
Web preview
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
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/aspect-ratio.tsx:
- Update the import paths to match your project setup.
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/aspect-ratio.tsx:
- Update the import paths to match your project setup.
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/aspect-ratio.tsx:
- Update the import paths to match your project setup.
-
Target iOS 17 or later.
-
Add the Swift package to your app:
- Copy the theme files every view uses:
- Copy the source:
- Add the files to your app target.
- Apply the Compose compiler plugin, enable Compose, set
compileSdkto 37 or higher andminSdkto 24 or higher, and add the dependencies:
- Copy the theme files every view uses:
- Copy the source:
- Update the
com.esuiss.uipackage names to match your project.
Usage
Examples
Square
Set ratio={1} for a square image.
Web preview
Portrait
Set ratio={9 / 16} for a tall image.
Web preview
Placeholder
A placeholder surface with an icon instead of an image.
Web preview
Accessibility
- The view adds no role; give images their own
accessibilityLabel.
API Reference
AspectRatio
A view that keeps its width and height at a fixed ratio.
| Prop | Type | Default |
|---|---|---|
ratio | number | 1 |
render | React.ReactElement | ((props, state) => React.ReactElement) | - |
Also accepts the props of View.
Pass ratio as width divided by height, such as 16 / 9. Size the content inside with h-full w-full, and put rounding or clipping on a parent view. Use render to render another element in place of the default View.
AspectRatio
A view that keeps its width and height at a fixed ratio.
| Parameter | Type | Default |
|---|---|---|
ratio | CGFloat | 1 |
content | () -> Content | - |
AspectRatio
A view that keeps its width and height at a fixed ratio.
| Parameter | Type | Default |
|---|---|---|
ratio | Float | - |
modifier | Modifier | Modifier |
content | @Composable BoxScope.() -> Unit | - |