Toast

The toast is used to show alerts on top of an overlay

Toasts can be configured to appear at either the top or the bottom of an application window, and it is possible to have more than one toast onscreen at a time.

Import#

import { useToast } from "@nature-ui/core";

Usage#

function ToastExample() {
const toast = useToast();
return (
<Button
onClick={() =>
toast({
title: "Account created.",
description: "We've created your account for you.",
status: "success",
duration: 9000,
isClosable: true,
})
}
className="bg-primary-500"
>
Show Toast
</Button>
);
}

Custom component#

Display a custom component instead of the default Toast UI.

function Example() {
const toast = useToast();
return (
<Button
className="bg-primary-500"
onClick={() =>
toast({
position: "bottom-left",
render: () => (
<Box className="bg-blue-500 m-2 text-white p-3">Hello World</Box>
),
})
}
>
Show Toast
</Button>
);
}

Warning#

function Example() {
const toast = useToast();
return (
<Button
className="bg-primary-500"
onClick={() =>
toast({
title: "Warning.",
description: "This is a warning.",
status: "warning",
duration: 9000,
isClosable: true,
})
}
>
Show Warning Toast
</Button>
);
}

Error#

function ErrorToast() {
const toast = useToast();
return (
<Button
className="bg-primary-500"
onClick={() =>
toast({
title: "An error occurred.",
description: "Unable to create user account.",
status: "error",
duration: 9000,
isClosable: true,
})
}
>
Show Error Toast
</Button>
);
}

Position#

Using the position prop you can adjust where your toast will be popup from.

function PositionExample() {
const toast = useToast();
return (
<Button
className="bg-primary-500"
onClick={() =>
toast({
position: "bottom-left",
title: "Account created.",
description: "We've created your account for you.",
status: "success",
duration: 9000,
isClosable: true,
})
}
>
Show Toast
</Button>
);
}

Props#

NameTypeDefaultDescription
titlestringThe title of the toast.
isClosablebooleanfalseIf true adds a close button to the toast.
onClosefunctionCallback function to close the toast.
descriptionstringThe description of the toast.
statussuccess, danger, warning, infoThe status of the toast.
durationnumber5000msDuration before dismiss in milliseconds, or null to never dismiss.
positiontop, top-left, top-right, bottom, bottom-left, bottom-rightbottomPosition the toast will popup out from.
render(props: {onClose:() => void, id: string}) => React.ReactNodeCallback function to display a custom toast.