Skip to content

API ​

Suitable for scenarios such as opening a specified image array after a button click.

Playground ​

Playground

vue
<script setup>
import { ref } from 'vue'
import { useViewer } from 'v-viewer'
const sourceImages = [
  '/images/photo-01.jpg',
  '/images/photo-02.jpg',
  '/images/photo-03.jpg',
  '/images/photo-04.jpg',
  '/images/photo-05.jpg',
  '/images/photo-06.jpg',
  '/images/photo-07.jpg',
  '/images/photo-08.jpg',
  '/images/photo-09.jpg',
  '/images/photo-10.jpg',
].map((original) => ({
  src: original.replace('/images/', '/images/thumbnails/'),
  'data-source': original,
}))
const images = ref(sourceImages.slice(0, 5))
const options = ref({ toolbar: true, url: 'data-source', initialViewIndex: 0 })
const { show, isShown } = useViewer({ images, options })
function add() {
  images.value.push(sourceImages[images.value.length])
}
function remove() {
  images.value.pop()
}
</script>

<template>
  <main class="viewer-example">
    <div class="controls">
      <button :disabled="images.length === sourceImages.length" @click="add">
        Add image
      </button>
      <button :disabled="images.length === 0" @click="remove">
        Remove image
      </button>
      <button class="open" :disabled="images.length === 0" @click="show()">
        Show viewer
      </button>
      <div class="toolbar" role="group" aria-label="Toggle toolbar">
        <span>Toggle toolbar</span>
        <button
          :aria-pressed="options.toolbar ? 'true' : 'false'"
          @click="options.toolbar = true"
        >
          On
        </button>
        <button
          :aria-pressed="options.toolbar ? 'false' : 'true'"
          @click="options.toolbar = false"
        >
          Off
        </button>
      </div>
    </div>
    <p>Images: {{ images.length }}</p>
    <p>{{ isShown ? 'Gallery open' : 'Ready to explore' }}</p>
  </main>
</template>

url: 'data-source' uses the image’s data-source attribute as the preview URL; initialViewIndex sets the starting image index (from 0).

Parameters and return values ​

useViewer({ images?, options? })

ParameterDescription
imagesImage array, ref or getter for temporary galleries.
optionsViewer.js options, ref or getter.
Return valueDescription
viewerShallowRef<ViewerJs | undefined>
isShownReactive modal visibility.
show(index?)Open at an optional image index.
hide()Close normally with the configured exit transition and hide / hidden callbacks; destroy the instance and remove temporary DOM after closing.
destroy()Immediately destroy the instance and remove temporary DOM, without an exit transition or hide / hidden callbacks; show() can recreate it.

Call useViewer inside setup. It cleans up on scope disposal.

Closing or destroying the preview removes its temporary DOM. Changes to images or options during a preview apply the next time it opens. Calling show(index) again switches images within the current gallery.

Use viewer.value to call all Viewer.js methods. Viewer.js methods

Options ​

Pass these settings through options in useViewer({ images, options }). The API always opens in modal mode. inline is fixed to false and cannot be overridden.

These match Viewer.js options. Each name links to its original documentation; available settings and defaults depend on the installed Viewer.js version.

PropTypePurpose
backdropboolean / stringControl the modal backdrop; static keeps clicks on it from closing the viewer.
buttonbooleanShow the button in the upper-right corner.
classNamestringAdd custom classes to the viewer root.
containerstring / ElementChoose the modal container using an element or CSS selector.
filterfunctionInclude images for which the filter returns true; exclude the others.
fullscreenboolean / FullscreenOptionsRequest browser fullscreen during playback, optionally with Fullscreen API settings.
focusbooleanFocus the active thumbnail on initialization; requires keyboard.
inheritedAttributesarrayChoose additional source-image attributes to copy; src and alt are always copied.
initialCoveragenumberSet the initial image coverage, greater than 0 and at most 1.
initialViewIndexnumberChoose the starting image index, also used when view() receives no index.
autoplaybooleanAdvance slides automatically during playback.
intervalnumberSet the delay between slides in milliseconds.
keyboardbooleanAllow keyboard controls.
loadingbooleanDisplay a loading indicator while fetching an image.
loopbooleanWrap image navigation between the first and last images.
magnifierboolean / objectShow a hover magnifier in fullscreen view, with configurable size, ratio and opacity; unavailable on touch screens.
maxZoomRationumber / functionLimit magnification with a ratio or a function of the image and its data.
minZoomRationumber / functionLimit reduction with a ratio or a function of the image and its data.
movablebooleanAllow image movement.
navbarboolean / number / string / objectConfigure thumbnail visibility, size and position, with further layout settings in object form.
navigationboolean / number / objectConfigure the side navigation buttons, including individual visibility and size.
preloadbooleanLoad adjacent images before they are viewed.
rotatablebooleanAllow image rotation.
rotateOnGesturebooleanRotate through gesture events, such as two-finger gestures in Mac Safari.
rotateOnTouchbooleanRotate with two fingers on a touch screen.
scalablebooleanAllow axis scaling, including horizontal and vertical flips.
slideOnTouchbooleanNavigate between images with touch swipes.
slideOnWheelboolean / stringNavigate with the wheel, optionally requiring modifier keys; applies over thumbnails or when wheel zoom does not handle the event.
titleboolean / number / function / arrayConfigure title visibility and content; the default uses alt or the filename from the URL.
toggleOnDblclickbooleanSwitch between natural and initial image sizes on double-click.
toolbarboolean / number / objectConfigure toolbar placement and button visibility, size and click behavior.
tooltipbooleanDisplay the current zoom percentage while zooming.
transitionboolean / objectEnable CSS transitions, or disable selected action transitions using an object.
urlstring / functionRead the full image URL from a named image attribute or a function result.
zIndexnumberSet the modal viewer’s CSS stacking order.
zoomOnGesturebooleanZoom through gesture events, such as pinch gestures in Mac Safari.
zoomOnTouchbooleanZoom through dragging gestures on a touch screen.
zoomOnWheelboolean / stringZoom with the wheel, optionally requiring one or more modifier keys.
zoomRationumberSet the ratio increment for wheel zoom.
zoomablebooleanAllow image zooming.

Callbacks ​

Set callback functions such as viewed(event) in options. They correspond to Viewer.js events and receive the original DOM event, with operation data in event.detail. Click a name for its original documentation.

NameWhen it fires / useful data
readyThe viewer UI is initialized. In modal mode this can happen on the first opening.
show / shownBefore opening / after the opening transition completes in modal mode.
hide / hiddenBefore closing / after the closing transition completes in modal mode.
view / viewedBefore displaying an image / after it is displayed. detail.index is zero-based; image and originalImage identify the preview and source images.
move / movedBefore / after moving. detail.x, y, oldX and oldY describe the position change.
rotate / rotatedBefore / after rotation. detail.degree and oldDegree describe the angle change.
scale / scaledBefore / after scaling or flipping. detail.scaleX, scaleY, oldScaleX and oldScaleY describe the change.
zoom / zoomedBefore / after zooming. detail.ratio and oldRatio contain the zoom ratios.
play / stopBefore starting / stopping a slideshow.
playingBefore showing each slide during playback.

Released under the MIT License.