Image
Image with a load-failure placeholder and optional fullscreen preview (zoom / rotate / multi-image switching). Native loading="lazy" + decoding="async".
Basic usage

import { useState } from 'react'
import { LhImage, LhImageViewer } from '@lh-ui/react'
export default function Demo() {
const [open, setOpen] = useState(false)
return (
<>
<LhImage src="/images/ink.png" alt="Sample image" sx={{ w: 240, h: 140 }} />
<LhImage src="/missing.png" alt="Sample image" />
<LhImage src="/images/ink.png" alt="Sample image" fit="contain" sx={{ w: 110, h: 80 }} />
<LhImage src="/images/ink.png" alt="Sample image" lazy />
<LhImage
src="/images/thumb.png"
alt="Previewable image"
preview
previewSrcList={['/images/a.png', '/images/b.png']}
/>
{open && (
<LhImageViewer
images={['/images/a.png', '/images/b.png']}
initialIndex={1}
onClose={() => setOpen(false)}
/>
)}
</>
)
}
LhImage Props
| Prop | Description | Type | Options | Default |
|---|---|---|---|---|
| src | Image address | string | — | — |
| alt | Alternative text (required at the TS level for accessibility) | string | — | — |
| fit | Fit mode (native object-fit) | 'contain' | 'cover' | 'fill' | 'none' | 'scale-down' | — | 'cover' |
| preview | Whether clicking opens the preview | boolean | — | false |
| previewSrcList | Preview image list (falls back to [src] when absent) | string[] | — | — |
| lazy | Native lazy loading (loads when entering the viewport) | boolean | — | false |
| error | Load-failure placeholder content (maps to plus's #error slot) | ReactNode | — | Locale text "Failed to load" |
| sx | Layout shorthand | SxProps | — | — |
LhImageViewer Props
The fullscreen image viewer is exported independently: it opens when rendered (isomorphic to plus's teleport + v-if); visibility is driven by the parent.
| Prop | Description | Type | Options | Default |
|---|---|---|---|---|
| images | Preview image list | string[] | — | — |
| initialIndex | Initial image index | number | — | 0 |
| sx | Layout shorthand (applied to the preview image) | SxProps | — | — |
LhImage events
| Event | Description | Callback signature | Vue equivalent |
|---|---|---|---|
| onLoad | Fired when the image loads successfully | (event: SyntheticEvent<HTMLImageElement>) => void | @load |
| onError | Fired when the image fails to load (switches to the placeholder) | (event: SyntheticEvent<HTMLImageElement>) => void | @error |
LhImageViewer events
| Event | Description | Callback signature | Vue equivalent |
|---|---|---|---|
| onClose | Fired when the preview closes (close button / Esc) | () => void | @close |
| onSwitch | Fired when the image switches (arrow keys included) | (index: number) => void | @switch |
When the image fails to load, it is replaced entirely by the failure placeholder (error prop first, falling back to the locale text "Failed to load"); the failure flag resets automatically when src changes, so a new source retries the image branch (plus has no such reset; the React version aligns with the avatar reset discipline).
src / alt / fit / preview / previewSrcList / lazy / sx match lh-ui-plus's LhImage props; React has no slots, so plus's #error fallback slot maps to the error prop, and the load / error events map to onLoad / onError. The LhImageViewer is isomorphic (teleport → createPortal); the React version holds no internal visibility state and opens when rendered — control it with conditional rendering, and convert plus's "button-wrapped opener" pattern to state-driven rendering.
alt is required at the TS level, ruling out images without alternative text; the viewer outputs role="dialog" + aria-modal="true", a focus trap, Esc to close and arrow keys to switch, with every toolbar button named (zoom in / zoom out / rotate / previous / next / close); the zoom and rotate transitions respect prefers-reduced-motion.
Styles
import '@lh-ui/react/style/image'