Skip to content

Conversation

@javiercn
Copy link
Member

@javiercn javiercn commented Jan 2, 2026

Summary

Fixes #52514

@onclick:preventDefault was not functioning when enhanced navigation was enabled. Clicking a link with @onclick:preventDefault would still navigate away instead of preventing the default behavior.

Root Cause

Enhanced navigation's native click handler (document.addEventListener('click', onDocumentClick)) was intercepting clicks before Blazor's EventDelegator could call event.preventDefault() via the @onclick:preventDefault directive.

Solution

Defer enhanced navigation's click handling to EventDelegator's notifyAfterClick callback, ensuring it runs after Blazor's event handlers have processed the click (including calling preventDefault if specified). This follows the same pattern used by NavigationManager.ts.

When an EventDelegator is available (interactive scenarios), click handling is deferred to the callback. In pure SSR scenarios without EventDelegator, the native click handler continues to work as before.

Changes

  • NavigationEnhancement.ts: Added attachEnhancedNavigationClickHandlerToEventDelegator() to register enhanced nav click handling via EventDelegator's notifyAfterClick callback
  • BrowserRenderer.ts: Wire up enhanced navigation to use EventDelegator when a renderer is created
  • E2E test: Added PreventDefaultOnClickIsRespectedWithEnhancedNavigation test to verify the fix
  • Test page: Added PageWithPreventDefaultLink.razor for the E2E test

Fixes #52514

Enhanced navigation's native click handler was intercepting clicks before
Blazor's EventDelegator could call event.preventDefault() via the
@OnClick:preventDefault directive. This caused links with preventDefault
to still navigate when enhanced navigation was enabled.

The fix defers enhanced navigation's click handling to EventDelegator's
notifyAfterClick callback, ensuring it runs after Blazor's event handlers
have processed the click (including calling preventDefault if specified).

Changes:
- NavigationEnhancement.ts: Added attachEnhancedNavigationClickHandlerToEventDelegator()
  to register enhanced nav click handling via EventDelegator's afterClick callback
- BrowserRenderer.ts: Wire up enhanced navigation to use EventDelegator
- Added E2E test to verify the fix
@github-actions github-actions bot added the area-blazor Includes: Blazor, Razor Components label Jan 2, 2026
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Blazor] preventDefault does not function in Auto render mode, even if the page is marked with InteractiveWebAssembly

2 participants