API
useImageViewer()
Open, control and observe the viewer from code.
const viewer = useImageViewer()
Auto-imported. Returns the same object everywhere in the app — the viewer is a single instance.
Methods
open(items, index?, options?)
Promise<void>
Opens the viewer.
items is an ImageViewerItem or an array of them, index the image to start from (0 by default), optionsoverrides of the global options for this viewer only. Resolves once the viewer is mounted. Ignored while a viewer is already open.configure(options)
void
Sets options for every viewer from then on, on top of
nuxt.config — including the ones opened by a click on an image. The place for custom buttons, which can't be in nuxt.config. Call it in a client plugin.close()
void
Closes with the animation back into the page.
next()
void
Scrolls to the next image.
prev()
void
Scrolls to the previous image.
goTo(index)
void
Scrolls to an image by its index, taking the shortest way round the endless strip.
State
isOpen
Readonly<Ref<boolean>>
true from open() until the closing animation starts.index
Readonly<Ref<number>>
Index of the current image.
items
Readonly<Ref<readonly ImageViewerSlide[]>>
Images of the open viewer, with everything resolved:
thumb, width, height and alt are always set.Example
<script setup lang="ts">
import type { ImageViewerItem } from 'nuxt-image-viewer'
const photos: ImageViewerItem[] = [
{ src: '/photos/valley.jpg', width: 3000, height: 1500, caption: 'Valley' },
{ src: '/photos/ocean.jpg', width: 2400, height: 1600, caption: 'Ocean' },
]
const { open, isOpen, index } = useImageViewer()
</script>
<template>
<button type="button" @click="open(photos)">
Open gallery
</button>
<p v-if="isOpen">
Looking at {{ photos[index]?.caption }}
</p>
</template>
On the server
There is no viewer on the server. useImageViewer() returns a stub with isOpen always false and methods that do nothing, so calling it in setup is safe in SSR.