Components
Checkbox
A yes or no choice, or one of several options that can be picked together.
import { Checkbox } from '@dex/shared/components/checkbox'
import { Label } from '@dex/shared/components/label'
export function CheckboxDefault() {
return (
<div className="flex items-start gap-3">
<Checkbox id="checkbox-role-alerts" defaultChecked />
<Label htmlFor="checkbox-role-alerts" className="font-normal">
Email me when a new role matches my profile
</Label>
</div>
)
}
Show me day-rate and fixed-term roles as well as permanent ones.
import { Checkbox } from '@dex/shared/components/checkbox'
import { FieldDescription } from '@dex/shared/components/field'
import { Label } from '@dex/shared/components/label'
export function CheckboxWithDescription() {
return (
<div className="flex items-start gap-3">
<Checkbox id="checkbox-contract" />
<div className="flex flex-col gap-0.5">
<Label htmlFor="checkbox-contract">Open to contract roles</Label>
<FieldDescription>
Show me day-rate and fixed-term roles as well as permanent ones.
</FieldDescription>
</div>
</div>
)
}
'use client'
import { useState } from 'react'
import { Checkbox } from '@dex/shared/components/checkbox'
import { FieldDescription, FieldLegend, FieldSet } from '@dex/shared/components/field'
import { Label } from '@dex/shared/components/label'
export function CheckboxGroup() {
const roles = ['Backend engineering', 'Machine learning', 'Platform and infrastructure', 'Data']
const [chosen, setChosen] = useState(['Backend engineering', 'Machine learning'])
const toggle = (role: string, isChecked: boolean) =>
setChosen((current) => (isChecked ? [...current, role] : current.filter((r) => r !== role)))
return (
<FieldSet>
<FieldLegend>Which roles should Dex look for?</FieldLegend>
<FieldDescription>Choose all that apply.</FieldDescription>
<div className="flex flex-col gap-3">
{roles.map((role) => (
<div key={role} className="flex items-start gap-3">
<Checkbox
id={`checkbox-group-${role}`}
checked={chosen.includes(role)}
onCheckedChange={(isChecked) => toggle(role, isChecked)}
/>
<Label htmlFor={`checkbox-group-${role}`} className="font-normal">
{role}
</Label>
</div>
))}
</div>
</FieldSet>
)
}
'use client'
import { useState } from 'react'
import { Checkbox } from '@dex/shared/components/checkbox'
import { Label } from '@dex/shared/components/label'
export function CheckboxIndeterminate() {
const locations = ['London', 'Manchester', 'Remote, UK']
const [chosen, setChosen] = useState(['London'])
const isAll = chosen.length === locations.length
const toggle = (location: string, isChecked: boolean) =>
setChosen((current) =>
isChecked ? [...current, location] : current.filter((l) => l !== location),
)
return (
<div className="flex flex-col gap-3">
<div className="flex items-start gap-3">
<Checkbox
id="checkbox-locations-all"
checked={isAll}
indeterminate={chosen.length > 0 && !isAll}
onCheckedChange={(isChecked) => setChosen(isChecked ? locations : [])}
/>
<Label htmlFor="checkbox-locations-all">All locations</Label>
</div>
<div className="flex flex-col gap-3 pl-8">
{locations.map((location) => (
<div key={location} className="flex items-start gap-3">
<Checkbox
id={`checkbox-location-${location}`}
checked={chosen.includes(location)}
onCheckedChange={(isChecked) => toggle(location, isChecked)}
/>
<Label htmlFor={`checkbox-location-${location}`} className="font-normal">
{location}
</Label>
</div>
))}
</div>
</div>
)
}
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 CheckboxInvalid() {
const [hasAccepted, setHasAccepted] = useState(false)
return (
<div className="flex items-start gap-3">
<Checkbox
id="checkbox-terms"
checked={hasAccepted}
onCheckedChange={setHasAccepted}
aria-invalid={!hasAccepted}
aria-describedby={hasAccepted ? undefined : 'checkbox-terms-error'}
/>
<div className="flex flex-col gap-2">
<Label htmlFor="checkbox-terms" className="font-normal">
I agree to the candidate terms and privacy policy
</Label>
{!hasAccepted && (
<p id="checkbox-terms-error" role="alert" className="text-sm leading-5 text-destructive">
Accept the terms to continue.
</p>
)}
</div>
</div>
)
}
'use client'
import { useState } from 'react'
import { Checkbox } from '@dex/shared/components/checkbox'
import { FieldLegend, FieldMessage, FieldSet } from '@dex/shared/components/field'
import { Label } from '@dex/shared/components/label'
export function CheckboxInvalidGroup() {
const patterns = ['Remote', 'Hybrid', 'In the office']
const [chosen, setChosen] = useState<string[]>([])
const isInvalid = chosen.length === 0
const toggle = (pattern: string, isChecked: boolean) =>
setChosen((current) =>
isChecked ? [...current, pattern] : current.filter((p) => p !== pattern),
)
return (
<FieldSet invalid={isInvalid}>
<FieldLegend>How do you want to work?</FieldLegend>
<div className="flex flex-col gap-3">
{patterns.map((pattern) => (
<div key={pattern} className="flex items-start gap-3">
<Checkbox
id={`checkbox-invalid-${pattern}`}
checked={chosen.includes(pattern)}
onCheckedChange={(isChecked) => toggle(pattern, isChecked)}
/>
<Label htmlFor={`checkbox-invalid-${pattern}`} className="font-normal">
{pattern}
</Label>
</div>
))}
</div>
{isInvalid && <FieldMessage error="Choose at least one option." />}
</FieldSet>
)
}
import { Checkbox } from '@dex/shared/components/checkbox'
import { Label } from '@dex/shared/components/label'
export function CheckboxDisabled() {
return (
<div className="flex flex-col gap-3">
<div className="flex items-start gap-3">
<Checkbox id="checkbox-relocation" disabled />
<Label htmlFor="checkbox-relocation" className="font-normal">
Open to relocating
</Label>
</div>
<div className="flex items-start gap-3">
<Checkbox id="checkbox-right-to-work" disabled defaultChecked />
<Label htmlFor="checkbox-right-to-work" className="font-normal">
I have the right to work in the UK
</Label>
</div>
</div>
)
}
- Use a checkbox for a yes or no choice, or for picking several options from a list. For exactly one option use a radio group, and for a setting that applies straight away use a switch.
- Every box has a label, joined to it with
idandhtmlForso pressing the label ticks the box. The label sits 12px from the box and takesfont-normalunless a description follows it. - A group sits in a
FieldSetwith its question as theFieldLegend. Setinvalidon theFieldSet, not on each box, and put one message under the group. - A single required box takes
aria-invaliditself, and its message lines up with the label, not the box. indeterminateis for a parent box whose group is partly selected. It is a prop of its own, not a value ofchecked.- A disabled box that is ticked keeps its fill, so it still shows the value.