Class: Ariadne::FlashComponent

Inherits:
Component
  • Object
show all
Includes:
IconHelper
Defined in:
app/components/ariadne/flash_component.rb

Overview

Use ‘FlashComponent` to inform users of successful messages, pending actions, or urgent notices.

Constant Summary collapse

DEFAULT_SCHEME =
:default
DISMISSIBLE_SCHEME_CLASS_MAPPINGS =
{
  default: "text-blue-500 bg-blue-50 hover:bg-blue-100 focus:ring-offset-blue-50 focus:ring-blue-600",
  info: "text-blue-500 bg-blue-50 hover:bg-blue-100 focus:ring-offset-blue-50 focus:ring-blue-600",
  success: "text-green-500 bg-green-50 hover:bg-green-100 focus:ring-offset-green-50 focus:ring-green-600",
  warning: "text-yellow-500 bg-yellow-50 hover:bg-yellow-100 focus:ring-offset-yellow-50 focus:ring-yellow-600",
  danger: "text-red-500 bg-red-50 hover:bg-red-100 focus:ring-offset-red-50 focus:ring-red-600",
}.freeze
VALID_DISMISSIBLE_SCHEMES =
DISMISSIBLE_SCHEME_CLASS_MAPPINGS.keys.freeze
BG_SCHEME_CLASS_MAPPINGS =
{
  default: "bg-blue-50",
  info: "bg-blue-50",
  success: "bg-green-50",
  warning: "bg-yellow-50",
  danger: "bg-red-50",
}.freeze
VALID_BG_SCHEMES =
BG_SCHEME_CLASS_MAPPINGS.keys.freeze
CONTENT_SCHEME_CLASS_MAPPINGS =
{
  default: "text-blue-700",
  info: "text-blue-700",
  success: "text-green-700",
  warning: "text-yellow-700",
  danger: "text-red-700",
}.freeze
VALID_CONTENT_SCHEMES =
CONTENT_SCHEME_CLASS_MAPPINGS.keys.freeze

Constants included from FetchOrFallbackHelper

Ariadne::FetchOrFallbackHelper::InvalidValueError, Ariadne::FetchOrFallbackHelper::TRUE_OR_FALSE

Constants inherited from Component

Component::INVALID_ARIA_LABEL_TAGS

Constants included from Status::Dsl

Status::Dsl::STATUSES

Constants included from ViewHelper

ViewHelper::HELPERS

Constants included from TestSelectorHelper

TestSelectorHelper::TEST_SELECTOR_TAG

Instance Method Summary collapse

Methods included from IconHelper

#check_icon_presence!, #has_partial_icon?, #icon_presence!, #variant_presence!

Methods included from FetchOrFallbackHelper

#check_incoming_attribute, #check_incoming_tag, #check_incoming_value, #fetch_or_raise, #fetch_or_raise_boolean

Methods included from LoggerHelper

#logger, #silence_deprecations?, #silence_warnings?

Methods included from JoinStyleArgumentsHelper

#join_style_arguments

Methods included from TestSelectorHelper

#add_test_selector

Methods included from ClassNameHelper

#class_names

Constructor Details

#initialize(tag: :div, dismissible: false, scheme: DEFAULT_SCHEME, classes: "", attributes: {}) ⇒ FlashComponent

Returns a new instance of FlashComponent.

Examples:

Schemes

<%= render(Ariadne::FlashComponent.new) { "This is a flash message!" } %>
<%= render(Ariadne::FlashComponent.new(scheme: :warning)) { "This is a warning flash message!" } %>
<%= render(Ariadne::FlashComponent.new(scheme: :danger)) { "This is a danger flash message!" } %>
<%= render(Ariadne::FlashComponent.new(scheme: :success)) { "This is a success flash message!" } %>

Dismissible

<%= render(Ariadne::FlashComponent.new(dismissible: true)) { "This is a dismissible flash message!" } %>

Icon

<%= render(Ariadne::FlashComponent.new) do |component| %>
  <% component.icon(icon: :"user-group", variant: HeroiconsHelper::Icon::VARIANT_OUTLINE) %>
  Look at this icon.
<% end %>

With actions

<%= render(Ariadne::FlashComponent.new) do |component| %>
  <% component.action do %>
    <%= render(Ariadne::ButtonComponent.new(size: :s)) { "Take action" } %>
  <% end %>
  This is a flash message with actions!
<% end %>

Parameters:

  • tag (Symbol, String) (defaults to: :div)

    The rendered tag name.

  • dismissible (Boolean) (defaults to: false)

    Whether the component can be dismissed with an X button.

  • icon (Symbol, String)

    Name of <%= link_to_heroicons %> to use.

  • scheme (Symbol) (defaults to: DEFAULT_SCHEME)

    <%= one_of(Ariadne::FlashComponent::VALID_CONTENT_SCHEMES) %>

  • classes (String) (defaults to: "")

    <%= link_to_classes_docs %>

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

    <%= link_to_attributes_docs %>



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/components/ariadne/flash_component.rb', line 98

def initialize(tag: :div, dismissible: false, scheme: DEFAULT_SCHEME, classes: "", attributes: {})
  @dismissible = dismissible

  @tag = check_incoming_tag(:div, tag)

  @scheme = fetch_or_raise(VALID_CONTENT_SCHEMES, scheme)

  @classes = class_names(
    CONTENT_SCHEME_CLASS_MAPPINGS[@scheme],
    classes
  )

  @attributes = attributes
end