Skip to the solution
MediumMachine Coding11 min read

Build an Image Gallery with lightbox

761 views

The problem

Question presented to candidate: "Build a photo gallery page: a responsive grid of thumbnails, and clicking one opens it full screen in a lightbox. In the lightbox the user can go to the previous or next photo, use the arrow keys, see a caption and a '3 / 9' counter, and close it with Escape, a close button or a click on the dark background. It must be usable with a keyboard and a screen reader."

Requirements:

  • A CSS grid of thumbnail buttons that reflows from one column on a phone to many on a desktop.
  • A lightbox overlay with the large photo, caption, counter, previous / next (wrapping) and close.
  • Keys inside the lightbox: Left / Right, Escape, and Tab that stays inside the dialog.
  • Opening moves focus into the dialog; closing returns focus to the thumbnail that opened it.
  • The page behind does not scroll while the lightbox is open; neighbouring photos are preloaded.

What a strong answer should cover:

  • One piece of shared state, the open index (or null for closed), owned by the gallery and passed to the lightbox: a controlled component.
  • Dialog semantics: role="dialog", aria-modal="true", aria-labelledby pointing at the caption.
  • Focus management: initial focus, a focus trap for Tab and Shift+Tab, and focus restore on close.
  • Scroll lock that saves and restores the previous body overflow, and cleanup on unmount.
  • Rendering the overlay at the end of body (portal / Teleport) or an equivalent, and why stacking contexts make that useful.
  • Preloading neighbours with new Image() and lazy-loading thumbnails.

Clarifying questions expected:

  • "Should the browser Back button close the lightbox (URL state), and should a photo be linkable?"
  • "Do we need swipe or pinch-zoom on touch devices?"
  • "Can we use the native <dialog> element, or must it support older browsers?"

Code / implementation expected: Yes. A gallery and lightbox component with keyboard support, focus trap and restore, scroll lock and wrap-around navigation.

  • React
  • Gallery
  • Lightbox

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

11 min read ยท 10 sections

React solution

Every file of the working solution. Open it in the playground to run and change it.

ReactComplete solution
import Gallery from "./src/components/Gallery";
import { photos } from "./src/data/photos";

export default function App() {
  return (
    <main className="app">
      <h1>Photo gallery</h1>
      <p className="hint">
        Click a photo. In the lightbox use the arrows or Left / Right keys, Esc
        or the backdrop to close.
      </p>
      <Gallery items={photos} />
    </main>
  );
}

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 43 of 107 in Machine coding

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

Build an Autocomplete / Typeahead search

Continue