Class: UI::TooltipTrigger

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

Overview

Trigger - Phlex implementation

The interactive element that shows/hides the tooltip on hover/focus. Supports asChild pattern for composition with other components.

Examples:

Default trigger (renders as button)

render UI::Trigger.new { "Hover me" }

With asChild - compose with Button

render UI::Trigger.new(as_child: true) do |attrs|
  render UI::Button.new(**attrs, variant: :outline) { "Hover" }
end

With asChild - custom element

render UI::Trigger.new(as_child: true) do |attrs|
  span(**attrs, class: "cursor-help") { "Help" }
end

Instance Method Summary collapse

Methods included from TooltipTriggerBehavior

#tooltip_trigger_classes, #tooltip_trigger_data_attributes, #tooltip_trigger_html_attributes

Constructor Details

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

Returns a new instance of TooltipTrigger.

Parameters:

  • as_child (Boolean) (defaults to: false)

    If true, yields attributes to block instead of rendering button

  • attributes (Hash)

    Additional HTML attributes



25
26
27
28
# File 'app/components/ui/tooltip_trigger.rb', line 25

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

Instance Method Details

#view_template(&block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/components/ui/tooltip_trigger.rb', line 30

def view_template(&block)
  trigger_attrs = tooltip_trigger_html_attributes.deep_merge(@attributes)

  if @as_child
    # asChild mode: yield attributes to block
    # The caller is responsible for rendering an element with these attributes
    yield(trigger_attrs) if block_given?
  else
    # Default mode: render as button
    button(**trigger_attrs, &block)
  end
end