# Gallery in VitePress

> Add a VisualCI screenshots gallery to a VitePress site with the visualci-gallery-vitepress plugin and component, rendered into each page when the site builds, and fail the build when the manifest can't be read.

Source: https://visualci.com/docs/gallery/vitepress/
All of VisualCI's documentation: https://visualci.com/llms.txt

The `visualci-gallery-vitepress` package puts a [public gallery](https://visualci.com/docs/gallery.md) in a VitePress page: a tab for each group, light and dark, desktop and phone, and a full-size view. Its Vite plugin reads each manifest when the site builds, and the `<Gallery>` component renders it into the page's HTML, in Markdown or a Vue component. In the browser the gallery comes to life, and again after each client-side navigation. If a manifest can't be read, the build fails.

It needs VitePress 1 (Vite 5 or later and Vue 3.4 or later), and brings the [JS library](https://visualci.com/docs/gallery/js.md) with it:

```sh
npm install visualci-gallery-vitepress@0.7.0
```

The plugin needs your project's manifest URL, `https://app.visualci.com/g/<org>/<project>.json`, which works once the project's gallery is on. The examples use `acme/web`. Every snippet below is a file from the [example site](https://github.com/DocSpring/visualci/tree/main/clients/gallery/vitepress/examples/site), which VisualCI's tests build with `vitepress build` and check in a browser.

## The plugin and the theme

Add the plugin to `.vitepress/config.ts` with the manifests your galleries show:

<!-- example: clients/gallery/vitepress/examples/site/.vitepress/config.ts -->
```ts
import { defineConfig } from "vitepress";
import { visualciGallery } from "visualci-gallery-vitepress";

// The plugin reads each manifest when the site builds; the Gallery component renders one of them.
export default defineConfig({
  title: "Acme",
  outDir: process.env.VITEPRESS_OUT_DIR || ".vitepress/dist",
  themeConfig: {
    nav: [
      { text: "Screenshots", link: "/screenshots" },
      { text: "Tour", link: "/tour" },
      { text: "The app", link: "/app" },
    ],
  },
  vite: {
    plugins: [visualciGallery({ manifests: ["https://app.visualci.com/g/acme/web.json"] })],
  },
});
```

Then `enhanceApp` from `visualci-gallery-vitepress/theme` in `.vitepress/theme/index.ts`. It registers `<Gallery>` for every page and keeps `<html data-theme>` in step with VitePress's dark mode, which the gallery reads:

<!-- example: clients/gallery/vitepress/examples/site/.vitepress/theme/index.ts -->
```ts
import DefaultTheme from "vitepress/theme";
import { enhanceApp } from "visualci-gallery-vitepress/theme";
import "./custom.css";

// enhanceApp registers <Gallery> for every page's Markdown and lets galleries follow VitePress's dark mode.
export default { extends: DefaultTheme, enhanceApp };
```

To register it yourself, import the component from `visualci-gallery-vitepress/Gallery`. It brings the gallery's stylesheet with it.

## The smallest page

`screenshots.md`:

<!-- example: clients/gallery/vitepress/examples/site/screenshots.md -->
```md
# Screenshots

<Gallery
  manifest="https://app.visualci.com/g/acme/web.json"
  :title="null"
  :screens="{ overview: { title: 'Overview', caption: 'Everything at a glance.' } }"
  :order="['overview']"
/>
```

`manifest` must be one of the plugin's `manifests`; the page names the config when it isn't. The image URLs always serve the approved version, so a changed screenshot shows on your site without a rebuild. A new screenshot, or one whose size changed, shows after the next build.

## Options

Every attribute but `manifest` is a gallery option, passed to the library as it is, in either spelling (`:heading-level` or `:headingLevel`), so `class` and `style` don't reach the element around the gallery: style it through the `--vg-*` variables or an element of your own around it. Bind anything that isn't a string with `:`. On this page the gallery gets a heading, and two screens get a title and a caption. `order` puts two screens first. The Mac approver group gets a new title and shows its windows whole, and the iPhone tab comes first:

<!-- example: clients/gallery/vitepress/examples/site/tour.md -->
```md
# Tour

<Gallery
  manifest="https://app.visualci.com/g/acme/web.json"
  title="See it in action"
  :heading-level="2"
  :screens="{
    overview: { title: 'Overview', caption: 'What needs you, at a glance.' },
    'ios/sessions': { title: 'Sessions', caption: 'Every session on every machine.' },
  }"
  :order="['overview', 'access']"
  :groups="{ 'Mac approver': { title: 'On the Mac', fit: 'contain' } }"
  :group-order="['iPhone', 'Web']"
/>
```

A screen's key is its screenshots' name without the theme and width: `overview-light-1440` and `overview-dark-390` are the screen `overview`. Groups go by the names in `visualci.yml`'s `gallery` section. `:title="null"` leaves the gallery's heading out, under the page's own. Give each gallery on a page its own `id`. [Gallery options](https://visualci.com/docs/gallery/options.md) describes every option, and how screenshot names pair into screens.

The plugin takes `timeout` (milliseconds for each try, default 20000), `attempts` (default 3) and `fetch` beside `manifests`.

## Theming

Set the `--vg-*` variables to VitePress's theme tokens, and the gallery's colours follow its light and dark mode. The example site's `.vitepress/theme/custom.css`:

<!-- example: clients/gallery/vitepress/examples/site/.vitepress/theme/custom.css -->
```css
/* The gallery's --vg-* variables set to VitePress's theme tokens, which change with its light and dark toggle. */
.vg {
  --vg-ink: var(--vp-c-text-1);
  --vg-muted: var(--vp-c-text-2);
  --vg-paper: var(--vp-c-bg);
  --vg-paper-2: var(--vp-c-bg-soft);
  --vg-rule: var(--vp-c-divider);
  --vg-accent: var(--vp-c-brand-1);
}
```

[Gallery options](https://visualci.com/docs/gallery/options.md#theming) lists every variable.

## Light and dark

By default each group has Light and Dark buttons. The gallery starts in the page's theme, and the buttons switch the screenshots without changing your page. With `:theme-switch="false"` the gallery follows VitePress's appearance toggle instead:

<!-- example: clients/gallery/vitepress/examples/site/app.md -->
```md
# The app

The gallery follows VitePress's own light and dark toggle.

<Gallery manifest="https://app.visualci.com/g/acme/web.json" :title="null" :theme-switch="false" />
```

## When the manifest can't be read

The plugin tries three times, 20 seconds each, then fails the build with an error that names the URL. A page fails when its gallery has nothing to show or an option is invalid. The site you have keeps serving its last build until VisualCI answers again.

## Pinning the version

Install an exact version, so an update is a change you make, and commit your lockfile:

```sh
npm install --save-exact visualci-gallery-vitepress@0.7.0
```

From 0.6.0 it depends on `visualci-gallery` at exactly its own version, so a new release's markup, stylesheet and script arrive together when you upgrade `visualci-gallery-vitepress`. Upgrade that package, not `visualci-gallery` on its own: before 0.6.0, upgrading only the library left the component on its older copy, and new features such as the search box showed up unstyled. If your site also lists `visualci-gallery` itself, keep it at the same version.

## A band on the homepage

The strip layout shows every screen at one size in a single row that scrolls sideways, with no tabs and no captions. With `see-all` it links to the full page. The example site's `index.md`:

<!-- example: clients/gallery/vitepress/examples/site/index.md -->
```md
# Acme

<Gallery
  manifest="https://app.visualci.com/g/acme/web.json"
  layout="strip"
  :title="null"
  :credit="false"
  :see-all="{ href: '/screenshots' }"
/>
```
