This is a solution to the Interactive comments section challenge on Frontend Mentor. Frontend Mentor challenges help you improve your coding skills by building realistic projects.
Users should be able to:
- View the optimal layout for the app depending on their device's screen size
- See hover states for all interactive elements on the page
- Create, Read, Update, and Delete comments and replies
- Upvote and downvote comments
- Bonus: Use
localStorageto save the current state in the browser so that it persists when the browser is refreshed - Bonus: Use timestamps instead of the
createdAtstrings fromdata.jsonand track the time since each comment or reply was posted
- First-level comments are ordered by their score, whereas nested replies are ordered by time added.
- Replying to a comment adds the new reply to the bottom of the nested replies within that comment.
- A confirmation modal pops up before a comment or reply is deleted.
- Adding a new comment or reply uses the
currentUserobject fromdata.json. - You can only edit or delete your own comments and replies.
This solution also makes some choices that the brief leaves open:
- Votes: one vote per entry. Pressing the active vote removes it, the other arrow replaces it, and your own entries can't be voted on.
- Replies to replies stay in the same thread. The editor starts with an
@usernamemention you can edit or delete; the reply is always addressed to that user. - Forms: only one reply or edit form is open at a time. Escape closes it and focus goes back to the button that opened it.
- Content: empty comments can't be sent. A counter appears in the last 100 of the 1000 allowed characters and announces changes after a pause.
- Deleting a comment also removes its replies. Cancelling keeps everything as it was, including an open edit.
- Time: labels such as "2 weeks ago" refresh every minute, and the exact date shows on hover.
- Solution URL: GitHub Repository
- Live Site URL: Interactive comments section
- Semantic HTML5 markup
- Mobile-first workflow with Flexbox and CSS Grid
- React (v19) - JS library, with the React Compiler
- TypeScript - JS superset
- Vite - Frontend build tool
- Tailwind CSS (v4) - For styles, with the palette in
src/index.css - Base UI - Accessible primitives, through local shadcn/ui wrappers (button, textarea, alert dialog)
- class-variance-authority - Button variants
- Motion - Entry, exit and layout animations
-
State: one reducer (
src/contexts/commentsReducer.ts) behind a context and custom hooks. Drafts and the delete dialog stay local to their components. The open reply or edit form lives in its own context, so opening one closes the other. -
Model: entries are a discriminated union. A reply knows its thread and who it answers, and it never has replies of its own:
export type CommentTarget = | { kind: "comment"; id: string } | { kind: "reply"; threadId: string; id: string };
-
Rules in the reducer: IDs and timestamps come from the caller, while author, score and vote are set by the reducer. An action that breaks a rule (editing someone else's reply, voting on your own comment, empty content) returns the previous state.
-
Ordering: roots are sorted only for display, so ties keep their original order:
comments.toSorted((a, b) => b.score - a.score). -
Persistence: the first visit saves the data from
data.json, and every change is saved after it. Before stored data is used, a structural check confirms the fields the page reads: the current user, and for every comment and reply its id, content, valid date, score, vote and author. Missing, invalid, outdated or incomplete data starts again from the original data, while a valid empty list stays empty:const stored = JSON.parse(localStorage.getItem(STORAGE_KEY) ?? "null"); if (stored?.version === STORAGE_VERSION && isCommentsState(stored.state)) return stored.state;
-
Time: the seed labels become ISO timestamps on the first load. A single shared clock ticks every minute and
Intl.RelativeTimeFormatformats the labels, so a 9-day-old reply reads "1 week ago".
Reply and reply-edit forms start with @username, as in the design, and the field's label names the recipient (“Reply to amyrobson”). The mention is ordinary text, so Backspace, selection, paste and undo work without special handling. The recipient lives in replyingTo: before validating and saving, a leading mention is dropped, and the published reply shows it again from replyingTo, even if it was deleted from the editor:
export function stripMention(content: string, replyingTo?: string) {
const text = normalizeContent(content);
const mention = `@${replyingTo}`;
if (!replyingTo || !text.startsWith(mention)) return text;
const rest = text.slice(mention.length);
if (/^[\w.-]/.test(rest)) return text;
return normalizeContent(rest.replace(/^[,:]/, ""));
}The character counter uses the same function, so it counts what will actually be stored.
Each entry stores the current user's vote (-1, 0 or 1). The score is updated from the difference, so switching from upvote to downvote moves it by two:
const vote = nextVote(entry.currentUserVote, direction); // same arrow → 0
const score = entry.score - entry.currentUserVote + vote;With AnimatePresence a deleted comment or a closing form stays in the DOM while it fades out. useIsPresent makes it inert, so it can't be focused or submitted twice. During layout animations the sliding siblings could also cover a form that just opened, and focus-within:z-10 keeps the focused entry on top:
const isPresent = useIsPresent();
return (
<motion.li
layout="position"
inert={!isPresent}
className="relative focus-within:z-10"
>
<article>{/* header, content, score, actions */}</article>
</motion.li>
);The delete button disappears with its comment, so focus can't return to it. When the deletion is confirmed, the dialog's finalFocus returns Reply or Edit on the next entry in the same list, or the previous one, instead of the trigger. A lone reply uses its parent comment; an empty list uses “Add a comment”. Cancel and Escape return focus to the Delete button.
<AlertDialogContent
finalFocus={() => (isConfirmedRef.current ? getFocusAfterConfirm() : true)}
/>The score pulses after each vote, but it is also an aria-live region. Remounting it would change what screen readers announce, so the pulse switches between two identical variants instead:
<motion.span
aria-live="polite"
variants={scoreVariants}
animate={votes % 2 === 0 ? "even" : "odd"}
>
{score}
</motion.span>- Scaling up with reducer and context - The state structure.
- Base UI Alert Dialog - Initial and final focus,
onOpenChangeComplete. - Base UI: animation - CSS transitions with
data-starting-styleanddata-ending-style. - Motion: AnimatePresence and layout animations - Exits,
popLayoutand reordering. - Intl.RelativeTimeFormat - The relative time labels.
- inert - Keeping exiting content out of reach.
I used Claude, through Claude Code, as a pair reviewer, while keeping the decisions and the testing on my side.
- Planning first: every block (UI primitives, domain rules, persistence and time, layout review, animations) started from an audit of the code and a plan I approved, and closed with small commits.
- Decisions: the open points of the brief (vote policy, replies to replies, content limit, what deleting a thread removes) were listed as questions, and I chose each answer.
- Context in local files: an
AGENTS.mdwith the working rules, and notes on the architecture, the open decisions and the acceptance criteria.
- Frontend Mentor - @FerdinandoGeografo
- LinkedIn - @FerdinandoGeografo
- GitHub - @FerdinandoGeografo


