跳到主要内容

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: >= 0 is the open image index, -1 (or omitted) means closed. onIndexChange fires on any index change (paging, and on open from closed); every close path (overlay tap, Esc, close button, imperative close()) fires onClose.
  • The imperative handle (LhImagePreviewRef) exposes open(index?) (defaults to 0, normalized via resolveImagePreviewIndex) and close(), so it works in uncontrolled mode without any modelValue plumbing.
  • Gestures (web self-drawn form): drag to pan while scale > 1 (clamped by clampPan on release), horizontal swipe to page while scale === 1 (50px threshold), wheel zoom (clampScale, bounded by minZoom / maxZoom), two-finger pinch (pointer-distance ratio), and keyboard navigation (← / → page, Esc closes) 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​

PropDescriptionTypeDefault
imagesImage url list (empty list never opens)string[][]
modelValueCurrent index (-1 / omitted = closed)number-1
loopLoop pagingbooleantrue
showIndexShow the index badgebooleantrue
swipeDurationSwipe / pan transition duration in msnumber300
closeOnPressOverlayClose when the overlay is clickedbooleantrue
showCloseShow the desktop close buttonbooleantrue
minZoomMinimum zoomnumber1
maxZoomMaximum zoomnumber3
labelAccessible name (aria-label; falls back to 图片预览 第 i / N 张)string—
sxQuick layout propsLhSxProps—
onIndexChangeFires on any index change (paging, open)(index: number) => void—
onCloseFires 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" and aria-modal="true", with aria-label from getImagePreviewAria (custom label wins, otherwise 图片预览 第 i / N 张); it is focused on open so keyboard arrows and Esc land on the preview.
  • Nav buttons and the close button carry aria-labels (image.prev / image.next / image.close locale keys), and are hidden when the target index is -1 (boundary of a non-loop list, or a single image).