Skip to content

Accessible form patterns for shadcn

Rules / Forms: 15 rules and 12 guidelines

Every field in a form raises the same questions. What is it called, and does that name stay on screen? When does an error appear, where, and when does it go away? Which fields may be left empty? Can the browser fill in the address? Which keyboard does a phone show, and does Enter send the form? shadcn’s Input, Label, Checkbox and RadioGroup work with the keyboard and expose the right roles, but none of them answers those questions. The patterns on this page do.

Where forms go wrong

Each of these looks fine in a screenshot:

  • A placeholder used as the label. Once the person types, the field has no visible name. FRM-001
  • A label that is drawn but not connected to its control, so a screen reader announces an unnamed text box. FRM-010
  • Errors collected in a banner at the top or in a toast, so nobody can tell which field is wrong. FRM-002a
  • An error that shows up while the person is still typing their email address, or one that stays after it has been fixed. FRM-002b
  • An asterisk that nothing explains, or required fields marked on one form and optional fields on the next. FRM-005
  • autocomplete="off" on a name or address field, or a password field that blocks paste. FRM-007, FRM-008
  • An email field typed as plain text, or a card number in type="number", so a phone shows the wrong keyboard. FRM-014, FRM-014a, FRM-014b, guidelines (our choice)
  • A search field where Enter does nothing, or a checkbox whose text does nothing when clicked. FRM-015, FRM-016a, and FRM-016b, a guideline (our choice)
  • A field that rejects a pasted phone number because of its spaces, or cuts a long paste at its limit without a word. FRM-017, FRM-017a, FRM-017b, FRM-017c, guidelines (our choice)
  • A long page form that loses everything when the tab closes. FRM-004c, FRM-004d, guidelines (our choice)
  • A checkout that asks for the delivery address twice. FRM-009
  • A form sent with problems that leaves focus on the submit button, so keyboard users hunt for the problem. FRM-011, a guideline (our choice)
  • A select that loads another page, or a checkbox that sends the form, as soon as its value changes. FRM-012a, FRM-012b
  • A date field whose calendar opens on focus and takes focus with it, so Tab never gets past the field. FRM-013

How the rules fit together

First each field gets a name that people can see (FRM-001) and that assistive technology can read (FRM-010). Then the form shows which fields are required or optional, one way and in the label (FRM-005), and it fills in what the browser or the process already knows (FRM-007, FRM-009). Signing in has its own rule, because a password field that refuses paste can lock people out (FRM-008).

Each field also gets the keyboard its value needs: an input type for email, phone and web addresses (FRM-014), the digit keypad for digits (FRM-014a), and a text input, never a number input, for digits that are not a quantity (FRM-014b). Every field takes a paste (FRM-017) and removes the spaces and separators people write before checking (FRM-017a). A free-text field with a limit states it and shows how much is used (FRM-017b), and says in words when it is reached (FRM-017c). Enter in a single-line field sends the form (FRM-015), and a click on the text beside a checkbox or radio button changes it (FRM-016a), as on a switch (FRM-016b). All of these except FRM-015 and FRM-016a are guidelines (our choice).

When something is wrong, three texts work as one. FRM-002a says where the error goes: as text at the field. FRM-002b says when: after the person leaves a field they changed, or when they send the form, and gone as soon as the value is valid. FRM-011, a guideline (our choice), says where focus goes when a form is sent with problems: to the first problem or to a summary of them. The wording is TXT-001a.

Three texts cover what a control may set off. FRM-012a: changing a value does not send a form, open a page, window or dialog, or move focus, unless text before the control says it will. Changes in place, such as a list that filters, are allowed. FRM-012b goes further: a control that holds a choice, such as a select, radio group, checkbox or switch, only records it and may update the view in place. Going to another page or step, sending data or running a command takes a button, a link or a menu of actions, even with a notice. FRM-013: a control receiving focus never sends a form, opens a page, window or dialog, or moves focus on, and no notice changes that.

Forms in dialogs add two more questions. Each one needs a Cancel that always works and changes nothing (FRM-003). Closing one with unsaved input asks first, but only when something would be lost: FRM-004a covers full-screen dialogs, and FRM-004b, a guideline, smaller dialogs and sheets. Leaving a page raises the same question. FRM-004c asks first by every route out, and FRM-004d keeps what people entered in a longer page form and shows that it is kept; both are guidelines. The dialog patterns cover the rest of how those dialogs behave, and the button patterns cover the submit button, including when it may be unavailable.

What WCAG requires and what the judges decide

The rules on autocomplete, signing in, repeated entry, names in code and changes on input and on focus come straight from WCAG 2.2: 1.3.5 Identify Input Purpose, 3.3.8 Accessible Authentication (Minimum), 3.3.7 Redundant Entry, 4.1.2 Name, Role, Value, 3.2.2 On Input and 3.2.1 On Focus.

WCAG asks for labels (3.3.2 Labels or Instructions) but not that they stay visible at the control, and it does not say where and when errors appear, how required fields are marked, or that a dialog needs a Cancel. The judges decide those, and on most of them every judge who spoke was in support. Visible labels are a majority: two of the three judges support FRM-001. Keeping choices apart from actions even where a notice would satisfy 3.2.2 (FRM-012b) goes beyond the law, and all three judges support it. So does Enter sending the form (FRM-015), and both judges who spoke support a checkbox or radio button whose text can be clicked (FRM-016a). Asking before a small dialog or a page throws input away (FRM-004b, FRM-004c), keeping a draft (FRM-004d), moving focus to the first problem (FRM-011), and the texts on input types, switch text, paste and length limits are guidelines, our choice, because too few judges have spoken; our reasons are on each page.

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.

Guidelines, our choice

The judges are split or too few have spoken, so we choose and give our reasons. A product that chooses otherwise is not wrong.

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