# Gallery in React and Next.js

> Add a VisualCI screenshots gallery to a React site with visualci-gallery-react, rendered on the server in Next.js's app router and hydrated in the browser, and fail the build when the manifest can't be read.

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

The `visualci-gallery-react` package puts a [public gallery](https://visualci.com/docs/gallery.md) in a React page: a tab for each group, light and dark, desktop and phone, and a full-size view. In Next.js's app router, `<Gallery>` is a server component: it reads the manifest on the server and renders the gallery into the page's HTML, and a small client component brings it to life in the browser. A page that's prerendered when the site builds fails the build when the manifest can't be read.

It needs React 18 or later (tested with React 19 and Next.js 16), and brings the [JS library](https://visualci.com/docs/gallery/js.md) with it:

```sh
npm install visualci-gallery-react@0.8.1
```

`<Gallery>` needs your project's manifest URL, `https://app.visualci.com/g/<org>/<project>/gallery.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/react/examples/next), which VisualCI's tests build with `next build` and check in a browser.

## The smallest page

`app/screenshots/page.tsx`:

<!-- example: clients/gallery/react/examples/next/app/screenshots/page.tsx -->
```tsx
import { Gallery } from "visualci-gallery-react";

export const metadata = { title: "Screenshots" };

export default function Screenshots() {
  return (
    <Gallery
      manifest="https://app.visualci.com/g/acme/web/gallery.json"
      screens={{ overview: { title: "Overview", caption: "Everything at a glance." } }}
      order={["overview"]}
    />
  );
}
```

The stylesheet comes from the library. Import it once, in the root layout, with the rest of your global CSS:

<!-- example: clients/gallery/react/examples/next/app/layout.tsx -->
```tsx
import Link from "next/link";
import type { ReactNode } from "react";
import "visualci-gallery/gallery.css";
import "./site.css";
import { ThemeToggle } from "./theme-toggle";

export default function Layout({ children }: { children: ReactNode }) {
  return (
    <html lang="en">
      <body>
        <nav>
          <Link href="/">Home</Link> <Link href="/https/visualci.com/screenshots">Screenshots</Link> <Link href="/https/visualci.com/tour">Tour</Link>{" "}
          <Link href="/https/visualci.com/app">The app</Link> <ThemeToggle />
        </nav>
        {children}
      </body>
    </html>
  );
}
```

Next.js prerenders the page when the site builds, so the screenshots are in its HTML before any script runs. A dynamic page renders it on each request instead, and a manifest that can't be read is an error on that request. 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.

`<Gallery>` is an async server component, so it works in a server component's tree only. Elsewhere (the pages router, a single-page app, a React page rendered by your own build), render the HTML yourself with `render(await load(options))` from `visualci-gallery/render`, in `getStaticProps` or a loader, and pass it to `GalleryMarkup` from `visualci-gallery-react/client`. `GalleryMarkup` hydrates the HTML once it's on the page and lets the gallery go when the component unmounts, so it works across client-side navigations.

## Options

The component's props are the gallery's options, passed to the library 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/react/examples/next/app/tour/page.tsx -->
```tsx
import { Gallery } from "visualci-gallery-react";

export const metadata = { title: "Tour" };

export default function Tour() {
  return (
    <Gallery
      manifest="https://app.visualci.com/g/acme/web/gallery.json"
      title="See it in action"
      headingLevel={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" } }}
      groupOrder={["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 heading out, for a page that has its own. Give each gallery on a page its own `id`.

`<Gallery>` also takes `timeout` (milliseconds for each try, default 20000), `attempts` (default 3) and `fetch` (one to use in place of the global `fetch`). [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 site's tokens. When the tokens change with your site's theme, the gallery's colours change with them. The example site's `app/site.css`:

<!-- example: clients/gallery/react/examples/next/app/site.css -->
```css
/* The site's tokens, light and dark (the reader's setting, or the toggle's <html data-theme>), and the gallery's --vg-* variables set to them.
   color-scheme too: the gallery's other defaults (Canvas, LinkText) follow it. */
:root {
  color-scheme: light;
  --color-text: #1d1d1f;
  --color-muted: #6e6e73;
  --color-bg: #ffffff;
  --color-brand: #0a66c2;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --color-text: #f5f5f7;
    --color-muted: #a1a1a6;
    --color-bg: #111114;
    --color-brand: #4c9aff;
  }
}
[data-theme="dark"] {
  color-scheme: dark;
  --color-text: #f5f5f7;
  --color-muted: #a1a1a6;
  --color-bg: #111114;
  --color-brand: #4c9aff;
}
body {
  max-width: 72rem;
  margin: 0 auto;
  padding: 0 1.5rem 3rem;
  font-family: system-ui, sans-serif;
  color: var(--color-text);
  background: var(--color-bg);
}
nav {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 1.25rem;
  padding: 1rem 0;
}
nav a {
  color: var(--color-brand);
  text-decoration: none;
}
#theme-toggle {
  margin-left: auto;
  font: inherit;
}
.vg {
  --vg-ink: var(--color-text);
  --vg-muted: var(--color-muted);
  --vg-paper: var(--color-bg);
  --vg-accent: var(--color-brand);
}
```

[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 (`<html data-theme="light">` or `"dark"` when it's set, otherwise the reader's system setting), and the buttons switch the screenshots. They don't change your page.

If your site has its own theme toggle on `<html data-theme>`, turn the buttons off with `themeSwitch={false}`. The gallery then follows `data-theme` as it changes:

<!-- example: clients/gallery/react/examples/next/app/app/page.tsx -->
```tsx
import { Gallery } from "visualci-gallery-react";

export const metadata = { title: "The app" };

export default function App() {
  return <Gallery manifest="https://app.visualci.com/g/acme/web/gallery.json" themeSwitch={false} />;
}
```

The example site's toggle, `app/theme-toggle.tsx`:

<!-- example: clients/gallery/react/examples/next/app/theme-toggle.tsx -->
```tsx
"use client";
// The site's own toggle sets <html data-theme>, starting from the reader's setting. A gallery with themeSwitch={false}
// follows it.
export function ThemeToggle() {
  return (
    <button
      id="theme-toggle"
      type="button"
      onClick={() => {
        const html = document.documentElement;
        const dark = html.dataset.theme
          ? html.dataset.theme === "dark"
          : matchMedia("(prefers-color-scheme: dark)").matches;
        html.dataset.theme = dark ? "light" : "dark";
      }}
    >
      Light or dark
    </button>
  );
}
```

## When the manifest can't be read

`<Gallery>` tries three times, 20 seconds each, then throws an error that names the URL. On a prerendered page that fails the build, and the site you have keeps serving its last build until VisualCI answers again. It also throws when the manifest has nothing to show or a prop is invalid.

## 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-react@0.8.1
```

The package takes its markup, stylesheet and script from `visualci-gallery`. 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-react`. 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 `seeAll` it links to the full page. The example site's `app/page.tsx`:

<!-- example: clients/gallery/react/examples/next/app/page.tsx -->
```tsx
import { Gallery } from "visualci-gallery-react";

export default function Home() {
  return (
    <main>
      <h1>Acme</h1>
      <Gallery
        manifest="https://app.visualci.com/g/acme/web/gallery.json"
        layout="strip"
        title={null}
        credit={false}
        seeAll={{ href: "/screenshots" }}
      />
    </main>
  );
}
```
