Accessibility in Practice: Dialogs, Menus and the Keyboard in React
What shadcn/ui and Radix give you for free and what you still check yourself: a dialog that traps focus and gives it back, a menu driven by arrow keys, the ARIA attributes in the real DOM, and a div-button that Tab skips.
What you will learn
- Follow the focus through a Radix dialog: trap, Escape and return
- Use a dropdown menu that handles the arrow keys for you
- Read the ARIA attributes Radix writes into the DOM
- Find and fix a div-button the keyboard cannot reach, and run a six-point check
Before you start
- shadcn/ui: Components You Own
- React components and props
Picture a small task page and a person who cannot use a mouse. Maybe their hand hurts, maybe they use a screen reader. They only have the keyboard. Without a mouse, focus is the only pointer they have.
In this article we test a dialog and a menu with the keyboard only. The question is simple: what does shadcn/ui (built on Radix) give you for free, and what do you still have to check yourself?
A dialog is a meeting room
Think of a dialog as a meeting room.
- You walk in. Press Enter on Delete, and the focus moves inside, onto Cancel.
- The door locks. Tab goes round and round inside the room and never leaks out to the page.
- You leave. Escape closes the room, and the focus goes back to the seat you came from, the Delete button.
The library moves the focus for you. Here is how.
The delete dialog
import {
Dialog, DialogClose, DialogContent,
DialogDescription, DialogTitle, DialogTrigger,
} from "@/components/ui/dialog"
export default function DeleteDialog() {
return (
<Dialog>
<DialogTrigger className="btn red">Delete</DialogTrigger>
<DialogContent>
<DialogTitle>Delete this task?</DialogTitle>
<DialogDescription>
Write report will be removed for everyone.
</DialogDescription>
<div className="mt-5 flex justify-end gap-2">
<DialogClose className="btn">Cancel</DialogClose>
<DialogClose className="btn red">Delete task</DialogClose>
</div>
</DialogContent>
</Dialog>
)
}
- Line 9, the trigger. A real button, so Tab reaches it and Enter opens the dialog.
- Line 10, the content. This is the room, and Radix traps the focus inside it.
- Line 11, the title, with the description after it. They give the dialog a name, and a screen reader reads them aloud.
- Line 16, the close buttons. Real buttons again, so Enter works, and Escape closes the dialog too.
The Dialog parts come from a small file in src/components/ui/dialog.tsx that wraps Radix, the same way shadcn/ui does. If you have not seen that pattern yet, read shadcn/ui: components you own.
Frame by frame
These are the keys pressed in the running app, and where the focus landed each time:
Tab, Tab button "Delete"
Enter button "Cancel"
Tab button "Delete task"
Tab again button "Cancel"
Escape button "Delete"

Look at the fourth line. After Delete task, Tab goes back to Cancel. It wrapped around and did not escape to the page behind. That is the focus trap. Then Escape closes the dialog and the focus is back on Delete.
Menu: arrow keys
import {
DropdownMenu, DropdownMenuContent,
DropdownMenuItem, DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
export default function ActionsMenu() {
return (
<DropdownMenu>
<DropdownMenuTrigger className="btn">
Actions
</DropdownMenuTrigger>
<DropdownMenuContent>
<DropdownMenuItem>Edit</DropdownMenuItem>
<DropdownMenuItem>Duplicate</DropdownMenuItem>
<DropdownMenuItem>Archive</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
)
}
Line 9 is the trigger, another real button: Enter or Space opens the menu. Line 12 is the menu content. Radix gives it the role menu and handles the keys. The items, Edit, Duplicate and Archive, are reached with the arrow keys.
Tab button "Actions"
Enter div "Edit" role=menuitem
ArrowDown div "Duplicate" role=menuitem
Escape button "Actions"

You wrote no key handling at all. It all comes from the library.
The attributes in the real DOM
What does the page tell a screen reader? Here is the DOM with the dialog open, copied from the running app:
<button aria-haspopup="dialog" aria-expanded="true" aria-controls="radix-_r_2_" data-state="open">
<div role="dialog" id="radix-_r_2_" aria-labelledby="radix-_r_3_" aria-describedby="radix-_r_4_" data-state="open" tabindex="-1">
<h2 id="radix-_r_3_">
<p id="radix-_r_4_">
<div id="root" aria-hidden="true" data-aria-hidden="true">
The trigger says aria-haspopup="dialog" and aria-expanded="true". The dialog has role="dialog", with aria-labelledby pointing at the title and aria-describedby pointing at the description. And the page behind, #root, is aria-hidden, so it is skipped.
And with the menu open:
<div role="menu" id="radix-_r_1_" aria-orientation="vertical" aria-labelledby="radix-_r_0_" data-state="open">
<div role="menuitem" tabindex="0" data-highlighted="" data-orientation="vertical">
<div role="menuitem" tabindex="-1" data-orientation="vertical">
The role is menu and the items are menuitem. Only the highlighted item has tabindex="0", so the arrow keys pass it along. Radix writes all of this for you. But you still supply the words: the title, the labels, the text. You do not need to memorise these attributes. You need to know what to look for.
Break it: a div-button the keyboard cannot reach
Imagine someone builds the Save button like this:
const save = () => alert("Saved")
const look = "btn blue"
// A div that looks like a button. The mouse works, the keyboard cannot reach it.
export default function BrokenToolbar() {
return (
<div className="flex gap-2">
<div className={look} onClick={save}>Save</div>
<button className="btn">Cancel</button>
</div>
)
}
A div with a click handler. It looks like a button and the mouse works perfectly. Now press Tab from the top:
Tab 1 button "Actions"
Tab 2 button "Delete"
Tab 3 button "Cancel"
Tab 4 body "Team tasksWrite repo"
Save is skipped completely, and the fourth Tab leaves the page. A keyboard user can never press Save. The fix is small, a real button:
const save = () => alert("Saved")
const look = "btn blue"
// The fix: a real button. Tab reaches it, Enter and Space press it.
export default function GoodToolbar() {
return (
<div className="flex gap-2">
<button className={look} onClick={save}>Save</button>
<button className="btn">Cancel</button>
</div>
)
}
Tab 1 button "Actions"
Tab 2 button "Delete"
Tab 3 button "Save"
Tab 4 button "Cancel"

A div has no keyboard behaviour at all. A real button gives you Tab, Enter and Space for free. The project also keeps the focus ring visible on every button with one rule in index.css:
/* A visible focus ring on every button, for keyboard users. */
button:focus-visible { outline: 2px solid #2563eb; outline-offset: 2px; }
The six-point checklist
- Real elements. Anything clickable is a button or a link, never a div.
- Visible focus. You can always see where the focus is. Keep the focus ring.
- Names. An icon button gets an
aria-label, and every input gets a label. - Tab order. It follows the page: top to bottom, left to right.
- Contrast. Text stands out from its background in every theme. The numbers are in colour, contrast and dark palettes.
- The keyboard-only test. Unplug the mouse, open every dialog and menu, and press Escape.
Two minutes before every release. That is the habit.
Recap
| Radix gives you | You check |
|---|---|
| Focus trap and focus return in dialogs | Names: titles, labels, aria-label on icon buttons |
| Arrow keys and Escape in menus | Contrast and a visible focus ring |
| Roles and ARIA attributes | Real buttons, and a keyboard-only test |
We covered the foundations: real buttons, a visible focus ring, and a dialog and a menu that handle the keys. AI can generate the rest, and you check it with the keyboard.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
One Request, Two Answers: the Same Post as an HTML Page and as JSON
Ask a Django blog for the same post twice: once as a 12,731-byte HTML page built by a template, once as 2,976 bytes of JSON built by a serializer. Compare the headers, see who builds the page, and why the server still owns every write.
A Blog Design from a Prompt: Downloading Figma Make Code and Reading App.tsx and BlogCard.tsx
Make a blog design from a prompt in Figma Make, download src.zip, unzip it with Python's zipfile module, and read the two files that matter: App.tsx with its selectedPost state and BlogCard.tsx with its props.
How a Web Page Works: HTML Tags, Elements and Attributes
A web page is a text file full of labelled pieces. Learn tags, attributes and nesting with three tiny pages, and see what breaks when an attribute or a closing tag is wrong.
Semantic HTML: header, nav, main, aside and footer as Page Landmarks
The same page written as div soup and with semantic tags looks identical, but only one gives screen readers and search engines landmarks and an outline. Plus tables, div, span and the one-main rule.
