Class: Fluxbit::AlertComponent

Inherits:
Component
  • Object
show all
Includes:
Config::AlertComponent
Defined in:
app/components/fluxbit/alert_component.rb

Overview

The ‘Fluxbit::AlertComponent` is a customizable alert component that extends `Fluxbit::Component`. It provides various options to display alert messages with different styles, icons, and behaviors such as close functionality and animations.

Example usage:

= render Fluxbit::AlertComponent.new(color: :success, icon: "check").with_content("Alert message")

Constant Summary

Constants inherited from Component

Component::ComponentObj

Instance Method Summary collapse

Methods inherited from Component

#add, #add_popover_or_tooltip, #anyicon, #element_name, #fx_id, #options, #random_id, #remove_class, #render_popover_or_tooltip, #target

Constructor Details

#initialize(**props) ⇒ Fluxbit::AlertComponent

Initializes the alert component with the given properties.

Examples:

= render Fluxbit::AlertComponent.new(color: :success, icon: "check").with_content("Alert message")

Parameters:

  • props (Hash)

    The properties to customize the alert.

Options Hash (**props):

  • :color (Symbol, String) — default: :info

    The color style of the alert.

  • :icon (Symbol, String, Boolean) — default: :default

    The icon to display in the alert or ‘false` to omit.

  • :class_icon (String) — default: ''

    Additional CSS classes for the icon.

  • :can_close (Boolean) — default: true

    Determines if the alert can be closed.

  • :remove_class (String) — default: ''

    Classes to remove from the default class list.

  • :dismiss_timeout (Integer) — default: 3000

    The timeout in milliseconds before the alert dismisses itself.

  • :fade_in_animation (Boolean) — default: true

    Determines if the alert should fade in.

  • :out_animation (Symbol) — default: :fade_out

    The type of fade out/dismiss animation.

  • :all_rounded (Boolean) — default: true

    Determines if the alert has rounded corners.

  • **props (Hash)

    Remaining options declared as HTML attributes.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/components/fluxbit/alert_component.rb', line 34

def initialize(**props)
  super
  @props = props
  @color = define_color(@props.delete(:color) || @@color)
  @icon = define_icon(@props.delete(:icon) || :default, @props.delete(:class_icon) || "")
  @can_close = @props[:can_close].nil? ? @@can_close : @props.delete(:can_close)
  @all_rounded = @props[:all_rounded].nil? ? @@all_rounded : @props.delete(:all_rounded)
  @props["id"] = fx_id if @props["id"].nil?
  @props[:role] = "alert"
  prop_fade_in_automation = @props.delete(:fade_in_animation)

  animation_props(
    dismiss_timeout: [ (@props.delete(:dismiss_timeout) || @@dismiss_timeout).to_i, 0 ].max,
    fade_in_animation: prop_fade_in_automation.nil? ? @@fade_in_animation : prop_fade_in_automation,
    out_animation: @props.delete(:out_animation) || @@out_animation
  )
  declare_classes
  @props[:class] = remove_class(@props.delete(:remove_class) || "", @props[:class])
end

Instance Method Details

#animation_props(dismiss_timeout:, fade_in_animation:, out_animation:) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/components/fluxbit/alert_component.rb', line 54

def animation_props(dismiss_timeout:, fade_in_animation:, out_animation:)
  return if [ :dont_remove, nil ].include?(out_animation) || dismiss_timeout.to_i == 0

  @props["data-controller"] = "notification"
  @props["data-action"] = "notification#hide"
  @props["data-notification-delay-value"] = dismiss_timeout.to_s

  add(to: @props, class: "transition transform duration-1000 hidden")

  if fade_in_animation
    @props["data-transition-enter-from"] = "opacity-0"
    @props["data-transition-enter-to"] = "opacity-100"
  else
    @props["data-transition-enter-from"] = "opacity-100"
    @props["data-transition-enter-to"] = "opacity-100"
  end

  @props["data-transition-leave-from"] = styles[:animations][out_animation.to_sym][:from]
  @props["data-transition-leave-to"] = styles[:animations][out_animation.to_sym][:to]
end

#callObject



75
76
77
78
79
80
81
# File 'app/components/fluxbit/alert_component.rb', line 75

def call
   :div, @props do
    concat @icon
    concat (:div, content, class: "ml-3 text-sm font-medium")
    concat close_button
  end
end