![]() |
VOOZH | about |
dotnet add package BlazorJS --version 2.2.0
NuGet\Install-Package BlazorJS -Version 2.2.0
<PackageReference Include="BlazorJS" Version="2.2.0" />
<PackageVersion Include="BlazorJS" Version="2.2.0" />Directory.Packages.props
<PackageReference Include="BlazorJS" />Project file
paket add BlazorJS --version 2.2.0
#r "nuget: BlazorJS, 2.2.0"
#:package BlazorJS@2.2.0
#addin nuget:?package=BlazorJS&version=2.2.0Install as a Cake Addin
#tool nuget:?package=BlazorJS&version=2.2.0Install as a Cake Tool
BlazorJS is a small package to have better interaction with JavaScript.
Add the nuget Package BlazorJS to your blazor project
Open _Imports.razorfrom your project and add using for BlazorJS
...
@using BlazorJS
The scripts component allows you to include every javascipt file easily to your pages or components.
For example open any page like the index.razor and add
<Scripts src="js/myjsfile.js"></Scripts>
This component can also load stylesheet files
<Scripts src="js/myjsfile.js,css/mystyle.css"></Scripts>
Multiple js files can be loaded with a comma seperator ,
<Scripts src="js/myjsfile.js, js/myjsfile2.js"></Scripts>
The Dynamic Invocation extension for IJSRuntime allows for dynamic invocation of JavaScript functions from C#. This extension provides a single method, DInvokeVoidAsync, which takes in two arguments: a function to be invoked and an array of objects to be passed as arguments to that function.
Usage
The DInvokeVoidAsync method can be called on any instance of IJSRuntime, and takes in general two arguments: a function to be invoked and an array of objects to be passed as arguments to that function.
Here are some examples of how the method can be used:
await jsRuntime.DInvokeVoidAsync(window => window.alert("test"));
// DONT COPY THIS!! SAMPLE FOR NOT WORKING
await jsRuntime.DInvokeVoidAsync(window => window.alert(currentCount));
await jsRuntime.DInvokeVoidAsync((window, c) => window.alert(c), currentCount);
await jsRuntime.DInvokeVoidAsync((window, c, p2, p3..) => window.alert(c), currentCount, param2, param3, ...);
await jsRuntime.DInvokeVoidAsync((window, c, x) =>
{
window.alert(c);
window.console.log(x);
}, currentCount, "Flo");
await jsRuntime.DInvokeVoidAsync(window => window.alert(currentCount), JSArgument.For(currentCount).ToArray());
await jsRuntime.DInvokeVoidAsync(window => window.alert(currentCount + " - " + date + name), JSArgument.For(currentCount).And(date).And(name));
var date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
var name = "John";
await jsRuntime.DInvokeVoidAsync(window => window.alert(currentCount + " - " + date + name), JSArgument.For(currentCount).And(date).And(name));
All seen samples are also callable with an generic arg to use return values.
var res = await jsRuntime.DInvokeAsync<string>(window => window.prompt());
Console.WriteLine(res);
var hash = await jsRuntime.DInvokeAsync<string>(window =>
{
window.alert(currentCount);
return window.location.hash + "_" + currentCount;
}, new[] { JSArgument.For(currentCount) });
await jsRuntime.DInvokeVoidAsync(document => document.location.hash = hash, new[] { JSArgument.For(hash) });
// After alerting the currentCount we update the url in browser like this #1_2_3_4...
It is important to note that the function passed to DInvokeVoidAsync must be a lambda expression that takes in a dynamic and dynamic parameter.
Conclusion This extension provides a simple and easy way to invoke JavaScript functions from C# with support for dynamic arguments.
A simple possibility to hook events that should be extended more but currently supports an easy OnBlur extension for any element.
private BlazorJSEventInterop<PointerEventArgs> _jsEvent;
_jsEvent = new BlazorJSEventInterop<PointerEventArgs>(_jsRuntime);
await _jsEvent.OnBlur(OnFocusLeft, ".element-selector");
private Task OnFocusLeft(PointerEventArgs arg)
{
return Task.CompletedTask;
}
You can also use it manually with any event you want to.
private BlazorJSEventInterop<PointerEventArgs> _jsEvent;
_jsEvent = new BlazorJSEventInterop<PointerEventArgs>(_jsRuntime);
await _jsEvent.AddEventListener("NameOfEvent", async args => { await YourCallBack(); }, ".element-selector");
BlazorJs provides a small base component called BlazorJsBaseComponent<T> to create a JS wrapper component.
This is a simple way to create a JS object reference from a module and use it in your blazor component.
<ins>YourComponent.razor</ins>
@inherits BlazorJs.BlazorJsBaseComponent<YourComponent>
<div @ref="ElementReference">
</div>
<ins>YourComponent.razor.cs</ins>
public partial class YourComponent
{
protected override string ComponentJsFile() => "./js/PathToYourComponent.js";
protected override string ComponentJsInitializeMethodName() => "initializeMethodForYourComponent";
[Parameter]
public string SomeGeneralParam { get; set; }
[Parameter, ForJs]
public int ParamForJs { get; set; } = 100;
[Parameter, ForJs("anotherParamForJsWithDifferentNameInJs")]
public int AnotherParamForJs { get; set; } = 100;
protected override async Task OnJsOptionsChanged()
{
// This method will automatically be called when a parameter marked with [ForJs] has changed
if (JsReference != null)
await JsReference.InvokeVoidAsync("setOptions", MyJsOptions());
}
private object MyJsOptions()
{
return this.AsJsObject(new
{
configValueWirthoutParam = 123,
});
}
/// <summary>
/// Gets the JavaScript arguments to pass to the component.
/// We override here because by default only the element reference abd dotnet reference is passed but we want to have directly the JsOptions available.
/// </summary>
public override object[] GetJsArguments() => new[] { ElementReference, CreateDotNetObjectReference(), MyJsOptions() };
}
ComponentJsFile() in this case ./js/PathToYourComponent.js<ins>./js/PathToYourComponent.js</ins>
class YourComponent {
elementRef;
dotnet;
constructor(elementRef, dotNet, options) {
this.elementRef = elementRef;
this.dotnet = dotNet;
this.createWhatever(options);
}
createWhatever(options) {
// Do something with the options
console.log(options.paramForJs);
console.log(options.anotherParamForJsWithDifferentNameInJs);
console.log(options.configValueWirthoutParam);
}
setOptions(options) {
// Just update the options with the new ones
}
dispose() {
// Dispose everything you created
}
}
window.YourComponent = YourComponent;
// This method will be called from the BlazorJsBaseComponent and should match the name you have defined in `ComponentJsInitializeMethodName()`
export function initializeMethodForYourComponent(elementRef, dotnet, options) {
return new YourComponent(elementRef, dotnet, options);
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 is compatible. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 netstandard2.1 is compatible. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 3 NuGet packages that depend on BlazorJS:
| Package | Downloads |
|---|---|
|
MudBlazor.Extensions
MudBlazor.Extensions is a small extension library for MudBlazor from https://mudblazor.com/ |
|
|
AuralizeBlazor
AuralizeBlazor is a wrapper component for audioMotion-analyzer. |
|
|
MudExRichTextEditor
MudExRichTextEditor is a custom reusable control that allows us to easily consume Quill combining in a MudBlazor project. |
Showing the top 2 popular GitHub repositories that depend on BlazorJS:
| Repository | Stars |
|---|---|
|
fgilde/MudBlazor.Extensions
MudBlazor.Extensions from https://www.mudex.org is a small extension for MudBlazor from https://mudblazor.com
|
|
|
fgilde/MudExRichTextEditor
Quill based RichEditor component for MudBlazor
|
| Version | Downloads | Last Updated |
|---|---|---|
| 2.2.0 | 239,256 | 1/7/2025 |
| 2.1.6 | 63,286 | 6/28/2024 |
| 2.1.5 | 62,965 | 2/2/2024 |
| 2.1.4 | 15,259 | 11/25/2023 |
| 2.1.3 | 527 | 11/24/2023 |
| 2.1.2 | 10,242 | 11/2/2023 |
| 2.0.9 | 11,516 | 9/13/2023 |
| 2.0.8 | 5,148 | 7/29/2023 |
| 2.0.7 | 623 | 7/28/2023 |
| 2.0.6 | 1,730 | 7/14/2023 |
| 2.0.5 | 837 | 6/13/2023 |
| 2.0.4 | 16,144 | 4/21/2023 |
| 2.0.3 | 6,567 | 3/2/2023 |
| 2.0.2 | 2,767 | 2/15/2023 |
| 2.0.1 | 754 | 2/8/2023 |
| 2.0.0 | 3,273 | 1/23/2023 |
| 1.0.4 | 3,041 | 12/9/2022 |
| 1.0.3 | 774 | 12/9/2022 |
| 1.0.2 | 761 | 12/9/2022 |