Class: UI::PopoverComponent

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

Overview

PopoverComponent - ViewComponent implementation

Container for popover trigger and content. Uses PopoverBehavior concern for shared styling logic.

Supports asChild pattern for composition without wrapper elements.

Examples:

Basic usage

<%= render UI::PopoverComponent.new do %>
  <%= render UI::TriggerComponent.new do %>
    <button>Click me</button>
  <% end %>
  <%= render UI::ContentComponent.new do %>
    Popover content
  <% end %>
<% end %>

With asChild - pass attributes to custom element

<%= render UI::PopoverComponent.new(as_child: true) do |popover| %>
  <%= render UI::InputGroupAddonComponent.new(**popover.popover_attrs) do %>
    <%= render UI::PopoverTriggerComponent.new(as_child: true) do |trigger| %>
      <%= render UI::InputGroupButtonComponent.new(**trigger.trigger_attrs) { "Info" } %>
    <% end %>
    <%= render UI::PopoverContentComponent.new { "Content" } %>
  <% end %>
<% end %>

Instance Method Summary collapse

Methods included from PopoverBehavior

#popover_classes, #popover_data_attributes, #popover_html_attributes

Constructor Details

#initialize(as_child: false, placement: "bottom", offset: 4, trigger: "click", hover_delay: 200, classes: "", align: nil, side_offset: nil, **attributes) ⇒ PopoverComponent

Returns a new instance of PopoverComponent.

Parameters:

  • as_child (Boolean) (defaults to: false)

    When true, yields self to block for attribute access

  • placement (String) (defaults to: "bottom")

    Placement of the popover (e.g., “bottom”, “top-start”)

  • offset (Integer) (defaults to: 4)

    Distance in pixels from the trigger

  • trigger (String) (defaults to: "click")

    Trigger type (“click” or “hover”)

  • hover_delay (Integer) (defaults to: 200)

    Delay in milliseconds for hover trigger

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

    Additional CSS classes to merge

  • attributes (Hash)

    Additional HTML attributes



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/view_components/ui/popover_component.rb', line 39

def initialize(
  as_child: false,
  placement: "bottom",
  offset: 4,
  trigger: "click",
  hover_delay: 200,
  classes: "",
  align: nil,
  side_offset: nil,
  **attributes
)
  @as_child = as_child
  @placement = placement
  @offset = side_offset || offset
  @trigger = trigger
  @hover_delay = hover_delay
  @classes = classes
  @align = align
  @attributes = attributes
end

Instance Method Details

#callObject



65
66
67
68
69
70
71
72
73
74
75
# File 'app/view_components/ui/popover_component.rb', line 65

def call
  if @as_child
    # asChild mode: yield self to block, child accesses popover_attrs
    content
  else
    # Default: render wrapper div with controller
     :div, **popover_html_attributes do
      content
    end
  end
end

#popover_attrsObject

Returns popover attributes for as_child mode



61
62
63
# File 'app/view_components/ui/popover_component.rb', line 61

def popover_attrs
  popover_html_attributes.deep_merge(@attributes)
end