Class: UI::HoverCardTriggerComponent

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

Overview

TriggerComponent - ViewComponent 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::TriggerComponent.new { "Hover me" } %>

With asChild - compose with Button (yields attributes)

<%= render UI::TriggerComponent.new(as_child: true) do |trigger_attrs| %>
  <%= render UI::ButtonComponent.new(**trigger_attrs, variant: :link) { "@nextjs" } %>
<% end %>

With custom tag

<%= render UI::TriggerComponent.new(tag: :a, href: "#") { "Link" } %>

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) ⇒ HoverCardTriggerComponent

Returns a new instance of HoverCardTriggerComponent.

Parameters:

  • as_child (Boolean) (defaults to: false)

    If true, yields attributes to block instead of wrapping

  • 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/view_components/ui/hover_card_trigger_component.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

#callObject



46
47
48
49
50
51
52
53
# File 'app/view_components/ui/hover_card_trigger_component.rb', line 46

def call
  # Default: render with full styling and tabindex for keyboard focus
  attrs = trigger_html_attributes.merge(@attributes)
  attrs[:tabindex] ||= "0" if @tag == :span || @tag == :div
   @tag, **attrs do
    content
  end
end

#render_in(view_context, &block) ⇒ Object

Override render_in to pass trigger_attrs to block when as_child is true



34
35
36
37
38
39
40
41
42
43
44
# File 'app/view_components/ui/hover_card_trigger_component.rb', line 34

def render_in(view_context, &block)
  @view_context = view_context

  if @as_child && block
    # asChild pattern: call the block with trigger attributes
    # Block should use these attrs on the actual trigger element
    view_context.capture(trigger_html_attributes, &block)
  else
    super
  end
end