Kebechet.Blazor.EventHandlers.LongPress 2.5.2.2

Prefix Reserved
dotnet add package Kebechet.Blazor.EventHandlers.LongPress --version 2.5.2.2
                    
NuGet\Install-Package Kebechet.Blazor.EventHandlers.LongPress -Version 2.5.2.2
                    
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.LongPress" Version="2.5.2.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Kebechet.Blazor.EventHandlers.LongPress" Version="2.5.2.2" />
                    
Directory.Packages.props
<PackageReference Include="Kebechet.Blazor.EventHandlers.LongPress" />
                    
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.LongPress --version 2.5.2.2
                    
#r "nuget: Kebechet.Blazor.EventHandlers.LongPress, 2.5.2.2"
                    
#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.LongPress@2.5.2.2
                    
#: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.LongPress&version=2.5.2.2
                    
Install as a Cake Addin
#tool nuget:?package=Kebechet.Blazor.EventHandlers.LongPress&version=2.5.2.2
                    
Install as a Cake Tool

"Buy Me A Coffee"

Blazor.EventHandlers.LongPress

NuGet Version NuGet Downloads Twitter

This package contains @onlongpress event handler built from John Doherty's JS long-press

Motivation: For similar functionality you can also use oncontextmenu what is built-in event. But it doesnt work on iOS devices. Source

  • Before using the JS, I renamed triggered event name from long-press to longpress to be able to use it in Blazor.
  • I have also changed default show time from 1500ms to 400ms Based on android longpress implementation.
  • In case you would like to specify your custom time, you can do so by adding tag attribute with your custom time like:
<div @onlongpress="LongPressHandler" data-long-press-delay="100"> 

TIP: For better usage experience put: @using Blazor.EventHandlers.LongPress into _Imports.razor file.

TIP2 For combination with @onlongpress consider also use of @onshortclick

  • ⚠️ Both packages default to 400ms, but each reads its own attribute: data-long-press-delay here, data-shortclick-long-press-ms in ShortClick. Change one and change the other with it, or a press between the two limits is both a click and a long press, or neither.
    • data-long-press-delay is read from the pressed element and its ancestors up to <body>, never from <html>. Where ShortClick's attribute sits on <html>, put this one on <body>.

⚠️ Patched until upstream ships the fix

The upstream script starts its timer when its pointerdown handler runs, checks it once per animation frame, and cancels it on pointerup. A press held for the delay but released before the timer catches up therefore raises nothing: up to about 35ms past the delay on an idle phone, and much more when the main thread is busy at the touch. Paired with ShortClick, which treats such a press as not a tap, neither a click nor a long press happens.

This package ships the script built from upstream PR 57, which raises the long press on release when the events' own timestamps say it was held for the delay. A long press raised that way still swallows the following click when its handler calls preventDefault.

  • The patch goes away with the upstream release that contains it.
  • Hold_PastTheDelayWhileTheMainThreadWasBusyAtTheTouch_RaisesLongPressOnRelease in tests/Blazor.EventHandlers.LongPress.Browser.Tests fails if an update of the script drops it.

⚠️ Auto-initializers

  • In previous MAUI versions JS initializers were not working. So if you dont see text "Registering LongPress.js" in your JS console then you have to register this event handler manually in your index.html. At the end after blazor script put this:
<script defer src="_content/Kebechet.Blazor.EventHandlers.LongPress/LongPress.js"></script>
<script async>
	window.onload = function () {
		console.log("Registering LongPress.js");

		Blazor.registerCustomEventType('longpress', {
			createEventArgs: event => {
				return {
					bubbles: event.bubbles,
					cancelable: event.cancelable,
					screenX: event.detail.screenX,
					screenY: event.detail.screenY,
					clientX: event.detail.clientX,
					clientY: event.detail.clientY,
					offsetX: event.detail.offsetX,
					offsetY: event.detail.offsetY,
					pageX: event.detail.pageX,
					pageY: event.detail.pageY,
					sourceElement: event.srcElement.localName,
					targetElement: event.target.localName,
					timeStamp: event.timeStamp,
					type: event.type,
				};
			}
		});
	};
</script>
  • and then it should work as expected

Used resources

License

This repository is licensed with the MIT license.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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 is compatible.  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 is compatible.  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
2.5.2.2 394 11/24/2026
2.5.2.1 3,126 4/25/2026
2.5.2 153 4/24/2026
2.1.1 9,656 11/7/2025
2.1.0 275 11/7/2025
2.0.0 2,798 6/28/2025
1.2.0 12,223 6/23/2024
1.1.0 2,047 4/26/2024
1.0.1 1,416 7/4/2023
1.0.0-preview1 283 5/21/2023

Fixed: a press held for the delay but released before the script's timer caught up raised no longpress (with ShortClick, neither a click nor a long press happened). The release now raises it, measured by the events' own timestamps. Patch from https://github.com/john-doherty/long-press-event/pull/57, removed with the upstream release that contains it.