---
title: "useImageViewer()"
description: "Open, control and observe the viewer from code."
canonical_url: "https://kukson777.github.io/nuxt-image-viewer/docs/api/use-image-viewer"
---
# useImageViewer()

> Open, control and observe the viewer from code.

```ts
const viewer = useImageViewer()
```

Auto-imported. Returns the same object everywhere in the app — the viewer is a single instance.

## Methods

<field-group>
<field name="open(items, index?, options?)" type="Promise<void>">

Opens the viewer. `items` is an [`ImageViewerItem`](https://kukson777.github.io/nuxt-image-viewer/docs/api/types#imagevieweritem) or an array of them, `index` the image to start from (`0` by default), `options` [overrides](https://kukson777.github.io/nuxt-image-viewer/docs/api/types#imagevieweropenoptions) of the global options for this viewer only. Resolves once the viewer is mounted. Ignored while a viewer is already open.

</field>

<field name="configure(options)" type="void">

Sets [options](https://kukson777.github.io/nuxt-image-viewer/docs/api/types#imagevieweropenoptions) 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](https://kukson777.github.io/nuxt-image-viewer/docs/customization/custom-buttons), which can't be in `nuxt.config`. Call it in a client plugin.

</field>

<field name="close()" type="void">

Closes with the animation back into the page.

</field>

<field name="next()" type="void">

Scrolls to the next image.

</field>

<field name="prev()" type="void">

Scrolls to the previous image.

</field>

<field name="goTo(index)" type="void">

Scrolls to an image by its index, taking the shortest way round the endless strip.

</field>
</field-group>

## State

<field-group>
<field name="isOpen" type="Readonly<Ref<boolean>>">

`true` from `open()` until the closing animation starts.

</field>

<field name="index" type="Readonly<Ref<number>>">

Index of the current image.

</field>

<field name="items" type="Readonly<Ref<readonly ImageViewerSlide[]>>">

Images of the open viewer, with everything resolved: `thumb`, `width`, `height` and `alt` are always set.

</field>
</field-group>

## Example

```vue
<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.


## Sitemap

See the full [sitemap](https://kukson777.github.io/nuxt-image-viewer/sitemap.md) for all pages.
