Skip to content

Blazor webassembly: @onwheel:preventDefault has no effect #24932

Closed
@sergey-su

Description

@sergey-su

Describe the bug

Adding @onwheel:preventDefault attribute does not prevent the default scrolling behavior.

To Reproduce

In Counter demo project, on Index page

<h1 @onwheel:preventDefault>Hello, world!</h1>

The page still bumps up or down when I perform two fingers scroll gesture which indicates that the event was not suppressed.

Further technical details

Counter demo project on mac from command line. Versions:

    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="5.0.0-preview.7.20365.19" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="5.0.0-preview.7.20365.19" PrivateAssets="all" />
    <PackageReference Include="System.Net.Http.Json" Version="5.0.0-preview.7.20364.11" />

Workarouond

interop.js:

window.myUtils = {
    addDefaultPreventingHandler: function (element, eventName) {
        element.addEventListener(eventName, e => e.preventDefault(), { passive: false });
    }
};

the page:

protected override async Task OnAfterRenderAsync(bool firstRender)
{
    if (firstRender)
        await jsRuntime.InvokeVoidAsync("myUtils.addDefaultPreventingHandler", myElementReference, "wheel");
}

Possibly blazor does not set the passive option correctly when it adds its event listener. It should disable the scroll optimization by setting passive: false when @on<EVENT>:preventDefault is present for a scrolling event in the razor markup.

Metadata

Metadata

Assignees

Labels

Pillar: Technical DebtPriority:1Work that is critical for the release, but we could probably ship withoutaffected-fewThis issue impacts only small number of customersarea-blazorIncludes: Blazor, Razor ComponentsbugThis issue describes a behavior which is not expected - a bug.feature-blazor-component-modelAny feature that affects the component model for Blazor (Parameters, Rendering, Lifecycle, etc)severity-minorThis label is used by an internal tool

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions