Guide
Open from code
useImageViewer() opens any list of images, with or without thumbnails on the page.
useImageViewer() is auto-imported. It opens the viewer with your own list of images, scrolls it and tells you where it is.
const viewer = useImageViewer()
await viewer.open(
[
{ src: '/photos/valley.jpg', width: 3000, height: 1500, caption: 'Valley' },
{ src: '/photos/ocean.jpg', width: 2400, height: 1600, caption: 'Ocean' },
],
1, // start index
{ loop: false, bgOpacity: 1 }, // options for this viewer only
)
isOpen: false index: 0
Items
Only src is required.
src
string required
The full image.
width / height
number
Natural size. Without it the image is loaded and measured first, so the viewer opens a moment later — pass it when you know it.
thumb
string
A copy shown at once while
src loads. Defaults to element.currentSrc, otherwise src.element
HTMLElement
The page element the image opens from and closes back into. Without it the viewer fades in and out.
alt
string
Alternative text. Defaults to the
alt of element.caption
string
Caption under the image.
Flying from your own thumbnails
Pass the elements and the viewer animates from them exactly like it does for <ZoomImage>:
<script setup lang="ts">
const viewer = useImageViewer()
const thumbs = useTemplateRef<HTMLImageElement[]>('thumbs')
function open(index: number) {
viewer.open(photos.map((photo, i) => ({ ...photo, element: thumbs.value?.[i] })), index)
}
</script>
<template>
<button v-for="(photo, i) in photos" :key="photo.src" @click="open(i)">
<img ref="thumbs" :src="photo.thumb" :alt="photo.caption">
</button>
</template>
Controlling an open viewer
const { isOpen, index, items, next, prev, goTo, close } = useImageViewer()
watch(index, i => history.replaceState(null, '', `#photo-${i}`))
goTo(3) // takes the shortest way round the endless strip
close() // with the animation back into the page
See the full API reference.
The viewer exists only in the browser. On the server
useImageViewer() returns a harmless stub: isOpen is false and the methods do nothing, so it's safe to call in setup.