Skip to
Keyboard shortcuts

Use the up and down arrow keys to move between items. Press Alt+Shift+K to open this list again.

Alpha version out now. The API may still change.

ErrorSummary

All the problems of a failed submit in one place at the top of the form, each a link to its field. It takes focus when it appears and after each failed submit, so a screen reader can say its title and a keyboard user starts at the list.

Package
@kvirn-ui/react
Testing
Automated. Core 0 of 2, Release 0 of 3

When to use it#

  • Use it after a failed submit, when people may not see every error at once: a form with several fields, or a long one.
  • Show it at the top of the main content, above the form, and only while there are errors. Pass the submit count as focusKey, so a second failed submit moves focus again.
  • Write each link as the field’s own error, with what to do: “Enter your email address”, not “Invalid”. Keep the error under the field too.
  • Not for a message that isn’t about a form: use an Alert. Don’t update it while the user types.

Example#

Use cases#

A form that fails to send

On submit, check the answers, mark the fields invalid and show the summary. Each link and its field’s error say the same words, and each link points at the id you gave the field with controlId.

Props in this example:

Warning: Don't forget

Set controlId on every Field.Root a link points at, and keep the Field.ErrorMessage under the control (3.3.1 Error Identification). A link to an id that isn’t on the page goes nowhere, and a development warning says so.

An error on a group of fields

When the question is the group, such as first and last name, the error belongs to the Fieldset. Link to the first box that is wrong: the link focuses it and scrolls its label into view. A group of radio buttons links to its first option in the same way.

What is your name?

Props in this example:

A choice and a confirmation that were left out

Most forms mix text fields with options and a confirmation. A group of radio buttons links to its first option, and a checkbox to the box itself. Set controlId on the first RadioGroup.Option and on the checkbox’s Field.Root, and link to those ids.

Which vehicle is the permit for?

Props in this example:

A summary inside a dialog

A form in a Dialog needs the same summary. The dialog’s title is an h2, so give the summary’s Title as="h3" to keep the outline right. Clear the errors when the dialog closes, because its content stays in the page.

Change phone number

We send a code by text message to confirm the new number.

Props in this example:

  • as on ErrorSummary.Title
  • focusKey on ErrorSummary.Root

Say it in the page title too

The page title shows in the browser tab and is read first when a page loads. With prefixDocumentTitle the title starts with “Error:” while the summary is shown, and it is restored when you remove it.

Tab title now:

Props in this example:

Information: Implement like this

If a router owns the title, leave the prop off and add the word from errorSummary.titlePrefix there.

Accessibility#

What it does for you#

  • APG pattern: none. It applies the GOV.UK Error summary pattern and WCAG technique G139 (create a mechanism that allows users to jump to errors), with ARIA22's alternative (a focus move) in place of a live region.
  • Deviations: none. It is not role="alert": the focus move is the announcement.
  • Native elements used: <ul>, <li>, <a href="#id">, <h2> (from Alert.Title; as picks h3 to h6), and <ol> with as on the List.

An Alert.Danger (see the Alert page) at the top of a form, shown after a failed submit, that lists every problem as a link to its field. Focus moves to it once, so the screen reader reads it and a keyboard user starts at the list.

PartElement / roleARIANotes
Root<div> (Alert.Danger) → grouparia-labelledby = the Title's idtabindex="-1": focusable by script, not a Tab stop. No aria-live, no role="alert". Icon, status word and colour come from Alert.Danger
Title<h2> (Alert.Title) → headingidDefault text errorSummary.title. The status word ("Error:") comes first
List<ul> → listrole="list"One item per problem. role="list" is explicit: the theme draws no markers, and Safari would drop the list semantics without it
Item<li> → listitemnone
Link<a href="#controlId"> → linknoneIts text is the field's error text, so it reads the same as the message under the field

It announces nothing.

Information: Did you know?

The summary has no live region (a part of the page that a screen reader reads aloud when it changes) on purpose: the focus move is the announcement, and a live region on top of it would read it twice. A screen reader usually says only the name of a focused group, so listen with the screen readers your users have to confirm what they hear.

What you need to do#

  • Render it only after a failed submit, at the top of main, before the step caption and the form, and pass the submit count as focusKey so a second failed submit moves focus again.
  • Each Link's controlId is the id of the control (Field.Root controlId) or, for a group, of its first option. Its text is the same words as the field's error, including what to do (3.3.3 Error Suggestion (AA)).
  • Keep the field's own error under the control (3.3.1 Error Identification (A)): the summary adds to it.
  • Update document.title with prefixDocumentTitle unless a router owns the title, then do it there with errorSummary.titlePrefix.
  • Remove the summary when the form has no errors; never show it while the user types.

Visual and modes#

  • Focus indicator: the summary shows a focus ring when the browser treats the focus as keyboard focus; the links have the usual link ring.
  • Target size: the links sit in a list with the theme's spacing and are at least 24 by 24 CSS pixels (2.5.8 Target Size (Minimum) (AA)).
  • forced-colors behaviour: the Alert's bar uses the system's text colour, and the status icon and word still say danger.
  • reduced-motion behaviour: scrolling is instant, never smooth.

Focus management#

  • Initial focus: on mount, and each time focusKey changes, the summary root takes focus (focus() on tabindex="-1"). With no focusKey, once on mount. disableAutoFocus skips both, for a static preview where the move would scroll the page (focus › disableAutoFocus leaves focus where it was, on mount and when focusKey changes); an app leaves it off. It is rendered only while there are errors and never updates while the user types.
  • A link: preventDefault, focus({ preventScroll: true }) on the control with that id, then the control's label (label[for]) or its fieldset's legend is scrolled into view. Never smooth scrolling. A click with Ctrl, Meta, Shift, Alt or a non-primary button, and a link whose target is missing, are left to the browser (the hash navigation).
  • A link whose control can't take focus (hidden, disabled, not focusable): focus() leaves document.activeElement elsewhere, so the link does not preventDefault and the browser's own hash jump runs. A development warning error-summary-control-unfocusable:<id> is shown once per id (error-summary.test.tsx › links › a link whose control cannot take focus is left to the browser and warns once). After a native jump to a non-focusable target focus can fall to body; point the link at the focusable control.
  • A group of options: link to the first option's id.
  • Trap: no. Restore to: not applicable. After a fix the consumer removes the summary on the next submit; focus then moves to the new one or, with no errors, to where the submit goes.
  • Never obscured by: the summary is scrolled into view by focus(). Pair it with scroll-padding-top for a sticky header and scroll-padding-bottom for the on-screen keyboard (2.4.11 Focus Not Obscured (Minimum) (AA)).
  • The focus ring shows when the browser treats the focus as keyboard focus (:focus-visible).

Warning: Don't forget

If your page has a sticky header, set scroll-padding-top on the page, so the focused summary isn’t hidden behind it (2.4.11 Focus Not Obscured (Minimum), AA).

Allowed elements#

A tag outside the list changes the page's outline or semantics (1.3.1 Info and Relationships (A), 4.1.2 Name, Role, Value (A)). as is a string, so it works from a Server Component.

Part`as`Why
RootnoneThe Root is an Alert.Danger, a named group that takes focus
Titleh2 (default), h3, h4, h5, h6The level the page's outline needs. No h1 (the page's title) and no p (the summary needs a heading for the group's name)
Listul (default), olA list of problems, optionally numbered
Item, Linknone<li> and <a href="#id">: the link must stay a link (4.1.2 Name, Role, Value (A))

WCAG success criteria#

  • 3.3.1 Error Identification (A), 3.3.3 Error Suggestion (AA): each problem is named in text, linked to its field (links › each link goes to its control id and reads the field error text).
  • 2.4.3 Focus Order (A): focus moves to the summary on a failed submit and then to the field (focus › moves focus to the summary on mount, focus › moves focus again when focusKey changes, even from a link inside it, › a link moves focus to the field).
  • 4.1.3 Status Messages (AA): the summary takes focus, so it needs no live region (announcements › nothing is said in the Announcer).
  • 1.3.1 Info and Relationships (A), 4.1.2 Name, Role, Value (A): native list and links, a named group. 2.4.2 Page Titled (A): the prefix on the page title (document title › prefixes the page title while shown and restores it on unmount).
  • 2.4.7 Focus Visible (AA): the summary and the links show a focus ring (Visual / modes; reviewed in the stories, no named test).
  • 1.4.1 Use of Color (A): the links are underlined and the Alert's danger word and icon say danger, so colour is not the only cue (theme properties, no named test).
  • 1.4.3 Contrast (Minimum) (AA): the theme's colour pairs for the Alert meet the minimum contrast, checked automatically for every theme, and axe finds no violation (accessibility › no axe violations with the summary focused).
  • 2.5.8 Target Size (Minimum) (AA): the links are at least 24 by 24 CSS pixels (Visual / modes; reviewed in the stories, no named test).
  • Axe: no violations with the summary focused (accessibility › no axe violations with the summary focused).

Keyboard#

Focus
native
Selection follows focus
n/a
Arrows wrap
n/a
Shortcuts
none

The summary is not a widget. Focus is moved to it by script, once per submit; then the links are native Tab stops in DOM order. A link's activation focuses the field. The component never intercepts a key.

KeyWhereWhat happens
Tabthe summary has focusMoves to the first link in the list
Test: error-summary.test.tsx › keyboard › Tab from the summary moves to the first link
Tabon a linkMoves to the next link, and after the last one on into the page (the form's first field)
Test: error-summary.test.tsx › keyboard › Tab moves through the links and then out of the summary
Shift+Tabon the first linkMoves to the stop before the summary: the summary itself is not a Tab stop
Test: error-summary.test.tsx › keyboard › Shift+Tab from the first link goes to the stop before the summary
Enteron a linkMoves focus to the field and scrolls its label into view
Test: error-summary.test.tsx › keyboard › Enter on a link moves focus to its field

API reference#

import { ErrorSummary, useErrorSummary } from '@kvirn-ui/react'

ErrorSummary.Root

Renders An Alert.Danger: a <div> with the role group, named by the Title, and tabindex="-1", so code can move focus to it and it is not a Tab stop. It has no live region. It takes every attribute of a <div> and passes ref to it. Also exported as ErrorSummaryRoot. The default theme is described on Theming.

PropTypeDefaultDescription
focusKeystring | number–Moves focus to the summary on mount and each time it changes. Pass the submit count, so a second failed submit moves focus again. Without it, focus moves once, on mount.
disableAutoFocusbooleanfalseNever moves focus to the summary, on mount or when focusKey changes. For a static preview, such as a docs page or a screenshot, where the move would scroll the page. Leave it off in an app: the focus move is how the summary is announced.
prefixDocumentTitlebooleanfalsePuts the text errorSummary.titlePrefix (“Error:”) before the page title (document.title) while the summary is shown, and restores the title when it is removed. Leave it off if your router owns the title.
messagesPartial<KvirnMessages['errorSummary']>–Per-instance override of the title and of the page title prefix, such as { title: "Fix the errors" }.
refRef<HTMLElement>–Reaches the element, whichever it is.
Class or attributeValuesMeaning
kv-error-summaryalwaysThe part class. The default theme styles it.
kv-alert, kv-alert--dangeralwaysFrom Alert.Danger: the danger colour, icon and status word.
rolegroupA named group, not an alert and not a landmark.
tabindex-1Focusable by script, so it takes focus; not a Tab stop.
aria-labelledbythe Title’s idNames the group.

ErrorSummary.Title

Renders <h2> with the role heading, level 2, from Alert.Title. The status word “Error:” comes first. Without children it reads errorSummary.title. Also exported as ErrorSummaryTitle.

PropTypeDefaultDescription
as'h2' | 'h3' | 'h4' | 'h5' | 'h6''h2'Sets the heading level, for example as="h3". The status word stays. There is no h1 and no p: the summary needs a heading for the group’s name. Children replace the default text.
refRef<HTMLElement>–Reaches the element, whichever it is.
Class or attributeValuesMeaning
kv-alert-titlealwaysFrom Alert.Title.
kv-alert-statusalwaysThe status word that starts the Title.
idgeneratedNames the group.

ErrorSummary.List

Renders <ul> with the role list, one Item per problem. Also exported as ErrorSummaryList.

PropTypeDefaultDescription
as'ul' | 'ol''ul'A list of problems, optionally numbered.
refRef<HTMLElement>–Reaches the element, whichever it is.
Class or attributeValuesMeaning
kv-error-summary-listalwaysThe part class.
rolelistExplicit, because the theme draws no markers and Safari then drops the list.

ErrorSummary.Item

Renders <li> with the role listitem, holding one Link. Also exported as ErrorSummaryItem.

PropTypeDefaultDescription
refRef<HTMLElement>–Reaches the element, whichever it is.
Class or attributeValuesMeaning
kv-error-summary-itemalwaysThe part class.

Renders <a href="#controlId"> with the role link. Its text is the field’s own error text. A click or Enter focuses the control and scrolls its label (or, for a group, its legend) into view. With a modifier key, when no element has the id, or when the control can’t take focus, the browser handles it. It takes every attribute of an <a> except href, and passes ref to it. Also exported as ErrorSummaryLink.

PropTypeDefaultDescription
controlIdstringRequiredThe id of the control the error is about, or of a group’s first option. Sets href to “#” plus the id. Set the id with controlId on Field.Root.
refRef<HTMLAnchorElement>–Reaches the link.
Class or attributeValuesMeaning
kv-linkalwaysThe Link class.
kv-error-summary-linkalwaysThe part class.

useErrorSummary

Options

PropTypeDefaultDescription
focusKeystring | number–Moves focus to the summary on mount and each time it changes. Pass the submit count.
disableAutoFocusbooleanfalseNever moves focus to the summary. For a static preview only.
prefixDocumentTitlebooleanfalsePuts errorSummary.titlePrefix before document.title while the summary is shown.
messagesPartial<KvirnMessages['errorSummary']>–Per-instance override of the title and of the page title prefix, such as { title: "Fix the errors" }.

Result

PropTypeDefaultDescription
rootPropsErrorSummaryRootPartProps–Spread on the summary’s element, usually an Alert.Danger: class, role group, tabIndex -1, aria-labelledby and a callback ref that moves focus.
titlePropsErrorSummaryTitlePartProps–Spread on the heading. The id names the group.
listPropsErrorSummaryListPartProps–Spread on the <ul>: class and role list.
itemPropsErrorSummaryItemPartProps–Spread on an <li>.
getLinkProps(controlId: string) => ErrorSummaryLinkPartProps–Spread on an <a>: class, href and the click handler that focuses the control and scrolls its label into view. A modified click and a missing control are left to the browser.
titlestring–The text errorSummary.title: the default text of the heading.

Strings

These are the default texts in @kvirn-ui/i18n.

For your whole app, pass messages to KvirnProvider. For one ErrorSummary, pass its messages prop.

errorSummary.title

The default text of the Title. It names the focused group.

LanguageDefault text
SvenskaDet finns ett problem
SuomiLomakkeessa on virheitä
Norsk bokmålDet er et problem
Norsk nynorskDet er eit problem
EnglishThere is a problem

errorSummary.titlePrefix

Put before the page title while the summary is shown, with prefixDocumentTitle.

LanguageDefault text
SvenskaFel:
SuomiVirhe:
Norsk bokmålFeil:
Norsk nynorskFeil:
EnglishError:

Known issues#

  • A screen reader may read only the group's title when it takes focus. The first Tab reads the first error.

Testing#

The automated tests prove the keyboard behaviour, the names and states, that axe finds no violations in every story and theme, and, for the components they cover, that the stories hold up in forced colours, reduced motion, text spacing and a 320 pixel width. An Automated row passes on these tests alone. A Core or Release row passes only when an automated agent check and a person on the real assistive technology have both checked it. "agent-verified" means automated tests in the repository (with a virtual screen reader for the screen reader rows), not the real assistive technology.

AT + browser + OSAgent checkHuman checkResultNotes
Automated (re-runnable tests)
axe, WCAG 2.2 A and AA, every story, 4 themespassn/apasserror-summary.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apasserror-summary.test.tsx (axe and role, name and state): it is an Alert.Danger group named by its title, with a list of links
Keyboard contract rows (Vitest browser)passn/apass4 rows, each names a test in error-summary.test.tsx
Focus order and managementpassn/apasserror-summary.test.tsx: focus › moves focus to the summary on mount
Display modes (forced colours, 320px reflow, text spacing, reduced motion)passn/apassthe four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI
Core (required for beta)
NVDA + Chrome + Windowspendingpendingpending
VoiceOver + Safari + iOSpendingpendingpending
Release (before 1.0 and each minor)
JAWS + Chrome + Windowspendingpendingpending
TalkBack + Chrome + Androidpendingpendingpending
VoiceOver + Safari + macOSpendingpendingpending

What to check#

With a screen reader and the keyboard, try:

  • Submit an empty form: focus moves to the summary and "Error: There is a problem" is read.
  • Tab: the first error link is read.
  • Press Enter on a link: focus moves to the field, and its label, "invalid" and the error are read.
  • Submit again: focus returns to the summary.
  • Shift+Tab on the first link goes to the stop before the summary.
  • With prefixDocumentTitle, the tab title starts with "Error:" while the summary shows.