Card media
Reserved media geometry with an accessible fallback and optional token scrim.
npx @swipeui/cli add card-mediaVariant
Preset
Material
Scheme
Feel
When to use it
- The picture at the top of a card that should hold its shape while it loads, for example a 4:3 photo in a feed.
- Product, article or place artwork that may fail to load and needs a calm, readable fallback.
- Artwork with text over its lower edge, where an optional scrim keeps that text legible.
- Any image whose space must be reserved up front so the content below never jumps.
When not to use it
- Small icons or avatars. Use Avatar or an icon instead.
- Swipeable galleries of several images. Carousel handles paging between them.
- Before-and-after comparisons. Image compare gives you the draggable divider.
- Full-screen hero images in a header. Collapsing hero header manages the scroll effects.
Installation
Run this from your app's root. If SwipeUI isn't set up yet, run npx @swipeui/cli init first; it sets up Uniwind, the theme tokens and the cn helper.
npx @swipeui/cli add card-media- Adds
components/swipeui/card/card-media.tsxand 3 shared helpers. - Installs expo-image, expo-linear-gradient, uniwind, clsx, tailwind-merge, class-variance-authority. In Expo apps it uses
npx expo installso versions match your SDK.
Usage
import { CardMedia } from '@/components/swipeui/card/card-media';
<CardMedia source={photo} description="Mountain landscape" />;API reference
CardMediaProps
| Prop | Type | Default | Description |
|---|---|---|---|
source? | | — | Image source; remount with a new key when replacing content. |
description | | — | Accessible artwork description; empty string marks decoration. |
aspectRatio? | | | Width divided by height. |
scrim? | | | Optional lower-edge scrim. |
loadingLabel? | | | Localized loading message. |
fallbackLabel? | | | Localized missing/failed message. |
testID? | | — | Root identifier. |
Composition and behavior
- source is an Expo Image source. Pass a new key when you replace it.
- aspectRatio reserves the shape before the image loads; the default is 4:3.
- scrim adds a gradient on the lower edge in your theme's colours, and is skipped with Reduce Transparency.
- Loading and failure states keep the same size and show translatable text.
- It's built to drop into Card's media slot, but works on its own too.
Behavior and gestures
- It doesn't respond to touch on its own; inside a tappable card, a tap on the picture runs the card's action.
- While the image loads, the reserved area shows a localized loading message instead of an empty hole.
- If the image fails, the same area shows a fallback message ("Image unavailable" by default) without changing size.
- Swapping to a new image source starts a fresh load, so an old failure never sticks to new artwork.
Accessibility
- description is required. Write what the picture shows, such as "Hikers on a ridge at sunrise", or pass an empty string for purely decorative images so screen readers skip them.
- The loading and fallback messages are real text that screen readers can read, and you can translate them with loadingLabel and fallbackLabel.
- With Reduce Transparency switched on, the scrim is removed so text sits on the image without a translucent layer.
- When the image fails to load, no broken-image icon or empty frame is announced.
Motion
- Static on its own; any movement comes from the parent that animates it.
- Static: no fades or transitions are added, so loading never animates.
- Any movement comes from the parent, such as a pressed card scaling down.
Performance
- Uses Expo Image, which caches and decodes off the JavaScript thread.
- The aspect ratio reserves space before the image arrives, so layout never shifts.
- Give the component a new key when you replace artwork, so stale load state is dropped.
Notes for AI
- Always pass description; use an empty string only for decoration.
- Set aspectRatio to the artwork's real shape (width divided by height); the default is 4/3.
- Use scrim only when text sits on top of the image.
Compatibility
| Runs on | Supported |
|---|---|
| Expo SDK 57 | Yes |
| Expo SDK 58 | Yes |
| React Native CLI (with Expo modules) | Yes |
| Expo Go | Yes |
| iOS | Yes |
| Android | Yes |
This table describes API support and static bundle checks. Native runtime, device accessibility and bare Uniwind compatibility remain pending.