ImagePreview
Image preview component: a native API thin shell — all Mini Program platforms and H5 route through uni.previewImage (pinch zoom and swiping come free from the native preview page). The component itself renders no overlay; it only renders the default slot as a trigger wrapper, while open(index) / close() are the imperative switches. The API surface stays isomorphic with the plus self-drawn form. Serves product-detail zoomed images, album browsing and similar scenarios.
Platform differences (registered): this port is a uni.previewImage thin shell. In plus, the preview is a self-drawn viewer overlay (teleported to body with gesture zoom / swipe paging / wheel zoom); Mini Programs have no document or teleport equivalent, so this port does not self-draw an overlay and calls
uni.previewImageinstead — H5 gets a runtime-implemented layer, WeChat gets the native preview page. The component renders only the trigger wrapper; index math (loop normalization / clamping), open/close semantics and the accessibility label all reuse share-core pure functions. There is no native close API:close()only setsmodelValue = -1and emitsclose; the native preview page's own close action gives no callback, so the controlled state must be settled by the consumer.
Basic usage
The component wraps the trigger content (default slot); call open() via ref to raise the native preview:
<template>
<lh-image-preview
ref="previewRef"
:images="images"
v-model="current"
>
<button @click="previewRef?.open()">Preview</button>
</lh-image-preview>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
const previewRef = ref<{ open: (index?: number) => void; close: () => void } | null>(null)
const current = ref(-1)
const images = [
'https://example.com/1.png',
'https://example.com/2.png',
'https://example.com/3.png',
]
</script>open(index) opens image 0 by default; the index is first normalized by resolveImagePreviewIndex (loop wraps with modulo; non-loop clamps to the bounds), and only on a successful raise does it write back modelValue and emit change.
Starting index and controlled open/close
v-model binds the current index (-1 = closed); writing a valid index to modelValue from outside is treated as "open" (raises the native preview), while -1 is treated as "close":
<lh-image-preview
ref="previewRef"
:images="images"
v-model="current"
/>// Open the 3rd image (index 2)
previewRef.value?.open(2)
// Equivalent controlled write
current.value = 2
// Close: sets -1 and emits close (no native close API — registered difference)
previewRef.value?.close()List scenario
Tapping a thumbnail to preview its image is the classic product-detail / album usage:
<view
v-for="(src, i) in images"
:key="i"
@click="previewRef?.open(i)"
>
<image :src="src" mode="aspectFill" />
</view>Props
| Prop | Description | Type | Default |
|---|---|---|---|
| images | Image URL list (empty values filtered out) | string[] | — |
| modelValue | Current index (controlled; -1 or unset = closed) | number | -1 |
| loop | Loop paging (clamps at both ends when false) | boolean | true |
| showIndex | Show index indicator (carried by the native preview; registered as ineffective) | boolean | true |
| swipeDuration | Swipe animation duration ms (registered as ineffective) | number | 300 |
| closeOnPressOverlay | Close on overlay tap (registered as ineffective) | boolean | true |
| showClose | Show the close button on desktop (registered as ineffective) | boolean | true |
| minZoom | Pinch zoom lower bound (consumed by the native preview; registered as ineffective) | number | 1 |
| maxZoom | Pinch zoom upper bound (consumed by the native preview; registered as ineffective) | number | 3 |
| label | Accessibility name (defaults to "Image preview i of N") | string | — |
| customClass | Custom class (forwarded to the root node) | string | — |
| sx | sx quick layout props | LhSxProps | — |
Methods (defineExpose)
| Method | Description | Arguments |
|---|---|---|
| open | Open the preview: normalizes the index via resolveImagePreviewIndex, calls uni.previewImage, writes back modelValue and emits change on success | index?: number (default 0) |
| close | Close the preview: no native close API; sets modelValue = -1 and emits close | — |
Events
| Event | Description | Callback args |
|---|---|---|
| update:modelValue | Index write-back (v-model) | value: number |
| change | Index change (raised on open / controlled switch) | index: number |
| close | Close (raised by close or an external -1 write) | — |
Scoped slot
The default slot acts as the trigger wrapper with these scope params:
| Param | Description | Type |
|---|---|---|
| index | Current index (-1 = closed) | number |
| total | Total number of images | number |
| indexText | Index text (e.g. 1 / 5; empty string for an empty list) | string |
| open | Open method (forwarded so the trigger can call it directly) | (index?: number) => void |
| close | Close method | () => void |
Accessibility
The root node carries role="dialog" + aria-modal="true"; aria-label defaults to the share-core "Image preview i of N" string and can be customized with label.
Platform differences
- Downgraded to a
uni.previewImagenative thin shell (available on all platforms): zoom and swiping are provided by the native preview page, so this port implements no gestures; there is no customization for overlay opacity, close button, paging buttons, index indicator or swipe duration — the matching props exist only for API-surface parity (registered as ineffective). - No native close API:
close()only setsmodelValue = -1and emitsclose; the native preview page's close (tap on blank area / back key) gives no callback, so the controlled state is settled by the consumer. - Raise-failure fallback:
open()wraps the call in try/catch; in extreme environments (e.g. no uni object injected on H5) the error is logged as a warn and skipped —modelValueis not written back andchangeis not emitted. - Empty-list guard: when
imagesis empty (or all empty values),open()silently skips the native API call and emits nothing. - Style product: the
lh-image-previewrules in the share style source target the web self-drawn form (overlay / paging buttons); the thin shell has no matching nodes, so this component does not consume a style.css product.
Differences from the Vue3 version
- Logic semantics (index normalization, visibility, accessibility label) are identical to plus (same share core).
- The view changed from a self-drawn viewer overlay to the native
uni.previewImageAPI (see "Platform differences"). - Gestures (pinch zoom / swipe paging / wheel zoom) are fully carried by native capabilities and are not implemented here.
- Adds the
customClassprop: custom class names cannot pierce styleIsolation on uni-app, so they are passed via a prop.

