Blazor.FocusTrapJs 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Blazor.FocusTrapJs --version 1.0.1                
NuGet\Install-Package Blazor.FocusTrapJs -Version 1.0.1                
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.FocusTrapJs" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Blazor.FocusTrapJs --version 1.0.1                
#r "nuget: Blazor.FocusTrapJs, 1.0.1"                
#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.FocusTrapJs as a Cake Addin
#addin nuget:?package=Blazor.FocusTrapJs&version=1.0.1

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

FocusTrapJs

All documentation on this site and parameters were taken from this documentation.

Visit FocusTrapJs website

Installation

dotnet add package Blazor.FocusTrapJs

Program.cs

builder.Services.AddScoped<FocusTrapJsProvider>();

Description

This is a basic library that provides access to the focus-trap library and makes it easy to capture focus within a DOM node.

There may come a time when you find it important to catch focus inside a DOM node - so that when the user presses Tab or Shift+Tab or clicks the mouse, the user cannot escape from a particular loop of focused elements.

Examples

The template must have tabindex otherwise nothing will work.

@inherits FocusTrapComponent
@implements IAsyncDisposable

<div id="@_id">
    <div tabindex="0"></div>
</div>

@code {
    
    private string _id => $"F{GetHashCode()}";
    
    [Inject] public required FocusTrapJsProvider FocusTrap { get; set; }
    
    [Parameter] public EventCallback<bool> OnFocusChanged { get; set; }
    
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await FocusTrap.InitTrap(_id, DotObj, Settings);
        }

        await base.OnAfterRenderAsync(firstRender);
    }
    
    [JSInvokable]
    public override async ValueTask OnActivate()
    {
        await OnFocusChanged.InvokeAsync(true);
    }

    [JSInvokable]
    public override async ValueTask OnDeactivate()
    {
        await OnFocusChanged.InvokeAsync(false);
    }
 
    public async ValueTask DisposeAsync()
    {
        await FocusTrap.RemoveTrap(_id);
    }
}

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Blazor.FocusTrapJs:

Package Downloads
Blazor.HeadlessUI.Dropdown

A lightweight and customizable headless UI Dropdown component for Blazor projects.

GitHub repositories

This package is not used by any popular GitHub repositories.