🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

Blazor.FocusTrapJs

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

Blazor.FocusTrapJs

Trap focus within a DOM node.

1.0.2
Source
NuGet
Version published
Maintainers
1
Created
Source

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);
    }
}

Keywords

focus

FAQs

Package last updated on 21 Jan 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts