Module: StimulusOverlayHelpers::ViewHelpers

Defined in:
lib/stimulus_overlay_helpers/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

what it does:

  • a) wraps the button_content in element like: %button{ data: { controller: 'dropdown', ... } } and renders it at place
  • b) wraps the panel_content in a corresponding element and renders it to the overlays_box (because of z-index-hierarchy)
  • c) the stimulus controller always adds the class .dropdown-panel to the panel
  • options: ** class: custom class for the dropdown-button ** panel_class: custom class for the dropdown-panel ** src: custom src for the dropdown-panel, triggers a GET xhr request that fills up the panel

Parameters:

  • Proc (button_content)

    or String for content of the button element

  • true (panel_at_place)

    (default: false) => it would render the panel into content_for(:overlays_box) on sticky parts (left-menu / top-bar) panels should be at place for avoiding that the panel would scroll with the content



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/stimulus_overlay_helpers/view_helpers.rb', line 17

def dropdown(button_content, title = nil, options = {}, &panel_content)
  if title.is_a?(Hash)
    options = title.dup
    title = nil
  end

  panel_at_place = options.delete(:panel_at_place)
  src = options.delete(:src)
  button_class = [options.delete(:button_class), 'dropdown-button', options[:class]].compact.join(' ')
  panel_class = [options.delete(:class), 'dropdown-panel'].compact.join(' ')
  close_btn_proc = StimulusOverlayHelpers.close_button_proc

  id = "dropdown-panel-#{SecureRandom.hex(4)}"

  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  # create the Button
  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  button_content = capture(&panel_content_button = button_content) if button_content.is_a?(Proc)
  button_options = options.merge(class: button_class, data: { controller: 'hotwire-svelte-helpers-dropdown', panel_id: id })
  button_element = (:div, button_content, button_options)

  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  # create the panel
  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  panel_options = options.merge(class: panel_class, style: 'display: none;', id: id)
  panel_options['data-src'] = src if src.present?

  panel_element = (:div, panel_options) do
    safe_join([
                (:div, class: 'header') do
                  concat (:div, title, class: 'title')
                  concat (:div, class: 'buttons') { close_btn_proc.present? ? close_btn_proc.call(self) : (:span, 'X', class: 'close-button', data: { close: true }) }
                end,
                (:div, class: 'body') do
                  capture(&panel_content) if block_given?
                end,
                ((:div, nil, id: 'arrow') if StimulusOverlayHelpers.dropdown_arrow),
              ])
  end

  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  # return the result
  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  if panel_at_place
    button_element + panel_element
  else
    content_for(:overlays_box, panel_element)
    button_element
  end
end

what it does:

  • a) wraps the button_content in element like: %button{ data: { controller: 'modal', ... } } and renders it at place
  • b) wraps the panel_content in a full-screen overlay element and renders it to the overlays_box (because of z-index-hierarchy)
  • c) the stimulus controller always adds the class .modal-panel to the panel and .modal-overlay to the backdrop

Parameters:

  • Proc (button_content)

    or String for content of the button element

  • true (panel_at_place)

    (default: false) => it would render the overlay into content_for(:overlays_box) on sticky parts (left-menu / top-bar) overlays should be at place for avoiding that the overlay would scroll with the content



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/stimulus_overlay_helpers/view_helpers.rb', line 79

def modal(button_content, title = nil, options = {}, &panel_content)
  if title.is_a?(Hash)
    options = title.dup
    title = nil
  end

  panel_at_place = options.delete(:panel_at_place)
  src = options.delete(:src)
  button_class = [options.delete(:button_class) || 'modal-button', options[:class]].compact.join(' ')
  panel_class = [options.delete(:class), 'modal-panel'].compact.join(' ')
  close_btn_proc = StimulusOverlayHelpers.close_button_proc

  id = "modal-overlay-#{SecureRandom.hex(4)}"

  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  # create the Button
  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  button_content = capture(&button_content) if button_content.is_a?(Proc)
  button_options = options.merge(class: button_class, data: { controller: 'hotwire-svelte-helpers-modal', panel_id: id })
  button_element = (:div, button_content, button_options)

  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  # create the overlay
  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  panel_options = options.merge(class: panel_class)
  panel_options['data-src'] = src if src.present?

  overlay_element = (:div, class: 'modal-overlay', style: 'display:none;', id: id) do
    (:div, panel_options) do
      safe_join([
                  (:div, class: 'header') do
                    concat (:div, title, class: 'title')
                    concat (:div, class: 'buttons') { close_btn_proc.present? ? close_btn_proc.call(self) : (:span, 'X', class: 'close-button', data: { close: true }) }
                  end,
                  (:div, class: 'body') do
                    capture(&panel_content) if block_given?
                  end
                ])
    end
  end

  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  # return the result
  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  if panel_at_place
    button_element + overlay_element
  else
    content_for(:overlays_box, overlay_element)
    button_element
  end
end

#tooltip(tip, options = {}) ⇒ Object

tooltip helper

what it does:

  • a) wraps the label in element like: %span{ data: { controller: 'tooltip', ... } } and renders it at place
  • b) wraps the tip in a corresponding panel element and renders it to the overlays_box (because of z-index-hierarchy)
  • c) the stimulus controller always adds the class .tooltip-panel to the panel

Parameters:

  • Proc (tip)

    or String for the tooltip content (the panel shown on hover)

  • true (panel_at_place)

    (default: false) => it would render the panel into content_for(:overlays_box) on sticky parts (left-menu / top-bar) panels should be at place for avoiding that the panel would scroll with the content

  • Number (delay)

    (default: 0.4) => delay in seconds before the tooltip is shown



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/stimulus_overlay_helpers/view_helpers.rb', line 143

def tooltip(tip, options = {}, &)
  label = capture(&) if block_given?
  tip = capture &tip if tip.is_a?(Proc)
  return '' if label.blank?
  return label if tip.blank?

  panel_at_place = options.delete(:panel_at_place) || false
  delay = options.delete(:delay) || 0.4
  src = options.delete(:src)
  label_class = [options.delete(:label_class) || 'tooltip-label', options[:class]].compact.join(' ')
  panel_class = [options.delete(:class), 'tooltip-panel'].compact.join(' ')

  id = "tooltip-#{SecureRandom.hex(4)}"

  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  # create the Label
  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  label_options = options.merge(class: label_class, data: { controller: 'hotwire-svelte-helpers-tooltip', panel_id: id, delay: delay })
  label_element = (:span, label, label_options)

  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  # create the panel
  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  panel_options = options.merge(class: panel_class, style: 'display: none', id: id)
  panel_options['data-src'] = src if src.present?

  panel_element = (:div, panel_options) do
    ( :div, '', id: "arrow") +
      (:div, tip, class: 'tooltip-content')
  end

  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  # return the result
  # xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

  if panel_at_place
    label_element + panel_element
  else
    content_for(:overlays_box, panel_element)
    label_element
  end
end