# Gallery in Astro

> Add a VisualCI screenshots gallery to an Astro site with the visualci-astro-gallery component, rendered when the site builds, and fail the build when the manifest can't be read.

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

The `visualci-astro-gallery` component reads a [public gallery](https://visualci.com/docs/gallery.md)'s manifest when your site builds and renders the gallery into the page's HTML: a tab for each group, light and dark, desktop and phone. It adds the stylesheet and a small script that brings the gallery to life in the browser (the tabs, the switches and the full-size view). If the manifest can't be read, the build fails. [Fleetlayer's screenshots page](https://fleetlayer.ai/screenshots/) is built with it.

It needs Astro 5 or later (tested with Astro 7), and brings the [JS library](https://visualci.com/docs/gallery/js.md) with it:

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

The component 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/astro/examples/site), which VisualCI's tests build with `astro build`.

## The smallest page

`src/pages/screenshots.astro`:

<!-- example: clients/gallery/astro/examples/site/src/pages/screenshots.astro -->
```astro
---
import Gallery from "visualci-astro-gallery";
---

<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Screenshots</title>
  </head>
  <body>
    <Gallery
      manifest="https://app.visualci.com/g/acme/web.json"
      screens={{ overview: { title: "Overview", caption: "Everything at a glance." } }}
      order={["overview"]}
    />
  </body>
</html>
```

The component's stylesheet and script are files under `/_astro/`, with no inline script or style, so the component itself works under a strict Content Security Policy. Your own pages still need to meet that policy. Astro puts small scripts and stylesheets inline, so in the example site below the layout's theme toggle and `site.css` end up inline in each page. A site under a strict policy has to allow those (with hashes, for example) or have Astro write them to files. With Astro's view transitions, the script hydrates the new page's galleries after each navigation.

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

The component's props are the gallery's options. 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. The page uses the example site's layout, shown under [Light and dark](#light-and-dark):

<!-- example: clients/gallery/astro/examples/site/src/pages/tour.astro -->
```astro
---
import Gallery from "visualci-astro-gallery";
import Layout from "../layouts/Layout.astro";
---

<Layout title="Tour">
  <Gallery
    manifest="https://app.visualci.com/g/acme/web.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"]}
  />
</Layout>
```

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

After the page has loaded, the gallery preloads the images a reader is likely to look at next. `preload={false}` turns that off, and `preload={{ count: 200, bytes: 20_000_000 }}` sets your own limits. It's on by default, and [Preloading](https://visualci.com/docs/gallery/options.md#preloading) says what it fetches and when.

The component 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

The gallery's stylesheet follows the page's text colour and `color-scheme` out of the box. To give the gallery your site's look, set the `--vg-*` variables to your own tokens. When the tokens change with your site's theme, the gallery's colours change with them. The example site's `src/styles/site.css`:

<!-- example: clients/gallery/astro/examples/site/src/styles/site.css -->
```css
/* The site's own tokens, light and dark. */
:root {
  --color-text: #1c2024;
  --color-text-muted: #60646c;
  --color-bg: #ffffff;
  --color-surface: #f4f5f7;
  --color-border: #dfe1e6;
  --color-brand: #3e63dd;
  --radius: 10px;
}
[data-theme="dark"] {
  --color-text: #edeef0;
  --color-text-muted: #9ba1a6;
  --color-bg: #111113;
  --color-surface: #1b1b1f;
  --color-border: #2e3035;
  --color-brand: #8da4ef;
}

/* The gallery in the site's colours. Set on .vg or any element around it, these win in any stylesheet order. */
.vg {
  --vg-ink: var(--color-text);
  --vg-ink-2: var(--color-text);
  --vg-muted: var(--color-text-muted);
  --vg-paper: var(--color-bg);
  --vg-paper-2: var(--color-surface);
  --vg-rule: var(--color-border);
  --vg-accent: var(--color-brand);
  --vg-radius-md: var(--radius);
  --vg-radius-lg: calc(var(--radius) * 1.5);
  --vg-title-size: 2.25rem;
}
```

The layout below imports it. Set the variables on `.vg` or on any element around the gallery. The gallery's stylesheet declares its defaults on the root with no specificity, so your values win even though Astro puts `site.css` before the component's stylesheet here. [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, and the reader's system setting when it isn't set:

<!-- example: clients/gallery/astro/examples/site/src/pages/app.astro -->
```astro
---
import Gallery from "visualci-astro-gallery";
import Layout from "../layouts/Layout.astro";
---

<Layout title="The app">
  <Gallery manifest="https://app.visualci.com/g/acme/web.json" themeSwitch={false} />
</Layout>
```

The example site's layout, `src/layouts/Layout.astro`, has the toggle:

<!-- example: clients/gallery/astro/examples/site/src/layouts/Layout.astro -->
```astro
---
import "../styles/site.css";

const { title } = Astro.props;
---

<html lang="en" data-theme="light">
  <head>
    <meta charset="utf-8" />
    <title>{title}</title>
  </head>
  <body>
    <button id="theme-toggle" type="button">Light or dark</button>
    <slot />
    <script>
      // The site's own toggle sets <html data-theme>. A gallery with themeSwitch={false} follows it.
      document.getElementById("theme-toggle")!.addEventListener("click", () => {
        const html = document.documentElement;
        html.dataset.theme = html.dataset.theme === "dark" ? "light" : "dark";
      });
    </script>
  </body>
</html>
```

## When the manifest can't be read

The component tries three times, 20 seconds each, then fails the build with an error that names the URL. It also fails the build when the manifest has nothing to show or a prop 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 to the component is a change you make, and commit your lockfile:

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

The component takes its markup, stylesheet and script from `visualci-gallery`, so the HTML and the script that hydrates it always come from one copy of the library. 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-astro-gallery`. 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 `src/pages/index.astro`:

<!-- example: clients/gallery/astro/examples/site/src/pages/index.astro -->
```astro
---
import Gallery from "visualci-astro-gallery";
import Layout from "../layouts/Layout.astro";
---

<Layout title="Acme">
  <h1>Acme</h1>
  <Gallery
    manifest="https://app.visualci.com/g/acme/web.json"
    layout="strip"
    title={null}
    credit={false}
    order={["overview", "access"]}
    seeAll={{ href: "/screenshots/" }}
  />
</Layout>
```

A strip has no Light and Dark buttons: it follows the page, as with `themeSwitch={false}`. `seeAll`'s text is "See all screenshots" unless you give it a `text`.
