Components
Toast
A short message that appears at the top right to confirm an action, then goes away.
'use client'
import { Button } from '@dex/shared/components/button'
import { toast } from '@dex/shared/components/toast'
export function ToastDefault() {
return (
<Button variant="outline" onClick={() => toast('Role saved')}>
Save role
</Button>
)
}
'use client'
import { Button } from '@dex/shared/components/button'
import { toast } from '@dex/shared/components/toast'
export function ToastWithDescription() {
return (
<Button
variant="outline"
onClick={() =>
toast('Interview booked', {
description: 'Monday 9 March at 10:00 with QuantumLeap.',
})
}
>
Book interview
</Button>
)
}
'use client'
import { Button } from '@dex/shared/components/button'
import { toast } from '@dex/shared/components/toast'
export function ToastTypes() {
return (
<>
<Button variant="outline" onClick={() => toast.success('Application sent')}>
Success
</Button>
<Button
variant="outline"
onClick={() =>
toast.error('We couldn’t save your CV', {
description: 'Check your connection and try again.',
})
}
>
Error
</Button>
<Button variant="outline" onClick={() => toast.warning('This role closes tomorrow')}>
Warning
</Button>
<Button
variant="outline"
onClick={() => toast.info('Dex saved your CV while you were editing')}
>
Info
</Button>
</>
)
}
'use client'
import { Button } from '@dex/shared/components/button'
import { toast } from '@dex/shared/components/toast'
export function ToastWithAction() {
return (
<Button
variant="outline"
onClick={() =>
toast('Role skipped', {
action: {
label: 'Undo',
onClick: () => toast('Role restored'),
},
})
}
>
Skip role
</Button>
)
}
'use client'
import { Button } from '@dex/shared/components/button'
import { toast } from '@dex/shared/components/toast'
export function ToastOneAtATime() {
return (
<Button
variant="outline"
onClick={() =>
toast.error('The questions for this role have changed', { id: 'role-questions-changed' })
}
>
Submit application
</Button>
)
}
An app mounts one Toaster in its providers, inside the theme provider so it follows light and dark. It needs no props: success, error, warning and info toasts take their colour by default. After that, any client code calls toast() from @dex/shared/components/toast. Toasts appear at the top right.
- A toast reports something that has already happened: "Role saved", "Application sent". Call it from the handler once the request has succeeded or failed, not when the button is pressed.
- Use the type that fits:
toast.successwhen the action worked,toast.errorwhen it failed,toast.infofor news that is neither. Each has its own icon. - Give a toast an
idwhen the same event can fire more than once, so it is shown once.