Socket
Socket
Sign inDemoInstall

a11y-focus-trap

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    a11y-focus-trap

A simple module to trap the tab navigation inside a container very useful for modal and dialog.


Version published
Maintainers
1
Install size
3.37 kB
Created

Readme

Source

A11y Focus trap

A simple javascript module to trap focus in an HTML element and loop de navigation that is made using the tab key inside this element. This pretty useful for modal or alert element and help your navigation to be accessible by users with disabilities.

Configuration

Install using NPM

npm install a11y-focus-trap

You can also clone the github repo and insert it directly in your project via the script tag.

Usage

Import the module in the desired file

var a11yFocusTrap = require('./a11y-focus-trap');

Have and HTML element where you want the focus to be trapped in such as:

<div id="modal">
    <h3>Focus need to be trap in those element</h3>
    <button id="close-modal">Close</button>
    <form>
        <div><input type="text" placeholder="First Name"/></div>
        <div><input type="text" placeholder="Last Name"/></div>
        <div><input type="submit" /></div>
    </form>
</div>

Call the trapFocus method which have two parameter. The first one is the element in which the focus will be trapped in and the second is the current active element to return the focus on once we exit the trap.

Example:

var containerElement = document.getElementById('modal');
a11yFocusTrap.trapFocus(containerElement, document.activeElement);

To cancel the focus trap, call the unTrapFocus method. This will also return the focus on the current active element that you passed as the second parameter of the trapFocus method.

Example:

a11yFocusTrap.unTrapFocus();

Thats it !

Keywords

FAQs

Last updated on 24 Sep 2018

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc