--- layout: layouts/page.njk title: Toast description: A succinct message that is displayed temporarily. toc: - label: Usage id: usage children: - label: HTML + JavaScript id: usage-html-js children: - label: "Step 1: Include the JavaScript file" id: usage-html-js-1 - label: "Step 2: Add the toaster HTML" id: usage-html-js-2 - label: "Step 3: Add your toasts" id: usage-html-js-3 - label: HTML structure id: usage-html-js-4 - label: JavaScript config object id: usage-html-js-5 - label: Jinja and Nunjucks id: usage-macro --- {% from "toast.njk" import toaster, toast %} {% from "macros/code_preview.njk" import code_preview %} {% from "macros/code_block.njk" import code_block %} {% set code_server_side %}{% endset %} {{ code_preview("toast-server-side", code_server_side, "max-w-md") }} {% set code_client_side %}{% endset %} {{ code_preview("toast-client-side", code_client_side, "max-w-md") }}

Usage

HTML + JavaScript

Step 1: Include the JavaScript file

You can either include the JavaScript file for all the components, or just the one for this component by adding this to the <head> of your page:

{% set code_script %}{% endset %} {{ code_block(code_script | prettyHtml, "html") }}
Components with JavaScript {% lucide "arrow-right" %} Use the CLI {% lucide "arrow-right" %} toast.js {% lucide "arrow-right" %}

Step 2: Add the toaster HTML

Toasts are displayed in a parent element, the "toaster". Add this to this to at the end of your <body>:

{% set code_toaster %}
{% endset %} {{ code_block(code_toaster | prettyHtml, "html") }}

You can set up the alignment of the toaster using the data-align attribute: data-align="start", data-align="center", or data-align="end" (default to data-align="end").

Step 3: Add your toasts

If you decide to server-render your toasts, or load them using asynchronoulsy with something like HTMX, you can just add the toast's markup to the toaster:

{% set code_toast_html %}
{{ toast( category="success", title="Success", description="A success toast called from the front-end.", action={ label: "Dismiss", click: "close()" } ) }}
{% endset %} {{ code_block(code_toast_html | prettyHtml, "html") }}
HTML structure {% lucide "arrow-right" %}

If you need to create a toast from the front-end, you can dispatch a custom basecoat:toast event as such:

{{ code_block(code_client_side | prettyHtml, "html") }}
JavaScript config object {% lucide "arrow-right" %}

HTML structure

<div class="toast">
Wraps around the toast component. You can add a data-duration attribute to set the duration of the toast in milliseconds (e.g. data-duration="5000" for 5 seconds). If not provided, the default duration is 3000ms (3 seconds) or 5000ms (5 seconds) for error toasts.
<div class="toast-content">
The content of the toast.
<svg aria-hidden="true"> Optional
The toast's icon.
<section>
The toast's message.
<h2>
The toast's title.
<p> Optional
The toast's description.
<footer> Optional
The toast's buttons. When clicked, the toast will be closed (unless the button's onclick is set to e.preventDefault()).
<button type="button" class="btn" onclick="{ ONCLICK }"> or <a href="{ URL }" class="btn" > Optional
The toast's action button. This can either be a link (with a href attribute) or a button (with an onclick attribute).
<button type="button" class="btn-outline" onclick="{ ONCLICK }"> Optional
The toast's cancel button (with an optional onclick attribute).

JavaScript config object

duration Optional
The duration of the toast in milliseconds. If not provided, the default duration is 3000ms (3 seconds) or 5000ms (5 seconds) for error toasts.
category Optional
Category of the toast, either success, info, warning, or error.
title
The title of the toast.
description Optional
The description of the toast.
action Optional
Action button.
label Optional
The label of the cancel button. If not provided, the default label is "Dismiss".
onclick Optional
The onclick of the cancel button.
cancel Optional
Cancel button.
label Optional
The label of the cancel button. If not provided, the default label is "Dismiss".
onclick Optional
The onclick of the cancel button.

Jinja and Nunjucks

You can use the toaster() and toast() Nunjucks or Jinja macros for this component.

Use Nunjucks or Jinja macros {% lucide "arrow-right" %} Jinja macro {% lucide "arrow-right" %} Nunjucks macro {% lucide "arrow-right" %}
{% set raw_code_toaster %}{% raw %}{% from "toast.njk" import toaster %} {{ toaster( toasts=[ { type: "success", title: "Success", description: "A success toast called from the front-end.", action: { label: "Dismiss", click: "close()" } }, { type: "info", title: "Info", description: "An info toast called from the front-end.", action: { label: "Dismiss", click: "close()" } } ] ) }}{% endraw %}{% endset%} {% set raw_code_toast %}{% raw %}{% from "toast.njk" import toast %} {{ toast( title="Event has been created", description="Sunday, December 03, 2023 at 9:00 AM", cancel={ label: "Undo" } ) }}{% endraw %}{% endset %} {% from "tabs.njk" import tabs %} {% set tabsets = [ { tab: "toaster()", panel: code_block(raw_code_toaster, "jinja", class="") }, { tab: "toast()", panel: code_block(raw_code_toast, "jinja", class="") } ] %} {{ tabs(id="macros", tabsets=tabsets) }}