The pressable surface for images and videos: images in messages, album thumbnails, video covers and the viewer’s filmstrip. Pressing drops it to 80% opacity without scaling, the focus ring is drawn inside the image (two-tone, visible on any picture), and the selected item is outlined and read as selected.
Status: experimental · Components: MediaButton
Import
Section titled “Import”import { MediaButton } from 'nacre'The media (Image / Video) fills the surface as children; the layout gives the size with width / height. overlay sits in the middle for decorations such as a play mark; the whole card is the button. label is the file name, or the type when there is none. The first one is the focus ring’s color: Tab to it and the ring still stands out.
import { Text, View, signal } from 'nsh'import { FLOAT_CONTROL_CLASS, Icon, MediaButton, floatShadow, TEXT_SIZE } from 'nacre'
export function MediaButtonUsage() { const opened = signal('') return ( <View class="gap-3"> <View class="flex-row flex-wrap items-start gap-3"> <MediaButton testID="media-usage-image" label="product-shot.png" width={200} height={140} onPress={() => opened.set('Image')} > {/* 和焦点环同色:Tab 到这张,看双色环在同色图上也分得出来 */} <View class="w-full h-full bg-ring" /> </MediaButton> <MediaButton testID="media-usage-video" label="walkthrough.mp4" width={240} height={135} onPress={() => opened.set('Video')} overlay={ <View class={`w-9 h-9 rounded-full items-center justify-center ${FLOAT_CONTROL_CLASS}`} style={{ boxShadow: floatShadow() }}> <Icon name="play" size={16} color="foreground" /> </View> } > <View class="w-full h-full bg-chart-4" /> </MediaButton> </View> <Text testID="media-usage-opened" class={`${TEXT_SIZE.small} text-muted-foreground`}> {opened() ? `$Opened:${opened()}` : 'Press one to open the preview'} </Text> </View> )}Examples
Section titled “Examples”Retry failed upload
Section titled “Retry failed upload”Two lines in overlay; label names the retry action. The whole image is pressable.
import { Text, View, signal } from 'nsh'import { MediaButton, TEXT_SIZE } from 'nacre'
export function MediaButtonRetry() { const retried = signal(false) return <View class="gap-2"> <MediaButton testID="media-retry" label="Retry" width={64} height={64} onPress={() => retried.set(true)} overlay={<View class="absolute inset-0 bg-foreground/45 items-center justify-center gap-0.5"><Text class={`${TEXT_SIZE.caption} text-background`}>Upload failed</Text><Text class={`${TEXT_SIZE.caption} text-background underline`}>Retry</Text></View>}> <View class="w-full h-full bg-chart-1" /> </MediaButton> <Text testID="media-retried">{retried() ? 'Retried' : ''}</Text> </View>}Filmstrip (selection)
Section titled “Filmstrip (selection)”Pass selected (a boolean) when switching the current item in a set: the current one gets a 2pt foreground outline and is read as selected; the others drop to 50% and come back on hover.
Phone: Give selected to switch the current item in a strip: it gets a 2 pt foreground inner outline and is announced as selected; the others drop to 50%.
import { View, signal } from 'nsh'import { MediaButton } from 'nacre'
const SWATCHES = ['bg-chart-1', 'bg-chart-2', 'bg-chart-3', 'bg-chart-4', 'bg-chart-5']
export function MediaButtonStrip() { const index = signal(1) return ( <View class="flex-row flex-wrap items-center gap-2 p-3 rounded-lg bg-background"> <Each of={SWATCHES} key={(s) => s}> {(swatch, i) => ( <MediaButton testID={`media-strip-${i()}`} label={`$Image ${i() + 1}`} radius="md" width={48} height={48} selected={i() === index()} onPress={() => index.set(i())} > <View class={`w-full h-full ${swatch()}`} /> </MediaButton> )} </Each> </View> )}Framed and disabled
Section titled “Framed and disabled”framed adds a 1pt border around light images without shrinking them; disabled ignores presses, leaves the Tab order and drops to 50%.
Phone: framed adds a 1 pt outline for light images without shrinking them; disabled ignores taps and drops to 50%.
import { Text, View } from 'nsh'import { MediaButton, TEXT_SIZE } from 'nacre'
export function MediaButtonStates() { return ( <View class="flex-row flex-wrap items-start gap-3"> <MediaButton testID="media-framed" label="Screenshot on white" framed width={96} height={96} onPress={() => {}}> <View class="w-full h-full bg-background" /> </MediaButton> <MediaButton testID="media-disabled" label="Expired image" disabled width={96} height={96} overlay={<Text class={`${TEXT_SIZE.caption} text-foreground`}>Expired</Text>} > <View class="w-full h-full bg-chart-1" /> </MediaButton> </View> )}Keyboard
Section titled “Keyboard”| Key | Action |
|---|---|
| Tab | Each item is one stop; the focus ring appears inside the image |
| Space / Return | Press: open the preview (onPress) |
Accessibility
Section titled “Accessibility”- Read as a button named by label; the media and the overlay are decorative and not read separately.
- The focus ring is drawn inside the image: 2pt in the ring color with a 1pt line of the background color inside it. A ring outside would be clipped by bubbles and scroll areas; a one-color ring vanishes on an image of the same color, and the line in between keeps contrast with any picture.
- Selection doesn’t rely on opacity alone: the current item has a 2pt foreground outline and is read as selected; outside a set (no selected) no selection state is reported.
Do / Don’t
Section titled “Do / Don’t”Do
- Use the file name as label, or “Image” / “Video” when there is none
- Pass selected (a boolean) to every item of a filmstrip
- Pass framed for light images on light backgrounds
Don’t
- Use it as an icon or text button (use IconButton / Button)
- Put pressable controls in overlay (the whole card is the button; nested buttons are unreachable by keyboard and screen readers)
- Add a press scale to media
Property descriptions follow the Chinese source comments.
MediaButton
Section titled “MediaButton”| Prop | Type | Default | Description |
|---|---|---|---|
label * |
string |
— | 读屏名:文件名,没有就写类型(「图片」「视频」) |
onPress |
() => void |
— | |
selected |
boolean |
— | 在一组可选项里(查看器底部的缩略图条)时给 boolean:当前项画 2pt 前景色内描边、读屏读 selected; 其余项压到 50%,悬停恢复。不传表示不在选择组里(消息里的图片) |
disabled |
boolean |
— | 不可点:不响应按下、不进 Tab 顺序,读屏读 disabled;外观压到 50% |
radius |
MediaButtonRadius |
'lg' |
圆角,默认 lg(10);查看器缩略图条用 md(8);贴着气泡裁切边的图片给 none |
framed |
boolean |
— | 1pt 描边框住图片:浅色图片放在浅底上(相册缩略图)时给,图不会被描边挤小 |
width |
number |
— | 宽与高:表面不会自己量媒体,尺寸由布局给(也可以用 class 撑开) |
height |
number |
— | |
class |
string |
— | 布局类(外边距、绝对定位、flex) |
overlay |
JSX.Element |
— | 叠在媒体上、居中的内容:播放标记、上传失败重试的说明。只是装饰,整张卡才是按钮 |
children |
JSX.Element |
— | 媒体本身:Image / Video,撑满(class="w-full h-full" 或 absolute inset-0) |
testID |
string |
— | |
viewRef |
(node: ViewRef) => void |
— |
* required