Blazor App - simple bug, hard to fix - make DOM/window properties and events available in Blazor
There is a bug in default Blazor application template in client project in Shared\NavMenu.cshtml file. It is very simple but I have absolutely no idea how we can fix this in Blazor (without JavaScript)
ToggleNavMenu function is connected to onclick events in two places:
<button class="navbar-toggler" onclick=@ToggleNavMenu>
and
<div class=@(collapseNavMenu ? "collapse" : null) onclick=@ToggleNavMenu>
Both places are necessary if application works on small screen. First one shows or hides menu when we click on "hamburger" button. Second one hides menu when we click on any menu option.
Unfortunately on big screen we have a problem. "Hamburger" button is not visible and it is OK, but each time we click on menu (strictly speaking on sidebar) collapseNavMenu variable is changed between true/false and collapse class is added/removed to/from <div> element. In site.css file we have:
@media (min-width: 768px) {
.sidebar .collapse {
/* Never collapse the sidebar for wide screens */
display: block;
}
}
so it is not a problem on big screen. But if callapseNavMenu variable is false and we make our browser windows smaller, menu will be visible - it should be hidden.
To solve the problem we should have access to browser window size from C# code and don't toggle the variable if it is 768px or wider.
To solve this and plenty of other problems related to responsive applications we should have access to DOM/window events and properties like onscroll, onresize, different width/height properties, pageXOffest, pageYOffset, and more.
Is it planned in the future?
There are some (Blazor / src / Microsoft.AspNetCore.Blazor / Components / EventHandlers.cs ) of them, might not be all.
Thanks @dlr1 - I have found scroll event, but all other are missing.
In general we expect there to be .NET packages available that wrap relevant parts of the JS API surface. I know the Mono team is working on building a kind of uber-API-package that provides C# wrappers to just about everything in one package.
We're collaborating with Mono on making sure our JS interop mechanisms are compatible.
<button class="navbar-toggler" @onclick="ToggleNavMenu">
private void ToggleNavMenu()
{
collapseNavMenu = !collapseNavMenu;
}
<div class="@NavMenuCssClass" @onclick="HideNavMenu">
private void HideNavMenu()
{
collapseNavMenu = true;
}