Class: DaisyUI::Swap

Inherits:
BaseComponent show all
Defined in:
app/components/daisy_ui/actions/swap.rb

Overview

Swap component implementing DaisyUI’s swap styles

Examples:

Basic usage

<%= render(SwapComponent.new(
  states: { on: 'ON', off: 'OFF' }
)) %>

Theme toggle with icons

<%= render(SwapComponent.new(
  states: {
    on: helpers.sun_icon('h-6 w-6'),
    off: helpers.moon_icon('h-6 w-6')
  },
  button: true,
  effect: :rotate
)) %>

Constant Summary collapse

VARIANTS =

Available variants from DaisyUI

{
  primary: 'text-primary',
  secondary: 'text-secondary',
  accent: 'text-accent',
  info: 'text-info',
  success: 'text-success',
  warning: 'text-warning',
  error: 'text-error',
  ghost: 'text-base-content',
  neutral: 'text-neutral'
}.freeze
SIZES =

Available sizes

{
  xs: 'text-xs',
  sm: 'text-sm',
  md: 'text-base',
  lg: 'text-lg'
}.freeze
EFFECTS =

Available effects

{
  rotate: 'swap-rotate',
  flip: 'swap-flip',
  flip_active: 'swap-flip-active'
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(states:, value: false, variant: nil, size: nil, effect: nil, active: false, button: false, **system_arguments) ⇒ Swap



57
58
59
60
61
62
63
64
65
66
67
# File 'app/components/daisy_ui/actions/swap.rb', line 57

def initialize(states:, value: false, variant: nil, size: nil, effect: nil, active: false, button: false,
               **system_arguments)
  @states = validate_states!(states)
  @value = ActiveModel::Type::Boolean.new.cast(value)
  @variant = build_argument(variant, VARIANTS, 'variant')
  @size = build_argument(size, SIZES, 'size')
  @effect = build_argument(effect, EFFECTS, 'effect')
  @active = active
  @button = button
  super(**system_arguments)
end

Instance Method Details

#callObject



69
70
71
72
73
74
75
76
77
# File 'app/components/daisy_ui/actions/swap.rb', line 69

def call
  tag.label(**label_html_attributes) do
    safe_join([
      tag.input(**input_html_attributes),
      render_state(:on),
      render_state(:off)
    ].compact)
  end
end