Class: Shadcn::ToggleComponent

Inherits:
BaseComponent
  • Object
show all
Defined in:
app/components/shadcn/toggle_component.rb

Overview

Toggle component for a two-state button Matches shadcn/ui Toggle component

Examples:

Basic usage

<%= render Shadcn::ToggleComponent.new(aria_label: "Toggle bold") do %>
  <svg>...</svg>
<% end %>

Outline variant

<%= render Shadcn::ToggleComponent.new(variant: :outline, pressed: true) do %>
  Italic
<% end %>

Constant Summary collapse

VARIANTS =
{
  default: "bg-transparent",
  outline: "border border-input bg-transparent shadow-sm hover:bg-accent hover:text-accent-foreground"
}.freeze
SIZES =
{
  sm: "h-8 px-2",
  default: "h-9 px-3",
  lg: "h-10 px-3"
}.freeze
BASE_CLASSES =
"inline-flex items-center justify-center rounded-md text-sm font-medium transition-colors hover:bg-muted hover:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 data-[state=on]:bg-accent data-[state=on]:text-accent-foreground"

Instance Attribute Summary

Attributes inherited from BaseComponent

#class_name, #data, #html_options

Instance Method Summary collapse

Methods inherited from BaseComponent

#content

Methods included from Rails::Helpers::ClassNameHelper

#cn

Constructor Details

#initialize(variant: :default, size: :default, pressed: false, disabled: false, aria_label: nil, **options) ⇒ ToggleComponent

Returns a new instance of ToggleComponent.

Parameters:

  • variant (Symbol) (defaults to: :default)

    :default or :outline

  • size (Symbol) (defaults to: :default)

    :sm, :default, or :lg

  • pressed (Boolean) (defaults to: false)

    Initial pressed state

  • disabled (Boolean) (defaults to: false)

    Whether toggle is disabled

  • aria_label (String) (defaults to: nil)

    Accessibility label



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/components/shadcn/toggle_component.rb', line 36

def initialize(
  variant: :default,
  size: :default,
  pressed: false,
  disabled: false,
  aria_label: nil,
  **options
)
  super(**options)
  @variant = variant
  @size = size
  @pressed = pressed
  @disabled = disabled
  @aria_label = aria_label
end