Skip to content

Commit

Permalink
add navbar component
Browse files Browse the repository at this point in the history
  • Loading branch information
woylie committed Nov 20, 2023
1 parent a4eaf80 commit a7b7cc5
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions lib/doggo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,100 @@ defmodule Doggo do
|> JS.pop_focus()
end

@doc """
Renders a navigation bar.
## Usage
<Doggo.navbar>
<:brand><.link navigate={~p"/"}>Pet Clinic</.link></:brand>
<Doggo.navbar_items>
<:item><.link navigate={~p"/about"}>About</.link></:item>
<:item><.link navigate={~p"/services"}>Services</.link></:item>
<:item>
<.link navigate={~p"/login"} class="button">Log in</.link>
</:item>
</Doggo.navbar_items>
</Doggo.navbar>
You can place multiple navigation item lists in the inner block. If the
`.navbar` is styled as a flex box, you can use the CSS `order` property to
control the display order of the brand and lists.
<Doggo.navbar>
<:brand><.link navigate={~p"/"}>Pet Clinic</.link></:brand>
<Doggo.navbar_items classes={["navbar-main-links"]}>
<:item><.link navigate={~p"/about"}>About</.link></:item>
<:item><.link navigate={~p"/services"}>Services</.link></:item>
</Doggo.navbar_items>
<Doggo.navbar_items classes={["navbar-user-menu"]}>
<:item>
<.link navigate={~p"/login"} class="button">Log in</.link>
</:item>
</Doggo.navbar_items>
</Doggo.navbar>
If you have multiple `<nav>` elements on your page, it is recommended to set
the `aria-label` attribute.
<Doggo.navbar aria-label="main navigation">
<!-- ... -->
</Doggo.navbar>
"""

attr :classes, :list, default: [], doc: "Additional CSS classes."
attr :rest, :global, doc: "Any additional HTML attributes."

slot :brand, doc: "Slot for the brand name or logo."

slot :inner_block,
doc: """
Slot for navbar items. Use the `navbar_items` component here to render
navigation links or other controls.
"""

def navbar(assigns) do
~H"""
<nav class={["navbar" | @classes]} {@rest}>
<div :if={@brand != []} class="navbar-brand">
<%= render_slot(@brand) %>
</div>
<%= render_slot(@inner_block) %>
</nav>
"""
end

@doc """
Renders a list of navigation items.
Meant to be used in the inner block of the `navbar` component.
## Usage
<Doggo.navbar_items>
<:item><.link navigate={~p"/about"}>About</.link></:item>
<:item><.link navigate={~p"/services"}>Services</.link></:item>
<:item>
<.link navigate={~p"/login"} class="button">Log in</.link>
</:item>
</Doggo.navbar_items>
"""

attr :classes, :list, default: [], doc: "Additional CSS classes."
attr :rest, :global, doc: "Any additional HTML attributes."

slot :item,
required: true,
doc: "A navigation item, usually a link or a button."

def navbar_items(assigns) do
~H"""
<ul class={["navbar-items" | @classes]} {@rest}>
<li :for={item <- @item}><%= render_slot(item) %></li>
</ul>
"""
end

@doc """
Renders a list of properties, i.e. key/value pairs.
Expand Down

0 comments on commit a7b7cc5

Please sign in to comment.