Skip to main content

Image

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

Basic usage​

Basic usage
Sample imageImage failed to load
fit=fillfit=containfit=coverfit=nonefit=scale-down
Previewable image
Lazy-loaded image 1Lazy-loaded image 2Lazy-loaded image 3Lazy-loaded image 4
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​

PropDescriptionTypeOptionsDefault
srcImage addressstring——
altAlternative text (required at the TS level for accessibility)string——
fitFit mode (native object-fit)'contain' | 'cover' | 'fill' | 'none' | 'scale-down'—'cover'
previewWhether clicking opens the previewboolean—false
previewSrcListPreview image list (falls back to [src] when absent)string[]——
lazyNative lazy loading (loads when entering the viewport)boolean—false
errorLoad-failure placeholder content (maps to plus's #error slot)ReactNode—Locale text "Failed to load"
sxLayout shorthandSxProps——

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.

PropDescriptionTypeOptionsDefault
imagesPreview image liststring[]——
initialIndexInitial image indexnumber—0
sxLayout shorthand (applied to the preview image)SxProps——

LhImage events​

EventDescriptionCallback signatureVue equivalent
onLoadFired when the image loads successfully(event: SyntheticEvent<HTMLImageElement>) => void@load
onErrorFired when the image fails to load (switches to the placeholder)(event: SyntheticEvent<HTMLImageElement>) => void@error

LhImageViewer events​

EventDescriptionCallback signatureVue equivalent
onCloseFired when the preview closes (close button / Esc)() => void@close
onSwitchFired when the image switches (arrow keys included)(index: number) => void@switch
Failure placeholder and reset

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).

Vue equivalent

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.

Accessibility

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'