Sidebar

Edit Component

Y Sidebar Yew Usage

Adding Sidebar to your project is simple:

  1. Make sure your project is set up with Yew. Follow their Getting Started Guide for setup instructions.

  2. Add the Sidebar component to your dependencies by including it in your Cargo.toml file:

    cargo add sidebar --features=yew
    
  3. Import the Sidebar component into your Yew component and start using it in your app.

🛠️ Usage

Integrating the Sidebar into your Yew application is straightforward. Follow the steps below:

  1. Import the Sidebar, Menu, MenuItem, Submenu, and Profile components:

    use yew::prelude::*;
    use sidebar::yew::item::MenuItem;
    use sidebar::yew::menu::Menu;
    use sidebar::yew::sidebar::Sidebar;
    use sidebar::yew::submenu::Submenu;
  2. Use them in your application:

    use yew::prelude::*;
    use sidebar::yew::item::MenuItem;
    use sidebar::yew::menu::Menu;
    use sidebar::yew::sidebar::Sidebar;
    use sidebar::yew::submenu::Submenu;
    
    #[function_component(App)]
    pub fn app() -> Html {
        let selected = use_state(|| String::from("Dashboard"));
    
        html! {
            <Sidebar
                user_name="Ferris"
                designation="Admin"
                user_img="/assets/logo.webp"
                on_logout={Callback::from(|_| log::info!("Logout!"))}
                logo_img_url="/logo.svg"
                logo_href="/"
            >
                <Menu sub_heading="Main">
                    <MenuItem
                        label="Dashboard"
                        href="/dashboard"
                        icon_html={html! {<span>{ "📊" }</span>}}
                        selected={selected.clone()}
                    />
                    <MenuItem
                        label="Settings"
                        href="/settings"
                        icon_html={html! {<span>{ "⚙️" }</span>}}
                        selected={selected.clone()}
                    />
                    <Submenu title="More" icon_html={html! {<span>{ "➕" }</span>}}>
                        <MenuItem
                            label="Reports"
                            href="/reports"
                            icon_html={html! {<span>{ "📁" }</span>}}
                            selected={selected.clone()}
                        />
                    </Submenu>
                </Menu>
            </Sidebar>
        }
    }

🔧 Props

+---------------------------------------------------------------+
|                          Sidebar                              |
|  (style: width: 270px; background: white;)                    |
|                                                               |
|  +---------------------------------------------------------+  |
|  |                     Header (Logo + Toggle)              |  |
|  |   (header_style: display: flex; align-items: center;)   |  |
|  |                                                         |  |
|  |   [Logo]                             [Toggle ◀ or ▶]    |  |
|  +---------------------------------------------------------+  |
|                                                               |
|  +---------------------------------------------------------+  |
|  |                        Menu                             |  |
|  |  (sub_heading shown if not collapsed)                   |  |
|  |                                                         |  |
|  |   +-------------------+   +-------------------+         |  |
|  |   |    MenuItem       |   |    MenuItem       |         |  |
|  |   |  [Icon] Label     |   |  [Icon] Label     |         |  |
|  |   +-------------------+   +-------------------+         |  |
|  |                                                         |  |
|  |   +-------------------+                                 |  |
|  |   |     Submenu       |                                 |  |
|  |   |  [Icon] Label  ▼  |(click to expand ▼ or collapse ▲)|  |
|  |   +-------------------+                                 |  |
|  |       |                                                 |  |
|  |       |--> +-------------------+                        |  |
|  |            |    MenuItem       |                        |  |
|  |            |  [Icon] Label     |                        |  |
|  |            +-------------------+                        |  |
|  +---------------------------------------------------------+  |
|                                                               |
|  +---------------------------------------------------------+  |
|  |                       Profile                           |  |
|  |  (shown only if not collapsed)                          |  |
|  |                                                         |  |
|  |  [Avatar] Username        Designation          [-]      |  |
|  +---------------------------------------------------------+  |
+---------------------------------------------------------------+
PropertyTypeDescriptionDefault
childrenChildrenWithProps<Menu>The Menu components rendered in sidebar[]
show_profileboolWhether to show the profile sectiontrue
user_name&'static strUser's name displayed in profile""
designation&'static strUser role/designation""
user_img&'static strPath to user image""
on_logoutCallback<()>Callback invoked when logout is clickedNo-op
style&'static strInline CSS for sidebar container"width: 270px; background: white;"
class&'static strCSS class for sidebar container""
header_style&'static strCSS for the sidebar header section"display: flex; ..."
header_class&'static strClass for the header div""
logo_img_url&'static strLogo image path""
logo_href&'static strLogo link target""
PropertyTypeDescriptionDefault
sub_heading&'static strOptional section heading""
childrenChildrenChildren MenuItem or Submenu[]
style&'static strInline styles"padding: 1rem;"
class&'static strCSS class""
PropertyTypeDescriptionDefault
label&'static strMenu item label""
href&'static strNavigation URL""
icon_htmlHtmlIcon HTML for the itemhtml!{}
badgeOption<&'static str>Optional badge (e.g., "3", "new")None
selectedUseStateHandle<String>Currently selected item stateRequired
anchor_style&'static strStyles for anchor wrapper"text-decoration: none;"
anchor_class&'static strClass for anchor wrapper""
item_style&'static strStyles when sidebar is expanded"display: flex; ..."
item_class&'static strClass for item container""
collapsed_style&'static strStyles for collapsed sidebar"display: flex; ..."
collapsed_label_style&'static strStyles for label text in collapsed mode"font-size: 10px; text-align: center;"
selected_style&'static strStyles for selected item"background-color: #1e293b; color: white;"
badge_style&'static strStyle for badge"background: red; ..."
PropertyTypeDescriptionDefault
title&'static strSubmenu title""
icon_htmlHtmlOptional iconhtml!{}
childrenChildrenWithProps<MenuItem>Menu items to be shown when expanded[]
class&'static strCSS class""
style&'static strInline style"padding: 10px; cursor: pointer; ..."

Profile Component Props

PropertyTypeDescriptionDefault
user_name&'static strName of the user""
designation&'static strUser's designation/role""
user_img&'static strProfile image path""
is_collapsedboolWhether sidebar is collapsedfalse
on_logoutCallback<()>Callback triggered on logout clickNo-op
style&'static strStyling for the profile container"display: flex; align-items: center; ..."
class&'static strClass for the container""

💡 Notes

  • Sidebar handles responsive behavior based on viewport width (< 768px = collapsed).
  • MenuItem auto-detects and highlights current item using the selected state and URL query param selected.
  • You must pass a shared UseStateHandle<String> to selected in every MenuItem to track active selection.
  • Submenu provides collapsible behavior to group nested items.
  • Profile is automatically hidden in collapsed mode.
  • Customize with style and class props for full design flexibility.
  • ContextProvider enables state sharing (collapsed/expanded) across components via SidebarContext.

Usage

CLI Usage

You can import Sidebar directly into your project to customize and modify it using the opensass cli:

os add sidebar yew