Embed highly customizable toasts in your web app. Toasts are synced with the feed of Courier Inbox messages.
This is the latest version of the Courier Toast Web Components SDK, recommended for new and existing apps.Coming from an earlier version? We recommend upgrading — check out the
migration guide for the React SDK, which
is a thin wrapper around Toast Web Components and exposes a similar API.
To use the SDK, you need to generate a JWT (JSON Web Token) for your user. This JWT should always be generated by your backend server, never in client-side code.
If you’ve already setup authentication for Courier Inbox, you can skip this step. Courier Toasts and Inbox share
an authentication mechanism and connection to the Courier backend.
When your app needs to authenticate a user, your client
should make a request to your own backend (ex. GET https://your-awesome-app.com/api/generate-courier-jwt).
Toasts are short-lived notifications that notify users and prompt them to take action.
Courier’s Toast component is connected to the feed of Courier Inbox messages.
Importing @trycourier/courier-ui-toast registers Courier’s Web Components (<courier-toast>).
Copy
Ask AI
<body> <courier-toast></courier-toast> <script type="module"> import { Courier } from "@trycourier/courier-ui-toast"; // Generate a JWT for your user on your backend server const jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."; // Authenticate the user Courier.shared.signIn({ userId: $YOUR_USER_ID, jwt: jwt }); </script></body>
<courier-toast> exposes various attributes and methods to customize its appearance and functionality.
A note on terminology: In method names and documentation, toast is used to refer
to the entire stack of toasts maintained and presented by <courier-toast>, while
toast item refers to a single toast displayed for a message.
Attribute
Type
Default
Description
auto-dismiss
boolean
false
Whether toast items should auto-dismiss.
auto-dismiss-timeout-ms
integer
5000
If auto-dismiss is enabled, the timeout in milliseconds before dismissal.
dismiss-button
"visible" | "hidden" | "hover" | "auto"
"auto"
Display option for the dismiss button. "auto" makes the button always visible if auto-dismiss is false, and visible on hover if auto-dismiss is true.
light-theme
json
undefined
JSON-stringified CourierToastTheme instance applied if mode is light. The theme provided here is merged with the SDK’s default theme values.
dark-theme
json
undefined
JSON-stringified CourierToastTheme instance applied if mode is dark. The theme provided here is merged with the SDK’s default theme values.
If the auto-dismiss attribute is set, the dismiss button (x) will only be visible on hover
and each toast item will automatically be dismissed. A countdown bar is shown to indicate the time
remaining before the toast disappears.The time the toast is displayed can be configured with the attribute auto-dismiss-timeout-ms.
The countdown bar color is theme-able. See Styles and Theming below.
Set the handler invoked when a toast item is clicked. The handler is called with
CourierToastItemClickEvent, included below. The click handler will be added to
either the default CourierToastItem or a custom toast item implementation if one
is provided via setToastItem.As an example, you might attach a custom click handler to open a URL from the message.
/** * Event metadata passed to the callback for * {@link CourierToast.onToastItemClick}. */export type CourierToastItemClickEvent = { /** The message that was clicked. */ message: InboxMessage; /** The toast item component clicked. */ toastItem: CourierToastItem | HTMLElement;};
If a message contains actions, toast items
will include a button for each. By default, these buttons do not implement any functionality.Use onToastItemActionClick to set the handler invoked when an action button is clicked.
The handler is called with CourierToastItemActionClickEvent, included below.As an example, you might attach a click handler to open the URL in the action that was clicked
and dismiss the toast.
/** * Event metadata passed to the callback for * {@link CourierToast.onToastItemActionClick}. */export type CourierToastItemActionClickEvent = { /** The message for the toast item that was clicked. */ message: InboxMessage; /** The action for the action button that was clicked. */ action: InboxAction;}
The example below themes various style elements, sets a custom SVG as the icon,
and hides the dismiss button. Building on the previous click handling example,
it listens to the click event to open the action URL attached to the message
and dismiss the message.
Customize only the content area of each toast item while reusing the default
stack structure and styling by passing a factory function to setToastItemContent.By default the dismiss button is still rendered if custom content is set. The example
below disables the default dismiss button by setting the dismiss-button="hidden"
attribute, builds up content with a custom dismiss button, and dismisses the toast with a
call to CourierToastDatastore.
Customize each toast item’s appearance and behavior by passing a factory
function to setToastItem, which will be called to create a toast item. This
is useful for complete control over the toast and its stack, since the custom
item will not have any default styling attached.auto-dismiss and auto-dismiss-timeout-ms are valid attributes when using both the
default and custom toast items. If auto-dismiss is true, the custom item will be automatically removed after auto-dismiss-timeout-ms milliseconds. You may want to use those parameters passed to the factory function to create the item.
CourierToastDatastore is the central repository of Inbox messages from which
the <courier-toast> listens to messages to display and dismiss. As seen in a
few examples above, it can be useful to manually manipulate the datastore to
dismiss messages in response to user interactions, like clicking an action button.CourierToastDatastore is a singleton and should be used through its shared instance.
Add a message, which will display it as a toast item. Since toasts are
ephemeral, calling addMessage can be useful in development to quickly
prototype how toasts appear in your app.For example, add a message with an action. Messages must include messageId.
app.ts
Copy
Ask AI
import { CourierToastDatastore } from "@trycourier/courier-ui-toast";CourierToastDatastore.shared.addMessage({ title: "Lorem ipsum dolor sit", body: "Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet", messageId: "abcd-1234-abcd-1234", actions: [ { "content": "Click me!" } ]});
Remove a message, which will dismiss any toasts currently displayed for this
message. removeMessage can be useful to implement your own toast dismissal
logic, as in the example below.