Class: SdrViewComponents::Elements::ToastComponent

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/sdr_view_components/elements/toast_component.rb

Overview

Component for rendering a toast element. The disappearing toast uses ‘sdr_view_components/toast_controller` to remove itself from the DOM after its fade-out animation completes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseComponent

#args_for, #merge_actions, #merge_classes

Constructor Details

#initialize(title:, text: nil, close_text: nil, variant: :black, disappearing: false) ⇒ ToastComponent

Returns a new instance of ToastComponent.



9
10
11
12
13
14
15
16
# File 'app/components/sdr_view_components/elements/toast_component.rb', line 9

def initialize(title:, text: nil, close_text: nil, variant: :black, disappearing: false)
  @title = title
  @text = text
  @close_text = close_text
  @variant = variant
  @disappearing = disappearing
  super()
end

Instance Attribute Details

#close_textObject (readonly)

Returns the value of attribute close_text.



18
19
20
# File 'app/components/sdr_view_components/elements/toast_component.rb', line 18

def close_text
  @close_text
end

#disappearingObject (readonly)

Returns the value of attribute disappearing.



18
19
20
# File 'app/components/sdr_view_components/elements/toast_component.rb', line 18

def disappearing
  @disappearing
end

#textObject (readonly)

Returns the value of attribute text.



18
19
20
# File 'app/components/sdr_view_components/elements/toast_component.rb', line 18

def text
  @text
end

#titleObject (readonly)

Returns the value of attribute title.



18
19
20
# File 'app/components/sdr_view_components/elements/toast_component.rb', line 18

def title
  @title
end

#variantObject (readonly)

Returns the value of attribute variant.



18
19
20
# File 'app/components/sdr_view_components/elements/toast_component.rb', line 18

def variant
  @variant
end

Instance Method Details

#background_colorObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/components/sdr_view_components/elements/toast_component.rb', line 32

def background_color
  case variant
  when :red
    'bg-stanford-digital-red'
  when :green
    'bg-stanford-digital-green'
  when :poppy
    'bg-poppy-dark'
  else
    'bg-stanford-black'
  end
end

#classesObject



20
21
22
# File 'app/components/sdr_view_components/elements/toast_component.rb', line 20

def classes
  merge_classes('toast align-items-center show', ('toast-disappear' if disappearing))
end

#dataObject



28
29
30
# File 'app/components/sdr_view_components/elements/toast_component.rb', line 28

def data
  { controller: 'sdr-toast' } if disappearing
end

#toast_body_classesObject



24
25
26
# File 'app/components/sdr_view_components/elements/toast_component.rb', line 24

def toast_body_classes
  merge_classes([background_color], %w[toast-body text-white])
end