# ImagePreview

An image preview overlay: a full-screen mask plus image browsing, with an index indicator, desktop prev / next and close buttons, keyboard ←/→ navigation and Esc to close, wheel zoom, and drag panning with a horizontal swipe to flip pages. Index conversion, zoom clamping, and drag bounds all live in the pure logic of @lh-ui/share/core/image-preview; this component only provides the reactive glue and gesture templates. Styles come from share styles/image-preview.

modelValue is the controlled current index (-1 or not passed = closed). Two-way binding uses the .sync convention:

<LhImagePreview :images="images" :model-value.sync="current" />
Image preview (controlled open / loop / index indicator / close on mask)
<template>
  <div class="demo-col" style="gap: 18px; max-width: 480px">
    <div class="demo-row" style="gap: 8px">
      <LhButton size="small" @click="open(0)">Open the 1st image</LhButton>
      <LhButton size="small" @click="open(2)">Open the 3rd image</LhButton>
      <LhButton size="small" @click="open(99)">Out-of-range index is normalized</LhButton>
    </div>
    <div class="demo-row" style="gap: 8px">
      <LhButton size="small" @click="loop = !loop">Toggle loop (now {{ loop }})</LhButton>
      <LhButton size="small" @click="showIndex = !showIndex">Index indicator ({{ showIndex }})</LhButton>
      <LhButton size="small" @click="closeOverlay = !closeOverlay">Close on mask click ({{ closeOverlay }})</LhButton>
    </div>
    <LhImagePreview
      :images="images"
      :model-value.sync="current"
      :loop="loop"
      :show-index="showIndex"
      :close-on-press-overlay="closeOverlay"
      label="Ink gallery"
    />
    <span class="demo-note">current = {{ current }} (-1 = closed); ←/→ to flip, Esc to close, wheel to zoom</span>
  </div>
</template>

<script>
export default {
  data: () => ({
    current: -1,
    loop: true,
    showIndex: true,
    closeOverlay: true,
    images: [
      'data:image/svg+xml,%3Csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20width=%27640%27%20height=%27420%27%3E%3Crect%20width=%27640%27%20height=%27420%27%20fill=%27%23e8e2d5%27/%3E%3Ctext%20x=%2750%%27%20y=%2750%%27%20font-size=%2740%27%20fill=%27%232b4c7c%27%20text-anchor=%27middle%27%20font-family=%27serif%27%3E%E6%BA%AA%E5%B1%B1%3C/text%3E%3C/svg%3E',
      'data:image/svg+xml,%3Csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20width=%27640%27%20height=%27420%27%3E%3Crect%20width=%27640%27%20height=%27420%27%20fill=%27%23d8d0bd%27/%3E%3Ctext%20x=%2750%%27%20y=%2750%%27%20font-size=%2740%27%20fill=%27%23a63d40%27%20text-anchor=%27middle%27%20font-family=%27serif%27%3E%E6%99%B4%E5%B2%9A%3C/text%3E%3C/svg%3E',
      'data:image/svg+xml,%3Csvg%20xmlns=%27http://www.w3.org/2000/svg%27%20width=%27640%27%20height=%27420%27%3E%3Crect%20width=%27640%27%20height=%27420%27%20fill=%27%23cfc6b0%27/%3E%3Ctext%20x=%2750%%27%20y=%2750%%27%20font-size=%2740%27%20fill=%27%233a7d5a%27%20text-anchor=%27middle%27%20font-family=%27serif%27%3E%E6%9D%BE%E9%A3%8E%3C/text%3E%3C/svg%3E',
    ],
  }),
  methods: {
    open(index) {
      this.current = index
    },
  },
}
</script>

# Controlled index

modelValue is the controlled index: -1 (or not passed) means closed, >= 0 means open. Flipping / swiping / open() all flow the normalized index back through update:modelValue; modifying modelValue directly from outside works as well (out-of-range indices are normalized by resolveImagePreviewIndex: modulo wrap-around when loop is on, clamped to the bounds otherwise):

<LhImagePreview :images="images" :model-value="2" />

# Paging and loop

When loop (default true) is on, the first and last pages wrap around; when off, the desktop prev / next buttons are hidden at the boundaries (getPrevIndex / getNextIndex return -1, so nothing renders). On mobile (narrow screens) the paging buttons are hidden by styles and gesture paging takes over:

<LhImagePreview :images="images" :model-value.sync="current" :loop="false" />

# Gestures and keyboard

  • Horizontal swipe (at natural size, displacement over 50px): swipe right for the previous image, left for the next
  • Drag panning (when zoomed, scale > 1): clampPan clamps so no edge gap is exposed
  • Wheel zoom: steps in and out, clamped to [minZoom, maxZoom] (1~3 by default)
  • Keyboard: ← / → to flip pages, Esc to close (active while visible)
  • Mask click: closes when closeOnPressOverlay is true (default)

swipeDuration (300ms by default) controls the transition duration of paging / bounce-back.

# Methods

Called via ref (exposed by defineExpose):

Method Description
open(index?) Open the preview (the 0th image by default; the index is normalized)
close() Close the preview (sets -1 and emits close)
prev() Previous image (no-op at the boundary when not looping)
next() Next image (no-op at the boundary when not looping)
<LhImagePreview ref="preview" :images="images" :model-value.sync="current" />
this.$refs.preview.open(1)

# Props

Prop Description Type Default
images Image URL list (empty URLs are filtered out) string[] []
modelValue Current index (controlled; -1 / not passed = closed) number -
loop Loop paging boolean true
showIndex Show the index indicator (e.g. "1 / 5") boolean true
swipeDuration Swipe / paging transition duration in ms number 300
closeOnPressOverlay Close on mask click boolean true
showClose Show the close button on desktop boolean true
minZoom Zoom lower bound number 1
maxZoom Zoom upper bound number 3
label Accessibility name (falls back to the default message) string —

# Events

Event Description Callback args
update:modelValue Index change (.sync write-back, including -1 on close) value: number
change Preview index switch index: number
close Preview closed —

# Accessibility

The root element gets an aria-label injected by getImagePreviewAria (a custom label wins; otherwise it announces "Image preview, image i of N") and declares role="dialog" and aria-modal="true"; the prev / next and close buttons all carry aria-label.

Last Updated: 12/4/2026, 12:49:57 AM

Copyright © 2024-2026 FengMo WenXue