Flowbite::Toast

Renders a toast notification element.

By default the toast renders Flowbite’s markup and leaves dismissing it up to you, ie by passing data attributes to the close button.

With behavior: :stimulus the close button dismisses the toast like flowbite.js does: the toast fades out and is then hidden. This requires the flowbite--toast Stimulus controller, which you install using

1
bin/rails generate flowbite_components:behavior toast

and its dependency, stimulus-use. The controller dispatches a flowbite--toast:hide event when the toast starts hiding. Customize the transition using the leave-active, leave-from, and leave-to Stimulus classes, ie data: {"flowbite--toast-leave-active-class": "transition-opacity duration-500"}.

The controller is only wired to the default close button. When using the button slot, add data: {action: "flowbite--toast#dismiss"} to your button yourself.

Usage

1
<%= render(Flowbite::Toast.new(message: "Something has happened!")) %>

Passing options to the default button

1
2
3
4
<%= render(Flowbite::Toast.new(
button: {options: {data: {action: "toast#dismiss"}}},
message: "Something has happened!"
)) %>

Dismissing using the bundled Stimulus controller

1
2
3
4
<%= render(Flowbite::Toast.new(
behavior: :stimulus,
message: "Something has happened!"
)) %>

Using the button slot

1
2
3
4
5
<%= render(Flowbite::Toast.new(message: "Something has happened!")) do |toast| %>
<% toast.with_button do %>
<%= render(Flowbite::Toast::CloseButton.new(data: {action: "toast#dismiss"})) %>
<% end %>
<% end %>

Using the button slot with the bundled Stimulus controller

1
2
3
4
5
<%= render(Flowbite::Toast.new(behavior: :stimulus, message: "Something has happened!")) do |toast| %>
<% toast.with_button do %>
<%= render(Flowbite::Toast::CloseButton.new(data: {action: "flowbite--toast#dismiss"})) %>
<% end %>
<% end %>

Examples

Styles

Use these toast notification styles to show feedback messages to your users. Toast notifications appear with different colors and icons based on the style.

Dismissible

Control whether the toast can be dismissed with a close button.

Custom Styling

Add custom classes to the toast container.

API reference

Inherits from Flowbite

In namespace

Slots

button Flowbite::Toast::CloseButton The button rendered at the end of the toast. Defaults to a close button. Use `with_button` to set custom content.

Constructor

def initialize(message:, behavior: nil, button: {}, dismissible: true, style: :default, class: nil, **options)

Parameters

behavior Symbol, nil The client-side behavior to render markup for (:none, :stimulus). Use :stimulus to dismiss the toast using the Stimulus controller installed by `bin/rails generate flowbite_components:behavior toast`. Defaults to Flowbite::Components.default_behavior. See Flowbite::Behavior.
button Hash Configuration for the default button, rendered using the Toast::CloseButton component. The hash can contain: - `options`: Additional HTML options to pass to the button element, e.g. `{data: {action: "toast#dismiss"}}` Alternatively, you can use the `button` slot to provide the entire button element yourself.
class Array<String> Additional CSS classes for the toast container.
dismissible Boolean Whether the toast can be dismissed (default: true).
message String The message to display in the toast.
options Hash Additional HTML options for the toast container.
style Symbol The color style of the toast (:default, :success, :danger, :warning).

See also

Ruby API documentation for class Flowbite::Toast