Class: Daisy::DataDisplay::CollapseComponent

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

Overview

Note:

In details mode the toggle is the <summary> element itself, so the component keeps native disclosure semantics: it works with no JavaScript, stays keyboard-accessible, exposes the open property / attribute for programmatic control (e.g. details.open = true while filtering a list), and interactive elements near the title remain clickable — checkbox mode overlays an invisible input across the title area, which can swallow those clicks.

The Collapse component creates an expandable/collapsible section of content with a title that toggles visibility. It's similar to the Accordion component but designed for standalone use rather than groups.

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

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

Methods included from LocoMotion::Concerns::InspectableComponent

#build_inspect_string

Constructor Details

#initialize(*args, **kws, &block) ⇒ CollapseComponent

Creates a new collapse component.

Parameters:

  • kws (Hash) —

    The keyword arguments for the component.

Options Hash (**kws):

  • title (String) —

    The text to display in the title bar. You can also provide custom title content using the title slot.

  • checkbox (Boolean) —

    Whether to use a checkbox for toggle state (true) or focus/tabindex mode (false). Defaults to true.

  • details (Boolean) —

    Whether to render the DaisyUI details/summary flavor: the component becomes a <details> element, the title renders as its <summary>, and no hidden checkbox or tabindex is emitted (the checkbox option is ignored). Defaults to false.

  • open (Boolean) —

    Whether a details-mode collapse is initially expanded (renders the native open attribute). Only meaningful when details is true. Defaults to false.

  • tip (String) —

    The tooltip text to display when hovering over the component.



106
107
108
109
110
111
112
113
# File 'app/components/daisy/data_display/collapse_component.rb', line 106

def initialize(*args, **kws, &block)
  super

  @simple_title = config_option(:title)
  @checkbox = config_option(:checkbox, true)
  @details = config_option(:details, false)
  @open = config_option(:open, false)
end

Instance Attribute Details

#checkbox ⇒ Boolean (readonly)

Returns Whether to use a checkbox for toggle state (true) or focus/tabindex mode (false).

Returns:

  • (Boolean) —

    Whether to use a checkbox for toggle state (true) or focus/tabindex mode (false).



73
74
75
# File 'app/components/daisy/data_display/collapse_component.rb', line 73

def checkbox
  @checkbox
end

#details ⇒ Boolean (readonly)

Returns Whether to render native <details> / <summary> elements instead of the checkbox / focus mechanisms.

Returns:

  • (Boolean) —

    Whether to render native <details> / <summary> elements instead of the checkbox / focus mechanisms.



77
78
79
# File 'app/components/daisy/data_display/collapse_component.rb', line 77

def details
  @details
end

#open ⇒ Boolean (readonly)

Returns Whether a details-mode collapse starts expanded.

Returns:

  • (Boolean) —

    Whether a details-mode collapse starts expanded.



80
81
82
# File 'app/components/daisy/data_display/collapse_component.rb', line 80

def open
  @open
end

#simple_title ⇒ String (readonly)

Returns The title text when using the simple title option.

Returns:

  • (String) —

    The title text when using the simple title option.



69
70
71
# File 'app/components/daisy/data_display/collapse_component.rb', line 69

def simple_title
  @simple_title
end

Instance Method Details

#before_render ⇒ Object



115
116
117
118
119
120
# File 'app/components/daisy/data_display/collapse_component.rb', line 115

def before_render
  setup_component # Set base styles/attributes
  super           # Run concern setup hooks
  setup_title     # Set title part styles
  setup_wrapper   # Set wrapper part styles
end

#has_title? ⇒ Boolean

Returns:

  • (Boolean)


148
149
150
# File 'app/components/daisy/data_display/collapse_component.rb', line 148

def has_title?
  title? || @simple_title.present?
end

#render_checkbox? ⇒ Boolean

Details mode toggles via its summary element, so the hidden checkbox input is only rendered for checkbox mode.

Returns:

  • (Boolean)


140
141
142
# File 'app/components/daisy/data_display/collapse_component.rb', line 140

def render_checkbox?
  @checkbox && !@details
end

#setup_component ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'app/components/daisy/data_display/collapse_component.rb', line 122

def setup_component
  add_css(:component, "collapse")

  if @details
    set_tag_name(:component, :details)
    add_html(:component, { open: true }) if @open
  else
    add_html(:component, { tabindex: 0 }) unless @checkbox
  end
end

#setup_title ⇒ Object



133
134
135
136
# File 'app/components/daisy/data_display/collapse_component.rb', line 133

def setup_title
  add_css(:title, "collapse-title")
  set_tag_name(:title, :summary) if @details
end

#setup_wrapper ⇒ Object



144
145
146
# File 'app/components/daisy/data_display/collapse_component.rb', line 144

def setup_wrapper
  add_css(:wrapper, "collapse-content")
end