Class: UI::HoverCardTrigger

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

Overview

Trigger - Phlex implementation

Element that triggers the hover card on hover. Uses HoverCardTriggerBehavior for shared styling logic. Supports asChild pattern for composition.

Examples:

Basic usage

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: :link) { "@nextjs" }
end

With custom tag

render UI::Trigger.new(tag: :a, href: "#") { "Link trigger" }

Instance Method Summary collapse

Methods included from HoverCardTriggerBehavior

#trigger_classes, #trigger_data_attributes, #trigger_html_attributes

Constructor Details

#initialize(as_child: false, tag: :span, classes: "", **attributes) ⇒ HoverCardTrigger

Returns a new instance of HoverCardTrigger.

Parameters:

  • as_child (Boolean) (defaults to: false) —

    If true, yields attributes to block instead of rendering wrapper

  • tag (Symbol) (defaults to: :span) —

    HTML tag to use for the trigger (default: :span)

  • classes (String) (defaults to: "") —

    Additional CSS classes to merge

  • attributes (Hash) —

    Additional HTML attributes



26
27
28
29
30
31
# File 'app/components/ui/hover_card_trigger.rb', line 26

def initialize(as_child: false, tag: :span, classes: "", **attributes)
  @as_child = as_child
  @tag = tag
  @classes = classes
  @attributes = attributes
end

Instance Method Details

#view_template(&block) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/components/ui/hover_card_trigger.rb', line 33

def view_template(&block)
  trigger_attrs = trigger_html_attributes

  if @as_child
    # Yield attributes to block - child must accept and apply them
    yield(trigger_attrs) if block_given?
  else
    # Add tabindex for keyboard focus on non-focusable elements
    trigger_attrs[:tabindex] ||= "0" if [:span, :div].include?(@tag)
    # Default: render as specified tag (span by default)
    public_send(@tag, **trigger_attrs, &block)
  end
end