Giter Club home page Giter Club logo

react-use-focus-trap's Introduction

React Focus Trap (react-use-focus-trap)

ci

An extremely helpful React Hook to trap the focusable elements.

(The underlying Guideline: https://www.w3.org/TR/wai-aria-practices-1.1/examples/dialog-modal/dialog.html)

Improvement or feedback of any kind is always appreciated!

The Issue with Modals

When implementing Modals, people tend to forget that pro-users and users that are permanently or temporarily restricted will use other inputs than the mouse - e.g., a keyboard.

Sad Face

Now tabbing through a modal most often leads to the focus going z- or y-wise below the Modal. Good luck finding your way back then! It's a horror show. That's the point where this library comes into play. Add this hook, and lock ("trap") the focus into the modal.

Happy Face

Installation

npm install react-use-focus-trap

Usage

import React from "react";
import { useFocusTrap } from "react-use-focus-trap";

export function NiceModal() {
  const [trapRef] = useFocusTrap();

  return (
    <div class="my-weird-modal" ref={trapRef}>
      Foobar
    </div>
  );
}

Developing

There is a demo application for easy debugging included. To start developing, simply execute these commands:

npm install
npm run build
npm link
cd demo
npm install
npm start

This opens up a dev-server with a silly modal to test your code in.

react-use-focus-trap's People

Contributors

activenode avatar flowck avatar odddev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

odddev

react-use-focus-trap's Issues

Cannot Import `useFocusTrap` - ESM Export Error

I've tried to use react-use-focus-trap in a relatively standard Next.js project (https://github.com/ndom91/briefkasten/blob/main/package.json), and I get the following error when trying to visit a page where I've imported and used the package exactly as described in the README.

Usage: https://github.com/ndom91/briefkasten/blob/main/src/components/quick-add.jsx#L24

Error: SyntaxError: Cannot use import statement outside a module
image


Focusable elments in the background are technically still be focusable

We do not rely on making focusable elements in the background non-focusable. We rather make them unreachable by the default "tab key pressed"-event behavior.
This introduces two major issues:

  1. Focusing stuff through other methods than pressing tab still works (e.g. through a browser extension like Vinium)
  2. It breaks the Developer Tools (tab index does not show the "real" tab index)

Do not assume the key to focus change

Currently, we heavily rely on the assumption that tab is used to cycle forward through the focusable elements and shift + tab is used for the opposite direction.
We should rather have a solution tied to the "change focus" in favor of the "tab pressed" event.

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.