# Types > Every public type, importable from the package. ```ts import type { ImageViewerApi, ImageViewerButton, ImageViewerButtonIcon, ImageViewerControls, ImageViewerItem, ImageViewerOpenOptions, ImageViewerSlide, ImageViewerUi, ImageViewerLabels, ModuleOptions, } from 'nuxt-image-viewer' ``` All of them carry JSDoc, so your editor — and your coding agent — shows the docs inline. ## ImageViewerItem One image passed to `open()`. Only `src` is required. ```ts interface ImageViewerItem { /** Full-size image shown in the viewer. */ src: string /** Smaller copy already loaded on the page — shown at once while `src` loads. */ thumb?: string /** Natural size of `src`. Without it the image is measured before opening. */ width?: number height?: number alt?: string /** Text shown under the image. */ caption?: string /** Element on the page the image opens from and closes back into. */ element?: HTMLElement } ``` ## ImageViewerOpenOptions Overrides of the global options for one `open()` call. ```ts type ImageViewerOpenOptions = Partial<{ zIndex: number bgOpacity: number loop: boolean spacing: number maxZoom: number closeOnVerticalDrag: boolean closeOnBackdropClick: boolean idleDelay: number idleHidesCaption: boolean }> & { ui?: Partial labels?: Partial /** Your own components in place of the arrows. */ controls?: Partial /** Replace the buttons set with configure(). */ buttons?: ImageViewerButton[] } ``` ## ImageViewerButton A [custom button](https://kukson777.github.io/nuxt-image-viewer/docs/customization/custom-buttons) in the toolbar: an icon component, a text, or both. An icon-only button needs a `label`. ```ts type ImageViewerButton = { /** Unique name. Also the class modifier: .image-viewer__button--{name} */ name: string onClick: (context: { item: ImageViewerSlide, index: number, close: () => void }) => void } & ( | { icon: ImageViewerButtonIcon, text?: string, label: string } | { icon?: ImageViewerButtonIcon, text: string, label?: string } ) /** A Nuxt Icon name, as in , or any component. */ type ImageViewerButtonIcon = string | Component ``` ## ImageViewerControls Your [own arrows](https://kukson777.github.io/nuxt-image-viewer/docs/customization/custom-buttons#your-own-arrows) in place of the built-in ones — any components, they need no props: the viewer places them, scrolls on their click and names an icon-only button from the labels. ```ts interface ImageViewerControls { prev: Component next: Component } ``` ## ImageViewerUi ```ts interface ImageViewerUi { counter: boolean zoom: boolean fullscreen: boolean close: boolean arrows: boolean caption: boolean } ``` ## ImageViewerLabels ```ts interface ImageViewerLabels { dialog: string close: string zoomIn: string zoomOut: string fullscreen: string exitFullscreen: string prev: string next: string } ``` ## ImageViewerSlide An item with everything resolved — what `useImageViewer().items` holds. ```ts interface ImageViewerSlide { src: string thumb: string width: number height: number alt: string caption?: string element?: HTMLElement } ``` ## ImageViewerApi What `useImageViewer()` returns — see [useImageViewer()](https://kukson777.github.io/nuxt-image-viewer/docs/api/use-image-viewer). ## ModuleOptions The `imageViewer` key of `nuxt.config` — see [Options](https://kukson777.github.io/nuxt-image-viewer/docs/customization/options). # 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 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. 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. Closes with the animation back into the page. Scrolls to the next image. Scrolls to the previous image. Scrolls to an image by its index, taking the shortest way round the endless strip. ## State `true` from `open()` until the closing animation starts. Index of the current image. Images of the open viewer, with everything resolved: `thumb`, `width`, `height` and `alt` are always set. ## Example ```vue ``` ## 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. # Custom buttons > Add buttons of your own to the toolbar and put your own arrows in place. The toolbar takes buttons of your own — download, share, anything. They sit before the built-in ones, look the same and get the current image when clicked. The prev / next arrows can be replaced by your own components altogether. ## Add a button Wrap a gallery in `` and give it the buttons — they show up in the viewer opened by a click on any image inside: ```vue [components/Gallery.vue] ``` `` is a `data-image-viewer-group` with options of its own: it takes everything [`open()`](https://kukson777.github.io/nuxt-image-viewer/docs/customization/options) takes — buttons, arrows, `loop`, `ui`, labels — for this gallery only. Render it as another tag with `as="section"`. ### On the whole site, without groups Buttons the whole site needs go into a client plugin with `configure()` — then every viewer has them, no `` needed: ```ts [plugins/image-viewer.client.ts] // For every viewer on the site: a click on any , and open() from code export default defineNuxtPlugin(() => { useImageViewer().configure({ buttons: [ { name: 'download', label: 'Download', icon: 'i-lucide-download', onClick: ({ item }) => window.open(item.src), }, ], }) }) ``` They can't live in `nuxt.config`: buttons hold functions, and `nuxt.config` is serialized to JSON. ### From code When the viewer is opened with [`open()`](https://kukson777.github.io/nuxt-image-viewer/docs/guide/open-from-code), pass the buttons to it: ```ts // For this one viewer only, opened from code const viewer = useImageViewer() viewer.open(photos, 0, { buttons: [download] }) ``` The closer setting wins: `open()` and a group's options over `configure()`, `configure()` over `nuxt.config`. Buttons replace the ones from further away — `buttons: []` opens a viewer without them. ## Icon An icon name, as in ``, is drawn by [Nuxt Icon](https://nuxt.com/modules/icon) — Nuxt UI includes it. Any icon set of Iconify works: `i-lucide-download`, `i-heroicons-share`, `lucide:download`. Without Nuxt Icon, or for icons from elsewhere, pass a component — an icon library, your own SFC or a render function: ```vue [components/Gallery.vue] ``` The icon is sized by `--image-viewer-icon-size` and drawn by Nuxt Icon or the component itself, filled or stroked. ## Text With `text` the button becomes a pill with that text, which is also its accessible name. An icon can go next to it: ```vue [components/Gallery.vue] ``` ## Fields Unique name. Also the class modifier: `.image-viewer__button--download`. Nuxt Icon name or a component. Required without `text`. Text on the button. Required without `icon`. Accessible name. Required for an icon-only button; with `text` it defaults to the text. Called with `{ item, index, close }`: the current [`ImageViewerSlide`](https://kukson777.github.io/nuxt-image-viewer/docs/api/types#imageviewerslide), its index and a function that closes the viewer. TypeScript checks the combinations: a button without `icon` and `text`, or an icon-only one without `label`, doesn't compile. ## Your own arrows Replace the built-in prev / next arrows with your own buttons — a `UButton`, your own component, anything. They need nothing bound to them: the viewer puts them in the arrow's place, scrolls when they're clicked, hides them on touch screens and when the mouse rests, and gives an icon-only button its accessible name from [`labels`](https://kukson777.github.io/nuxt-image-viewer/docs/customization/options#labels). At the ends of a strip that isn't endless (`loop: false`, or fewer than three images) your arrow stays and looks disabled: its wrapper gets `aria-disabled="true"` — dimmed, a not-allowed cursor, no clicks, left out of the Tab round. Restyle it with `.image-viewer__arrow--own[aria-disabled="true"]`. ### In one gallery Write the arrows as `#prev` and `#next` slots of the group — no separate files, no imports: ```vue [components/Gallery.vue] ``` ### On the whole site, without groups Set the arrows once in a client plugin: every viewer gets them — a click on any ``, in a group or not, and `open()` from code. In a `.ts` file components come from `#components`: ```ts [plugins/image-viewer.client.ts] import { UButton } from '#components' // For every viewer on the site: no needed anywhere export default defineNuxtPlugin(() => { useImageViewer().configure({ controls: { prev: () => h(UButton, { icon: 'i-lucide-arrow-left', size: 'xl' }), next: () => h(UButton, { icon: 'i-lucide-arrow-right', size: 'xl' }), }, }) }) ``` An arrow here is any component: a render function as above, or your own SFC — `controls: { next: ViewerNext }`. ### From code ```ts // For this one viewer only, opened from code const viewer = useImageViewer() viewer.open(photos, 0, { controls: { next: () => h(UButton, { icon: 'i-lucide-arrow-right' }), }, }) ``` The closer setting wins, arrow by arrow: a gallery with its own `#next` keeps the `prev` from the plugin. ### Styling the place The arrow sits in a `.image-viewer__arrow` wrapper — `--prev` or `--next` — which positions it at the edge of the screen. Move it with your own CSS: ```css .image-viewer__arrow--next { top: auto; bottom: 24px; transform: none; } ``` ## Styling Custom buttons have the same classes as the built-in ones — `.image-viewer__button` and the [CSS variables](https://kukson777.github.io/nuxt-image-viewer/docs/customization/theming) — plus their own modifier; a button with a text also gets `--has-text`: ```css .image-viewer__button--share { --image-viewer-button-bg: var(--ui-primary); } ``` # Options > Global defaults in nuxt.config and overrides per open() call. Set the options under the `imageViewer` key. All of them are optional. ```ts [nuxt.config.ts] export default defineNuxtConfig({ modules: ['nuxt-image-viewer'], imageViewer: { bgOpacity: 0.9, maxZoom: 6, ui: { fullscreen: false }, hide: ['#intercom-container'], }, }) ``` Options marked **per call** can also be passed to [`open()`](https://kukson777.github.io/nuxt-image-viewer/docs/guide/open-from-code) for one viewer — the rest of the options still come from `nuxt.config`: ```ts useImageViewer().open(items, 0, { loop: false, ui: { counter: false } }) ``` ## Behaviour ### zIndex `number` · default `1000` · per call z-index of the viewer layer. Raise it above your own fixed bars and chat widgets. ### bgOpacity `number` · default `0.8` · per call Opacity of the backdrop, `0`–`1`. `1` hides the page completely. The colour is the [`--image-viewer-bg`](https://kukson777.github.io/nuxt-image-viewer/docs/customization/theming) variable. ### loop `boolean` · default `true` · per call Endless strip: the first image follows the last one. Needs at least 3 images — with two, the neighbour on both sides would be the same slide. ### spacing `number` · default `0.1` · per call Gap between slides as a share of the viewport width. ### maxZoom `number` · default `4` · per call Upper zoom limit relative to the image fitted into the screen. Images zoom up to their natural size, but at least 2×, and never above this value. ### closeOnVerticalDrag `boolean` · default `true` · per call Close by dragging the image up or down. When off, a vertical drag scrolls the strip like a horizontal one. ### closeOnBackdropClick `boolean` · default `true` · per call Close by a click or a tap outside the image. ### idleDelay `number` · default `4000` · per call Fade the controls — buttons, arrows, counter — out after the mouse rests for this long, in milliseconds, so the image is left alone. Any mouse move or key brings them back; over a button they stay. `0` keeps the controls shown. Only on devices with a mouse: touch screens keep the controls. ### idleHidesCaption `boolean` · default `true` · per call Fade the caption out together with the controls. `false` leaves the image with its caption. ```ts // A clean image after 2 s, but keep the caption const viewer = useImageViewer() viewer.open(items, 0, { idleDelay: 2000, idleHidesCaption: false }) ``` ## Interface ### ui `object` · per call, partial Which controls to show. All `true` by default.
Key
counter 2 / 5 in the top left corner. Hidden for a single image.
zoom Zoom in / out button.
fullscreen Fullscreen button — only where the Fullscreen API exists (not on iPhone).
close Close button.
arrows Previous / next buttons. Hidden on touch screens anyway.
caption Caption under the image.
### labels `object` · per call, partial Accessible names of the dialog and its buttons. English by default. ```ts labels: { dialog: 'Image viewer', close: 'Close', zoomIn: 'Zoom in', zoomOut: 'Zoom out', fullscreen: 'Fullscreen', exitFullscreen: 'Exit fullscreen', prev: 'Previous', next: 'Next', } ``` ## Page integration ### openClass `string` · default `'image-viewer-open'` Class added to `` while the viewer is open — handy to hide or adjust things of your own: ```css .image-viewer-open .cookie-banner { display: none; } ``` ### hide `string[]` · default `[]` CSS selectors hidden while the viewer is open. Chat widgets and other things that sit above every layer are the usual suspects. ```ts hide: ['jdiv', '#intercom-container', '.crisp-client'] ``` ### autoOpen `boolean` · default `true` Open the viewer on click, Enter and Space on any `img[data-image-viewer]`. Turn it off to open it only from code. ## Build These are read when the app builds and can't be changed per call. ### componentName `string | false` · default `'ZoomImage'` Name of the image component. `false` — don't register it and mark images with the [data attributes](https://kukson777.github.io/nuxt-image-viewer/docs/guide/plain-images) yourself. ### nuxtImage `boolean | 'auto'` · default `'auto'` Render `` inside the image component. `'auto'` — when `@nuxt/image` is installed. ### css `boolean` · default `true` Include the default styles. Turn off to [style from scratch](https://kukson777.github.io/nuxt-image-viewer/docs/customization/styling). ## Runtime config The runtime options also live in `useRuntimeConfig().public.imageViewer`, so they can be overridden with environment variables: ```bash [.env] NUXT_PUBLIC_IMAGE_VIEWER_Z_INDEX=2000 NUXT_PUBLIC_IMAGE_VIEWER_BG_OPACITY=1 ``` # Recipes > Common setups, copy-paste ready. ## Hide a chat widget while the viewer is open ```ts [nuxt.config.ts] export default defineNuxtConfig({ imageViewer: { hide: ['jdiv', '#intercom-container', '#crisp-chatbox'], }, }) ``` ## One image, no gallery Each `` in its own group: ```vue ``` Or from code: `useImageViewer().open({ src: '/photos/poster.jpg' })`. ## A gallery from an API ```vue ``` ## Deep link to a photo ```vue ``` ## A product gallery without the page dimming ```ts const viewer = useImageViewer() viewer.open(productPhotos, 0, { bgOpacity: 1, closeOnVerticalDrag: false, ui: { counter: false, fullscreen: false }, }) ``` ```css :root { --image-viewer-bg: #fff; --image-viewer-color: #111; --image-viewer-button-bg: transparent; --image-viewer-button-bg-hover: rgb(0 0 0 / 0.06); } ``` ## Labels from @nuxtjs/i18n ```ts const { t } = useI18n() const viewer = useImageViewer() viewer.open(items, 0, { labels: { dialog: t('viewer.dialog'), close: t('viewer.close'), prev: t('viewer.prev'), next: t('viewer.next'), }, }) ``` ## Fixed header that jumps when the page locks Without `scrollbar-gutter: stable` on ``, the page is padded by the scrollbar width while the viewer is open. Give your own fixed elements the same — or simply reserve the gutter: ```css html { scrollbar-gutter: stable; } ``` # Styling with classes > BEM classes on every part, a cascade layer, or no default styles at all. ## The cascade layer The default styles live in the `image-viewer` cascade layer. Any rule of yours outside a layer wins over them regardless of specificity — no `!important`, no fighting selectors: ```css .image-viewer__caption-text { font-style: italic; letter-spacing: 0.01em; } .image-viewer__counter { font-family: ui-monospace, monospace; } ``` The layer is declared after `base`, so Tailwind's preflight never breaks the viewer images. ## Classes ```txt .image-viewer the dialog layer — --zoomed, --closing, --idle, --idle-caption ├─ .image-viewer__backdrop backdrop, opacity set inline ├─ .image-viewer__track the strip, moved by transform │ └─ .image-viewer__slide one slide — --current │ └─ .image-viewer__frame fitted image box, positioned inline │ └─ .image-viewer__zoom zoomed and panned layer │ └─ .image-viewer__img --thumb (page copy), --full, --loaded └─ .image-viewer__ui controls layer ├─ .image-viewer__toolbar │ ├─ .image-viewer__counter │ └─ .image-viewer__buttons │ └─ .image-viewer__button --zoom, --fullscreen, --close ├─ .image-viewer__button.image-viewer__arrow --prev, --next └─ .image-viewer__caption └─ .image-viewer__caption-text ``` Icons are inline SVG with the `.image-viewer__icon` class, drawn with `stroke: currentColor`. ## Examples Buttons as squares in the corner: ```css .image-viewer__buttons { gap: 0; } .image-viewer__button { border-radius: 0; } ``` Caption at the top instead of the bottom: ```css .image-viewer__caption { top: 72px; bottom: auto; } ``` Arrows on touch screens too: ```css @media (pointer: coarse) { .image-viewer__arrow { display: inline-flex; } } ``` ## From scratch Turn the default styles off and write your own: ```ts [nuxt.config.ts] export default defineNuxtConfig({ imageViewer: { css: false, }, }) ``` Without the default styles you own the layout too. The minimum for a working viewer: `.image-viewer` is `position: fixed` over the window with `overflow: hidden` and `touch-action: none`; `__backdrop`, `__track` and `__ui` are `position: absolute; inset: 0`; `__slide` and `__frame` are `position: absolute`; `__zoom` and `__img` fill their parent. Start from the [default stylesheet](https://github.com/kukson777/nuxt-image-viewer/blob/main/src/runtime/image-viewer.css). # Theming > Restyle the viewer with CSS variables — try them live. The look of the viewer is a handful of CSS variables. Set them on `:root`, on `.image-viewer` or on any ancestor of the viewer. ## Theme builder Pick a preset or your own colours, then click a preview. The code below the previews is ready to paste. ## Variables
Variable Default
--image-viewer-bg #000 Backdrop colour. Its opacity is the bgOpacity option.
--image-viewer-color #fff Text and icons.
--image-viewer-font inherit Font family.
--image-viewer-counter-color --image-viewer-color The 2 / 5 counter.
--image-viewer-button-bg rgb(255 255 255 / 0.12) Button backing.
--image-viewer-button-bg-hover rgb(255 255 255 / 0.22) Button backing on hover and press.
--image-viewer-button-color --image-viewer-color Button icon.
--image-viewer-button-size 44px Width and height of buttons.
--image-viewer-button-radius 9999px Button corners.
--image-viewer-icon-size 24px Icon size.
--image-viewer-caption-bg rgb(0 0 0 / 0.6) Caption backing.
--image-viewer-caption-color --image-viewer-color Caption text.
--image-viewer-caption-radius 8px Caption corners.
--image-viewer-caption-font-size 14px Caption text size.
--image-viewer-focus-color #fff in the viewer, #2563eb on page images Focus ring.
## Following your colour mode Variables follow your own selectors, so a dark-mode viewer on a light site — or the other way round — is a few lines: ```css [assets/css/main.css] :root { --image-viewer-bg: #fff; --image-viewer-color: #18181b; --image-viewer-button-bg: rgb(0 0 0 / 0.06); --image-viewer-button-bg-hover: rgb(0 0 0 / 0.12); --image-viewer-caption-bg: rgb(255 255 255 / 0.9); } .dark { --image-viewer-bg: #000; --image-viewer-color: #fff; --image-viewer-button-bg: rgb(255 255 255 / 0.12); --image-viewer-button-bg-hover: rgb(255 255 255 / 0.22); --image-viewer-caption-bg: rgb(0 0 0 / 0.6); } ``` ## With Tailwind or Nuxt UI Point the variables at your design tokens: ```css [assets/css/main.css] :root { --image-viewer-button-bg: var(--ui-primary); --image-viewer-button-bg-hover: color-mix(in srgb, var(--ui-primary) 85%, white); --image-viewer-focus-color: var(--ui-primary); --image-viewer-font: var(--font-sans); } ``` Need more than variables? Every part has a [BEM class](https://kukson777.github.io/nuxt-image-viewer/docs/customization/styling). # AI agents > Point your coding agent at these docs. The module is built to be easy to use from an AI coding agent: every public type has JSDoc, the API is small and predictable, and this site serves its content as plain Markdown files an agent can read. ## llms.txt
URL
/llms.txt An index of every page with a short description.
/llms-full.txt The whole documentation in one Markdown file.
/raw/docs/.md Any page as Markdown, e.g. /raw/docs/guide/open-from-code.md .
## A prompt that works ```md Add nuxt-image-viewer to this Nuxt app (docs: https://kukson777.github.io/nuxt-image-viewer/llms-full.txt). Use for the images in , group them with data-image-viewer-group, pass the natural width and height of each full image, and match the viewer colours to our theme with the --image-viewer-* CSS variables. ``` ## The cheat sheet - **Mark images:** `` — width/height of the **full** image. Group with `data-image-viewer-group` on a container. - **Options for one gallery:** wrap it in `` — anything `open()` takes, buttons and arrows included. - **Plain images:** `img[data-image-viewer]` plus optional `data-image-viewer-src|width|height|caption`. - **From code:** `useImageViewer().open(items, index?, options?)` — auto-imported, client-only, items need only `src`. - **Configure:** `nuxt.config` → `imageViewer` (`ModuleOptions`); per call with `ImageViewerOpenOptions`. - **Style:** `--image-viewer-*` variables; `.image-viewer__*` BEM classes (unlayered CSS wins); `css: false` to start from scratch. - **Types:** `import type { ImageViewerItem, ImageViewerOpenOptions, ImageViewerApi } from 'nuxt-image-viewer'`. # Installation > Add the module to your Nuxt app. ### Install the module Use the Nuxt CLI — it installs the package and adds it to `modules`: ```bash [Terminal] npx nuxt module add nuxt-image-viewer ``` Or install it with your package manager and add it yourself: ```bash [pnpm] pnpm add nuxt-image-viewer ``` ```bash [npm] npm install nuxt-image-viewer ``` ```bash [bun] bun add nuxt-image-viewer ``` ```bash [yarn] yarn add nuxt-image-viewer ``` ```ts [nuxt.config.ts] export default defineNuxtConfig({ modules: ['nuxt-image-viewer'], }) ``` ### Mark an image ```vue [app.vue] ``` That's it — click the image. ## Requirements - Nuxt `>=3.13`, including Nuxt 4. - A browser with Pointer Events and the Web Animations API — every evergreen browser. With [Nuxt Image](https://image.nuxt.com) installed, `` renders ``: the page gets an optimized copy and the viewer opens the full image. See [ZoomImage](https://kukson777.github.io/nuxt-image-viewer/docs/guide/zoom-image#with-nuxt-image). # Introduction > A full-screen image viewer for Nuxt that opens where your images live. **Nuxt Image Viewer** is a lightbox module for Nuxt 3 and 4. An image expands from its place on the page, the viewer scrolls through the images of its group in an endless strip, zooms in every way people expect, and closes with a drag — the image flies back to where it came from. ## Why another lightbox It started inside a production app that needed a lightbox made for Nuxt: no global CSS to fight, no imperative setup per page, and no DOM work for the person writing the page. Every duration, curve, spring and gesture threshold is tuned to feel like a native photo app, and the whole viewer is one small Vue component. - **Mark, don't wire.** Add `` or a `data-image-viewer` attribute — no click handlers, no refs, no `onMounted`. - **No blank frames.** All slides are in the DOM and show the copy already loaded on the page while the full image loads. - **No dependencies.** Plain Vue, plain CSS and inline SVG icons. The viewer itself is loaded on the first open. - **Customisable at every level.** Module options, per-call options, CSS variables, BEM classes, or your own styles from scratch. ## What you get A component for images that open full screen. Open, close and scroll the viewer from code. Global defaults in `nuxt.config`, overrides per call. CSS variables with a live theme builder. # Quick start > The three ways to open the viewer, in five minutes. ## A gallery Put images in a container with `data-image-viewer-group`: the viewer scrolls through the images of that group only. Without a group it scrolls through every marked image of the page. ```vue ``` `width` and `height` are the natural size of the **full** image, not the size on the page. The viewer fits and zooms the image by them — up to its natural size, but at least 2×. ## Images you already have HTML from a CMS or Markdown doesn't need the component — tag the ``: ```html Dawn ``` ## From code ```ts const viewer = useImageViewer() viewer.open([ { src: '/photos/dawn.jpg', width: 2400, height: 1600 }, { src: '/photos/dusk.jpg' }, // no size: measured before opening ]) ``` ## Next - Restyle it on the [Theming](https://kukson777.github.io/nuxt-image-viewer/docs/customization/theming) page with the live builder. - Change behaviour with [Options](https://kukson777.github.io/nuxt-image-viewer/docs/customization/options). # Accessibility > Dialog semantics, keyboard, focus and labels. ## The dialog - The viewer is a `role="dialog"` with `aria-modal="true"`, named by the [`labels.dialog`](https://kukson777.github.io/nuxt-image-viewer/docs/customization/options#labels) option. - Focus moves into the dialog when it opens. and + go round its buttons and never leave it. - On close, focus returns to the page image the viewer closed into — or, when opened from code without an element, to whatever had focus before `open()`. - Slides other than the current one are `aria-hidden`; the counter is a polite live region, so screen readers announce `3 / 7` as you scroll. - Every button has an accessible name and visible focus (`--image-viewer-focus-color`). ## Page images - `` is focusable (`tabindex="0"`) and opens with Enter or Space. - `alt` falls back to the caption. Pass `alt` explicitly when the caption isn't a good description. - Page images get `cursor: zoom-in` and a focus ring — restyle them with `[data-image-viewer]`. ## Custom labels All names are options, globally or per call. Override only the ones you want to change — the rest keep their English defaults: ```ts [nuxt.config.ts] export default defineNuxtConfig({ imageViewer: { labels: { dialog: 'Photo gallery', close: 'Close gallery', prev: 'Previous photo', next: 'Next photo', }, }, }) ``` For other languages, pass translated labels per call — with `@nuxtjs/i18n`: `open(items, 0, { labels: { close: t('viewer.close') } })`. See the [i18n recipe](https://kukson777.github.io/nuxt-image-viewer/docs/customization/recipes#labels-from-nuxtjsi18n). ## Motion `prefers-reduced-motion: reduce` turns the animations off — see [Gestures](https://kukson777.github.io/nuxt-image-viewer/docs/guide/gestures#reduced-motion). # Gestures & keys > How people move through the viewer with a mouse, a finger or a keyboard. Gestures run on Pointer Events, the same for mouse and touch. The direction of a drag is decided once, by its first few pixels.
Mouse Touch Keyboard
Next / previous drag sideways, arrow buttons swipe
Zoom in / out click the image, wheel, zoom button double tap, pinch —
Pan a zoomed image drag drag —
Close click outside, drag up or down, close button tap outside, drag up or down
Move between buttons — —
## The feel It comes down to a few tuned numbers: - **Opening, closing and zoom** take 333 ms on `cubic-bezier(.4, 0, .22, 1)`. - **The strip** settles with a critically damped spring, starting at the velocity of your throw. A throw faster than 0.5 px/ms switches the slide even after a tiny drag; otherwise more than half of the neighbour has to be on screen. - **Swiping without a pause** never loses a swipe: a flick is judged by its own direction, even while the strip is still catching up with the previous slide. - **Drag to close** lags behind the finger (friction 0.6) and fades the backdrop — it's gone at a third of the screen height. It closes when the image, with its throw, would travel past 40 % of that third. - **Past the edges** of the strip and of a zoomed image, movement continues with friction 0.35. The strip springs back; a thrown zoomed image glides on and bounces slightly off its edge. - **Zoom** goes up to the natural size of the image, but at least 2× and at most [`maxZoom`](https://kukson777.github.io/nuxt-image-viewer/docs/customization/options#maxzoom). The point under the cursor or between the fingers stays in place. ## Controls that step aside With a mouse, the buttons, arrows and counter fade out when the mouse rests for 4 seconds and come back on the next move or key — the caption fades too, unless [`idleHidesCaption`](https://kukson777.github.io/nuxt-image-viewer/docs/customization/options#idlehidescaption) is off. Tune it with [`idleDelay`](https://kukson777.github.io/nuxt-image-viewer/docs/customization/options#idledelay). ## On a phone - A single tap on the image does nothing — a double tap zooms. A tap outside closes. - The arrow buttons are hidden on touch screens (`pointer: coarse`). - Lifting one finger of a pinch lets you keep panning with the other. ## Reduced motion With `prefers-reduced-motion: reduce` the viewer opens, closes, zooms and scrolls without animations; springs jump to their end value. # 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. ```ts 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 ) ``` ## Items Only `src` is required. The full image. Natural size. Without it the image is loaded and measured first, so the viewer opens a moment later — pass it when you know it. A copy shown at once while `src` loads. Defaults to `element.currentSrc`, otherwise `src`. The page element the image opens from and closes back into. Without it the viewer fades in and out. Alternative text. Defaults to the `alt` of `element`. Caption under the image. ## Flying from your own thumbnails Pass the elements and the viewer animates from them exactly like it does for ``: ```vue ``` ## Controlling an open viewer ```ts 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](https://kukson777.github.io/nuxt-image-viewer/docs/api/use-image-viewer). 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`. # Plain images > Open the viewer from any img with data attributes. `` is only a shortcut for a few attributes. Any `` with `data-image-viewer` opens the viewer — HTML from a CMS, rendered Markdown, a third-party component. ```html Alpine dawn ``` ## Attributes Marks the image. Only `` elements are picked up. The full image. Defaults to the image on the page (`currentSrc`). Natural width of the full image. Defaults to the natural width of the page image. Natural height of the full image. Defaults to the natural height of the page image. Caption under the image in the viewer. On a container: the viewer scrolls through the marked images inside it only. Add `tabindex="0"` so keyboard users can reach the image and open it with Enter or Space — `` does it for you. ## Markdown content With Nuxt Content, mark the images of a page in one place — a prose image component: ```vue [components/content/ProseImg.vue] ``` Then wrap the article in `data-image-viewer-group` so each page is its own gallery. ## Turning automatic opening off The plugin listens to clicks and keys on the whole document. To open the viewer only from code, turn it off: ```ts [nuxt.config.ts] export default defineNuxtConfig({ imageViewer: { autoOpen: false, }, }) ``` # Under the hood > How the viewer mounts, locks the page and stays fast. ## Mounting A module can't edit your `app.vue`, so the client plugin does it lazily: on the first open it imports the viewer component and renders it into a container in `` with your app's context; after the closing animation it unmounts it. Until someone opens an image, the viewer costs nothing but a click listener. The viewer component itself doesn't depend on Nuxt — it gets everything through props and reports back through events. ## The strip Every slide is in the DOM from the start. Each shows the copy of the image already loaded on the page (`currentSrc`), and the full image is loaded on top of it for the current slide and its neighbours. That's why there is never an empty frame, however fast you swipe. The endless strip works from three images: its position grows without limit, and every slide stands in the copy closest to it. A slide is always moved while it's off screen. ## The flight While the viewer is open, the page image of the current slide is `visibility: hidden` — the image flies out of its place instead of doubling it. The flight is a FLIP animation from the visible crop of the thumbnail, respecting `object-fit`, `object-position` and `border-radius` through an animated `clip-path`. ## Scroll lock The page can't scroll while the viewer is open. If `` doesn't have `scrollbar-gutter: stable`, the scrollbar width is compensated with `padding-right` on ``, so the page doesn't jump. Your own fixed elements may need the same — target them with the [`openClass`](https://kukson777.github.io/nuxt-image-viewer/docs/customization/options#openclass) on ``. The viewer layer is `100vw` wide rather than `inset: 0`: with a stable scrollbar gutter, `inset: 0` would leave a stripe on the right where the page shows through. ## Size The module ships a plugin, a composable, two tiny components, the viewer component and one CSS file — no runtime dependencies. The viewer is code-split and loaded on the first open. # ZoomImage > The component for images that open full screen. `` renders an image that opens the viewer on click, Enter and Space. It only sets attributes — the global plugin does the rest, so there are no handlers per image. ```vue ``` ## Props The full-size image. The viewer opens it; with Nuxt Image the page shows an optimized copy. Natural width of `src`. Natural height of `src`. Together with `width` it tells the viewer how to fit the image and how far it can zoom: up to the natural size, but at least 2×. Text under the image in the viewer. Also the default `alt`. Alternative text. Falls back to `caption`, then to an empty string. Every other attribute — `class`, `style`, `loading`, `sizes`, `decoding`, Nuxt Image props — passes through to the rendered image. ## Groups The viewer scrolls through the images of the closest `[data-image-viewer-group]`. Without one, it scrolls through every marked image of the page. ```vue ``` ### A group with its own options `` is such a group with options for its images only — anything [`open()`](https://kukson777.github.io/nuxt-image-viewer/docs/customization/options) takes, on top of the global ones: ```vue ``` It renders a `
`; `as="section"` makes it another tag. That's also the way to give one gallery its own [buttons](https://kukson777.github.io/nuxt-image-viewer/docs/customization/custom-buttons) — and its own arrows, as `#prev` / `#next` slots. ## With Nuxt Image When [`@nuxt/image`](https://image.nuxt.com) is installed, the component renders `` instead of ``. The page shows an optimized copy — use `sizes` to keep it small — and the viewer loads the original `src` on top of it. ```vue ``` That's what this site does: the thumbnails above are 320 px WebP copies from `/_ipx` (640 px on high-density screens), and the viewer opens the 2400 px originals. Until the original arrives, the viewer shows the copy already on the page — so there is never an empty frame. Detection is automatic. Force it either way with the [`nuxtImage`](https://kukson777.github.io/nuxt-image-viewer/docs/customization/options#nuxtimage) option. ## Thumbnails that crop Cropped thumbnails are fine: with `object-fit: cover` or `contain` (and any `object-position`) the image flies out of the visible crop and back into it, and rounded corners are animated too. ```vue ``` ## Renaming the component ```ts [nuxt.config.ts] export default defineNuxtConfig({ imageViewer: { componentName: 'LightboxImage', // or false to not register it at all }, }) ``` ## Resources for Agents - [API catalog: every service document this site publishes](https://kukson777.github.io/nuxt-image-viewer/.well-known/api-catalog) - [Sitemap (XML)](https://kukson777.github.io/nuxt-image-viewer/sitemap.xml) - [Sitemap (Markdown): every page on the site](https://kukson777.github.io/nuxt-image-viewer/sitemap.md) - [llms.txt: index of the documentation for LLMs](https://kukson777.github.io/nuxt-image-viewer/llms.txt) - [llms-full.txt: the full documentation as a single file](https://kukson777.github.io/nuxt-image-viewer/llms-full.txt) - [OpenAPI document: every route this site serves to agents](https://kukson777.github.io/nuxt-image-viewer/openapi.json)