Todae

Todae

ErrorMessage

Validation text below a control. A sibling, not a Field part. id is required outside a Form. There is no default role="alert" and no aria-live. invalid lives on the control or on Field.Root.

On this page

Mount the error only while it applies, and include its id in aria-describedby only then. It fades in as it mounts, over field.error.motion.duration.enter and field.error.motion.easing.enter, under reduced motion too, and goes at once. The text is in the page from the first frame, so the fade delays nothing a screen reader reads.

ErrorMessage owns its spacing. After a control, margin-block-start is field.label.gap (0.4rem). After Description, the sibling rule drops that to field.messages.gap (0.2rem). margin-block-end is 0.

import { Description, ErrorMessage, Field, Label } from '@tounsoo/todae';
import { useId, useState } from 'react';

export default function ErrorMessageSibling() {
  const id = useId();
  const hintId = useId();
  const errorId = useId();
  const [email, setEmail] = useState('not-an-email');
  const invalid = email !== '' && !email.includes('@');
  const describedBy = invalid ? `${hintId} ${errorId}` : hintId;
  return (
    <Field.Group>
      <Label.Root htmlFor={id}>Work email</Label.Root>
      <Field.Root id={id} invalid={invalid} aria-describedby={describedBy}>
        <Field.TextInput value={email} onChange={(event) => setEmail(event.target.value)} />
      </Field.Root>
      <Description.Root id={hintId}>We only use this for billing receipts.</Description.Root>
      {invalid ? <ErrorMessage.Root id={errorId}>Enter an address that contains an @.</ErrorMessage.Root> : null}
    </Field.Group>
  );
}
We only use this for billing receipts.Enter an address that contains an @.

To announce a submit-time error, pass role="alert" on this component. The package does not choose announcement policy.

In a Form

Inside a Form, a Field.Group or Fieldset.Root is a validation scope, and ErrorMessage.Root takes no id. It renders only while the scope shows a problem, with the problem's text unless you pass children, and the scope sets aria-invalid on its controls and adds the message's id to their aria-describedby while it renders. A function child gets the error's kind and message.

const id = useId();

<Field.Group>
  <Label.Root htmlFor={id}>Work email</Label.Root>
  <Field.Root id={id} name="email">
    <Field.TextInput type="email" required value={email} onValueChange={setEmail} />
  </Field.Root>
  <ErrorMessage.Root />
</Field.Group>

Accessible composition

Todae handles: ErrorMessage.Root renders a <span> with the id you pass, or the scope's inside a Form, data-todae-error and the error text color. It has no role and no aria-live, and writes nothing onto the control. aria-invalid comes from invalid on the control, Field.Root, RadioGroup.Root or a surrounding Fieldset, not from this component. In a Form, the scope also wires aria-invalid and aria-describedby.

You supply: Mount it only while the error applies, and only then add its id to the control's aria-describedby (on Field.Root inside a Field) and set invalid. Say what is wrong and how to fix it. For an error that appears on submit, pass role="alert" so it is announced. For a group, point aria-describedby on Fieldset.Root at it.

Watch for: invalid marks the control invalid but does not say why, so the message text is what tells people what went wrong. A role="alert" on a message that changes while the person types announces every change. A message hidden with CSS while its id stays in aria-describedby is still read.

When to use

When: Validation text below a control, after Description. Mount it and add its id to aria-describedby only while invalid; invalid lives on the control or Field.Root.

When not: Standing hints → Description. Showing while valid. Expecting auto-announce (no role="alert" by default; pass it for submit-time errors).

Props

ErrorMessage.Root props
NameTypeDescription
idstring

Id the author lists in aria-describedby on the control or Field.Root. Required outside a validation scope; inside one, leave it out to take the scope's error.

childrenReact.ReactNode| ((error: FormFieldError) => React.ReactNode)

Error text. Without an id, in a validation scope, a function of the scope's error is allowed too, and no children shows the error's message.

classNamestring

Optional class on the span.