# Gallery in MkDocs

> Add a VisualCI screenshots gallery to an MkDocs site with the visualci-gallery plugin, 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/mkdocs/
All of VisualCI's documentation: https://visualci.com/llms.txt

The `visualci-gallery` Python package comes with an MkDocs plugin. A `visualci-gallery` fenced block in any page becomes a [public gallery](https://visualci.com/docs/gallery.md), rendered when the site builds: a tab for each group, light and dark, desktop and phone, and a full-size view. The plugin adds the gallery's stylesheet and script to the site, and they bring it to life in the browser, again after each of Material's instant navigations. It's built and tested with Material for MkDocs. If a manifest can't be read, the build fails.

It needs MkDocs 1.6 or later and Python 3.10 or later (tested with Material for MkDocs 9.8):

```sh
pip install "visualci-gallery[mkdocs]==0.7.0"
```

A block 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/python/examples/mkdocs), which VisualCI's tests build and check in a browser.

## The plugin

Add `visualci-gallery` to the plugins in `mkdocs.yml`:

<!-- example: clients/gallery/python/examples/mkdocs/mkdocs.yml -->
```yaml
site_name: Acme
# Material's instant navigation (navigation.instant) needs the site's URL (SITE_URL, for a local preview).
site_url: !ENV [SITE_URL, https://acme.example/]
theme:
  name: material
  features:
    - navigation.instant
  font: false
  palette:
    - media: "(prefers-color-scheme: light)"
      scheme: default
      toggle: {icon: material/brightness-7, name: Dark mode}
    - media: "(prefers-color-scheme: dark)"
      scheme: slate
      toggle: {icon: material/brightness-4, name: Light mode}
extra_css:
  - stylesheets/site.css
markdown_extensions:
  - admonition
plugins:
  - search
  - visualci-gallery
nav:
  - index.md
  - screenshots.md
  - tour.md
  - app.md
  - overview.md
```

It takes `timeout` (seconds for each try, default 20) and `attempts` (default 3): `- visualci-gallery: {timeout: 30}`.

## The smallest page

`docs/screenshots.md`:

<!-- example: clients/gallery/python/examples/mkdocs/docs/screenshots.md -->
````md
# Screenshots

```visualci-gallery
manifest: https://app.visualci.com/g/acme/web.json
title: null
screens:
  overview: {title: Overview, caption: Everything at a glance.}
order: [overview]
```
````

The block is YAML: `manifest`, then the gallery's options. Each manifest is read once a build, however many pages show it. 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. A block can be indented, inside an admonition, a content tab or a list item. A block inside another code block stays code, so a page can show the syntax. Quote a screen key or group name that YAML would read as something else: `"on"`, `"no"`, `"2024"`.

## Options

The options are the [Python package's](https://visualci.com/docs/gallery/python.md), in its spelling (`heading_level`, `group_order`, `theme_switch`, `see_all`, `deep_links`, `new_badges`), passed to it as they are. 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/python/examples/mkdocs/docs/tour.md -->
````md
# Tour

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

## Theming

Set the `--vg-*` variables to your theme's tokens in a stylesheet in `extra_css`, and the gallery's colours follow its light and dark mode. The example site's `docs/stylesheets/site.css`, for Material:

<!-- example: clients/gallery/python/examples/mkdocs/docs/stylesheets/site.css -->
```css
/* The gallery's --vg-* variables set to Material's tokens, which change with its palette toggle. */
.vg {
  --vg-ink: var(--md-default-fg-color);
  --vg-muted: var(--md-default-fg-color--light);
  --vg-paper: var(--md-default-bg-color);
  --vg-rule: var(--md-default-fg-color--lightest);
  --vg-accent: var(--md-accent-fg-color);
}
```

Material sets the page's root font size to 20 px, so the plugin also scales the gallery's sizes (gaps, card widths, titles) to Material's 16 px article text, on `.md-typeset .vg`. To set a size yourself, use that selector too: `.md-typeset .vg { --vg-phone-card-min: 9rem; }`. [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 Material's palette toggle instead.

<!-- example: clients/gallery/python/examples/mkdocs/docs/app.md -->
````md
# The app

The gallery follows Material's light and dark toggle.

```visualci-gallery
manifest: https://app.visualci.com/g/acme/web.json
title: null
theme_switch: false
```
````

## One release's screenshots

`ref: v1.0.0` in a block (or in the plugin's settings, for every block) shows the gallery as it was at that release,
when your project [reports versions](https://visualci.com/docs/gallery/versions.md): the plugin reads `…/web@v1-0-0.json` in place of the
main manifest, and a block's `ref: null` goes back to it. `versions: true` adds a Version switch over the grid, so
readers can move between Latest and every release ([Versions](https://visualci.com/docs/gallery/options.md#versions)).

## When the manifest can't be read

The plugin tries three times, then fails the build with an error that names the page and the URL. It also fails the build when the manifest has nothing to show, the block isn't valid YAML, or an option is invalid. The site you have keeps serving its last build until VisualCI answers again.

## Pinning the version

Pin the package in your requirements, so an update is a change you make:

```sh
pip install "visualci-gallery[mkdocs]==0.7.0"
```

## 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 `docs/index.md`:

<!-- example: clients/gallery/python/examples/mkdocs/docs/index.md -->
````md
# Acme

```visualci-gallery
manifest: https://app.visualci.com/g/acme/web.json
layout: strip
title: null
credit: false
see_all: {href: screenshots/}
```
````
