Gallery in Docusaurus
The visualci-gallery-docusaurus package puts a public gallery in a Docusaurus page: a tab for each group, light and dark, desktop and phone, and a full-size view. The plugin reads each manifest when the site builds, and the <Gallery> component renders it into the page's HTML, in MDX or a React page. 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 Docusaurus 3 (tested with 3.10), and brings the JS library and the React components with it:
npm install visualci-gallery-docusaurus@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, which VisualCI's tests build with docusaurus build and check in a browser.
The plugin
Add the plugin to docusaurus.config.js with the manifests your galleries show. It also adds the gallery's stylesheet to every page:
// The plugin reads each manifest when the site builds; the Gallery component renders one of them.
export default {
title: "Acme",
url: "https://acme.example",
baseUrl: "/",
onBrokenLinks: "throw",
presets: [
[
"classic",
{
docs: { routeBasePath: "docs", sidebarPath: false },
blog: false,
theme: { customCss: "./src/css/custom.css" },
},
],
],
plugins: [["visualci-gallery-docusaurus", { manifests: ["https://app.visualci.com/g/acme/web.json"] }]],
themeConfig: {
colorMode: { respectPrefersColorScheme: true },
navbar: {
title: "Acme",
items: [
{ to: "/docs/screenshots", label: "Screenshots" },
{ to: "/docs/tour", label: "Tour" },
{ to: "/docs/app", label: "The app" },
],
},
},
};
The smallest page
docs/screenshots.mdx:
---
title: Screenshots
---
import Gallery from "visualci-gallery-docusaurus/Gallery";
<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
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:
---
title: Tour
---
import Gallery from "visualci-gallery-docusaurus/Gallery";
<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"]}
/>
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 describes every option, and how screenshot names pair into screens.
The plugin takes timeout (milliseconds for each try, default 20000) and attempts (default 3) beside manifests.
Theming
Set the --vg-* variables to the site's Infima tokens, and the gallery's colours follow Docusaurus's light and dark mode. The example site's src/css/custom.css, the classic theme's customCss:
/* The gallery's --vg-* variables set to the site's Infima tokens, which change with Docusaurus's light and dark
toggle. */
.vg {
--vg-ink: var(--ifm-font-color-base);
--vg-muted: var(--ifm-color-emphasis-700);
--vg-paper: var(--ifm-background-color);
--vg-rule: var(--ifm-color-emphasis-300);
--vg-accent: var(--ifm-color-primary);
}
Gallery options 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. Docusaurus marks its mode on <html data-theme>, which is what the gallery reads, so with themeSwitch={false} the gallery follows the navbar's toggle instead:
---
title: The app
---
import Gallery from "visualci-gallery-docusaurus/Gallery";
The gallery follows the site's own light and dark toggle.
<Gallery manifest="https://app.visualci.com/g/acme/web.json" title={null} themeSwitch={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 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 is a change you make, and commit your lockfile:
npm install --save-exact visualci-gallery-docusaurus@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-docusaurus. 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.tsx:
import Layout from "@theme/Layout";
import Gallery from "visualci-gallery-docusaurus/Gallery";
export default function Home() {
return (
<Layout title="Acme">
<main className="container margin-vert--lg">
<h1>Acme</h1>
<Gallery
manifest="https://app.visualci.com/g/acme/web.json"
layout="strip"
title={null}
credit={false}
seeAll={{ href: "/docs/screenshots" }}
/>
</main>
</Layout>
);
}