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
nullfor closed), owned by the gallery and passed to the lightbox: a controlled component. - Dialog semantics:
role="dialog",aria-modal="true",aria-labelledbypointing 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
bodyoverflow, 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.