|
| 1 | +# 🧬 Navbar Dioxus Usage |
| 2 | + |
| 3 | +Adding navbar to your project is simple: |
| 4 | + |
| 5 | +1. Make sure your project is set up with **Dioxus**. Refer to the [Dioxus Getting Started Guide](https://dioxuslabs.com/learn/0.6/getting_started) for setup instructions. |
| 6 | + |
| 7 | +1. Add the **navbar** library to your dependencies by including it in your `Cargo.toml` file: |
| 8 | + |
| 9 | + ```sh |
| 10 | + cargo add navbar --features=dio |
| 11 | + ``` |
| 12 | + |
| 13 | +1. Import the `Navbar` component into your Dioxus application. |
| 14 | + |
| 15 | +## 🛠️ Usage |
| 16 | + |
| 17 | +The following is a basic example showing how to use the `Navbar` component in your Dioxus app: |
| 18 | + |
| 19 | +```rust |
| 20 | +use dioxus::prelude::*; |
| 21 | +use navbar::dioxus::{Navbar, Menu, DropdownItem, MegaMenuItem}; |
| 22 | + |
| 23 | +#[component] |
| 24 | +fn App() -> Element { |
| 25 | + rsx! { |
| 26 | + Navbar { |
| 27 | + show_search: true, |
| 28 | + show_mega_menu: true, |
| 29 | + show_profile_menu: true, |
| 30 | + search_placeholder: "Search courses, docs...", |
| 31 | + mega_menu_items: vec![ |
| 32 | + MegaMenuItem { title: "Docs", description: "Official docs", link: "/docs" }, |
| 33 | + MegaMenuItem { title: "Tutorials", description: "Step-by-step guides", link: "/tutorials" }, |
| 34 | + MegaMenuItem { title: "API", description: "Full API Reference", link: "/api" }, |
| 35 | + ], |
| 36 | + dropdown_items: vec![ |
| 37 | + DropdownItem { id: 1, link: "/account", label: "Account", icon: None }, |
| 38 | + DropdownItem { id: 2, link: "/notifications", label: "Notifications", icon: None }, |
| 39 | + DropdownItem { id: 3, link: "/logout", label: "Logout", icon: None }, |
| 40 | + ], |
| 41 | + menus: vec![ |
| 42 | + Menu { id: 1, link: "/", name: "Home", icon_start: None, icon_end: None }, |
| 43 | + Menu { id: 2, link: "/explore", name: "Explore", icon_start: None, icon_end: None }, |
| 44 | + Menu { id: 3, link: "/pricing", name: "Pricing", icon_start: None, icon_end: None }, |
| 45 | + ], |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +## 🧩 Props |
| 52 | + |
| 53 | +### `Navbar` Component Props |
| 54 | + |
| 55 | +#### Main Props |
| 56 | + |
| 57 | +| Property | Type | Description | Default | |
| 58 | +| --------------------- | ------------------- | ---------------------------------------------- | ----------- | |
| 59 | +| `logo_src` | `&'static str` | Path to the logo image. | `""` | |
| 60 | +| `logo_alt` | `&'static str` | Alt text for the logo. | `"Logo"` | |
| 61 | +| `logo_link` | `&'static str` | Optional link for the logo. | `"/"` | |
| 62 | +| `menus` | `Vec<MenuItem>` | List of top-level menu items. | `[]` | |
| 63 | +| `show_search` | `bool` | Displays the search input if `true`. | `false` | |
| 64 | +| `search_state` | `Signal<String>` | Optional shared state for the search input. | `None` | |
| 65 | +| `search_placeholder` | `&'static str` | Placeholder for the search input. | `"Search"` | |
| 66 | +| `button_text` | `&'static str` | Text for the CTA button. | `""` | |
| 67 | +| `button_href` | `&'static str` | Link for the CTA button. | `"#"` | |
| 68 | +| `button_target` | `&'static str` | Target attribute for CTA link (e.g. `_blank`). | `"_self"` | |
| 69 | +| `show_mega_menu` | `bool` | Enables the mega menu when `true`. | `false` | |
| 70 | +| `mega_menu_items` | `Vec<MegaMenuItem>` | Items to show in the mega menu. | `[]` | |
| 71 | +| `show_profile_menu` | `bool` | Shows the profile dropdown menu. | `false` | |
| 72 | +| `dropdown_items` | `Vec<DropdownItem>` | Items for the profile dropdown. | `[]` | |
| 73 | +| `profile_image_url` | `&'static str` | URL for profile image. | `""` | |
| 74 | +| `profile_button_text` | `&'static str` | Text label for profile menu toggle. | `"Profile"` | |
| 75 | + |
| 76 | +#### Styling Props |
| 77 | + |
| 78 | +```sh |
| 79 | ++--------------------------------------------------------------------------+ |
| 80 | +| [Navbar] | <-- `navbar_class` & `navbar_style` |
| 81 | +| | |
| 82 | +| +--------------------------------------------------------------+ | <-- `container_class` & `container_style` |
| 83 | +| | [Logo] [Menu Items] [Search] [CTA Button] [Profile Menu] | | |
| 84 | +| +--------------------------------------------------------------+ | |
| 85 | +| | |
| 86 | ++--------------------------------------------------------------------------+ |
| 87 | +``` |
| 88 | + |
| 89 | +| Property | Type | Description | Default Style | |
| 90 | +| ---------------------- | -------------- | -------------------------------------- | ------------------------------------------------------------ | |
| 91 | +| `navbar_class` | `&'static str` | Class for outer navbar element. | `""` | |
| 92 | +| `navbar_style` | `&'static str` | Style for outer navbar element. | `display: flex; justify-content: space-between; ...` | |
| 93 | +| `container_class` | `&'static str` | Class for max-width inner container. | `""` | |
| 94 | +| `container_style` | `&'static str` | Style for inner container. | `max-width: 1200px; margin: auto; ...` | |
| 95 | +| `inner_class` | `&'static str` | Class for the content wrapper. | `""` | |
| 96 | +| `inner_style` | `&'static str` | Style for the content wrapper. | `display: flex; align-items: center; ...` | |
| 97 | +| `logo_class` | `&'static str` | Class for the logo. | `""` | |
| 98 | +| `logo_style` | `&'static str` | Style for the logo. | `height: 40px;` | |
| 99 | +| `menu_item_class` | `&'static str` | Class for menu items. | `""` | |
| 100 | +| `menu_item_style` | `&'static str` | Style for each menu item. | `padding: 0.5rem 1rem; color: black;` | |
| 101 | +| `dropdown_class` | `&'static str` | Class for dropdown menu. | `""` | |
| 102 | +| `dropdown_style` | `&'static str` | Style for dropdown menu. | `position: absolute; box-shadow: 0 4px 8px rgba(0,0,0,0.1);` | |
| 103 | +| `dropdown_item_class` | `&'static str` | Class for dropdown items. | `""` | |
| 104 | +| `dropdown_item_style` | `&'static str` | Style for dropdown items. | `padding: 0.5rem 1rem;` | |
| 105 | +| `search_input_class` | `&'static str` | Class for search input. | `""` | |
| 106 | +| `search_input_style` | `&'static str` | Style for search input. | `padding: 0.5rem; font-size: 1rem;` | |
| 107 | +| `button_class` | `&'static str` | Class for CTA button wrapper. | `""` | |
| 108 | +| `button_style` | `&'static str` | Style for CTA button wrapper. | `margin-left: 1rem;` | |
| 109 | +| `button_link_class` | `&'static str` | Class for CTA anchor inside button. | `""` | |
| 110 | +| `button_link_style` | `&'static str` | Style for CTA anchor inside button. | `background: #007bff; color: white; ...` | |
| 111 | +| `more_button_class` | `&'static str` | Class for the mega menu "More" button. | `""` | |
| 112 | +| `more_button_style` | `&'static str` | Style for the mega menu "More" button. | `font-weight: bold; border: none;` | |
| 113 | +| `mega_menu_class` | `&'static str` | Class for mega menu wrapper. | `""` | |
| 114 | +| `mega_menu_style` | `&'static str` | Style for mega menu wrapper. | `position: absolute; padding: 0;` | |
| 115 | +| `mega_menu_card_class` | `&'static str` | Class for each mega menu card. | `""` | |
| 116 | +| `mega_menu_card_style` | `&'static str` | Style for each mega menu card. | `background: white; display: flex; ...` | |
| 117 | +| `menu_toggle_class` | `&'static str` | Class for mobile hamburger icon. | `""` | |
| 118 | +| `menu_toggle_style` | `&'static str` | Style for mobile hamburger icon. | `flex-direction: column; gap: 5px;` | |
| 119 | +| `line_class` | `&'static str` | Class for hamburger icon lines. | `""` | |
| 120 | +| `line_style` | `&'static str` | Style for hamburger icon lines. | `width: 25px; height: 2px; background: black;` | |
| 121 | + |
| 122 | +## 💡 Notes |
| 123 | + |
| 124 | +- The navbar is **responsive** by default and adapts to screen size. |
| 125 | +- Hamburger toggle appears when the window width is <= 768px. |
| 126 | +- Click outside to auto-close mobile and dropdown menus via event listeners. |
| 127 | +- You can fully customize the layout using `style` and `class` props for each section. |
| 128 | +- Mega menu, search, CTA button, and profile menu are **optional** features that can be toggled via props. |
| 129 | +- All callback-based interactions like search input or menu toggling are handled with `use_signal`, `Callback`, and `use_effect`. |
| 130 | +- Supports accessibility with custom labels, alt tags, and interactive behaviors. |
0 commit comments