toast
Use it to render a toast. You can call it from anywhere, even outside of Solid.
Rendering the toast
You can call it with just a string.
Or provide an object as the second argument with more options. They will overwrite the options passed to <Toaster />
if you have provided any.
Render toast on page load
To render a toast on initial page load it is required that the function toast()
is called inside of a setTimeout
or requestAnimationFrame
.
Creating toasts
Success
Renders a checkmark icon in front of the message.
Error
Renders an error icon in front of the message.
Action
Renders a primary button, clicking it will close the toast and run the callback passed via onClick
. You can prevent the toast from closing by calling event.preventDefault()
in the onClick
callback.
You can also render jsx as your action.
Cancel
Renders a secondary button, clicking it will close the toast and run the callback passed via onClick
.
You can also render jsx in the cancel option.
Promise
Starts in a loading state and will update automatically after the promise resolves or fails. You can pass a function to the success/error messages to incorporate the result/error of the promise.
Loading
Renders a toast with a loading spinner. Useful when you want to handle various states yourself instead of using a promise toast.
Custom
You can pass jsx as the first argument instead of a string to render a custom toast while maintaining default styling.
Headless
Use it to render an unstyled toast with custom jsx while maintaining the functionality. This function receives the Toast
as an argument, giving you access to all properties.
Dynamic Position
You can change the position of the toast dynamically by passing a position
prop to the toast
function. It will not affect the positioning of other toasts.
Other
Updating toasts
You can update a toast by using the toast
function and passing it the id of the toast you want to update, the rest stays the same.
On Close Callback
You can pass onDismiss
and onAutoClose
callbacks to each toast. onDismiss
gets fired when either the close button gets clicked or the toast is swiped. onAutoClose
fires when the toast disappears automatically after it’s timeout (duration
prop).
Persisting toasts
If you want a toast to stay on screen forever, you can set the duration
to Infinity
.
Dismissing toasts programmatically
To remove a toast programmatically use toast.dismiss(id)
. The toast()
function return the id of the toast.
You can also dismiss all toasts at once by calling toast.dismiss()
without an id.
Rendering custom elements
You can render custom elements inside the toast like <a />
or custom components by passing a function instead of a string. This work for both the title and description.
API Reference
Property | Description | Default |
---|---|---|
description | Toast’s description, renders underneath the title. | - |
closeButton | Adds a close button. | false |
invert | Dark toast in light mode and vice versa. | false |
duration | Time in milliseconds that should elapse before automatically closing the toast. | 4000 |
position | Position of the toast. | bottom-right |
dismissible | If false , it’ll prevent the user from dismissing the toast. | true |
icon | Icon displayed in front of toast’s text, aligned vertically. | - |
action | Renders a primary button, clicking it will close the toast. | - |
cancel | Renders a secondary button, clicking it will close the toast. | - |
id | Custom id for the toast. | - |
onDismiss | The function gets called when either the close button is clicked, or the toast is swiped. | - |
onAutoClose | Function that gets called when the toast disappears automatically after it’s timeout (duration` prop). | - |
unstyled | Removes the default styling, which allows for easier customization. | false |
actionButtonStyle | Styles for the action button | {} |
cancelButtonStyle | Styles for the cancel button | {} |