C

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

PropTypeDefault说明
classNamestring自定义样式类名
srcstring资源地址
altstring替代文本
fallbackReact.ReactNodeFallback element shown while loading or on error
widthnumber | stringWidth
heightnumber | stringHeight
onError() => voidCalled when image fails to load
onLoad() => voidCalled when image loads successfully
...restReact.ComponentProps<"div">透传 <div> 原生属性

注意事项

  • Client Component: 使用 "use client",依赖 React hooks/状态,需在客户端渲染。
  • CVA 变体: 通过 class-variance-authority 管理样式变体,类型安全。变体对象可导出复用。
  • 原生属性: 继承所有 <div> 原生 HTML 属性。

📘 完整交互式示例与控件属性请查看 Storybook 文档