Skip to content

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.previewImage instead — 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 sets modelValue = -1 and emits close; 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:

vue
<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":

vue
<lh-image-preview
  ref="previewRef"
  :images="images"
  v-model="current"
/>
ts
// 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:

vue
<view
  v-for="(src, i) in images"
  :key="i"
  @click="previewRef?.open(i)"
>
  <image :src="src" mode="aspectFill" />
</view>

Props ​

PropDescriptionTypeDefault
imagesImage URL list (empty values filtered out)string[]—
modelValueCurrent index (controlled; -1 or unset = closed)number-1
loopLoop paging (clamps at both ends when false)booleantrue
showIndexShow index indicator (carried by the native preview; registered as ineffective)booleantrue
swipeDurationSwipe animation duration ms (registered as ineffective)number300
closeOnPressOverlayClose on overlay tap (registered as ineffective)booleantrue
showCloseShow the close button on desktop (registered as ineffective)booleantrue
minZoomPinch zoom lower bound (consumed by the native preview; registered as ineffective)number1
maxZoomPinch zoom upper bound (consumed by the native preview; registered as ineffective)number3
labelAccessibility name (defaults to "Image preview i of N")string—
customClassCustom class (forwarded to the root node)string—
sxsx quick layout propsLhSxProps—

Methods (defineExpose) ​

MethodDescriptionArguments
openOpen the preview: normalizes the index via resolveImagePreviewIndex, calls uni.previewImage, writes back modelValue and emits change on successindex?: number (default 0)
closeClose the preview: no native close API; sets modelValue = -1 and emits close—

Events ​

EventDescriptionCallback args
update:modelValueIndex write-back (v-model)value: number
changeIndex change (raised on open / controlled switch)index: number
closeClose (raised by close or an external -1 write)—

Scoped slot ​

The default slot acts as the trigger wrapper with these scope params:

ParamDescriptionType
indexCurrent index (-1 = closed)number
totalTotal number of imagesnumber
indexTextIndex text (e.g. 1 / 5; empty string for an empty list)string
openOpen method (forwarded so the trigger can call it directly)(index?: number) => void
closeClose 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.previewImage native 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 sets modelValue = -1 and emits close; 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 — modelValue is not written back and change is not emitted.
  • Empty-list guard: when images is empty (or all empty values), open() silently skips the native API call and emits nothing.
  • Style product: the lh-image-preview rules 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.previewImage API (see "Platform differences").
  • Gestures (pinch zoom / swipe paging / wheel zoom) are fully carried by native capabilities and are not implemented here.
  • Adds the customClass prop: custom class names cannot pierce styleIsolation on uni-app, so they are passed via a prop.