NavBuddy (NavBuddy v0.4.0)
View SourceNavBuddy - Advanced Configurable Navigation Component for Phoenix LiveView
NavBuddy provides comprehensive navigation components for Phoenix LiveView applications, featuring dropdowns, mega menus, mobile-responsive design, and accessibility support.
Basic Usage
defmodule MyAppWeb.LayoutView do
import NavBuddy.Component
def navigation_items do
[
%{id: "home", label: "Home", navigate: "/"},
%{id: "about", label: "About", navigate: "/about"},
%{
id: "products",
label: "Products",
navigate: "/products",
dropdown: [
%{id: "web", label: "Web Apps", navigate: "/products/web"},
%{id: "mobile", label: "Mobile Apps", navigate: "/products/mobile"}
]
}
]
end
endIn your LiveView template:
<.nav_menu items={navigation_items()} config={NavBuddy.default_config()} />Configuration
NavBuddy supports extensive configuration options:
config = %NavBuddy.Config{
orientation: :horizontal, # :horizontal | :vertical
theme: :auto, # :light | :dark | :auto
dropdown_trigger: :hover, # :hover | :click
mobile_behavior: :drawer, # :drawer | :collapse | :overlay
animations: true, # boolean
accessibility: true # boolean
}Navigation Structure
Navigation items support multiple levels of nesting and different types.
All navigation uses the navigate field which triggers LiveView's push_navigate
to maintain the LiveView connection without page reloads.
Simple Links
%{id: "home", label: "Home", navigate: "/"}Dropdown Menus
%{
id: "products",
label: "Products",
navigate: "/products",
dropdown: [
%{id: "web", label: "Web Development", navigate: "/products/web"},
%{id: "mobile", label: "Mobile Apps", navigate: "/products/mobile"}
]
}Multi-level Dropdowns
%{
id: "services",
label: "Services",
dropdown: [
%{
id: "development",
label: "Development",
dropdown: [
%{id: "frontend", label: "Frontend", navigate: "/services/frontend"},
%{id: "backend", label: "Backend", navigate: "/services/backend"}
]
}
]
}Mega Menus
%{
id: "solutions",
label: "Solutions",
mega_menu: %{
title: "Our Solutions",
description: "Comprehensive business solutions",
sections: [
%{
title: "For Small Business",
items: [
%{id: "starter", label: "Starter Package", navigate: "/solutions/starter"},
%{id: "growth", label: "Growth Package", navigate: "/solutions/growth"}
]
},
%{
title: "For Enterprise",
items: [
%{id: "enterprise", label: "Enterprise Suite", navigate: "/solutions/enterprise"},
%{id: "custom", label: "Custom Solutions", navigate: "/solutions/custom"}
]
}
]
}
}Permissions-based Navigation
%{
id: "admin",
label: "Admin Panel",
navigate: "/admin",
permissions: "admin" # Only visible to users with "admin" permission
}
%{
id: "reports",
label: "Reports",
navigate: "/reports",
permissions: ["manager", "admin"] # Visible to users with either permission
}
%{
id: "profile",
label: "Profile",
navigate: "/profile" # No permissions = visible to everyone
}Accessibility Features
NavBuddy includes comprehensive accessibility support:
- ARIA attributes for screen readers
- Keyboard navigation (arrow keys, enter, escape, tab)
- Focus management
- High contrast theme support
- Reduced motion support
Mobile Optimization
Multiple mobile behaviors are supported:
- Drawer: Slide-out navigation drawer
- Collapse: Collapsible menu sections
- Overlay: Full-screen navigation overlay
Theming
NavBuddy supports automatic theme detection and manual theme selection:
- Light theme
- Dark theme
- Auto (follows system preference)
- Custom themes via CSS custom properties
Summary
Functions
Returns the default configuration for NavBuddy.
Creates a navigation item with dropdown.
Example navigation items showcasing all features.
Filters navigation items based on user permissions.
Finds the active navigation item based on the current path.
Generates breadcrumb navigation from the active path.
Generates a unique menu ID for accessibility.
Creates a navigation item with simple mega menu.
Creates a simple navigation item.
Validates a navigation item structure.
Validates a list of navigation items.
Types
Functions
Returns the default configuration for NavBuddy.
Examples
iex> config = NavBuddy.default_config()
iex> config.orientation
:horizontal
iex> config = NavBuddy.default_config()
iex> config.theme
:auto
Creates a navigation item with dropdown.
Examples
iex> dropdown_items = [
...> %{id: "web", label: "Web Apps", navigate: "/products/web"},
...> %{id: "mobile", label: "Mobile Apps", navigate: "/products/mobile"}
...> ]
iex> NavBuddy.dropdown_item("products", "Products", dropdown_items)
%{id: "products", label: "Products", dropdown: [%{id: "web", label: "Web Apps", navigate: "/products/web"}, %{id: "mobile", label: "Mobile Apps", navigate: "/products/mobile"}]}
Filters navigation items based on user permissions.
Removes items that the user doesn't have permission to see. Items without permissions are always visible. When an item has a list of permissions, the user must have ALL of them to see the item.
Examples
iex> items = [
...> %{id: "home", label: "Home", navigate: "/"},
...> %{id: "admin", label: "Admin", navigate: "/admin", permissions: "admin"},
...> %{id: "user", label: "Profile", navigate: "/profile", permissions: "user"}
...> ]
iex> NavBuddy.filter_by_permissions(items, ["user"])
[
%{id: "home", label: "Home", navigate: "/"},
%{id: "user", label: "Profile", navigate: "/profile", permissions: "user"}
]
Finds the active navigation item based on the current path.
Examples
iex> items = [%{id: "home", navigate: "/"}, %{id: "about", navigate: "/about"}]
iex> NavBuddy.find_active_item(items, "/about")
[%{id: "home", navigate: "/", active: false}, %{id: "about", navigate: "/about", active: true}]
Generates breadcrumb navigation from the active path.
Examples
iex> items = [%{id: "home", label: "Home", navigate: "/"}]
iex> NavBuddy.generate_breadcrumbs(items, "/")
[%{id: "home", label: "Home", navigate: "/"}]