Class: UI::NavigationMenuLink

Inherits:
Phlex::HTML
  • Object
show all
Includes:
NavigationMenuLinkBehavior, SharedAsChildBehavior
Defined in:
app/components/ui/navigation_menu_link.rb

Overview

Link - Phlex implementation

Navigation link component. Supports asChild pattern for composition with link_to.

Examples:

Basic usage

render UI::Link.new(href: "/docs") { "Documentation" }

With asChild for Rails link_to

render UI::Link.new(as_child: true) do |link_attrs|
  link_to "Documentation", docs_path, **link_attrs
end

Active link

render UI::Link.new(href: "/docs", active: true) { "Documentation" }

As trigger style (for direct links without dropdown)

render UI::Link.new(as_child: true, trigger_style: true) do |link_attrs|
  link_to "About", about_path, **link_attrs
end

Instance Method Summary collapse

Methods included from SharedAsChildBehavior

#merge_attributes

Methods included from NavigationMenuLinkBehavior

#navigation_menu_link_classes, #navigation_menu_link_data_attributes, #navigation_menu_link_html_attributes, #navigation_menu_link_trigger_style_classes

Constructor Details

#initialize(href: nil, active: false, as_child: false, trigger_style: false, classes: "", **attributes) ⇒ NavigationMenuLink

Returns a new instance of NavigationMenuLink.

Parameters:

  • href (String) (defaults to: nil)

    URL for the link (ignored when as_child: true)

  • active (Boolean) (defaults to: false)

    Whether this link is currently active

  • as_child (Boolean) (defaults to: false)

    When true, yields attributes to block instead of rendering anchor

  • trigger_style (Boolean) (defaults to: false)

    When true, uses trigger styling (for direct links)

  • classes (String) (defaults to: "")

    Additional CSS classes to merge

  • attributes (Hash)

    Additional HTML attributes



32
33
34
35
36
37
38
39
# File 'app/components/ui/navigation_menu_link.rb', line 32

def initialize(href: nil, active: false, as_child: false, trigger_style: false, classes: "", **attributes)
  @href = href
  @active = active
  @as_child = as_child
  @trigger_style = trigger_style
  @classes = classes
  @attributes = attributes
end

Instance Method Details

#view_template(&block) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/components/ui/navigation_menu_link.rb', line 41

def view_template(&block)
  link_attrs = build_link_attributes

  if @as_child
    # Yield attributes to block - child must accept them
    yield(link_attrs) if block_given?
  else
    # Default: render as anchor
    a(**link_attrs) do
      yield if block_given?
    end
  end
end