Class: Flowbite::Sidebar::Item

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/flowbite/sidebar/item.rb

Overview

Renders a sidebar navigation item.

Each item renders as a list item containing a link. Optionally, an icon can be provided using the icon slot, which will be displayed before the label text.

Examples:

Basic item

<%= render Flowbite::Sidebar::Item.new(href: "/dashboard") { "Dashboard" } %>

Item with icon

<%= render(Flowbite::Sidebar::Item.new(href: "/dashboard")) do |item| %>
  <% item.with_icon do %>
    <svg class="w-5 h-5" ...>...</svg>
  <% end %>
  Dashboard
<% end %>

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(href:, class: nil, **options) ⇒ Item

Returns a new instance of Item.

Parameters:

  • class (Array<String>) (defaults to: nil)

    Additional CSS classes for the link element.

  • href (String)

    The URL for the navigation link.

  • options (Hash)

    Additional HTML attributes for the link element.



40
41
42
43
44
45
# File 'app/components/flowbite/sidebar/item.rb', line 40

def initialize(href:, class: nil, **options)
  super()
  @class = Array.wrap(binding.local_variable_get(:class))
  @href = href
  @options = options
end

Instance Attribute Details

#hrefObject (readonly)

Returns the value of attribute href.



26
27
28
# File 'app/components/flowbite/sidebar/item.rb', line 26

def href
  @href
end

#optionsObject (readonly)

Returns the value of attribute options.



26
27
28
# File 'app/components/flowbite/sidebar/item.rb', line 26

def options
  @options
end

Class Method Details

.classesObject



29
30
31
32
33
34
# File 'app/components/flowbite/sidebar/item.rb', line 29

def classes
  [
    "flex", "items-center", "px-2", "py-1.5", "text-body",
    "rounded-base", "hover:bg-neutral-tertiary", "hover:text-fg-brand", "group"
  ]
end

Instance Method Details

#callObject



47
48
49
50
51
52
53
54
55
# File 'app/components/flowbite/sidebar/item.rb', line 47

def call
  (:li) do
    link_options = {class: link_classes}.merge(options)
    (:a, href: href, **link_options) do
      concat(icon) if icon?
      concat((:span, content, class: label_classes))
    end
  end
end