Class: FoxTail::ModalComponent

Inherits:
BaseComponent show all
Includes:
Concerns::HasStimulusController
Defined in:
app/components/fox_tail/modal_component.rb

Defined Under Namespace

Classes: StimulusController

Instance Attribute Summary

Attributes inherited from BaseComponent

#html_attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseComponent

classname_merger, #initialize, stimulus_merger, use_stimulus?, #with_html_attributes, #with_html_class

Methods inherited from Base

fox_tail_config

Constructor Details

This class inherits a constructor from FoxTail::BaseComponent

Class Method Details

.stimulus_controller_nameObject



115
116
117
# File 'app/components/fox_tail/modal_component.rb', line 115

def stimulus_controller_name
  :modal
end

Instance Method Details

#before_renderObject



33
34
35
36
37
38
39
40
# File 'app/components/fox_tail/modal_component.rb', line 33

def before_render
  super

  html_attributes[:class] = root_classes
  html_attributes[:tabindex] = -1
  html_attributes[:aria] ||= {}
  html_attributes[:aria][:hidden] = true
end

#callObject



42
43
44
45
46
47
# File 'app/components/fox_tail/modal_component.rb', line 42

def call
  capture do
    concat trigger if trigger?
    concat render_modal
  end
end

#close_action(event: :click) ⇒ Object



21
22
23
# File 'app/components/fox_tail/modal_component.rb', line 21

def close_action(event: :click)
  stimulus_controller.action :hide, event: event
end

#close_button_component(options = {}) ⇒ Object



76
77
78
79
# File 'app/components/fox_tail/modal_component.rb', line 76

def close_button_component(options = {})
  options = merge_close_action options
  FoxTail::ButtonComponent.new options
end

#close_icon_button_component(icon_or_options = {}, options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/components/fox_tail/modal_component.rb', line 59

def close_icon_button_component(icon_or_options = {}, options = {})
  if icon_or_options.is_a? Hash
    options = icon_or_options
    icon_or_options = "x-mark"
  end

  options = merge_close_action options
  options[:variant] = :modal
  options[:color] = :modal
  options[:size] = :modal
  options[:theme] = theme.theme :close_icon_button

  component = FoxTail::IconButtonComponent.new options
  component.with_icon icon_or_options if icon_or_options.present?
  component
end

#merge_close_action(attributes, event: :click) ⇒ Object



25
26
27
28
29
30
31
# File 'app/components/fox_tail/modal_component.rb', line 25

def merge_close_action(attributes, event: :click)
  return attributes unless use_stimulus?

  attributes[:data] ||= {}
  attributes[:data][:action] = stimulus_merger.merge_actions attributes[:data][:action], close_action(event: event)
  attributes
end

#stimulus_controller_optionsObject



49
50
51
52
53
54
55
56
57
# File 'app/components/fox_tail/modal_component.rb', line 49

def stimulus_controller_options
  {
    static: static?,
    closeable: closeable?,
    open: open?,
    visible_classes: visible_classes,
    hidden_classes: hidden_classes
  }
end

#tag_idObject



17
18
19
# File 'app/components/fox_tail/modal_component.rb', line 17

def tag_id
  html_attributes[:id] ||= :"modal_#{SecureRandom.hex(4)}"
end