--- 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") }}
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:
Toasts are displayed in a parent element, the "toaster". Add this to this to at the end of your <body>:
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").
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:
If you need to create a toast from the front-end, you can dispatch a custom basecoat:toast event as such:
<div class="toast">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"><svg aria-hidden="true"> Optional<section><h2><p> Optional<footer> Optionalonclick is set to e.preventDefault()).
<button type="button" class="btn" onclick="{ ONCLICK }"> or <a href="{ URL }" class="btn" > Optionalhref attribute) or a button (with an onclick attribute).<button type="button" class="btn-outline" onclick="{ ONCLICK }"> Optionalonclick attribute).duration Optionalerror toasts.category Optionalsuccess, info, warning, or error.titledescription Optionalaction Optionallabel Optionalonclick Optionalcancel Optionallabel Optionalonclick OptionalYou can use the toaster() and toast() Nunjucks or Jinja macros for this component.