Module: Bh::Helpers::Toasts

Included in:
Bh::Helpers
Defined in:
lib/bh/helpers/toasts.rb

Overview

What the flash says, as the toasts every Bh page shows them in.

Constant Summary collapse

TONES =

Bootstrap's tone for each key, so a notice and an alert read apart; a key of the host's own reads neutral.

{ 'notice' => 'theme-success', 'alert' => 'theme-danger' }.freeze
HOLD =

The toast controller's hooks: the timed hide stops while a reader hovers or focuses a toast, so a message stays while someone reads it or aims for the X.

'mouseenter->toast#stopTimer mouseleave->toast#startTimer ' \
'focusin->toast#stopTimer focusout->toast#startTimer'

Instance Method Summary collapse

Instance Method Details

#toasts(data: {}) {|key, message| ... } ⇒ String?

Every message in the flash as a toast at the bottom right, visible at first paint and gone after a moment or a click. Only messages: a value that is not words is data some other part of a host keeps in the flash, and is not something to show. Nothing at all where there is nothing to say. data-turbo-temporary, so a toast born visible does not replay from the page's snapshot on every Back.

Parameters:

  • data (Hash) (defaults to: {})

    anything else the stack of them carries, for a host marking what the page it stands on is about.

Yields:

  • (key, message)

    what to draw in place of the message, for a host with a link to splice into it. The message itself where no block is given.

Returns:

  • (String, nil)

    the toasts, or nothing where the flash says nothing.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bh/helpers/toasts.rb', line 24

def toasts(data: {}, &)
  messages = flash.to_hash.select { |_, message| message.is_a? String }
  return if messages.empty?

  toasts = messages.map do |key, message|
    bh_toast key, block_given? ? yield(key, message) : message
  end

  tag.div safe_join(toasts), class: 'toast-container position-fixed bottom-0 end-0 p-3',
                             data: { turbo_temporary: '' }.merge(data)
end