How to close sidebar on click MenuItem

I have some code for a BlazorSidebar:

But what I want is that when one of the URLs in the sidebar is clicked, the sidebar toggles closed. I want the sidebar to close when I click a link, to reveal the full content of that clicked link's page. I have tried to do a ItemSelected="Toggle", but that did not work for some reason.

@code {
SfSidebar Sidebar;
public Orientation Orientation = Orientation.Horizontal;
public Orientation VerOrientation = Orientation.Vertical;
Dictionary HtmlAttribute = new Dictionary()
{
{"class", "sidebar-menu" }
};
Dictionary Tooltip = new Dictionary()
{
{"title", "sidebar-menu" }
};
public List MainMenuItems = new List{
new MenuItem {
Text = "Home",
IconCss ="oi oi-home",
Url = "/"
},
new MenuItem {
Text = "Customers",
IconCss = "oi oi-people",
Url = "PagePaths.CUSTOMERS"
},
new MenuItem {
Text = "Products",
IconCss = "oi oi-briefcase",
Url = "/products"
},
new MenuItem {
Text = "Discounts",
IconCss = "icon-tag icon",
Url = "/discount-setup"
},
new MenuItem {
Text = "Data Management",
IconCss = "oi oi-calculator",
Url = "/data",
Items = new List {
new MenuItem{ Text = "Daily Routes", Url = "/data?tab=daily-routes" },
new MenuItem{ Text = "Invoices", Url = "/data?tab=invoices" },
new MenuItem{ Text = "Acct Payments", Url = "/data?tab=acct-payments" },
new MenuItem{ Text = "Discrepancies", Url = "/data?tab=discrepancies" },
new MenuItem{ Text = "Customer Sales", Url = "/data?tab=customer-sales" }
}
},
new MenuItem {
Text = "Administration",
IconCss = "oi oi-graph",
Url = "/administration",
Items = new List {
new MenuItem{ Text = "Web Users", Url = "/administration?tab=web-users" },
new MenuItem { Separator = true },
new MenuItem{ Text = "Devices", Url = "/administration?tab=devices" },
new MenuItem{ Text = "Device Users", Url = "/administration?tab=device-users" },
new MenuItem { Separator = true },
new MenuItem{ Text = "Trucks", Url = "/administration?tab=trucks" },
new MenuItem{ Text = "Truck Routes", Url = "/administration?tab=truck-routes" },
new MenuItem { Separator = true },
new MenuItem{ Text = "Company Settings", Url = "/administration?tab=company-settings" },
new MenuItem{ Text = "Audit Log", Url = "/administration?tab=audit-log" }
}
}
};


public bool SidebarToggle = false;
public void Toggle()
{
SidebarToggle = !SidebarToggle;
}
private void onSelect(MenuEventArgs args)
{
SidebarToggle = !SidebarToggle;
}
}

9 Replies 1 reply marked as answer

SM Shalini Maragathavel Syncfusion Team November 23, 2021 01:32 PM UTC

Hi Bryce, 
 
Greetings from Syncfusion support.  
 
Based on your query, we suspect that you need to prevent Sidebar from closing while clicking the Menu items. Please find the Sidebar with Menu sample from the below link.

Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Sidebar_blazor-1249446659-1649491648.zip

Before proceeding further, please share the following information to validate further. 
  • Please elaborate on which case do you need to prevent the closing of Sidebar along with the complete Sidebar rendering code or modify the above sample according to your scenario.

  • Elaborate on your requirement in detail with pictorial or video demonstrationtion.

This information would help us to validate and provide the prompt solution 

Regards,
Shalini M. 



BS Bryce Steuer November 23, 2021 01:55 PM UTC

Snap 2021-11-23 at 07.43.25.png 

  In your code example, line 64 of MainLayout.Razor:

<SfButton ID="toggle" @onclick="@Toggle" IsToggle="true" CssClass="e-btn e-info">Toggle Sidebar</SfButton>

This is the function I want for each MenuItem. When a MenuItem is clicked in the sidebar, I want the sidebar to close. For example, if I click "Administration" in the sidebar above, I want the sidebar to automatically slide closed because while the sidebar is open, it is hiding some of the Administration page content as shown in the picture above. I would like the sidebar to slide closed if I click any of the MenuItems in the sidebar. Thanks





KR Keerthana Rajendran Syncfusion Team November 23, 2021 02:12 PM UTC

Hi Bryce, 

Thanks for the clarification. 

To achieve your requirement of closing Sidebar on Menu item click, we suggest you to set CloseOnDocumentClick as true for Sidebar. We have addressed similar requirement in below forum, please refer to the following link. 


Please let us know if you need further assistance. 

Regards, 
Keerthana R. 



BS Bryce Steuer November 23, 2021 02:42 PM UTC

Hello, this does not meet my requirement. From that link you provided:

"When that property is set as true, Sidebar will close, when clicking outside the sidebar region( any element in the document excluding Sidebar element and its inner content elements)."

However, to the contrary, I need the Sidebar to close when I click the MenuItem, which is an inner content element of the Sidebar. The CloseOnDocumentClick works to close the sidebar if I click somewhere outside of the Sidebar. What I want is to click a MenuItem in the sidebar and have the sidebar close when I click that in the sidebar.



SM Shalini Maragathavel Syncfusion Team November 24, 2021 12:22 PM UTC

Hi Bryce, 

Good day to you. 

Based on your query, we suspect that you need to close the Sidebar while clicking the Menu items. You can achieve your requirement by calling the Toggle method of Sidebar in ItemSelected event of Menu as demonstrated in the below code snippet, 
   
<SfSidebar @ref="sidebarObj" @bind-IsOpen="SidebarToggle"> 
    <ChildContent> 
        <div id="listcontainer"> 
            <SfMenu TValue="MenuItem" CssClass="custom_class"  Orientation="Syncfusion.Blazor.Navigations.Orientation.Vertical"> 
                <MenuEvents TValue="MenuItem" ItemSelected="ItemSelected" ></MenuEvents>
              . . .
 
           </SfMenu> 
         
        </div> 
        
    </ChildContent> 
</SfSidebar> 
 
@code { 
    public void ItemSelected(Syncfusion.Blazor.Navigations.MenuEventArgs<MenuItem> args) 
        { 
        this.sidebarObj.Toggle(); 
        } 
 
    } 

Please get back to us if you need further assistance. 

Regards,   
Shalini M. 


Marked as answer

BS Bryce Steuer November 24, 2021 06:38 PM UTC

Thank you very much for your time and professionalism. I really appreciate it.



KR Keerthana Rajendran Syncfusion Team November 25, 2021 06:50 AM UTC

Hi Bryce, 
 
Most welcome and thanks for your feedback. Please get back to us if you need any further assistance. We will be happy to assist you. 
 
Regards, 
Keerthana R. 



BS Bryce Steuer December 8, 2021 10:14 PM UTC

Hello again, I find that Toggle is not working, it says the method is deprecated and I should use IsOpen instead. But that does not work either. Just to remind you, this is how my code is now, and I want the sidebar to close (still showing dock though) when one of these MainMenuItems is clicked.




IL Indhumathy Loganathan Syncfusion Team December 9, 2021 01:09 PM UTC

Hi Bryce, 
 
We have validated your reported query in Blazor Sidebar component. But unable to reproduce the reported issue at our end. We are able to open the Sidebar using IsOpen property while clicking on the menu item. Please find the sample from below link. 
 
 
We suspect that the reported issue may occur due to some other reason. So kindly share us the below details. 
 
1.      Whether the ItemSelected event is triggered properly on menu item click? If yes, then share us the complete code snippet of Sidebar and Menu component.  
2.      Whether you are facing the same issue at above shared sample as well. 
3.      Syncfusion package version used in your sample. 
4.      If possible replicate the issue at the above shared sample.  
 
Please let us know if you need any further assistance. 
 
Regards, 
Indhumathy L 


Loader.
Up arrow icon