Toast
A toast is a modal overlay that displays alerts or notifications, crucial for user updates.
Installation
API Reference
Important Note
This is the documentation for gluestack-ui v2 (beta). For @gluestack-ui/themed (stable) documentation, refer to gluestack-ui v1.
This is an illustration of Toast component.
action
variant
function Example() {const toast = useToast()const [toastId, setToastId] = React.useState(0)const handleToast = () => {if (!toast.isActive(toastId)) {showNewToast()}}const showNewToast = () => {const newId = Math.random()setToastId(newId)toast.show({id: newId,placement: "top",duration: 3000,render: ({ id }) => {const uniqueToastId = "toast-" + idreturn (<Toast nativeID={uniqueToastId} action="muted" variant="solid"><ToastTitle>Hello!</ToastTitle><ToastDescription>This is a customized toast message.</ToastDescription></Toast>)},})}return (<Button onPress={handleToast}><ButtonText>Press Me</ButtonText></Button>)}
Installation
CLI
Manual
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 in note talking platform
function Example() {const toast = useToast()const [toastId, setToastId] = React.useState(0)const handleToast = () => {if (!toast.isActive(toastId)) {showNewToast()}}const showNewToast = () => {const newId = Math.random()setToastId(newId)toast.show({id: newId,placement: "top",duration: 3000,render: ({ id }) => {const uniqueToastId = "toast-" + idreturn (<Toastaction="error"variant="outline"nativeID={uniqueToastId}className="p-4 gap-6 border-error-500 w-full shadow-hard-5 max-w-[443px] flex-row justify-between"><HStack space="md"><Icon as={HelpCircleIcon} className="stroke-error-500 mt-0.5" /><VStack space="xs"><ToastTitle className="font-semibold text-error-500">Error!</ToastTitle><ToastDescription size="sm">Something went wrong.</ToastDescription></VStack></HStack><HStack className="min-[450px]:gap-3 gap-1"><Button variant="link" size="sm" className="px-3.5 self-center"><ButtonText>Retry</ButtonText></Button><Pressable onPress={() => toast.close(id)}><Icon as={CloseIcon} /></Pressable></HStack></Toast>)},})}return (<Button onPress={handleToast}><ButtonText>Press Me</ButtonText></Button>)}
Social media notification
function Example() {const toast = useToast()return (<ButtononPress={() => {toast.show({placement: "top",render: ({ id }) => {const toastId = "toast-" + idreturn (<ToastnativeID={toastId}className="p-4 gap-3 w-full sm:min-w-[386px] bg-background-0 shadow-hard-2 flex-row"><Avatar><AvatarFallbackText>JS</AvatarFallbackText><AvatarImagesource={{uri: "https://s3-alpha-sig.figma.com/img/cc35/9a95/ecf9cba1881effe726061fc6f4809371?Expires=1721001600&Key-Pair-Id=APKAQ4GOSFWCVNEHN3O4&Signature=Pyj83N4SzedRQj~mtWMGxuhBcBMcuGc6ppsgWMnKDu2iEEcD-NQaE2WHgwqq6k7uHxzBd~gecQN2HXDrkVTlxvryARvD8Ai5RkaicN8OdqqHojHdmT8y1Q1IPKjSf0HxHA0EgkkTS8RLqRXBwrXV6IaGsVKNxche92mdvCt0llYhzeQO0xLYOYIDUiOXONpcpP72QgRMZLbU3Ep2thUnwl6VuxFiphQPOKkLLUEeGhQF5KDQB71NG09Ypq2V3okej6dtPb0eo~IunDKiFIfDUwrj~2fD6Y~JRrVhmSIXloZlAPwCd37NwzT-Jl8pR4JIWqF4dRigx67HOSUExrezJg__",}}/></Avatar><VStack className="w-full"><HStack className="justify-between w-full"><Headingsize="sm"className="text-typography-950 font-semibold">Jacob Steve</Heading><Text size="sm" className="text-typography-500">2m ago</Text></HStack><Text size="sm" className="text-typography-500">commented on your photo</Text></VStack></Toast>)},})}}><ButtonText>Show Toast</ButtonText></Button>)}
Software update toast
function Example() {const toast = useToast()const [toastId, setToastId] = React.useState(0)const handleToast = () => {if (!toast.isActive(toastId)) {showNewToast()}}const showNewToast = () => {const newId = Math.random()setToastId(newId)toast.show({id: newId,placement: "top",duration: 3000,render: ({ id }) => {const uniqueToastId = "toast-" + idreturn (<ToastnativeID={uniqueToastId}className="p-4 gap-4 w-full max-w-[386px] bg-background-0 shadow-hard-2 flex-row"><Box className="h-11 w-11 items-center justify-center hidden min-[400px]:flex bg-background-50">{/* RefreshCw is imported from 'lucide-react-native' */}<Iconas={RefreshCw}size="xl"className="stroke-background-800"/></Box><VStack space="xl"><VStack space="xs"><HStack className="justify-between"><ToastTitle className="text-typography-900 font-semibold">Update available</ToastTitle><Pressable onPress={() => toast.close(id)}><Icon as={CloseIcon} className="stroke-background-500" /></Pressable></HStack><ToastDescription className="text-typography-700">A new software version is available for download.</ToastDescription></VStack><ButtonGroup className="gap-3 flex-row"><Buttonaction="secondary"variant="outline"size="sm"className="flex-grow"><ButtonText>Not now</ButtonText></Button><Button size="sm" className="flex-grow"><ButtonText>Update</ButtonText></Button></ButtonGroup></VStack></Toast>)},})}return (<Button onPress={handleToast}><ButtonText>Press Me</ButtonText></Button>)}
Message sent toast
function Example() {const toast = useToast()return (<ButtononPress={() => {toast.show({placement: "top",render: ({ id }) => {const toastId = "toast-" + idreturn (<ToastnativeID={toastId}className="px-5 py-3 gap-4 shadow-soft-1 items-center flex-row">{/* Send is imported from 'lucide-react-native' */}<Iconas={Send}size="xl"className="fill-typography-100 stroke-none"/><Dividerorientation="vertical"className="h-[30px] bg-outline-200"/><ToastTitle size="sm">Message sent successfully</ToastTitle></Toast>)},})}}><ButtonText>Show Toast</ButtonText></Button>)}