Class: UI::ButtonGroupText

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

Overview

Text - Phlex implementation

Displays text within a button group. Uses TextBehavior module for shared styling logic. Supports asChild pattern for rendering custom components.

Based on shadcn/ui ButtonGroup: ui.shadcn.com/docs/components/button-group

Examples:

Basic text

render UI::ButtonGroup.new do
  render UI::Text.new { "Label" }
  render UI::Button.new(variant: :outline) { "Button" }
end

With asChild (render as label)

render UI::ButtonGroup.new do
  render UI::Text.new(as_child: true) do |attrs|
    label(**attrs, for: "name") { "Name" }
  end
  input(type: "text", id: "name")
end

Instance Method Summary collapse

Methods included from ButtonGroupTextBehavior

#text_classes, #text_html_attributes

Constructor Details

#initialize(as_child: false, classes: "", **attributes) ⇒ ButtonGroupText

Returns a new instance of ButtonGroupText.

Parameters:

  • as_child (Boolean) (defaults to: false)

    Whether to yield attributes to a child component

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

    Additional CSS classes to merge

  • attributes (Hash)

    Additional HTML attributes



30
31
32
33
34
# File 'app/components/ui/button_group_text.rb', line 30

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

Instance Method Details

#view_template(&block) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'app/components/ui/button_group_text.rb', line 36

def view_template(&block)
  if @as_child
    # Yield attributes to the block for custom rendering
    yield(text_html_attributes.deep_merge(@attributes))
  else
    # Render as a div
    div(**text_html_attributes.deep_merge(@attributes), &block)
  end
end