Toast

Does it smell burnt in here?
Includes a Ruby component and corresponding Rails helper
Ruby
Includes a Stimulus controller
Stimulus

Example

<html data-controller="nk--toast">
  <body>
    <!-- ... -->
    <%= nk_button "Do the thing",
          data: { **nk_toast_action(title: "Hi", description: "There") } %>
    <!-- ... -->
    <%= nk_toast %>
  </body>
</html>

Flash messages

The Toast component comes with a built-in mechanism to show your application's flash messages.

Consider these Turbo enabled actions:

<%= nk_button_to "Eat hotdog", hotdog_path %>
<%= nk_destructive_button_to "Throw away hotdog", flash_path, method: :delete %>

Your controller sets the message:

class HotdogsController < ApplicationController
  def create
    respond_to do |format|
      format.turbo_stream { flash.now[:notice] = "Hope it was good!" }
    end
  end
end

And your action notifies NitroKit::Toast to pick it up.

In create.turbo_stream.erb:

<%= nk_toast_turbo_stream_refresh %>

Variants

:default

  • Default
    Your message
  • :success

  • Success
    Your message
  • :warning

  • Warning
    Your message
  • :error

  • Error
    Your message
  • Read more about variants

    Usage

    1. Add data-controller="nk--toast" to the <html> tag.
    2. Add <%= nk_toast %> to bottom of your <body> tag.
    nk_toast(variant: :default, **attrs) { |toast| ... }
    PropertyDefaultDescription
    variant:defaultOne of :default / :warning / :error / :success
    **attrsHTML attributes for <div> element
    toast.item(title: nil, description: nil, **attrs)
    PropertyDefaultDescription
    titlenil Title of the item
    descriptionnil Body text of the item
    **attrsHTML attributes for <li> element
    nk_toast_action(title: nil, description: nil)
    PropertyDefaultDescription
    titlenilPlain text content
    descriptionnilPlain text content
    eventnil JavaScript event that should trigger. Defaults to the default for the element.
    Use to add toasts from Stimulus events
    nk_toast_turbo_stream_refresh()
    PropertyDefaultDescription
    Use in your TurboStream responses to flush and show flash messages