Skip to content

Fix Blazor mobile menu after hydration - #68809

Draft
PureWeen wants to merge 4 commits into
dotnet:mainfrom
PureWeen:pureween-reproduce-blazor-menu-issue
Draft

PureWeen wants to merge 4 commits into
dotnet:mainfrom
PureWeen:pureween-reproduce-blazor-menu-issue

Conversation

@PureWeen

Copy link
Copy Markdown
Member

Summary

  • wrap each generated navigation menu in a scoped <blazor-nav-menu> custom element
  • attach and remove the menu listener through connectedCallback and disconnectedCallback, so hydration replacements receive behavior without a document-global listener
  • close the owning menu by assigning checked = false, which cannot accidentally reopen it
  • keep the global and per-page Components samples synchronized with the template
  • add a real prerender-to-Interactive Server E2E covering pointer and Enter-key navigation

This follows the custom-element lifecycle pattern already used by Blazor client validation and keeps listener ownership and disposal local to each menu instance.

Fixes #68677.

Testing

  • Strict E2E red: with the original one-time listener, both pointer and keyboard cases completed hydration and navigation, then failed only the final checkbox assertion (Expected: False, Actual: True).
  • Green: the unchanged focused E2E passes both cases (2 passed, 0 failed, 0 skipped).
  • Built the modified BlazorWebAppGlobal and BlazorWebAppPerPage samples.
  • Packed the local Web template and generated/compiled None, Server, WebAssembly, Auto, Individual-auth, global Server, global WebAssembly, and global Auto variants.
  • Exercised the generated global apps in Microsoft Edge through Playwright at 390x844:
    • Interactive Server: pointer and Enter
    • Interactive WebAssembly: pointer and Enter
    • Interactive Auto: cold Server-backed and warm WebAssembly-backed visits, pointer and Enter
  • All 8 final browser trials navigated from /counter to /weather, changed the checkbox from checked to unchecked, hid the menu, and had zero failed requests, console errors, or page errors.

Copilot AI added 3 commits August 24, 2026 13:36
Use document-level event delegation so the generated navigation menu continues to close after interactive hydration replaces its prerendered elements. Add a browser regression test for element replacement.

Co-authored-by: Copilot App <[email protected]>
Co-authored-by: Copilot App <[email protected]>

Copilot-Session: a01ff8c4-c002-4166-89d5-8264fe93ccc5
Co-authored-by: Copilot App <[email protected]>

Copilot-Session: a01ff8c4-c002-4166-89d5-8264fe93ccc5
Co-authored-by: Copilot App <[email protected]>

Copilot-Session: a01ff8c4-c002-4166-89d5-8264fe93ccc5
@Youssef1313

Copy link
Copy Markdown
Member

Did this PR get replaced by #68768 and can be closed now?

@Youssef1313 Youssef1313 added area-blazor Includes: Blazor, Razor Components area-commandlinetools Includes: Command line tools, dotnet-dev-certs, dotnet-user-jwts, and OpenAPI labels Sep 2, 2026

@kubaflo kubaflo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current-head review — d969769a7cfca6e7434940c40da0e9eba81cbeb7 (draft)

[P2] Keep the shared menu assertion compatible with standalone WebAssembly — src/ProjectTemplates/test/Templates.Blazor.Tests/BlazorTemplateTest.cs:348–354. TestBasicInteractionAsync now invokes this helper for every caller with Home enabled, including BlazorWasmStandaloneTemplate_Works and the PWA paths in BlazorWasmTemplateTest. The standalone navigation menu uses a <button class="navbar-toggler" @onclick="ToggleNavMenu"> and a collapse class, not a checkbox. Locator.IsCheckedAsync() is a checkbox/radio assertion and errors on that button. Thus the new shared test fails those existing template scenarios before it can verify collapse. Assert the observable .nav-scrollable visibility for all templates, or keep checkbox assertions restricted to the checkbox-based template. Confidence: high; established from callers, supplied standalone markup, and the locator contract, not a claimed local browser reproduction.

Prior feedback / upstream disposition: The existing question about replacement is material: #68768 is merged (2026-09-01, 87d1567f9efffe8269c9db6bcef02af314e3e6d0) and changes the same template/sample/test surfaces with the same custom-element lifetime mechanism. Its shared test uses visibility rather than checkbox state. Reconcile this draft with that merged work rather than treating it as an independent unlanded fix. I have not closed the PR or changed a thread.

The custom-element handler itself resolves the current subtree at click time, attaches/detaches locally, and closes rather than toggles; the sibling CSS selector remains within the wrapper.

Validation and limits: Inspected all changed files, shared test callers, standalone menu source, narrow-screen CSS, and the merged replacement's diff. Frozen git diff --check passes. No local template build/browser run or visual/media validation was performed; the body describes private browser/E2E trials not independently reproduced here. CI/template/Components checks report success, but do not invalidate the concrete standalone-selector mismatch above or establish the claimed browser matrix.

Scope: GPT-6.1 Sol; no model campaign, product fix, approval, or merge-readiness claim.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-blazor Includes: Blazor, Razor Components area-commandlinetools Includes: Command line tools, dotnet-dev-certs, dotnet-user-jwts, and OpenAPI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mobile menu remains open after navigation in Blazor Web App interactive render modes (.NET 11)

4 participants