Class: UI::TooltipComponent

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
TooltipBehavior
Defined in:
app/view_components/ui/tooltip_component.rb

Overview

TooltipComponent - ViewComponent implementation

Root container for tooltip. Manages tooltip state via Stimulus controller.

Supports asChild pattern for composition without wrapper elements.

Examples:

Basic usage

<%= render UI::TooltipComponent.new do %>
  <%= render UI::TooltipTriggerComponent.new { "Hover me" } %>
  <%= render UI::TooltipContentComponent.new { "Tooltip text" } %>
<% end %>

With asChild - pass attributes to custom element

<%= render UI::TooltipComponent.new(as_child: true) do |tooltip| %>
  <%= render UI::InputGroupAddonComponent.new(**tooltip.tooltip_attrs) do %>
    <%= render UI::TooltipTriggerComponent.new(as_child: true) do |trigger| %>
      <%= render UI::InputGroupButtonComponent.new(**trigger.trigger_attrs) { "Info" } %>
    <% end %>
    <%= render UI::TooltipContentComponent.new { "Content" } %>
  <% end %>
<% end %>

Instance Method Summary collapse

Methods included from TooltipBehavior

#tooltip_data_attributes, #tooltip_html_attributes

Constructor Details

#initialize(as_child: false, **attributes) ⇒ TooltipComponent

Returns a new instance of TooltipComponent.

Parameters:

  • as_child (Boolean) (defaults to: false)

    When true, yields self to block for attribute access

  • attributes (Hash)

    Additional HTML attributes



29
30
31
32
# File 'app/view_components/ui/tooltip_component.rb', line 29

def initialize(as_child: false, **attributes)
  @as_child = as_child
  @attributes = attributes
end

Instance Method Details

#callObject



39
40
41
42
43
44
45
46
47
48
49
# File 'app/view_components/ui/tooltip_component.rb', line 39

def call
  if @as_child
    # asChild mode: yield self to block, child accesses tooltip_attrs
    content
  else
    # Default: render wrapper div with controller
     :div, **tooltip_html_attributes.merge(@attributes.except(:data)) do
      content
    end
  end
end

#tooltip_attrsObject

Returns tooltip attributes for as_child mode



35
36
37
# File 'app/view_components/ui/tooltip_component.rb', line 35

def tooltip_attrs
  tooltip_html_attributes.deep_merge(@attributes)
end