Class: Daisy::Navigation::LinkComponent

Inherits:
LocoMotion::BaseComponent show all
Includes:
LocoMotion::Concerns::IconableComponent, LocoMotion::Concerns::LinkableComponent, LocoMotion::Concerns::TippableComponent
Defined in:
app/components/daisy/navigation/link_component.rb

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 included from LocoMotion::Concerns::IconableComponent

#has_icons?, #left_icon_html, #render_left_icon, #render_right_icon, #right_icon_html

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) ⇒ LinkComponent

Create a new instance of the LinkComponent.

Parameters:

  • args (Array)

    Looks for one or two positional arguments.

    • If passed two positional arguments, the first is considered the ‘title`

    and the second is considered the ‘href`.

    • If passed only one positional argument, it is treated as the ‘href`

    and we assume the ‘title` will be provided in the block.

    • If no text is passed in the block, we will use the ‘href` as the title.

  • kws (Hash)

    The keyword arguments for the component.

Options Hash (**kws):

  • title (String)

    The text to display in the link. Not required if providing block content.

  • href (String)

    The URL to visit when the link is clicked.

  • target (String)

    The target attribute for the anchor tag (e.g., “_blank”).

  • icon (String)

    The name of Hero icon to render inside the link. This is an alias of ‘left_icon`.

  • icon_css (String)

    The CSS classes to apply to the icon. This is an alias of ‘left_icon_css`.

  • icon_html (Hash)

    Additional HTML attributes to apply to the icon. This is an alias of ‘left_icon_html`.

  • left_icon (String)

    The name of Hero icon to render inside the link to the left of the text.

  • left_icon_css (String)

    The CSS classes to apply to the left icon.

  • left_icon_html (Hash)

    Additional HTML attributes to apply to the left icon.

  • right_icon (String)

    The name of Hero icon to render inside the link to the right of the text.

  • right_icon_css (String)

    The CSS classes to apply to the right icon.

  • right_icon_html (Hash)

    Additional HTML attributes to apply to the right icon.

  • css (String)

    Additional CSS classes for styling. Common options include:

    • Style: ‘link-primary`, `link-secondary`, `link-accent`

    • State: ‘link-hover`

    • Text: ‘text-sm`, `text-xl`, `text-2xl`

  • tip (String)

    The tooltip text to display when hovering over the component.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/components/daisy/navigation/link_component.rb', line 88

def initialize(*args, **kws)
  super

  if args.size == 1
    # If given one arg, assume it's the href and / or the title (if no block is given)
    @title = args[0]
    @href = args[0]
  elsif args.size == 2
    # If given two args, assume the first is the title and the second is the href
    @title = args[0]
    @href = args[1]
  else
    # Otherwise, assume they pass everything as keyword arguments
    @title = config_option(:title)
    @href = config_option(:href)
  end

  @target = config_option(:target)
end

Instance Method Details

#before_renderObject

Adds the relevant Daisy classes and applies the href and target attributes if provided.



112
113
114
115
116
# File 'app/components/daisy/navigation/link_component.rb', line 112

def before_render
  super

  setup_component
end

#callObject

Renders the link component.

Because this is an inline component which might be utlized alongside text, we utilize the ‘call` method instead of a template to ensure that no additional whitespace gets added to the output.



125
126
127
128
129
130
131
132
133
134
135
# File 'app/components/daisy/navigation/link_component.rb', line 125

def call
  if content?
    part(:component) { content }
  else
    part(:component) do
      concat(render_left_icon)
      concat(@title)
      concat(render_right_icon)
    end
  end
end