Class: Daisy::DataDisplay::FigureComponent

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

Overview

Note:

before_render sets the component's tag to <figure>, but LocoMotion::Concerns::LinkableComponent's setup runs afterward via super and overrides it to <a> whenever href is present. Since Figure is commonly embedded in Card's top_figure / bottom_figure slots, passing href: there changes the wrapping element from a <figure> to an <a>.

The FigureComponent is used to display images with optional captions. It is commonly used within cards and other content containers.

Constant Summary

Constants inherited from LocoMotion::BaseComponent

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

Instance Attribute Summary

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(**kws, &block) ⇒ FigureComponent

Creates a new figure component.

Parameters:

  • kws (Hash)

    The keyword arguments for the component.

Options Hash (**kws):

  • src (String)

    URL of the image to display in the figure.

  • position (Symbol)

    Position of the image relative to content. Must be :top (default) or :bottom.

  • alt (String)

    The alt text for the image, used by screen readers and shown when the image fails to load. Omitted when not provided.

  • href (String)

    A path or URL to which the user will be directed when the figure is clicked. Forces the Figure to use an <a> tag (see the note above about Card's figure slots).

  • target (String)

    The HTML target attribute for the <a> tag (_blank, _parent, or a specific tab / window / iframe, etc).

  • title (String)

    The HTML title attribute for the <a> tag, shown as a native tooltip on hover. Only applied when href is also provided.

  • turbo_frame (String)

    The Turbo Frame to target, rendered as data-turbo-frame.

  • turbo_action (String, Symbol)

    How Turbo Drive updates the browser history for the visit, rendered as data-turbo-action (e.g. :advance or :replace).

  • turbo_method (String, Symbol)

    The HTTP method Turbo should use for the request, rendered as data-turbo-method (e.g. :delete).

  • turbo_confirm (String)

    A confirmation prompt Turbo shows before submitting, rendered as data-turbo-confirm.

  • action (String)

    A Stimulus action wired to the figure via its data-action attribute. Stimulus infers the click event, so action: "my-controller#handle" works as a shorthand for action: "click->my-controller#handle".

  • css (String)

    Additional CSS classes for styling.



83
84
85
86
87
88
89
90
91
# File 'app/components/daisy/data_display/figure_component.rb', line 83

def initialize(**kws, &block)
  super

  @src = kws[:src]
  @alt = kws[:alt]
  @position = kws[:position] || :top

  validate_position!
end

Instance Method Details

#before_renderObject



93
94
95
96
97
98
# File 'app/components/daisy/data_display/figure_component.rb', line 93

def before_render
  set_tag_name(:component, :figure)
  add_html(:image, { src: @src, alt: @alt }) if @src

  super
end

#callObject



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'app/components/daisy/data_display/figure_component.rb', line 100

def call
  part(:component) do
    if @position == :bottom
      # Show content first, then image
      concat(content)
      concat(part(:image)) if @src
    else
      # Default: show image first, then content
      concat(part(:image)) if @src
      concat(content)
    end
  end
end