Skip to content

Keyboard and focus patterns for shadcn

Rules / Keyboard: 5 rules and 0 guidelines

Many people use an app without a mouse. Some use the keyboard because a pointer is hard for them, some use a screen reader, a switch or voice control, and some simply prefer it. For all of them, two things decide whether an app works at all. Can every action be reached and done from the keyboard? And can they see where they are?

shadcn’s components give a good start here. Button is a real <button>, and the Radix primitives under the checkbox, switch, radio group and dialogs handle the keys: the checkbox and the switch respond to Space, a radio group moves with the arrow keys, and dialogs move focus in and out on their own. Most keyboard problems appear in the code written around those components.

Where keyboard access goes wrong

  • A clickable div, card or table row that opens on click but cannot be reached with Tab. KBD-001
  • Actions that only appear on hover, such as an edit icon on a card. KBD-001
  • Drag and drop with no other way to move the item. KBD-001
  • outline: none with nothing to replace it, or a focus style too faint to see. KBD-001
  • A sticky header, cookie banner, chat button or toast that covers the field with focus, so the person tabs on blind. KBD-002
  • A single-letter shortcut that fires while someone uses voice control, with no way to turn it off, or shortcuts that nobody can find. KBD-003, KBD-003a, KBD-003b

How the rules fit together

KBD-001 is the base. Everything a pointer can do, the keyboard can do, unless the task depends on the path of the movement itself, such as drawing by hand. And whatever has focus is visibly marked, for as long as it has focus.

KBD-002 covers the next step. A focus ring only helps if it is on screen, so the focused element must never be completely hidden behind something the page put on top of it. Sticky headers and footers are the usual cause. When you add one, add matching scroll padding, so an element that receives focus is scrolled clear of it.

Three texts cover the shortcuts a product adds. KBD-003b builds them on a modifier key, such as Ctrl or Cmd, by default. KBD-003 lets people turn off or change any shortcut made only of character keys, unless it works only while its own control has focus. KBD-003a shows every shortcut beside its command or in one list.

Most other areas lean on these two. The dialog patterns say where focus goes when a dialog opens and closes (DLG-006) and keep Tab inside it (DLG-007). The form patterns move focus to the first problem, or to a summary of them, when a form is sent (FRM-011, a guideline, our choice). Enter in a single-line field sends the form (FRM-015). A tooltip opens on focus as well as on hover (TXT-002b). And the layout patterns keep the order the keyboard follows the same as the order on screen (LAY-002). In tables, a sortable heading and every row action are controls the keyboard reaches (TBL-002, TBL-003). A skip link gets the keyboard past a long menu (NAV-004, a guideline, our choice). And anything that can be dragged also needs a way that takes a single press, since a keyboard route alone does not serve someone with a pointer and no keys (ACT-007a).

Our theme has a single focus colour, and its contrast against the page is checked in both themes, so a visible indicator is the default rather than something each screen has to add.

The quickest test needs no tools. Put the mouse away and do one real task from start to finish: sign up, change a setting, delete something and get it back. Each time you lose track of where focus is, or cannot reach something, you have found a place where one of these rules is broken.

What WCAG requires and what the judges decide

Three rules in this area are requirements of WCAG 2.2. KBD-001 follows from 2.1.1 Keyboard and 2.4.7 Focus Visible, KBD-002 from 2.4.11 Focus Not Obscured (Minimum), and KBD-003 from 2.1.4 Character Key Shortcuts. They are the floor, not extra polish. The judges are recorded too: all three supported KBD-001, the one who spoke supported KBD-002 and both who spoke supported KBD-003, but for a requirement the verdict comes from the law. Showing every shortcut (KBD-003a) and a modifier key by default (KBD-003b) go beyond the law, and both judges who spoke support them.

Rules and guidelines

Each one has its own page with the full rule, Do and Don't cases, exceptions and the verdict. A rule is decided by WCAG 2.2 or by the judges. A guideline is our own choice. How the method works

Required by WCAG 2.2

A WCAG 2.2 Level AA criterion requires the behaviour. Where a rule adds advice of its own, it says so.

Decided by the judges

No WCAG criterion requires these. The judges who spoke support them, all or most of them.

  • KBD-003a Show every shortcut the product adds, beside its command or in one listRule. Unanimous (2 of 2). Proposed, may still change.Every keyboard shortcut the product adds can be found inside the product: shown beside the command it triggers, such as in a menu item or tooltip, or listed with the others in one place, such as a shortcuts dialog or help page.
  • KBD-003b Build a shortcut on a modifier key by defaultRule. Unanimous (2 of 2). Proposed, may still change.A keyboard shortcut the product adds includes a modifier key (Ctrl, Alt or Cmd) or Shift by default. A single key, such as n or /, works as a shortcut only after the person turns such shortcuts on, or only while its own control has focus.

All rules · Is shadcn accessible? · Give the rules to an AI agent

Justified by Blackwood & Blake helps you work toward WCAG 2.2 AA. It is not a certification. Read more