Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add breadcrumb component #133

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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