Command Palette

Search for a command to run...

Hectal
Phase 18: Accessibility in React
React · Exercise 5 of 6 PreviousNext
Intermediate

A confirm dialog that manages focus

The Delete photo button (id="open") shows a confirm box, but it's rendered with the open attribute, so it isn't modal: focus stays behind it and keyboard users can't tell it appeared.

Open it as a modal with showModal() (a ref and an effect that reacts to the open state), and close it with close(). Label the dialog with aria-labelledby pointing at its heading Delete this photo?. When it opens, focus must be on the Cancel button. Cancel (and Delete) close the dialog, and focus must return to the #open button.

Keep React state in sync when the user presses Escape: handle the dialog's onClose.

What Check tests

  1. The dialog starts closed
  2. Clicking #open opens it as a modal
  3. The dialog is labelled by its heading
  4. Focus is on the Cancel button
  5. Cancel closes the dialog and focus returns to #open
  6. It can be opened again after closing

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Your page

Press Run to see the page.

Console

Nothing logged yet.