Skip to content

Error Summary

When a submission stops, the user should learn why, and reach the field in one click. The server writes the heading and the list. The component does the two things the browser does not do by itself: it moves focus here when the box becomes visible, and it gives focus to the control itself when a link is followed.

// main.ts, runs once when the app starts
import { defineLtErrorSummary } from "@lo-tek/interfaces/error-summary"
import "@lo-tek/interfaces/tokens.css"
import "@lo-tek/interfaces/error-summary.css"
defineLtErrorSummary()
const box = lt.errorSummary({ count: errors.length, id: "errors" })
<lt-error-summary class="lt-error-summary" role="alert" tabindex="-1" id="errors">
<h2 class="lt-error-summary__title">The form has two errors</h2>
<ul class="lt-list">
<li><a href="#email">Enter an email address with an @</a></li>
<li><a href="#birth-date">Enter the date as day, month and year</a></li>
</ul>
</lt-error-summary>

The server writes the heading and the list. The component used to create the heading itself, and then it disappeared at the first update from the server and never came back. lt.errorSummary() writes the class, role="alert" and tabindex="-1" when the markup is made with JavaScript; otherwise the component sets them, and the class on the heading, and puts them back if a patch removes them. If the server has written a different role, it stays. See Markup and behaviour.

When there are no errors, the builder function sets hidden on the box. An empty summary with the heading “The form has 0 errors” is worse than none.

Each link in the list should point to the id of the control that failed. If it points to a <label>, the component follows the label on to the control, whether the label points with for or wraps the control.

<li><a href="#birth-date">Enter the date as day, month and year</a></li>
...
<label class="lt-label" for="birth-date">Date of birth</label>
<input class="lt-input" id="birth-date" type="date">
ClassWhat it is
.lt-error-summaryThe box itself. Sits on <lt-error-summary>
.lt-error-summary__titleThe heading above the list
VariableFallback
--lt-error-summary-paddingvar(--lt-spacing-4)
--lt-error-summary-radiusvar(--lt-spacing-1)
.lt-error-summary {
--lt-error-summary-padding: var(--lt-spacing-6);
}

The box is role="alert", so it is announced when it becomes visible. It has tabindex="-1" so it can receive focus without becoming a tab stop.

Two things are easy to forget, and both are the reason the component exists:

  • Focus moves to the box when it becomes visible. Without that, someone listening to the page does not know the submission stopped. Set data-autofocus="false" if you do not want it. The attribute is not called autofocus, because that is a boolean property on the element, and React 19 sets the property instead of the attribute. Opting out would then not work. The signal is that hidden is removed. If the box is already visible when the form fails again, the app has to toggle hidden on and off, or call focus() itself. The component does not move focus because the list changes, since a summary that shrinks while the user corrects would then pull focus out of the field. One exception: if focus did not land the first time, because the box was in a hidden panel, it tries again at the next change to the list, but only when nobody is in a field. If the box has no tabindex, the component sets it.
  • Focus goes to the control itself when a link is followed. A plain anchor link only gives focus to a target that can take it. If it points to a label or a heading, the browser scrolls the view, but document.activeElement becomes <body>, and the screen reader is not told where the user is. Tab continues from the target regardless; it is the focus and the announcement that are missing. The component follows a label to its control, and gives a target that cannot take focus tabindex="-1" for the jump.

The component also attaches to links the server sends in later, so a summary that is updated while the user corrects keeps working.