ImagePreview
Full-screen image previewer (createPortal to document.body): an overlay
(lh-image-preview__overlay), a gesture scope (lh-image-preview__wrapper)
carrying the image (lh-image-preview__image, transform: translate() scale()),
an index badge (lh-image-preview__index), a close button
(lh-image-preview__close) and desktop prev/next nav buttons
(lh-image-preview__nav--prev / --next). All index math, clamping and
masks are consumed from @lh-ui/share/core/image-preview pure functions, so
the semantics are identical across all platforms.
- Visibility is driven by
modelValue:>= 0is the open image index,-1(or omitted) means closed.onIndexChangefires on any index change (paging, and on open from closed); every close path (overlay tap,Esc, close button, imperativeclose()) firesonClose. - The imperative handle (
LhImagePreviewRef) exposesopen(index?)(defaults to0, normalized viaresolveImagePreviewIndex) andclose(), so it works in uncontrolled mode without anymodelValueplumbing. - Gestures (web self-drawn form): drag to pan while
scale > 1(clamped byclampPanon release), horizontal swipe to page whilescale === 1(50px threshold), wheel zoom (clampScale, bounded byminZoom/maxZoom), two-finger pinch (pointer-distance ratio), and keyboard navigation (←/→page,Esccloses) once the preview is focused.
Basic usage
ImagePreview (imperative ref / controlled modelValue / non-loop)
import { useRef } from 'react'
import { LhImagePreview, type LhImagePreviewRef } from '@lh-ui/react'
const IMAGES = ['ink-1.jpg', 'ink-2.jpg', 'ink-3.jpg']
export default function Demo() {
const ref = useRef<LhImagePreviewRef>(null)
return (
<>
<button type="button" onClick={() => ref.current?.open(0)}>
打开预览
</button>
<LhImagePreview ref={ref} images={IMAGES} />
</>
)
}
Controlled modelValue
const [index, setIndex] = useState(-1)
<LhImagePreview
images={IMAGES}
modelValue={index}
onIndexChange={setIndex}
onClose={() => setIndex(-1)}
/>
ImagePreview API
| Prop | Description | Type | Default |
|---|---|---|---|
| images | Image url list (empty list never opens) | string[] | [] |
| modelValue | Current index (-1 / omitted = closed) | number | -1 |
| loop | Loop paging | boolean | true |
| showIndex | Show the index badge | boolean | true |
| swipeDuration | Swipe / pan transition duration in ms | number | 300 |
| closeOnPressOverlay | Close when the overlay is clicked | boolean | true |
| showClose | Show the desktop close button | boolean | true |
| minZoom | Minimum zoom | number | 1 |
| maxZoom | Maximum zoom | number | 3 |
| label | Accessible name (aria-label; falls back to 图片预览 第 i / N 张) | string | — |
| sx | Quick layout props | LhSxProps | — |
| onIndexChange | Fires on any index change (paging, open) | (index: number) => void | — |
| onClose | Fires on any close path (overlay / Esc / close button / close()) | () => void | — |
Imperative handle
export interface LhImagePreviewRef {
/** Open at index (defaults to 0, normalized by resolveImagePreviewIndex) */
open: (index?: number) => void
/** Close (sets index to -1 and fires onClose) */
close: () => void
}
Accessibility
- The portal root carries
role="dialog"andaria-modal="true", witharia-labelfromgetImagePreviewAria(customlabelwins, otherwise图片预览 第 i / N 张); it is focused on open so keyboard arrows andEscland on the preview. - Nav buttons and the close button carry
aria-labels (image.prev/image.next/image.closelocale keys), and are hidden when the target index is-1(boundary of a non-loop list, or a single image).