Git Product home page Git Product logo

modal-focus-trap's Introduction

modal-focus-trap

Build Status Downloads Version License

This is a very simple and lightweight helper function that makes it easier to set up a focus trap in a modal window. This implementation is not library specific and can therefore be used with VanillaJS, React or any other library/framework.

How to use

First import the helper function. You don't necessarily need to name it focusTrap, you can use any name you want.

import focusTrap from 'modal-focus-trap';

Set up the focus trap by passing in an HTMLElement which is your modal element. The function returns a callback you can use later on to disengage the focus trap. Cache this callback somewhere in your code, you will need it later.

const modalElement = document.querySelector('.modal');
let restoreFocus = focusTrap(modalElement);

After closing the modal, you need to disengage the focus trap and restore the focus to the previously focused element by invoking the callback:

if (restoreFocus) {
    restoreFocus();
    restoreFocus = null;
}

Using with React

You can also create a hook to use this helper with React:

import { useRef, useEffect } from 'react';
import focusTrap from 'modal-focus-trap';

export default function useFocusTrap() {
    const ref = useRef(null);

    // Create on didMount.
    useEffect(() => {
        const destroy = focusTrap(ref.current);

        // Destroy on willUnmount.
        return destroy;
    }, []);

    return ref;
}

See this sample project for more info.

modal-focus-trap's People

Stargazers

 avatar  avatar

Watchers

 avatar

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.