Image 图片
数据展示
增强的图片组件,支持预览、加载失败占位、懒加载。
何时使用
需要图片功能时使用本组件。
代码示例
基础用法
tsx
import { Image } from "@/components/ui/image"
export function Basic() {
return <Image />
}样式变体
fit
| fit | 说明 | 默认 |
|---|---|---|
cover | ✓ | |
contain | ||
fill | ||
none |
rounded
| rounded | 说明 | 默认 |
|---|---|---|
none | ||
sm | ||
default | ✓ | |
lg | ||
full |
API / Props
Image
| Prop | Type | Default | 说明 |
|---|---|---|---|
className | string | — | 自定义样式类名 |
src | string | — | 资源地址 |
alt | string | — | 替代文本 |
fallback | React.ReactNode | — | Fallback element shown while loading or on error |
width | number | string | — | Width |
height | number | string | — | Height |
onError | () => void | — | Called when image fails to load |
onLoad | () => void | — | Called when image loads successfully |
| ...rest | React.ComponentProps<"div"> | — | 透传 <div> 原生属性 |
注意事项
- Client Component: 使用
"use client",依赖 React hooks/状态,需在客户端渲染。 - CVA 变体: 通过
class-variance-authority管理样式变体,类型安全。变体对象可导出复用。 - 原生属性: 继承所有
<div>原生 HTML 属性。
📘 完整交互式示例与控件属性请查看 Storybook 文档。