Toast
A message that appears on the screen to provide feedback on an action.
A message that appears on the screen to provide feedback on an action.
To set up the toast correctly, you’ll need to understand its anatomy and how we name its parts.
Each part includes a
data-part
attribute to help identify them in the DOM.
To use the Toast component in your application, you need to set it up using the
createToaster
hook. This hook manages the placement and grouping of toasts,
and provides a toast
object needed to create toast notification.
To create a basic Toast, use the toast.create
method to display a
notification.
import { Toast, createToaster } from '@ark-ui/react'
const Basic = () => {
const [Toaster, toast] = createToaster({
placement: 'top-end',
render(toast) {
return (
<Toast.Root>
<Toast.Title>{toast.title}</Toast.Title>
<Toast.Description>{toast.description}</Toast.Description>
<Toast.CloseTrigger>Close</Toast.CloseTrigger>
</Toast.Root>
)
},
})
return (
<>
<button onClick={() => toast.create({ title: 'Title', description: 'Description' })}>
Toast
</button>
<Toaster />
</>
)
}
import { Toast, createToaster } from '@ark-ui/solid'
const Basic = () => {
const [Toaster, toast] = createToaster({
placement: 'top-end',
render(toast) {
return (
<Toast.Root>
<Toast.Title>{toast().title}</Toast.Title>
<Toast.Description>{toast().description}</Toast.Description>
<Toast.CloseTrigger>Close</Toast.CloseTrigger>
</Toast.Root>
)
},
})
return (
<>
<button onClick={() => toast().create({ title: 'Title', description: 'Description' })}>
Toast
</button>
<Toaster />
</>
)
}
<script setup lang="ts">
null
</script>
<template><button @click="createBasicToast">Toast</button> <BasicToaster /></template>
To configure the Toast component, you can pass various options to the
toast.create
method. These options include title
, description
, type
,
duration
, and removeDelay
:
import { Toast, createToaster } from '@ark-ui/react'
const Customized = () => {
const [Toaster, toast] = createToaster({
placement: 'bottom-start',
render(toast) {
return (
<Toast.Root>
<Toast.Title>{toast.title}</Toast.Title>
<Toast.Description>{toast.description}</Toast.Description>
<Toast.CloseTrigger>Close</Toast.CloseTrigger>
</Toast.Root>
)
},
})
return (
<>
<button
onClick={() =>
toast.create({
title: 'Success',
description: 'This is a success toast',
type: 'success',
duration: 20000,
removeDelay: 250,
})
}
>
Toast
</button>
<Toaster />
</>
)
}
import { Toast, createToaster } from '@ark-ui/solid'
const Customized = () => {
const [Toaster, toast] = createToaster({
placement: 'bottom-start',
render(toast) {
return (
<Toast.Root>
<Toast.Title>{toast().title}</Toast.Title>
<Toast.Description>{toast().description}</Toast.Description>
<Toast.CloseTrigger>Close</Toast.CloseTrigger>
</Toast.Root>
)
},
})
return (
<>
<button
onClick={() =>
toast().create({
title: 'Success',
description: 'This is a success toast',
type: 'success',
duration: 20000,
removeDelay: 250,
})
}
>
Toast
</button>
<Toaster />
</>
)
}
<script setup lang="ts">
null
</script>
<template><button @click="createCustomizedToast">Toast</button> <CustomizedToaster /></template>
For cases where you need more flexibility in rendering, the Toast component offers the ability to use a custom render function. This allows you to customize the content of the toast:
import { Toast, createToaster } from '@ark-ui/react'
const CustomRender = () => {
const [Toaster, toast] = createToaster({
placement: 'top-end',
// custom render may go directly into the function below
render(toast) {
return (
<Toast.Root>
<Toast.Title>{toast.title}</Toast.Title>
<Toast.Description>{toast.description}</Toast.Description>
<Toast.CloseTrigger>Close</Toast.CloseTrigger>
</Toast.Root>
)
},
})
return (
<>
<button
onClick={() =>
toast.create({
title: 'Please checkout',
render: (toast) => (
<div>
{toast.title} <a href="https://ark-ui.com">Ark UI</a>
</div>
),
})
}
>
Toast
</button>
<Toaster />
</>
)
}
import { Toast, createToaster } from '@ark-ui/solid'
const CustomRender = () => {
const [Toaster, toast] = createToaster({
placement: 'top-end',
// custom render may go directly into the function below
render(toast) {
return (
<Toast.Root>
<Toast.Title>{toast().title}</Toast.Title>
<Toast.Description>{toast().description}</Toast.Description>
<Toast.CloseTrigger>Close</Toast.CloseTrigger>
</Toast.Root>
)
},
})
return (
<>
<button
onClick={() =>
toast().create({
title: 'Please checkout',
render: (toast) => (
<div>
{toast().title} <a href="https://ark-ui.com">Ark UI</a>
</div>
),
})
}
>
Toast
</button>
<Toaster />
</>
)
}
<script setup lang="ts">
null
</script>
<template><button @click="createCustomRenderToast">Toast</button> <CustomRenderToaster /></template>
Prop | Type | Default |
---|---|---|
asChild Render as a different element type. | boolean |
Prop | Type | Default |
---|---|---|
asChild Render as a different element type. | boolean |
Prop | Type | Default |
---|---|---|
asChild Render as a different element type. | boolean |
Prop | Type | Default |
---|---|---|
placement The placement of the toast | Placement | |
render | (api: MachineApi<PropTypes>) => Element | |
count | number | |
dir The document's text/writing direction. | 'ltr' | 'rtl' | "ltr" |
doc The owner document of the machine. | Document | |
duration The duration the toast will be visible | number | |
getRootNode A root node to correctly resolve document in custom environments. E.x.: Iframes, Electron. | () => Node | ShadowRoot | Document | |
gutter The gutter or spacing between toasts | string | |
id The unique identifier of the machine. | string | |
max The maximum number of toasts that can be shown at once | number | |
offsets The offset from the safe environment edge of the viewport | string | Record<'left' | 'right' | 'top' | 'bottom', string> | |
pauseOnInteraction Whether to pause the toast when interacted with | boolean | |
pauseOnPageIdle Whether to pause toast when the user leaves the browser tab | boolean | |
pointerdownNode The related target when the element is blurred. Used as a polyfill for `e.relatedTarget` | HTMLElement | |
removeDelay The duration for the toast to kept alive before it is removed. Useful for exit transitions. | number | |
rootNode The root node of the machine. Useful for shadow DOM. | ShadowRoot | |
zIndex The z-index applied to each toast group | number |
Prop | Type | Default |
---|---|---|
asChild Render as a different element type. | boolean |
Prop | Type | Default |
---|---|---|
asChild Render as a different element type. | boolean |
Previous
Tags InputNext
Toggle Group