Accessible delete dialog requiring the typed repository name
A repository settings page has a Danger zone with a button that deletes the repository. Because deleting can’t be undone, the button opens a confirmation dialog that asks the user to type the repository name first. Build the page and the dialog with the right ARIA roles, states and properties, so a screen reader announces what is about to happen and keyboard users never lose their place.
The starter code includes the repository name:
const REPO_NAME = 'acme/billing-api';
Repository: acme/billing-api, and a <button> whose text is exactly Delete repository.aria-haspopup="dialog", and aria-expanded that is "false" while the dialog is closed and "true" while it is open. While it is open, also point aria-controls at the dialog’s id.Render a backdrop element whose direct child is the dialog panel. The panel:
role="alertdialog" (it interrupts the user to confirm a destructive action) and aria-modal="true",Delete acme/billing-api?, referenced by aria-labelledby,This permanently deletes the repository, its issues, and its pull requests. This cannot be undone., referenced by aria-describedby,Type acme/billing-api to confirm, a Cancel button and a Delete this repository button.disabled attribute, or aria-disabled="true" plus a guard) until the input, after trimming spaces at both ends, equals REPO_NAME exactly. The comparison is case-sensitive.acme/billing-api was deleted. in an element with role="status". Move focus to that message (give it tabIndex={-1} / tabindex="-1"), because the button that had focus is gone. Enter does nothing while the name doesn’t match.keydown on document.Click "Delete repository" dialog opens, aria-expanded="true", focus in the input
Type "acme/billing" "Delete this repository" stays disabled
Type "ACME/billing-api" still disabled (case-sensitive)
Press Escape dialog closes, focus back on "Delete repository"
Click "Delete repository" input is empty again
Type " acme/billing-api " button enabled (outer spaces are ignored)
Press Enter dialog closes, "acme/billing-api was deleted." is focused
<body>.keydown listener when the dialog closes or the component unmounts.The tests click your buttons, type into the input, press Enter and dispatch keydown events with key: 'Escape' on the focused element (they bubble to document). You don’t need test ids or class names. The test frame is hidden, so the tests track focus through element.focus() calls and document.activeElement: move focus with .focus() (React’s autoFocus also works). They check that:
aria-haspopup="dialog" and the right aria-expanded.role="alertdialog" element, with aria-modal="true", and its aria-labelledby and aria-describedby elements contain the title and description above. The input is found by its label text, and the buttons by their text.disabled or aria-disabled="true".mousedown, mouseup and click on the panel’s parent element.acme/billing-api was deleted appears after deleting, and document.activeElement is the element showing it.A closed dialog can be removed from the page or hidden (hidden, display: none, or visibility: hidden). Capitalization and extra spacing in the texts don’t matter.