Kebechet.Blazor.EventHandlers.ShortClick 3.1.0

Prefix Reserved
dotnet add package Kebechet.Blazor.EventHandlers.ShortClick --version 3.1.0
                    
NuGet\Install-Package Kebechet.Blazor.EventHandlers.ShortClick -Version 3.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Kebechet.Blazor.EventHandlers.ShortClick" Version="3.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Kebechet.Blazor.EventHandlers.ShortClick" Version="3.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Kebechet.Blazor.EventHandlers.ShortClick" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Kebechet.Blazor.EventHandlers.ShortClick --version 3.1.0
                    
#r "nuget: Kebechet.Blazor.EventHandlers.ShortClick, 3.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Kebechet.Blazor.EventHandlers.ShortClick@3.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Kebechet.Blazor.EventHandlers.ShortClick&version=3.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Kebechet.Blazor.EventHandlers.ShortClick&version=3.1.0
                    
Install as a Cake Tool

"Buy Me A Coffee"

Blazor.EventHandlers.ShortClick

NuGet Version NuGet Downloads Twitter

This package contains @onshortclick event handler.

Motivation: In order to unify development experience among multiple platforms I have decided to create onshortclick event for cases when you would like to use @onlongpress event handler. Because for example on Windows browser onclick event is triggered even after holding mousedown for more than 500ms. On the other side mobile devices behave differently. And onclick is not triggered after more than 400-500ms .

  • usage:
<button @onshortclick="OnShortClick">Click me</button>

or the combination with @onlongpress:

<button @onlongpress="OnLongPress" @onshortclick="OnShortClick">Click or longpress me</button>

Click mode (opt-in, since 3.1.0)

Click mode gives a plain @onclick the semantics of @onshortclick: a click that a pointer made reaches the page only when its press was a short click. You keep writing @onclick, and the handler runs on click, the last event of a tap.

Turn it on for the whole page with an attribute on <html> (or <body>):

<html data-shortclick-mode="click">
  • Blazor WebAssembly and .NET MAUI Blazor Hybrid: wwwroot/index.html.
  • Blazor Web App (interactive server, WebAssembly or auto, and static SSR): the <html> element in App.razor.

ShortClick.js reads the attribute on every click, so the attribute can be changed at runtime. The value is matched in any case (Click works too), as HTML matches its own enumerated attribute values. The nearest element carrying the attribute decides, the clicked element included: put it on one container to turn the mode on there only, or give a subtree any other value to turn it back off, for example around a third-party component that relies on long-press clicks:

<div data-shortclick-mode="off">...</div>

What it drops - the click and its default action:

  • a press held for 400ms or longer (or the page's own limit, see How long a press may be held): a long-pressed link does not navigate, a held checkbox does not toggle, a held submit button does not submit
  • a press that moved further than a short click may (see MOVE_THRESHOLD in ShortClick.js): a drag or a text selection that ends on a card does not activate it

What it never touches:

  • keyboard activation (Enter, Space) and element.click() (detail === 0), and clicks a script dispatches (isTrusted === false). Unlike @onshortclick, an @onclick element stays operable by keyboard.
  • text-entry controls: <input> types that edit a value (text, number, date, range...), <textarea>, <select> and contenteditable content, also inside a web component's shadow root. A long press or a drag there places the caret or selects text, so their clicks are left alone.
  • a click that follows no press the script measured, such as a press whose pointerdown or pointerup a component's own script stopped from propagating, or an assistive technology's activation. Only a press measured as not short loses its click, and only a click that arrives within 350ms of that press's pointerup.
  • @onshortclick, which keeps working the same with or without the mode.

A handler that runs on click cannot be followed by a ghost click (see below): nothing of the tap comes after it.

It is off by default because it changes every click in the page, third-party components included, and on a desktop a user who holds the mouse button down for 400ms or longer no longer clicks at all.

The listener is registered on window in the capture phase when ShortClick.js loads, so it runs before Blazor's own click listener on document. A dropped click is stopped with preventDefault() and stopImmediatePropagation() and never reaches Blazor.

Where it lets clicks through that it should not (it always fails open, never the other way):

  • ShortClick.js is added as an async script by the package's JS initializer, before Blazor starts, and may finish loading after Blazor's first render. A tap in between is not filtered.
  • In a browser without Pointer Events (iOS 12 and older), the mouse events that follow a touch measure the press again as a short one, so a touch long press keeps its click.

A quick tap released while the main thread is busy (a WebAssembly render, a garbage collection) still clicks: the press is timed by its events' own timestamps, see What counts as a short click.

Measured on devices (2026-11, one test page injected into a .NET MAUI BlazorWebView):

  • Android 16 WebView (Chrome 157, Samsung Galaxy A53): a press held past Android's own long-press timeout fires no click at all, only contextmenu, with or without the mode. A press of 450-480ms does fire click; click mode drops it. A swipe ends in pointercancel and no click.
  • iOS 26.2 WKWebView (iPhone 11): a long press of 800ms does fire click, so without the mode a long-pressed link navigates and a held checkbox toggles; click mode drops it. A drag fires no click.
  • iOS has historically dispatched click only to elements it considers clickable (links, form controls, elements with cursor: pointer or a click listener of their own), while Blazor listens on document. On iOS 26.2 a tap on a plain <div> whose only click listener was on document did receive click. Older iOS versions were not tested: on those, give a tappable non-interactive element cursor: pointer or use a <button>.

How long a press may be held (since 3.1.0)

By default a press held for 400ms or longer is a long press, not a short click (Android's long-press timeout). People who tap slowly need longer: Android's "Touch & hold delay" accessibility setting goes up to 1.5s, and iOS has "Hold Duration". Raise the limit with an attribute, in milliseconds:

<html data-shortclick-long-press-ms="1000">
  • It applies to @onshortclick and to click mode alike. Both take their verdict from the same measurement, so they never disagree on what a short press is.
  • The nearest element carrying the attribute decides, starting from the element the press ends on: <html> sets the page's limit, and a container can set its own.
  • A value that is not a positive number (empty, 0, negative, text) falls back to 400.
  • It is read on every press, so the app can change it at runtime, for example from a setting in its own UI. The library does not read the operating system's setting.

Ghost clicks (behaviour change in 3.0.0)

shortclick is dispatched on pointerup, which is before the browser dispatches the compatibility click for the same gesture. A handler is therefore free to re-render the DOM in between, and Blazor applies its render batch synchronously.

On touch, that compatibility click is hit-tested against the DOM as it stands when it is dispatched. So if your handler removed the element that was pressed, the click lands on whatever now occupies that point. When the replacement is a <button type="submit"> or an <a href>, it activates through the browser's native default action - no handler involved, nothing you can intercept from C#.

A concrete case: an "Edit" button that swaps itself for "Cancel" + "Save". Tapping the right half of "Edit" enables edit mode and submits the form, because "Save" lands under the finger before the click is dispatched.

(This never shows up with a mouse: click is fired on the nearest common ancestor of mousedown and mouseup, so a removed element simply yields no click. It only reproduces on touch - use device emulation, e.g. iPhone SE.)

Since 3.0.0 the library swallows that click, but only when it did not land on the element that was pressed (or an ancestor/descendant of it). That covers both ghost shapes: the pressed element was removed and the click lands on its replacement, or the pressed element is still in the DOM but no longer hit-testable (moved, covered, pointer-events: none) and the click falls through to whatever is behind it. A click that lands on the pressed element is genuine and keeps reaching submit / href / your handler as before. Keyboard and programmatic clicks (detail === 0) are never swallowed. The guard is armed only for the ~350ms following a shortclick and disarms itself if no click arrives, so it can never swallow an unrelated later tap.

This is a breaking change if you were (knowingly or not) depending on that trailing click firing.

Note that shortclick never fires for keyboard activation - Enter/Space produce a click with no pointer events. If an element must be operable by keyboard, keep a plain @onclick on it.

What counts as a short click

A press is a short click when the same pointer goes down and up within 400ms (Android's long-press timeout, or the page's own limit, see How long a press may be held) and does not move too far in between. @onshortclick and click mode use this one check.

  • How far it may move: the distance between the pointerdown and the pointerup in screenX/screenY, divided by devicePixelRatio, rounded, must be at most 8. The constant comes from Android's 8dp touch slop, but screenX/screenY are already CSS pixels, so the press may really move up to 8 × devicePixelRatio CSS pixels: about 21 on a phone at a devicePixelRatio of 2.625, and 8 on a desktop at 1.
  • The hold is measured between the two events' own timeStamps, not by when the handlers run. On a busy main thread (a render or a garbage collection in a WebAssembly app) the handlers run late, and a quick tap would otherwise measure as a long press and be dropped.
  • Every pointer is tracked on its own (by pointerId), so a second finger does not overwrite where the first one started.
  • One gesture raises at most one shortclick: when several fingers tap together, the first finger that lifts raises it and the others raise nothing.
  • Touch, mouse and pen gestures are tracked apart. A mouse or a pen is always the primary pointer, and pressing it does not end a finger's press that is still in progress.
  • A cancelled press is forgotten. Browsers send pointercancel and no pointerup when the system takes the gesture over, such as a home swipe from Android's gesture bar that starts over your element.

Tests

tests/Blazor.EventHandlers.ShortClick.Browser.Tests drives the shipping ShortClick.js in Chromium and WebKit through Playwright:

dotnet test tests/Blazor.EventHandlers.ShortClick.Browser.Tests

The first run downloads the browser builds of the pinned Playwright version.

TODO

  • minification
  • build pipeline that runs the browser tests on pull requests

License

This repository is licensed with the MIT license.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.1.0 128 11/23/2026
3.0.0 2,194 7/13/2026
2.0.0 9,044 6/23/2024
1.0.2 1,080 10/8/2023
1.0.1 323 7/4/2023
1.0.0-preview3 283 5/29/2023
1.0.0-preview2 278 5/28/2023
1.0.0-preview1 273 5/28/2023

Added an opt-in click mode: with data-shortclick-mode="click" on <html>, <body> or any element, a click that a pointer made reaches the page only when its press was a short click, so a plain @onclick behaves like @onshortclick. A press held for 400ms or longer, or moved past the threshold, loses its click and the click's default action. Keyboard and programmatic clicks always pass, and text-entry controls are exempt. Off by default; @onshortclick is unchanged. Also added data-shortclick-long-press-ms: how long a press may be held and still be a short click (default 400ms), for @onshortclick and click mode alike, from the nearest element carrying it; a value that is not a positive number falls back to 400. Fixed: a press is timed by its events' own timestamps, so a quick tap released while the main thread is busy (a WebAssembly render) is no longer taken for a long press; every pointer is tracked on its own, one gesture raises at most one shortclick, touch, mouse and pen presses are tracked apart, and a cancelled press is forgotten.