Toast

Toast is a component that can display alerts, notifications, or messages on top of an overlay layer. It is commonly used to inform users of important information or actions.
Installation
API Reference
This is an illustration of Toast component.
action
variant
function Example() {
const toast = useToast()
return (
<Button
onPress={() => {
toast.show({
placement: "top",
render: ({ id }) => {
const toastId = "toast-" + id
return (
<Toast nativeID={toastId} action="attention" variant="solid">
<VStack space="xs">
<ToastTitle>New Message</ToastTitle>
<ToastDescription>
Hey, just wanted to touch base and see how you're doing.
Let's catch up soon!
</ToastDescription>
</VStack>
</Toast>
)
},
})
}}
>
<ButtonText>Press Me</ButtonText>
</Button>
)
}

Installation

Run the following command:

npx gluestack-ui add toast

API Reference

To use this component in your project, include the following import statement in your file.
import { useToast, Toast } from "@/components/ui/toast"
export default () => (
<Toast>
<ToastTitle />
<ToastDescription />
</Toast>
)

Component Props

This section provides a comprehensive reference list for the component props, detailing descriptions, properties, types, and default behavior for easy project integration.

Toast

It inherits all the properties of React Native's View component.
Prop
Type
Default
Description
duration
number or null
5000
The delay before the toast hides (in milliseconds). If set to null, toast will never dismiss.
onCloseComplete
()=>{}
-
Callback function to run side effects after the toast has closed.
placement
'top'| 'top right' | 'top left' | 'bottom' | 'bottom left' | 'bottom right'
bottom
Position of toast on the web page.
render?: (props: any)
ReactNode
-
Renders a toast component
avoidKeyboard
bool
false
If true and the keyboard is opened, the Toast will move up equivalent to the keyboard height.
containerStyle
ViewStyle
-
Container style object for the toast.

ToastTitle

Contains all Text related layout style props and actions. It inherits all the properties of React Native's Text component.

ToastDescription

Contains all Text related layout style props and actions. It inherits all the properties of React Native's Text component.

Accessibility

We have outlined the various features that ensure the Toast component is accessible to all users, including those with disabilities. These features help ensure that your application is inclusive and meets accessibility standards.Adheres to the WAI-ARIA design pattern.

Keyboard

  • Tab + Enter: Triggers the toast's action.

Screen Reader

  • VoiceOver: When the toast is focused, the screen reader will announce the toast's title.

Props

Toast component is created using View component from react-native. It extends all the props supported by React Native View and the props mentioned below.

Toast

Name
Value
Default
action
error | warning | success | info | attention
attention
variant
solid | outline | accent
solid

Examples

The Examples section provides visual representations of the different variants of the component, allowing you to quickly and easily determine which one best fits your needs. Simply copy the code and integrate it into your project.

Toast with actions

A versatile Toast component with customizable actions, enabling users to take various actions directly from the notification popup for enhanced usability and convenience.
function Example() {
const toast = useToast()
const toastActions = [
{
actionType: "info",
title: "Info",
description:
"Your order has been received and is being processed. You will receive a confirmation email shortly.",
},
{
actionType: "success",
title: "Success!",
description: "Your changes have been saved successfully.",
},
{
actionType: "warning",
title: "Warning!",
description:
"Your account subscription will expire in 5 days. Please renew your subscription to avoid interruption of service.",
},
{
actionType: "error",
title: "Error!",
description:
"There was an error processing your request. Please try again later.",
},
{
actionType: "attention",
title: "Attention!",
description:
"Please review and accept our updated terms and conditions before continuing to use the application.",
},
]
return (
<Center className="h-80">
<VStack space="md">
{toastActions.map((action, index) => {
return (
<Button
key={index}
onPress={() => {
toast.show({
placement: "top",
render: ({ id }) => {
const toastId = "toast-" + id
return (
<Toast nativeID={toastId} action={action.actionType}>
<VStack space="xs">
<ToastTitle>{action.title}</ToastTitle>
<ToastDescription>
{action.description}
</ToastDescription>
</VStack>
</Toast>
)
},
})
}}
>
<ButtonText>{action.actionType}</ButtonText>
</Button>
)
})}
</VStack>
</Center>
)
}

Toast with variants

A versatile Toast component with multiple variants, offering different styles and visual cues to effectively convey various types of notifications and feedback to users.
variant
function Example() {
const toast = useToast()
return (
<Button
onPress={() => {
toast.show({
placement: "top",
render: ({ id }) => {
const toastId = "toast-" + id
return (
<Toast nativeID={toastId} variant="solid" action="success">
<VStack space="xs">
<ToastTitle>Attention!</ToastTitle>
<ToastDescription>
Please review and accept our updated terms and conditions
before continuing to use the application.
</ToastDescription>
</VStack>
</Toast>
)
},
})
}}
>
<ButtonText>Press Me</ButtonText>
</Button>
)
}

Toast with placement

A Toast component with different placement options allows for the flexible positioning of toast notifications, enabling them to appear in various locations within a user interface, enhancing user visibility and providing a customizable approach to displaying temporary messages or alerts.
placement
function Example() {
const toast = useToast()
const placements = {
bottom: "Looks like you found the hiding spot at the bottom of the page!",
top: "The top spot is your favorite hiding spot! Keep hunting for success.",
"top right":
"You're hiding in plain sight at the top right corner! You are good at this game.",
"top left":
"You're a master of disguise hiding in the top left corner! Keep up the sneakiness.",
"bottom left":
"You're not the best at hiding, but we found you in the bottom left corner.",
"bottom right": "Found you! Don't worry, we wont tell anyone.",
}
const placement = "bottom"
return (
<Button
onPress={() => {
toast.show({
placement: placement,
render: ({ id }) => {
const toastId = "toast-" + id
return (
<Toast nativeID={toastId}>
<ToastDescription>{placements[placement]}</ToastDescription>
</Toast>
)
},
})
}}
>
<ButtonText>Press Me</ButtonText>
</Button>
)
}

Dismissable Toast

A dismissable Toast component offers users the ability to dismiss or close the toast notification, providing control and convenience in managing temporary messages or alerts within a user interface.
function Example() {
const toast = useToast()
return (
<Button
onPress={() => {
toast.show({
placement: "top",
render: ({ id }) => {
const toastId = "toast-" + id
return (
<Toast nativeID={toastId} className="bg-success-700">
<Icon as={CheckIcon} className="text-typography-0 mt-1 mr-3" />
<VStack space="xs">
<ToastTitle className="text-typography-50">
Download Complete
</ToastTitle>
<ToastDescription className="text-typography-50">
Your file 'wadewarren.docx' has been downloaded
successfully. You can find it in your Downloads folder.
</ToastDescription>
</VStack>
<Pressable onPress={() => toast.close(id)} className="mt-1">
<Icon as={CloseIcon} className="text-typography-0" />
</Pressable>
</Toast>
)
},
})
}}
>
<ButtonText>Press Me</ButtonText>
</Button>
)
}

Toast custom duration

A Toast component with custom duration allows for specifying the length of time the toast notification remains visible, offering flexibility in controlling the duration of temporary messages or alerts within a user interface.
duration
function Example() {
const toast = useToast()
const duration = "5000"
return (
<Button
onPress={() => {
toast.show({
placement: "top",
duration: duration,
render: ({ id }) => {
const toastId = "toast-" + id
return (
<Toast nativeID={toastId} className="bg-secondary-700 p-3">
<Icon
as={MessageCircleIcon}
className="text-typography-0 mt-1 mr-3"
/>
<VStack space="xs">
<ToastTitle className="text-typography-50">
New Message
</ToastTitle>
<ToastDescription className="text-typography-50">
Hey, just wanted to touch base and see how you're doing.
Let's catch up soon!
</ToastDescription>
</VStack>
<Pressable onPress={() => toast.close(id)} className="mt-1">
<Icon as={CloseIcon} className="text-typography-0" />
</Pressable>
</Toast>
)
},
})
}}
>
<ButtonText>Press Me</ButtonText>
</Button>
)
}

Preserve Toast

A Toast component with preserve toast functionality retains the notification on the screen until explicitly dismissed, ensuring important messages or alerts remain visible and accessible to the user, even during subsequent interactions within the user interface.
function Example() {
const toast = useToast()
return (
<Button
onPress={() => {
toast.show({
placement: "top",
duration: null,
render: ({ id }) => {
const toastId = "toast-" + id
return (
<Toast nativeID={toastId} className="bg-error-700 p-3">
<Icon
as={AlertTriangle}
className="text-typography-0 mt-1 mr-3"
/>
<VStack space="xs">
<ToastTitle className="text-typography-50">
Account Security Alert
</ToastTitle>
<ToastDescription className="text-typography-50">
Your account password was recently changed. If you did not
authorize this change, please contact our support team
immediately.
</ToastDescription>
</VStack>
<Pressable onPress={() => toast.close(id)} className="mt-1">
<Icon as={CloseIcon} className="text-typography-50" />
</Pressable>
</Toast>
)
},
})
}}
>
<ButtonText>Press Me</ButtonText>
</Button>
)
}
Edit this page on GitHub