Components
Errors
The control's border turns red and a message under it says what to do next.
One colour, one place, one way of writing.
- One colour. The control's border turns
destructiveand so does the message. The label stays foreground. - One place. The message sits under the control, 8px below it, and takes the place of any helper text while it shows.
- One way of writing. The message says what to do next, in one sentence.
Focus never hides an error: a focused control keeps its red border, and a 1px red inset doubles it. A switch never errors, and a slider only errors when it is required and still not set.
Enter your minimum base salary.
'use client'
import { useState } from 'react'
import { Field, FieldControl, FieldLabel, FieldMessage } from '@dex/shared/components/field'
import { Input } from '@dex/shared/components/input'
export function ErrorsField() {
const [salary, setSalary] = useState('')
return (
<Field invalid={!salary} className="max-w-sm">
<FieldLabel>Minimum base salary</FieldLabel>
<FieldControl>
<Input
inputMode="numeric"
placeholder="Enter amount"
value={salary}
onChange={(event) => setSalary(event.target.value)}
/>
</FieldControl>
<FieldMessage
description="Your base salary, before bonus and equity."
error={salary ? undefined : 'Enter your minimum base salary.'}
/>
</Field>
)
}
'use client'
import { useState } from 'react'
import { Chip, ChipGroup } from '@dex/shared/components/chip-group'
import { FieldLegend, FieldMessage, FieldSet } from '@dex/shared/components/field'
export function ErrorsGroup() {
const [place, setPlace] = useState('')
return (
<FieldSet invalid={!place}>
<FieldLegend>Where do you want to work?</FieldLegend>
<ChipGroup type="single" value={place} onValueChange={setPlace}>
<Chip value="london">London</Chip>
<Chip value="remote">Remote, UK</Chip>
<Chip value="hybrid">Hybrid</Chip>
</ChipGroup>
{!place && <FieldMessage error="Choose where you want to work." />}
</FieldSet>
)
}
Accept the terms to continue.
'use client'
import { useState } from 'react'
import { Checkbox } from '@dex/shared/components/checkbox'
import { Label } from '@dex/shared/components/label'
export function ErrorsSingleCheckbox() {
const [hasAccepted, setHasAccepted] = useState(false)
return (
<div className="flex items-start gap-3">
<Checkbox
id="errors-terms"
checked={hasAccepted}
onCheckedChange={setHasAccepted}
aria-invalid={!hasAccepted}
aria-describedby={hasAccepted ? undefined : 'errors-terms-message'}
/>
<div className="flex flex-col gap-2">
<Label htmlFor="errors-terms" className="font-normal">
I agree to the candidate terms and privacy policy
</Label>
{!hasAccepted && (
<p id="errors-terms-message" role="alert" className="text-sm leading-5 text-destructive">
Accept the terms to continue.
</p>
)}
</div>
</div>
)
}
'use client'
import { useState } from 'react'
import { Button } from '@dex/shared/components/button'
export function ErrorsFormLevel() {
const [hasFailed, setHasFailed] = useState(false)
return (
<div className="flex w-full max-w-md flex-col gap-4">
{hasFailed && (
<p role="alert" className="text-sm leading-5 text-destructive">
We couldn't send your application. Try again, or email hello@meetdex.ai.
</p>
)}
<div className="flex items-center justify-between">
<Button variant="ghost" onClick={() => setHasFailed(false)}>
Back
</Button>
<Button onClick={() => setHasFailed(true)}>Send application</Button>
</div>
</div>
)
}
One sentence that says what to do, ending in a full stop. It starts with one of four verbs: Enter, Choose, Upload or Accept. Never "Please", "required", "invalid" or "This field".
| Situation | Pattern | Example |
|---|---|---|
| Typed field left empty | Enter your {thing}. | Enter your work email. |
| Wrong format | Enter a {thing} like {example}. | Enter an email address like name@company.com. |
| Nothing chosen | Choose {a thing}. | Choose a notice period. |
| Group with nothing chosen | Choose at least one option. | Choose at least one option. |
| Out of range | Choose {a thing} {rule}. | Choose an end date after the start date. |
| File rejected | Upload {a thing} {rule}. | Upload a PDF or DOCX under 10 MB. |
| Consent not given | Accept {the thing} to continue. | Accept the terms to continue. |
| Server or network | We couldn't {action}. Try again, or {contact}. | We couldn't send your answers. Try again, or email hello@meetdex.ai. |
- A single control goes in a
Fieldwithinvalid.FieldControlpassesaria-invalidand the message's id to the control, andFieldMessageshows itserrorin place of itsdescription. - A group goes in a
FieldSetwithinvalid, which turns every option inside it red. Settingaria-invalidon the group itself does the same. FieldMessagegives the errorrole="alert". A message written by hand, as under a single checkbox, needs the role and anaria-describedbyfrom the control.- On submit, move focus to the first field with an error.
- Keep the form's main button active, and show the errors when it is pressed.