Class: Daisy::Actions::SwapComponent

Inherits:
LocoMotion::BaseComponent show all
Includes:
LocoMotion::Concerns::TippableComponent
Defined in:
app/components/daisy/actions/swap_component.rb

Overview

The Swap component allows toggling between two states, “on” and “off”, with an optional indeterminate state. It provides a flexible way to create animated toggles, switches, or any other element that needs to alternate between different visual states. The component supports both simple text/emoji swaps and complex HTML content swaps.

It also includes built-in animations that can be enabled through CSS classes like ‘swap-rotate` or `swap-flip`.

Includes the LocoMotion::Concerns::TippableComponent module to enable easy tooltip addition.

Defined Under Namespace

Classes: SwapIndeterminate, SwapOff, SwapOn

Constant Summary

Constants inherited from LocoMotion::BaseComponent

LocoMotion::BaseComponent::EMPTY_PART_IGNORED_TAGS, LocoMotion::BaseComponent::SELF_CLOSING_TAGS

Instance Attribute Summary collapse

Attributes inherited from LocoMotion::BaseComponent

#config, #loco_parent

Instance Method Summary collapse

Methods inherited from LocoMotion::BaseComponent

build, #component_ref, #config_option, #cssify, define_modifier, define_modifiers, define_part, define_parts, define_size, define_sizes, #empty_part_content, #inspect, #part, register_component_initializer, register_component_setup, #rendered_css, #rendered_data, #rendered_html, #rendered_stimulus_controllers, #rendered_tag_name, renders_many, renders_one, set_component_name, #set_loco_parent, #strip_spaces

Constructor Details

#initialize(on = nil, off = nil, checked = nil, **kws, &block) ⇒ SwapComponent

Creates a new instance of the SwapComponent.

Parameters:

  • on (String) (defaults to: nil)

    Simple text/emoji to display in the “on” state. If provided along with ‘off`, enables simple text swap mode.

  • off (String) (defaults to: nil)

    Simple text/emoji to display in the “off” state. If provided along with ‘on`, enables simple text swap mode.

  • checked (Boolean) (defaults to: nil)

    Whether the swap should start in the checked (“on”) state. Defaults to false.

  • kws (Hash)

    The keyword arguments for the component.

Options Hash (**kws):

  • on (String)

    Simple text/emoji for the “on” state. Alternative to providing it as the first argument.

  • off (String)

    Simple text/emoji for the “off” state. Alternative to providing it as the second argument.

  • checked (Boolean)

    Whether the swap should start checked. Alternative to providing it as the third argument.

  • indeterminate (Boolean)

    If true, starts the swap in an indeterminate state. Requires the indeterminate slot to be meaningful.

  • tip (String)

    The tooltip text to display when hovering over the component.



127
128
129
130
131
132
133
# File 'app/components/daisy/actions/swap_component.rb', line 127

def initialize(on = nil, off = nil, checked = nil, **kws, &block)
  super

  @checked = config_option(:checked, checked || false)
  @simple_on = config_option(:on, on)
  @simple_off = config_option(:off, off)
end

Instance Attribute Details

#simple_offString (readonly)

Returns The value of the ‘off` option. Usually text or emoji.

Returns:

  • (String)

    The value of the ‘off` option. Usually text or emoji.



96
97
98
# File 'app/components/daisy/actions/swap_component.rb', line 96

def simple_off
  @simple_off
end

#simple_onString (readonly)

Returns The value of the ‘on` option. Usually text or emoji.

Returns:

  • (String)

    The value of the ‘on` option. Usually text or emoji.



93
94
95
# File 'app/components/daisy/actions/swap_component.rb', line 93

def simple_on
  @simple_on
end

Instance Method Details

#before_renderObject

Sets up the component with various CSS classes and HTML attributes.



138
139
140
141
142
143
# File 'app/components/daisy/actions/swap_component.rb', line 138

def before_render
  setup_component # Set tag, base CSS
  setup_checkbox  # Setup the hidden checkbox part
  setup_on_off    # Setup the on/off part CSS
  super           # Run TippableComponent hook
end