Class: Primrose::Components::Navbar

Inherits:
Rose
  • Object
show all
Defined in:
lib/primrose/components/navbar.rb

Constant Summary collapse

TEMPLATE =
<<~ERB
  <nav class="<%= 'sticky' if @sticky %>">
    <%= @brand if @brand %>
    <ul>
      <% @links.each do |link| %>
        <li class="<%= 'active' if link[:url] == @active_link %>">
          <a href="<%= link[:url] %>"><%= link[:text] %></a>
          <% if link[:dropdown] %>
            <ul class="dropdown">
              <% link[:dropdown].each do |dropdown_link| %>
                <li><a href="<%= dropdown_link[:url] %>"><%= dropdown_link[:text] %></a></li>
              <% end %>
            </ul>
          <% end %>
        </li>
      <% end %>
    </ul>
  </nav>
ERB

Instance Attribute Summary

Attributes inherited from Rose

#children, #event_handlers, #state

Instance Method Summary collapse

Methods inherited from Rose

#add_child, #get_binding, #handle_error, #lifecycle, #on, #render_children, #trigger

Constructor Details

#initialize(*args, links:, active_link: nil, sticky: false, brand: nil) ⇒ Navbar

Returns a new instance of Navbar.



26
27
28
29
30
31
32
# File 'lib/primrose/components/navbar.rb', line 26

def initialize(*args, links:, active_link: nil, sticky: false, brand: nil)
  @links = links
  @active_link = active_link
  @sticky = sticky
  @brand = brand
  super()
end

Instance Method Details

#renderObject



34
35
36
# File 'lib/primrose/components/navbar.rb', line 34

def render
  Prim.render(TEMPLATE, self)
end


38
39
40
# File 'lib/primrose/components/navbar.rb', line 38

def set_active_link(new_active_link)
  @state.alter({ active_link: new_active_link })
end