ImagePreview
A full-screen image preview component (a self-drawn form on web): the root node is a Teleport-ed shadowed overlay on body; the index is controlled through v-model (-1 / not passed = closed). It supports looped paging, an index indicator, wheel zoom, dragging to pan while zoomed in, horizontal swipe paging at the natural size and keyboard operation. Index math, zoom clamping, pan boundaries, text and accessibility labels are all computed by the pure logic in @lh-ui/share/core/image-preview (resolveImagePreviewIndex / clampScale / clampPan / getImagePreviewIndexText); this component only writes reactive glue and gesture bindings.
Boundary with the built-in viewer of the Image component: Image's preview + preview-src-list is a lightweight embedded "tap the image to view" form (toolbar buttons for zoom / rotation, no v-model index); ImagePreview is a standalone controlled preview layer that owns the image list and the current index itself, suited for list / masonry scenarios where "the images are not rendered on the page". The two do not depend on each other — pick either per scenario, or replace Image's preview entry with ImagePreview (taking over @click and images yourself).
Basic Usage
Two driving forms: bind the index with v-model (external state controls open/close), or grab a ref and call open() / close() (an internal state serves as fallback, also usable when unbound).
<script setup lang="ts">
import { ref } from 'vue'
import { LhImagePreview } from '@lh-ui/vue3'
// -1 / not passed = closed; >= 0 is the current index
const index = ref(-1)
const images = [
'https://picsum.photos/900/600?random=1',
'https://picsum.photos/900/600?random=2',
'https://picsum.photos/900/600?random=3',
]
</script>
<template>
<LhButton size="small" @click="index = 0">Open the first image</LhButton>
<LhImagePreview v-model="index" :images="images" />
</template><script setup lang="ts">
import { ref } from 'vue'
import { LhImagePreview } from '@lh-ui/vue3'
const preview = ref<InstanceType<typeof LhImagePreview>>()
const images = [
'https://picsum.photos/900/600?random=1',
'https://picsum.photos/900/600?random=2',
'https://picsum.photos/900/600?random=3',
]
</script>
<template>
<LhButton size="small" @click="preview?.open(0)">Method open(0)</LhButton>
<LhImagePreview ref="preview" :images="images" />
</template>Gestures and keyboard
- Wheel zoom: scrolling the wheel over the image zooms directly (no Ctrl needed), within
minZoom–maxZoom(1–3 by default), clamped byclampScale - Drag to pan: when
scale > 1, press and drag to pan the image; the boundary is guarded byclampPan(no dark edges are exposed); on release it springs back overswipeDuration - Horizontal swipe paging: when
scale == 1, a left/right swipe beyond 50px switches to the previous / next image; on narrow screens (≤768px) the desktop paging buttons hide automatically and gestures take over - Keyboard: while open,
←/→turn pages andEscapecloses (window-level listener, operable even when focus is not inside the component) - Tap-to-close on the overlay can be turned off with
closeOnPressOverlay
<LhImagePreview v-model="index" :images="images" :loop="false" :max-zoom="5" />API
ImagePreview Props
| Prop | Description | Type | Default |
|---|---|---|---|
| images | Image URL list (empty values are filtered out automatically) | string[] | — |
| modelValue | Current index (controlled; -1 or not passed = closed) | number | — |
| loop | Loop paging (true by default) | boolean | true |
| showIndex | Show the index indicator (true by default) | boolean | true |
| swipeDuration | Swipe animation duration in ms (300 by default) | number | 300 |
| closeOnPressOverlay | Close on overlay tap (true by default) | boolean | true |
| showClose | Show the close button on desktop (true by default) | boolean | true |
| minZoom | Zoom lower bound (1 by default) | number | 1 |
| maxZoom | Zoom upper bound (3 by default) | number | 3 |
| label | Accessible name ("Image preview, image i of N" by default) | string | — |
ImagePreview Events
| Event | Description | Callback |
|---|---|---|
| update:modelValue | Current index write-back (including the closed state -1) | (index: number) |
| change | Fired when the user switches the index by interaction (open / previous / next / swipe / keyboard) | (index: number) |
| close | Fired on close | — |
ImagePreview Methods
The methods exposed by the component can be called via ref:
| Method | Description | Parameters |
|---|---|---|
| open | Open the preview (the index is normalized by core: loop wraps around with modulo, non-loop clamps to the boundary; 0 by default) | (index?: number) |
| close | Close the preview (sets the index to -1 and emits close) | — |
Accessibility
- The root node is
role="dialog"+aria-modal="true"; its accessible name defaults to "Image preview, image i of N" and can be overridden withlabel - The close and paging buttons all carry
aria-label(close / previous / next) - Keyboard operable:
←/→turn pages,Escapecloses — no need to focus inside the component - No images are rendered while closed (
v-if); no leftover media nodes remain in the DOM

