Skip to content

Commit

Permalink
add breadcrumb component
Browse files Browse the repository at this point in the history
  • Loading branch information
woylie committed Dec 13, 2023
1 parent 0b955d3 commit 4781111
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
65 changes: 65 additions & 0 deletions lib/doggo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,71 @@ defmodule Doggo do
"""
end

@doc """
Renders a breadcrumb navigation.
## Example
<.breadcrumb>
<:item patch="/categories">Categories</:item>
<:item patch="/categories/1">Reviews</:item>
<:item patch="/categories/1/articles/1">The Movie</:item>
</.breadcrumb>
"""
attr :aria_label, :string, default: "breadcrumb"

attr :class, :any,
default: [],
doc: "Additional CSS classes. Can be a string or a list of strings."

attr :rest, :global, doc: "Any additional HTML attributes."

slot :item, required: true do
attr :navigate, :string
attr :patch, :string
attr :href, :string
end

def breadcrumb(%{item: items} = assigns) do
[last_item | rest] = Enum.reverse(items)

assigns =
assign(assigns, :item, Enum.reverse([{:current, last_item} | rest]))

~H"""
<nav aria-label="Breadcrumb" class={["breadcrumb" | List.wrap(@class)]} {@rest}>
<ul>
<li :for={item <- @item}>
<.breadcrumb_link item={item} />
</li>
</ul>
</nav>
"""
end

defp breadcrumb_link(%{item: {:current, current_item}} = assigns) do
assigns = assign(assigns, :item, current_item)

~H"""
<.link
navigate={@item[:navigate]}
patch={@item[:patch]}
href={@item[:href]}
aria-current="page"
>
<%= render_slot(@item) %>
</.link>
"""
end

defp breadcrumb_link(assigns) do
~H"""
<.link navigate={@item[:navigate]} patch={@item[:patch]} href={@item[:href]}>
<%= render_slot(@item) %>
</.link>
"""
end

@doc """
Renders a button.
Expand Down
18 changes: 18 additions & 0 deletions priv/storybook/components/breadcrumb.story.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Storybook.Components.Breadcrumb do
use PhoenixStorybook.Story, :component

def function, do: &Doggo.breadcrumb/1

def variations do
[
%Variation{
id: :default,
slots: [
~s(<:item patch="/categories">Categories</:item>),
~s(<:item patch="/categories/1">Reviews</:item>),
~s(<:item patch="/categories/1/articles/1">The Movie</:item>)
]
}
]
end
end

0 comments on commit 4781111

Please sign in to comment.