Class: Fluxbit::Component

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/fluxbit/component.rb

Constant Summary collapse

ComponentObj =

Custom class to hold button properties and content

Data.define(:props, :content)

Instance Method Summary collapse

Constructor Details

#initialize(**props) ⇒ Component

Returns a new instance of Component.



9
10
11
12
13
14
15
16
17
# File 'app/components/fluxbit/component.rb', line 9

def initialize(**props)
  @popover_placement = props.delete(:popover_placement) || :right
  @popover_trigger = props.delete(:popover_trigger) || :hover # or :click
  @popover_text = props.delete(:popover_text)

  @tooltip_placement = props.delete(:tooltip_placement) || :right
  @tooltip_trigger = props.delete(:tooltip_trigger) || :hover # or :click
  @tooltip_text = props.delete(:tooltip_text)
end

Instance Method Details

#add(to:, first_element: false, **props) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'app/components/fluxbit/component.rb', line 19

def add(to:, first_element: false, **props)
  unless props[:class].nil?
    to[:class] = (to[:class] || "")
      .split
      .insert((first_element ? 0 : -1), props[:class])
      .join(" ")
  end
  to
end

#add_popover_or_tooltipObject



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/components/fluxbit/component.rb', line 48

def add_popover_or_tooltip
  if popover? || @popover_text.present?
    @props["data-popover-placement"] = @popover_placement
    @props["data-popover-trigger"] = @popover_trigger unless @popover_trigger == :hover
    @props["data-popover-target"] = target
  end

  if tooltip? || @tooltip_text.present?
    @props["data-tooltip-placement"] = @tooltip_placement
    @props["data-tooltip-trigger"] = @tooltip_trigger unless @tooltip_trigger == :hover
    @props["data-tooltip-target"] = target
  end
end

#anyicon(icon:, **props) ⇒ Object



69
70
71
# File 'app/components/fluxbit/component.rb', line 69

def anyicon(icon:, **props)
  Anyicon::Icon.render(icon: icon, **props)
end

#element_nameObject



81
82
83
84
85
# File 'app/components/fluxbit/component.rb', line 81

def element_name
  self.class.to_s.match(/Fluxbit::(\w+)Component/)[1].underscore
rescue
  "any"
end

#fx_idObject



77
78
79
# File 'app/components/fluxbit/component.rb', line 77

def fx_id
  @fx_id ||= "#{element_name}-#{random_id}"
end

#options(value, collection: nil, default: nil) ⇒ Object



33
34
35
36
37
38
39
# File 'app/components/fluxbit/component.rb', line 33

def options(value, collection: nil, default: nil)
  if collection.nil?
    value.nil? ? default : value
  else
    value.in?(collection) ? value : default
  end
end

#random_idObject



73
74
75
# File 'app/components/fluxbit/component.rb', line 73

def random_id
  (0...30).map { ("a".."z").to_a[rand(26)] }.join
end

#remove_class(elements, from) ⇒ Object



29
30
31
# File 'app/components/fluxbit/component.rb', line 29

def remove_class(elements, from)
  from.split.reject { |c| c.in?(elements.split) }.join(" ")
end

#render_popover_or_tooltipObject



41
42
43
44
45
46
# File 'app/components/fluxbit/component.rb', line 41

def render_popover_or_tooltip
  safe_join [
    (@popover_text.nil? ? "" : Fluxbit::PopoverComponent.new(id: target, **(@popover_props || {})).with_content(@popover_text).render_in(view_context)),
    (@tooltip_text.nil? ? "" : Fluxbit::TooltipComponent.new(id: target, **(@tooltip_props || {})).with_content(@tooltip_text).render_in(view_context))
  ]
end

#targetObject



62
63
64
65
66
67
# File 'app/components/fluxbit/component.rb', line 62

def target
  @popover_target ||= "#{
    @props.try('for') ||
    @props.try(:for) ||
    (0...10).map { ('a'..'z').to_a[rand(26)] }.join}_target"
end