-
Notifications
You must be signed in to change notification settings - Fork 169
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
Unable to toggle drawer containing MudNavMenu
when on /Account
(SSR) pages.
#478
Comments
Sorry, I wasn't clear. It's the msedge_yvAG1GQxTt.mp4 |
MudNavMenu
doesn't work on /Account
(SSR) pages.MudNavMenu
when on /Account
(SSR) pages.
That's the key part, the drawer and the nav menu are very different things. This is expected for now and has been explained multiple times. When you are on static SSR pages, there is no interactivity, which means that any C# code will not execute. Our drawer functions only when there is interactivity, as the open state is managed by the C# code. I don't think 0phois/MudBlazor.StaticInput can do anything either. There are only three ways to handle this:
|
I wouldn't have created this issue had I seen anywhere that this is a known incompletion. I searched the issues and documentation, but didn't see anything specifically for this. Maybe I missed something, but I did look.
I understand how interactivity and render modes work. I understand why this isn't working. This works in the default Blazor templates (again, don't need to explain the technical reasons why it works in them). So a reasonable person, even one who understands the technical reasons why it's not working, would expect them to work in these templates as well. A reasonable person would either consider this a bug or an oversight. If you're frustrated from having to explain this (maybe I'm mistaken, but I kinda get that feeling), I suggest one or more of the following:
Anyhow, thank you for your work on MudBlazor. I've really been enjoying it for my own open-source stuff. Have a good day! |
#461 I'm not frustrated, and I apologize if it sounds passively aggressive. The reason we keep mentioning that this has been discussed multiple times is that, when .NET 8 was in its release candidate stage, we publicly stated in all channels that we would not support static SSR. MudBlazor was developed way before static SSR was introduced, and adopting it would require extensive rewriting. This also goes against our principle of not using JavaScript unless absolutely necessary. At this point, MudBlazor would essentially just wrap some JavaScript libraries into Blazor components, which was not our initial intention. This template represents what we could squeeze out from MudBlazor with static SSR, it's not perfect, but it's significantly better than what we had in the 1.0.0 template version.
Thank you for the feedback. I will work on improving the documentation. |
For anyone who arrives here and would like an example of a potential solution, here's what I did. This assumes you're using the default value for I'm sure edge cases will pop up at some point. 😄 MainLayout.razor: @inject NavigationManager NavManager
@* other content *@
@* Added id to toggle button. *@
<MudIconButton id="nav-drawer-toggle-button"
Icon="@Icons.Material.Filled.Menu"
Color="Color.Inherit"
Edge="Edge.Start"
OnClick="@(_ => DrawerToggle())" />
@* other content *@
@* Added id to drawer. *@
<MudDrawer @bind-Open="_drawerOpen"
id="nav-drawer"
ClipMode="DrawerClipMode.Always"
Elevation="2">
@* other content *@
@* Add JavaScript if we're on an Identity page. *@
@if (_isIdentityPage)
{
<script src="/Layout/MainLayout.razor.js"></script>
}
@code {
private bool _isIdentityPage;
// other content
protected override async Task OnInitializedAsync()
{
_isIdentityPage =
Uri.TryCreate(NavManager.Uri, UriKind.Absolute, out var currentUri) &&
currentUri.PathAndQuery.StartsWith("/Account");
// other content
}
} MainLayout.razor.js (new file in same folder as MainLayout.razor): /** @type {HTMLButtonElement} */
const toggleButton = document.getElementById("nav-drawer-toggle-button");
const navDrawer = document.getElementById("nav-drawer");
const drawerParent = navDrawer.parentElement;
const desktopQuery = window.matchMedia("(min-width: 960px)");
desktopQuery.addEventListener("change", ev => {
if (ev.matches) {
navDrawer.classList.add("mud-drawer-md");
navDrawer.classList.remove("mud-drawer--closed");
navDrawer.classList.add("mud-drawer--open");
drawerParent.classList.remove("mud-drawer-closed-responsive-md-left");
drawerParent.classList.add("mud-drawer-open-responsive-md-left");
}
});
toggleButton.addEventListener("click", () => {
const isDesktopWidth = desktopQuery.matches;
const hasBreakpoint = navDrawer.classList.contains("mud-drawer-md");
if (!isDesktopWidth && hasBreakpoint) {
navDrawer.classList.remove("mud-drawer-md");
return;
}
if (isDesktopWidth && !hasBreakpoint) {
navDrawer.classList.add("mud-drawer-md");
}
navDrawer.classList.toggle("mud-drawer--open");
navDrawer.classList.toggle("mud-drawer--closed");
drawerParent.classList.toggle("mud-drawer-closed-responsive-md-left");
drawerParent.classList.toggle("mud-drawer-open-responsive-md-left");
}); msedge_l3TRYqOoL8.mp4 |
Thank you so much @bitbound for sharing your solution. |
I just did a check to see how the Thanks for this answer! |
This is working great. Thank you so much. I'm just wondering what other tiny landmines are lurking. If it hadn't been for this, I'd be looking at other UI packages. I like MB, but I picked it just so I wouldn't have to tangle too much with JS. "And for anyone else who arrives here..." In the script that's shown, the folder for the to this: My |
@nhwilly Thanks for pointing this out. I updated my example with the correct path. :) |
Thanks bitbound. I got the hamberger working in my app. Blazor has an annoying habit of rendering the layout pages as SSR when I use webassembly and per page mode, I lose interactivity. Is there a way to add javascript to the Mudmenu item to expand the MudMenuItems? |
Out-of-the-box, it's not possible to toggle the drawer containing the
MudNavMenu
when you're on any of the account pages (using SSR). This is particularly problematic on mobile, since you're no longer able to navigate.I see that the templates are using 0phois/MudBlazor.StaticInput for the SSR pages, but they don't have a component for
MudIconButton
orMudNavMenu
. So I'm not sure if this issue belongs here as a bug, or in that repo as a feature request.The text was updated successfully, but these errors were encountered: