Repository navigation
Fix menu problem in templates - #68768
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes mobile navigation menus in Blazor Web App templates and samples so they collapse after navigation.
Changes:
- Uses delegated click handling for resilient menu behavior.
- Adds responsive test coverage across interactivity modes.
- Updates corresponding sample applications.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Summary |
|---|---|
src/ProjectTemplates/Web.ProjectTemplates/content/BlazorWeb-CSharp/BlazorWebCSharp.1/Components/Layout/NavMenu.razor.js |
Updates template menu handling. |
src/ProjectTemplates/test/Templates.Blazor.Tests/BlazorWebTemplateTest.cs |
Adds menu-collapse verification. |
src/ProjectTemplates/test/Templates.Blazor.Tests/BlazorTemplateTest.cs |
Adds viewport and navigation test support. |
src/Components/Samples/BlazorWebAppPerPage/Components/Layout/NavMenu.razor.js |
Applies the menu fix to the per-page sample. |
src/Components/Samples/BlazorWebAppGlobal.Client/Layout/NavMenu.razor.js |
Applies the menu fix to the global-interactivity sample. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
|
After more deep investigation process: The problem is connected to the transfer from non-interactivity to interactivity. We destroy and rebuild DOM in this process, which means that we do not have listeners that we had before. Connected to #42561 |
|
One thing I noticed while comparing this with our version: these test rows use the template’s default I added an explicit Would it make sense to add that case here so the regression test definitely covers the failing path? Written by Copilot with human help. |
|
/backport to release/11.0 |
|
Started backporting to |
Fix menu problem in templates
Mobile nav menu stays open after navigation in interactive render modes
Description
In .NET 11 the Blazor Web App template's mobile nav menu no longer collapses after you pick a page, but only when an interactive render mode is used (Interactive Server, WebAssembly, or Auto). Static SSR is unaffected.
#63828 removed the inline
onclick="document.querySelector('.navbar-toggler').click()"from.nav-scrollable(to avoid an inline event handler for CSP) and replaced it with aNavMenu.razor.jsmodule that resolved the element once and bound a listener to it:That module runs during prerendering, so the listener is attached to the prerendered
#nav-scrollableelement. On a root component's first interactive renderBrowserRenderer.updateComponentcallsdetachEventHandlersFromElementandemptyLogicalElement, discarding the prerendered markup and inserting new DOM nodes, so the listener is left on a detached element where it never fires again. Being an ES module, it is not re-evaluated to rebind. Under static SSR the element is never replaced, which is why that mode still works.The fix wraps the toggler and the menu in a
<nav-menu>custom element and moves the listener into its lifecycle callbacks:customElements.defineregisters the element once for the document, so whichever<nav-menu>is live gets the handler: the browser upgrades the prerendered element on page load, and firesconnectedCallbackagain on the replacement element Blazor creates during the interactive takeover. The listener stays scoped to the menu instead ofdocument, and no initializer ordering or re-evaluation is involved.The handler resolves the toggler at click time rather than in
connectedCallback, because Blazor inserts an element into the DOM before appending its children, so the subtree is not yet available when the callback runs.The handler also sets
toggler.checked = falseinstead of callingtoggler.click().click()toggles rather than closes, so on viewports at or above 641px - where the menu is always visible and the toggler is hidden - clicking a nav link used to silently check the toggler, leaving the menu stuck open the next time the window was narrowed.nav-menuis styleddisplay: contentsso the wrapper adds no box of its own and the existing layout, including the.navbar-toggler:checked ~ .nav-scrollablesibling rule, is unchanged.Changes
BlazorWeb-CSharptemplate:NavMenu.razorwraps the toggler and.nav-scrollablein<nav-menu>and drops the now-unusedid="nav-scrollable";NavMenu.razor.jsdefines the custom element;NavMenu.razor.cssaddsnav-menu { display: contents; }.BlazorWebAppGlobal.ClientandBlazorWebAppPerPagesamples: same change, keeping the duplicated files in sync with the template.VerifyNavMenuCollapsesAfterNavigationAsynchelper inBlazorTemplateTest, run as part ofTestBasicInteractionAsync.Testing
VerifyNavMenuCollapsesAfterNavigationAsyncresizes the page to 400x800 (below the 641px breakpoint where the menu collapses behind the toggler), opens the menu, clicks the Home link from inside the open menu, and asserts.nav-scrollablebecomes hidden. The original viewport is restored in afinally.The assertion is on
.nav-scrollablevisibility rather than on the toggler'scheckedstate, so it does not depend on how a given template implements the menu. That lets it run unconditionally for every Blazor template as part of the basic interaction test, gated only on the existingBlazorTemplatePages.Homeflag because it navigates Home. It therefore also covers the standalone Blazor WebAssembly template, whoseNavMenuuses a C#ToggleNavMenuhandler and acollapseclass instead of a toggler checkbox.Through
BlazorWebTemplate_Worksit runs for the None, Server, WebAssembly and Auto interactivity options against both the built and the published app. The None case is the useful control: it passed before this change and must keep passing.The assertion reuses the projects those tests already create, build and publish, so it adds no extra template build to the run.
Breaking changes
None. This only changes template and sample content; no product API or behavior outside the generated nav menu markup is affected. Existing apps are unaffected, since template content only applies to newly created projects.
Fixes #68677