Vibe coding with Claude Code course ยท Module 4: Instagram clone
The Spyglass Window: Grid and Photo Preview from a Sketch
In this lesson8
The gallery already has photos but on a phone three columns give thumbnails the size of a stamp, and no photo can be viewed in a large format. In this lesson Claude Code will build a grid that adapts to the screen and a preview window above the page. Such a window is called a modal: as long as it is open, the rest of the page waits beneath it. What you want, you will show Claude with a drawing, not with a long description.
The problem: a layout is hard to put into words
Try to describe the preview window so that nobody has doubts: where the author stands, where the cross is, whether the caption is under the photo or beside it. You get a long paragraph, and Claude will still guess part of it its own way. The good practices of Claude Code advise in such a situation: instead of describing, hand it over. In lesson 0_5 you handed over a file through @ and a command result. Today you hand over an image, a photo of a sketch from a sheet of paper.
A sketch as part of the request
Claude Code reads PNG, JPEG, GIF and WebP images. The documentation lists three ways to give them to it: drag the file into the Claude Code window, paste an image from the clipboard with the Ctrl+V shortcut (Alt+V on Windows and in WSL), or give a file path in the request. After pasting, an [Image #1] marker appears in the request. We did not test pasting in the Cursor terminal, so in the course, as in lesson 0_5, we give the path. That way works in every terminal.
Move the photo of the sheet from lesson 3_1 to your computer, save it as JPEG or PNG and put it in the project, in a new sketches/ folder, under the name galeria.jpg. Claude reads a file from the project folder without asking for permission, like any other project file.
Just remember that a sketch shows one screen at one moment. It will not tell how many columns there should be on a phone, what the Escape key does or what happens to page scrolling. A good request with an image therefore has two parts: the image says how something looks, and the text says how it works and in which file it should appear.
Request for Claude Code
The change touches three files, and a drawing can be read in several ways, so you start with a plan. The first sentence tells Claude to describe what it sees on the sketch. That way you check whether it read the drawing the way you did, before it writes a single line of code.
Request for Claude Code:
/plan First describe in a few sentences what you see on the sketch
sketches/gallery.jpg. Then plan a photo preview in Treasure Gallery.
Move the grid from @app/gallery/page.tsx into a new Client Component
components/Gallery.tsx: 1 column on a phone, 2 from sm, 3 from md,
4 from lg, gap-1 spacing, every thumbnail is a button. A click opens
the window components/PhotoModal.tsx laid out like on the sketch:
author, cross, big photo, caption. The window is closed by the cross,
the Escape key and a click on the dark backdrop, but not a click on the
window itself. Under the open window the page does not scroll, and
after closing it scrolls as before. End every step with npm run build
and npm run lint.Read the description of the sketch at the start of the plan. If Claude got wrong where the caption or the cross stands, fix the plan before accepting it, like in lesson 1_6. A fix in the plan costs one sentence, and in finished code a whole round.
What Claude wrote: the grid
Open components/Gallery.tsx. The file starts with "use client", because it holds state, and state lives in the browser (lesson 2_3). The state is the selected photo or null when the window is closed:
const [selected, setSelected] = useState<Photo | null>(null);Claude imported the Photo type from lib/photos.ts, so the gallery and the data speak the same language. Under the state stands the grid from lesson 3_1 in new clothes: a list in which every thumbnail is a button that opens the window:
<ul className="grid grid-cols-1 gap-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4">
{photos.map((photo, index) => (
<li key={photo.id}>
<button
type="button"
onClick={() => setSelected(photo)}
className="relative block aspect-square w-full cursor-pointer"
>
<Image
src={photo.src}
alt={photo.alt}
fill
sizes="(min-width: 1024px) 224px, (min-width: 768px) 33vw, (min-width: 640px) 50vw, 100vw"
loading={index < 3 ? "eager" : "lazy"}
className="object-cover"
/>
</button>
</li>
))}
</ul>The grid class turns on the grid (CSS Grid), and you read the column classes from the smallest screen, like in lesson 1_3: grid-cols-1 without a prefix applies everywhere, sm: from 640 pixels gives two columns, md: from 768 three, and lg: from 1024 four. gap-1 is the space between tiles, the same on every screen.
A button instead of a <div> with onClick matters: you reach a button with the Tab key and open it with Enter, and a screen reader reads its name from the photo's alt. sizes changed too. With min-width conditions the browser takes the first matching one, so the list goes from the largest screen. On lg a tile is 224 pixels, because the page is at most 896 pixels wide and there are four columns.
Under the list stands the window, drawn only when some photo is selected:
{selected && (
<PhotoModal photo={selected} onClose={() => setSelected(null)} />
)}&& draws the window only with a selected photo. Closing sets null, and then React removes PhotoModal from the screen entirely, together with its effects. This fact will be useful in a moment in the review.
What Claude wrote: the window
Open components/PhotoModal.tsx. The outer <div> is the dark backdrop over the whole screen, the inner one is the white window:
<div
className="fixed inset-0 z-50 flex items-center justify-center bg-black/80 p-4"
onClick={onClose}
>
<div
role="dialog"
aria-modal="true"
aria-labelledby="photo-modal-title"
className="w-full max-w-3xl overflow-hidden rounded-lg bg-white text-gray-900"
onClick={(event) => event.stopPropagation()}
>
{/* author with a cross, photo, caption */}
</div>
</div>fixed pins the backdrop to the browser window, inset-0 stretches it over the whole screen, and z-50 puts it above the rest of the page. A click on the backdrop calls onClose. A click on the white window would get there too, because an event travels up the element tree, so event.stopPropagation() stops it at the window. role="dialog" and aria-modal="true" tell screen readers that this is a dialog window and the rest of the page is inactive for the time being. aria-labelledby points to the heading with the author as the name of the window.
In the window header stands the cross:
<button
type="button"
onClick={onClose}
aria-label="Close preview"
autoFocus
className="rounded px-2 text-2xl leading-none"
>
ร
</button>The ร character alone tells a screen reader nothing, so the button's name comes from aria-label. autoFocus moves focus to the cross right after opening, so a person with a keyboard is in the window at once. The photo under the header has fill and object-contain, so that you see it whole.
Escape and scroll lock
Two behaviors from the request are side effects, so they live in useEffect from lesson 2_3. The first listens to the keyboard:
useEffect(() => {
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === "Escape") onClose();
};
window.addEventListener("keydown", handleKeyDown);
return () => window.removeEventListener("keydown", handleKeyDown);
}, [onClose]);On every key press the function checks event.key and with Escape closes the window. The function returned from the effect is the cleanup: React calls it when the component disappears from the screen, and then the listener is removed. Without cleanup every opening would add another listener.
The second effect blocks page scrolling under the window:
useEffect(() => {
const previous = document.body.style.overflow;
document.body.style.overflow = "hidden";
return () => {
document.body.style.overflow = previous;
};
}, []);On opening the effect remembers the current overflow value and sets "hidden", so the mouse wheel does not move the page in the background. The cleanup restores the remembered value when the window disappears from the screen.
Captain's review
Open localhost:3000/gallery and go through the window yourself:
- Columns. Narrow the browser window and count columns: four, three, two, one. If a phone still shows three, the class without a prefix is
grid-cols-3instead ofgrid-cols-1. - Three ways to close and one that does not close. The cross, Escape and a click on the dark backdrop close the window. A click on the photo or the caption must not close it.
- Scrolling after closing. Open the window, close it and scroll the page with the mouse wheel. This is the place for a typical agent mistake, an effect that blocks scrolling but does not clean up:
useEffect(() => {
document.body.style.overflow = "hidden";
}, []);Build and lint pass, the window works, and after closing the page stands still. PhotoModal disappeared together with its effect, and overflow: hidden stayed on <body>. In the diff look for overflow without a return in the same effect. Older tutorials restore scrolling in an else branch with an isOpen prop, but with a window drawn by && the component disappears before that branch gets a chance to run.
- Keyboard. Go through the thumbnails with the Tab key, open a photo with Enter and close it with Escape.
The last check is for the eyes: the result next to the sketch. The good practices of Claude Code count a screenshot next to a mockup among the checks that can be passed or failed, like the build or the linter. Take a screenshot of the open window, save it as sketches/result.png and describe only the difference:
Compare sketches/gallery.jpg with sketches/result.png. On the sketch the
author stands above the photo, and in the result below it. Fix only
that order in @components/PhotoModal.tsx and change nothing else.Two images and one sentence about the difference say more than a paragraph about the whole window, and the last sentence keeps Claude away from the rest of the file.
Next step
When the review passes, ask for a commit. Then a small extension:
Add "previous" and "next" arrows to the preview window. After the last
photo the next one is the first, and before the first the last. The
left and right arrow keys work the same way.In the diff look for division with a remainder: the index of the next photo is (index + 1) % photos.length, and of the previous one (index - 1 + photos.length) % photos.length. The % operator returns the remainder of a division, so after the last index zero comes back, and adding photos.length when going back protects against a negative number. If Claude rewrote the whole grid along the way, /rewind goes back to the state before the request.
In the editor below waits a grid with a preview window: change the preview width and count the columns, open a photo, close it in three ways and check whether the page scrolls again.
Remember: the sketch tells Claude how the window looks, the words tell how it works, and you check whether after closing the window the ship sails again.
Code for this lesson: App.jsx
// A grid with a preview window: Escape, a click on the backdrop and scroll lock
import { useEffect, useState } from 'react';
const photos = [
{
id: 'zachod-na-plazy',
src: 'https://images.unsplash.com/photo-1507525428034-b723cf961d3e?w=600',
alt: 'Waves on a sandy beach at sunset',
author: 'Morgan',
caption: 'We buried the chest here',
},
{
id: 'fale-z-gory',
src: 'https://images.unsplash.com/photo-1505118380757-91f5f5632de0?w=600',
alt: 'Turquoise waves at a white beach seen from above',
author: 'Polly',
caption: 'The bay from the lookout',
},
{
id: 'zaglowka-noca',
src: 'https://images.unsplash.com/photo-1534447677768-be436bb09401?w=600',
alt: 'A small sailboat on a calm sea under a starry sky',
author: 'Kapitan Redbeard',
caption: 'Night watch',
},
{
id: 'palmy',
src: 'https://images.unsplash.com/photo-1519046904884-53103b34b206?w=600',
alt: 'Palm trees and a straw umbrella on a white beach',
author: 'Kapitan Redbeard',
caption: 'The island from the map',
},
{
id: 'szlak',
src: 'https://images.unsplash.com/photo-1500530855697-b586d89ba3ee?w=600',
alt: 'A road between red rocks leading toward a mountain',
author: 'Morgan',
caption: 'The trail to the hideout',
},
];
function PhotoModal({ photo, onClose }) {
useEffect(() => {
const handleKeyDown = (event) => {
if (event.key === 'Escape') onClose();
};
window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown);
}, [onClose]);
useEffect(() => {
const previous = document.body.style.overflow;
document.body.style.overflow = 'hidden';
return () => {
document.body.style.overflow = previous;
};
}, []);
return (
<div
className="fixed inset-0 z-50 flex items-center justify-center bg-black/80 p-4"
onClick={onClose}
>
<div
role="dialog"
aria-modal="true"
aria-labelledby="photo-modal-title"
className="w-full max-w-md overflow-hidden rounded-lg bg-white text-gray-900"
onClick={(event) => event.stopPropagation()}
>
<div className="flex items-center justify-between p-3">
<h2 id="photo-modal-title" className="font-semibold">
{photo.author}
</h2>
<button
type="button"
onClick={onClose}
aria-label="Close preview"
autoFocus
className="rounded px-2 text-2xl leading-none"
>
ร
</button>
</div>
<div className="relative h-64 bg-black">
<img
src={photo.src}
alt={photo.alt}
className="absolute inset-0 h-full w-full object-contain"
/>
</div>
<p className="p-3 text-sm">{photo.caption}</p>
</div>
</div>
);
}
export default function App() {
const [selected, setSelected] = useState(null);
return (
<div className="min-h-screen bg-slate-100 p-4 text-slate-900">
<h1 className="mb-4 text-2xl font-bold">Treasure Gallery</h1>
{/* 1 column on a phone, 2 from sm, 3 from md, 4 from lg */}
<ul className="grid grid-cols-1 gap-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4">
{photos.map((photo) => (
<li key={photo.id}>
<button
type="button"
onClick={() => setSelected(photo)}
className="relative block aspect-square w-full cursor-pointer"
>
<img
src={photo.src}
alt={photo.alt}
className="absolute inset-0 h-full w-full object-cover"
/>
</button>
</li>
))}
</ul>
{selected && (
<PhotoModal photo={selected} onClose={() => setSelected(null)} />
)}
</div>
);
}Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. Which Tailwind class creates a grid with 3 columns?
2. What does the aspect-square class do in Tailwind?
These are 2 of 8 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Click in order
Click the pieces of the keyboard listener line in order so that the Escape key closes the window:
- Horizontal ordering
Arrange the pieces of the line with which the effect removes the keyboard listener when the window disappears from the screen:
- Vertical ordering
Order the Tailwind breakpoint prefixes from the smallest to the largest screen:
- Code editor
Responsive grid
- Horizontal ordering
Arrange the pieces of the line with which the effect cleanup restores page scrolling after the window closes:
- Click in order
Click the pieces in order so that the preview window is drawn only when a photo is selected:
- Code editor
Modal component
- Vertical ordering
Order the steps of a user interacting with the enlarged photo modal:
Tasks after this lesson
- QuestionQuiz: CSS Grid
- QuestionQuiz: aspect-square
- Click in orderClicking: Closing the Window with Escape
- Put in orderSorting: Cleaning Up the Keyboard Listener
- Put in orderSorting: Responsive breakpoints
- Coding taskPractice: Gallery grid
- QuestionQuiz: gap in the Grid
- Put in orderSorting: Restoring Scrolling
- QuestionQuiz: stopPropagation in the Window
- QuestionQuiz: Positioning the Window Backdrop
- Click in orderClicking: Window Only for a Selected Photo
- QuestionQuiz: Closing with a Key
- Coding taskPractice: Simple modal
- Put in orderSorting: Opening and closing a modal
- QuestionQuiz: Next Photo in the Window
- QuestionQuiz: A Sketch for Claude Code