Class: UI::Tooltip

Inherits:
Phlex::HTML
  • Object
show all
Includes:
TooltipBehavior
Defined in:
app/components/ui/tooltip.rb

Overview

Tooltip - Phlex implementation

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

Supports asChild pattern for composition without wrapper elements.

Examples:

Basic usage

render UI::Tooltip.new do
  render UI::Trigger.new { "Hover me" }
  render UI::Content.new { "Tooltip text" }
end

With asChild trigger

render UI::Tooltip.new do
  render UI::Trigger.new(as_child: true) do |attrs|
    render UI::Button.new(**attrs) { "Hover" }
  end
  render UI::Content.new { "Tooltip text" }
end

With asChild - pass attributes to custom element

render UI::Tooltip.new(as_child: true) do |tooltip_attrs|
  render UI::InputGroupAddon.new(**tooltip_attrs) do
    render UI::TooltipTrigger.new(as_child: true) do |trigger_attrs|
      render UI::InputGroupButton.new(**trigger_attrs) { "Info" }
    end
    render UI::TooltipContent.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) ⇒ Tooltip

Returns a new instance of Tooltip.

Parameters:

  • as_child (Boolean) (defaults to: false)

    When true, yields attributes to block instead of rendering wrapper

  • attributes (Hash)

    Additional HTML attributes



37
38
39
40
# File 'app/components/ui/tooltip.rb', line 37

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

Instance Method Details

#view_template(&block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/components/ui/tooltip.rb', line 42

def view_template(&block)
  tooltip_attrs = tooltip_html_attributes.deep_merge(@attributes)

  if @as_child
    # Yield data attributes to block - child receives controller setup
    yield(tooltip_attrs) if block_given?
  else
    # Default: render wrapper div with controller
    div(**tooltip_attrs) do
      yield if block_given?
    end
  end
end