Class: UI::ToggleComponent

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

Overview

ToggleComponent - ViewComponent implementation

A two-state button that can be either on or off. Uses ToggleBehavior module for shared styling logic.

Examples:

Basic usage

<%= render UI::ToggleComponent.new do %>
  Toggle
<% end %>

With variant and size

<%= render UI::ToggleComponent.new(variant: "outline", size: "lg", pressed: true) do %>
  Bookmarked
<% end %>

Disabled state

<%= render UI::ToggleComponent.new(disabled: true) do %>
  Disabled
<% end %>

Instance Method Summary collapse

Methods included from ToggleBehavior

#render_toggle, #toggle_classes, #toggle_html_attributes

Constructor Details

#initialize(variant: "default", size: "default", type: "button", pressed: false, disabled: false, classes: "", **attributes) ⇒ ToggleComponent

Returns a new instance of ToggleComponent.

Parameters:

  • variant (String) (defaults to: "default")

    Visual style variant (default, outline)

  • size (String) (defaults to: "default")

    Size variant (default, sm, lg)

  • type (String) (defaults to: "button")

    Button type attribute (button, submit, reset)

  • pressed (Boolean) (defaults to: false)

    Whether the toggle is pressed/active

  • disabled (Boolean) (defaults to: false)

    Whether the toggle is disabled

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

    Additional CSS classes to merge

  • attributes (Hash)

    Additional HTML attributes



32
33
34
35
36
37
38
39
40
# File 'app/view_components/ui/toggle_component.rb', line 32

def initialize(variant: "default", size: "default", type: "button", pressed: false, disabled: false, classes: "", **attributes)
  @variant = variant
  @size = size
  @type = type
  @pressed = pressed
  @disabled = disabled
  @classes = classes
  @attributes = attributes
end

Instance Method Details

#callObject



42
43
44
45
46
47
# File 'app/view_components/ui/toggle_component.rb', line 42

def call
  attrs = toggle_html_attributes
  attrs[:data] = attrs[:data].merge(@attributes.fetch(:data, {}))

   :button, content, **attrs.merge(@attributes.except(:data))
end