Giter VIP home page Giter VIP logo

focus-trap's Introduction

focus-trap

Add focus traps around elements.

Adding a focus traps is important part of making your site accessible. The most common example where you want to set up a focus trap is a modal. While modal is open, the user shouldn't be able to tab outside of it, and focus should be locked within the modal elements. You have to remember to make sure there is always a way how to exit this focus trap. In modal example it's usually the escape key, or close button in the corner.

Demo

Install

Using npm

npm install @daniel.husar/focus-trap

Using yarn

yarn add @daniel.husar/focus-trap

Usage

import FocusTrap from '@daniel.husar/focus-trap';
// Setup trap
const trap = new FocusTrap({ node: document.querySelector('#my-trap') });
// Release trap
trap.restore();

Constructor API

Constructor accept object with those properties:

new FocusTrap({
  node: document.querySelector('#my-trap') }),
  firstElement: () => document.querySelector('#first-element'),
  lastElement: () => document.querySelector('#last-element'),
  disableStartingTrap: () => false,
  disableEndingTrap: () => false,
});

node

Type: DOM element

Default: none

Required: true

This is the DOM node you want to set-up focus trap around

firstElement

Type: Function

Default: () => null

Required: none

Function that returns element that will replace first element when focus will jump back at the start. Usefull when you want to connect 2 focus traps together. Will fallback to firstElement in focus trap if function returns falsy value.

lastElement

Type: Function

Default: () => null

Required: none

Function that returns element that will replace last element when focus will jump back at the end. Usefull when you want to connect 2 focus traps together. Will fallback to lastElement in focus trap if function returns falsy value.

disableStartingTrap

Type: Function

Default: () => false

Required: none

Function that returns boolean if the starting trap should be disabled.

disableEndingTrap

Type: Function

Default: () => false

Required: none

Function that can returns boolean if the ending trap should be disabled.

Interface

recalculateFocusableElements

Type: Function

Recalculate focusable elements inside of your trap. Usefull if content of the node changes over time.

restore

Type: Function

Remove the focus trap from element.

License

MIT © Daniel Husar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.