Class: Panda::Core::Admin::FlashMessageComponent

Inherits:
Base
  • Object
show all
Defined in:
app/components/panda/core/admin/flash_message_component.rb

Constant Summary

Constants inherited from Base

Base::TAILWIND_MERGER

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message: "", kind: :default, temporary: true, subtitle: nil, **attrs) ⇒ FlashMessageComponent

Returns a new instance of FlashMessageComponent.



7
8
9
10
11
12
13
# File 'app/components/panda/core/admin/flash_message_component.rb', line 7

def initialize(message: "", kind: :default, temporary: true, subtitle: nil, **attrs)
  @message = message
  @kind = kind
  @temporary = temporary
  @subtitle = subtitle
  super(**attrs)
end

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind.



15
16
17
# File 'app/components/panda/core/admin/flash_message_component.rb', line 15

def kind
  @kind
end

#messageObject (readonly)

Returns the value of attribute message.



15
16
17
# File 'app/components/panda/core/admin/flash_message_component.rb', line 15

def message
  @message
end

#subtitleObject (readonly)

Returns the value of attribute subtitle.



15
16
17
# File 'app/components/panda/core/admin/flash_message_component.rb', line 15

def subtitle
  @subtitle
end

#temporaryObject (readonly)

Returns the value of attribute temporary.



15
16
17
# File 'app/components/panda/core/admin/flash_message_component.rb', line 15

def temporary
  @temporary
end

Instance Method Details

#icon_colour_cssObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/components/panda/core/admin/flash_message_component.rb', line 56

def icon_colour_css
  case @kind
  when :success
    "text-emerald-600"
  when :alert, :error
    "text-rose-600"
  when :warning
    "text-amber-600"
  when :info, :notice
    "text-sky-600"
  else
    "text-gray-500"
  end
end

#icon_cssObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/components/panda/core/admin/flash_message_component.rb', line 86

def icon_css
  case @kind
  when :success
    "fa-circle-check"
  when :alert, :error
    "fa-circle-xmark"
  when :warning
    "fa-triangle-exclamation"
  when :info, :notice
    "fa-circle-info"
  else
    "fa-circle-info"
  end
end

#notification_attrsObject



17
18
19
20
21
22
23
24
25
# File 'app/components/panda/core/admin/flash_message_component.rb', line 17

def notification_attrs
  {
    class: "pointer-events-auto w-full max-w-md translate-y-0 transform rounded-2xl border px-4 py-3 shadow-lg transition duration-300 ease-out sm:translate-x-0 starting:translate-y-2 starting:opacity-0 starting:sm:translate-x-2 starting:sm:translate-y-0 #{tone_classes}",
    data: {
      controller: "alert",
      alert_dismiss_after_value: (@temporary ? "5000" : nil)
    }.compact
  }
end

#render_close_buttonObject



45
46
47
48
49
50
51
52
53
54
# File 'app/components/panda/core/admin/flash_message_component.rb', line 45

def render_close_button
  (:div, class: "ml-4 flex shrink-0") do
    (:button,
      type: "button",
      class: "inline-flex items-center cursor-pointer opacity-60 hover:opacity-100",
      data: {action: "alert#close"}) do
      (:i, "", class: "fa-solid fa-xmark")
    end
  end
end

#render_contentObject



33
34
35
36
37
38
39
40
41
42
43
# File 'app/components/panda/core/admin/flash_message_component.rb', line 33

def render_content
  (:div, class: "ml-2 w-0 flex-1") do
    message_html = (:p, @message, class: "text-sm font-medium flash-message-title")
    subtitle_html = if @subtitle
      (:p, @subtitle, class: "mt-1 text-xs opacity-80 flash-message-subtitle")
    else
      "".html_safe
    end
    (message_html + subtitle_html).html_safe
  end
end

#render_iconObject



27
28
29
30
31
# File 'app/components/panda/core/admin/flash_message_component.rb', line 27

def render_icon
  (:div, class: "shrink-0") do
    (:i, "", class: "fa-solid size-5 #{icon_css} #{icon_colour_css}")
  end
end

#tone_classesObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/components/panda/core/admin/flash_message_component.rb', line 71

def tone_classes
  case @kind
  when :success
    "bg-emerald-50 text-emerald-700 border-emerald-200"
  when :alert, :error
    "bg-rose-50 text-rose-700 border-rose-200"
  when :warning
    "bg-amber-50 text-amber-700 border-amber-200"
  when :info, :notice
    "bg-sky-50 text-sky-700 border-sky-200"
  else
    "bg-white text-gray-700 border-gray-200"
  end
end