Skip to content

Lightbox

Images at the size of the window, one at a time: the screenshots of a report, the images of a bucket. Arrows move between them; Escape closes.

import { Lightbox } from '@sypnia/ui/react'

On this page
  1. Screenshots of a report
  2. When to use it
  3. For everyone
  4. Props

Screenshots of a report

Press a thumbnail and use the arrow keys.

Code
import { useState } from 'react';
import { Lightbox } from '@sypnia/ui/react';

// Press a thumbnail. Arrow keys move; Escape closes and the focus goes back to the thumbnail.
const shot = (title: string, hue: number) => 'data:image/svg+xml;utf8,' + encodeURIComponent(
  `<svg xmlns="http://www.w3.org/2000/svg" width="1280" height="800" viewBox="0 0 1280 800"><rect width="1280" height="800" fill="hsl(${hue} 30% 92%)"/><rect x="40" y="40" width="1200" height="64" rx="12" fill="hsl(${hue} 30% 82%)"/><rect x="40" y="140" width="380" height="620" rx="12" fill="hsl(${hue} 30% 86%)"/><rect x="460" y="140" width="780" height="300" rx="12" fill="hsl(${hue} 30% 86%)"/><text x="80" y="82" font-family="sans-serif" font-size="28" fill="hsl(${hue} 30% 25%)">${title}</text></svg>`);
const images = [
  { src: shot('Sign-in error', 10), alt: 'The sign-in page with the error «Wrong code»', caption: 'The error appears after the second try.' },
  { src: shot('Console log', 200), alt: 'The browser console with two red lines', caption: 'Two requests answer 401.' },
  { src: shot('Network tab', 140), alt: 'The network tab with the failing request selected' },
];
export default function Screenshots() {
  const [open, setOpen] = useState(false);
  const [index, setIndex] = useState(0);
  return (
    <>
      <div className="sy-row">
        {images.map((img, i) => (
          <button key={i} type="button" className="sy-card" onClick={() => { setIndex(i); setOpen(true); }} aria-label={`Open: ${img.alt}`}>
            <img src={img.src} alt="" width={160} height={100} />
          </button>
        ))}
      </div>
      <Lightbox label="Screenshots of the report" images={images} index={index} onIndex={setIndex} open={open} onClose={() => setOpen(false)} />
    </>
  );
}

When to use it

Use it when

  • Thumbnails that people open to read the details.
  • Give each image an alt that says what it shows, and a caption when it helps.

Do not use it when

  • A single small image that is readable as it is.
  • Documents: open PDFs in their own viewer.

For everyone

  • It is a dialog named by label; the counter is read when the image changes.
  • Arrow keys and the buttons move; Escape, the cross or a press on the space around the image closes it, and the focus goes back to the thumbnail.
  • On phones the buttons sit under the image, two by two.

Props

LightboxTypeDefault
imagesrequired LightboxImage[]
indexrequired number
labelrequired

What the images are: «Screenshots of the report».

string
onCloserequired () => void
onIndexrequired (index: number) => void
openrequired boolean
  1. Screenshots of a report
  2. When to use it
  3. For everyone
  4. Props

    Search pages, sections and terms