Class: Daisy::Navigation::LinkComponent

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

Overview

Creates a styled link component that can be used for navigation or inline text links. This component is designed to work similarly to Rails’ ‘link_to` helper.

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

Constructor Details

#initialize(*args, **kws) ⇒ LinkComponent

Create a new instance of the LinkComponent.

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”).

  • 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.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/components/daisy/navigation/link_component.rb', line 50

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.



74
75
76
77
78
# File 'app/components/daisy/navigation/link_component.rb', line 74

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.



87
88
89
# File 'app/components/daisy/navigation/link_component.rb', line 87

def call
  part(:component) { content || @title }
end