Blazor.DOMEvents 1.0.0

dotnet add package Blazor.DOMEvents --version 1.0.0                
NuGet\Install-Package Blazor.DOMEvents -Version 1.0.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="Blazor.DOMEvents" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Blazor.DOMEvents --version 1.0.0                
#r "nuget: Blazor.DOMEvents, 1.0.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.
// Install Blazor.DOMEvents as a Cake Addin
#addin nuget:?package=Blazor.DOMEvents&version=1.0.0

// Install Blazor.DOMEvents as a Cake Tool
#tool nuget:?package=Blazor.DOMEvents&version=1.0.0                

Blazor.DOMEvents

Description

This project contains a razor class library with the goal to provide methods that allow you to easily attach event listeners to your DOM in both Blazor Server and WASM.

For most purposes, attaching event listeners directly to your components or their html elements is sufficient. But in some cases it can be beneficial to attach directly to your document or window.

Why not do it yourself?

While it is not complicated to achieve this goal yourself, it does involve a bit of javascript and fiddling with deserialization that you should not have to deal with. Most annoyingly, the event arguments you receive in javascript can contain circular references and a lot of information that might not be relevant to you.

The approach I took was to only serialize those properties of the event argument on the javascript side that are also present in the type of EventArgs your're expecting on the C# side.

Targets

.Net 6

Install

PM> Install-Package Blazor.DOMEvents

Usage

Add services.

builder.Services.AddDOMEventManagement();

Inject the manager into any component.

@using Blazor.DOMEvents

@inject DOMEventManager dom

Request the document or window.

var doc = await dom.Document.Value;
var window = await dom.Window.Value;

Attach an event listener.

var listener = await doc.AddEventListener<KeyboardEventArgs>("keydown");

Add an event handler to the listener.

listener.EventRaised += async (s, e) =>
{
    KeyPressed = e.Key;
    await InvokeAsync(StateHasChanged);
};

For the serialization to work, AddEventListener<TEventArgs>(string eventType) passes an instance of TEventArgs to the js code, with which it figures out which properties of the event arguments to keep. If TEventArgs posses a public parameterless constructor, the instance is created for you.

If not, you should provide an instance yourself by calling:

AddEventListener<TEventArgs>(string eventType,TEventArgs template)

Demo

WASM

License

MIT

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 was computed.  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 was computed.  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. 
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
1.0.0 237 12/9/2022