An extremely helpful React Hook to trap the focusable elements / Hello Modals! Hello a11y!

Overview

react-use-focus-trap

Everytime when people implement Modals...

...People forget that pro users as well as 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 fact that the focus goes below the Modal and then good luck finding your way back. A horror show.

That is why you can simply add this hook and you should be good of with the most basic thing you can do: Locking into the modal while it is open.

Happy Face

Installation

npm i 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>;
}

An extremely helpful React Hook to trap the focusable elements / Hello Modals! Hello a11y! Any improvement / feedback is ALWAYS appreciated!

Comments
  • Cannot Import `useFocusTrap` - ESM Export Error

    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


    opened by ndom91 7
  • Introduced a new paradigm

    Introduced a new paradigm

    Instead of focusing on the elements to focus we remove the ones not to focus. This way around has several advantages over the old approach.

    Advantages:

    • We make fewer assumptions and do not interfere with the browser configuration and implementation
      • A user might have configured another key than "tab" to change focus
      • 0 based tab-index means "follow the system order", which might or might not be "after any positive value" (it should be, but depending on the user and browser configuration, it might not)
      • Dev tools still work! (e.g. enabling "show tab-order")
      • Focus can be still moved outside of the document into the browser UI (which is debatable since in the official specs it's also not that way, but I feel that's an issue in the official specifications tbh)
      • Doesn't break tools like Vinium
    • We have less logic to maintain
    • Code is far more testable
    opened by OddDev 4
  • Do not assume the key to focus change

    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.

    opened by OddDev 3
  • Focusable elments in the background are technically still be focusable

    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)
    opened by OddDev 2
  • Fixed bugs with the tab-order-sorting

    Fixed bugs with the tab-order-sorting

    0 based tab indexes are put AFTER positive valued ones negative valued tab indexes can not be focused at all Fixed both of these behaviors Added tests to reflect these changes

    opened by OddDev 1
  • chore: Setup CI pipeline

    chore: Setup CI pipeline

    Hi @activenode, I hope you are well, and thanks for bringing this project to life.

    I've noticed that @OddDev has recently introduced unit tests (#4), and I thought we could follow up by introducing a small pipeline to ensure that newer contributions don't break the existing working code.

    I've also added a status badge, however it will start working after this PR gets merged, because the badge source is point to your username: https://github.com/activenode/react-use-focus-trap/actions/workflows/ci.yml/badge.svg.

    Kind regards, Firmino

    opened by flowck 1
  • Added a unit testing library (mocha)

    Added a unit testing library (mocha)

    Added a util file Manipulated build script to respect this Moved integer conversion function to util.js Refactored integer conversion function Added mocha Added mocha tests for util

    opened by OddDev 0
Owner
David Lorenz
Enthuastic developer with three prime focus points: Quality, Usability and Usefulness.
David Lorenz
react-dialog is a custom react hook to use a dialog easily

react-dialog react-dialog is a custom react hook to use a dialog easily. Documentation To use react-dialog follow the documentation. useDialog Returns

Levy Mateus Macedo 2 Mar 29, 2022
ESLint plugin for react-hook-form

eslint-plugin-react-hook-form react-hook-form is an awsome library which provide a neat solution for building forms. However, there are many rules for

Chuan-Tse Kao 37 Nov 22, 2022
React hook library, ready to use, written in Typescript.

usehooks-ts React hook library, ready to use, written in Typescript. npm i usehooks-ts Created by Julien Caron and maintained with ❤️ by an amazing te

Julien 2.8k Jan 5, 2023
Small (0.5 Kb) react hook for getting media breakpoints state info in runtime

tiny-use-media Small (0.5 Kb) react hook for getting media breakpoints state info in runtime Usage npm i tiny-use-media --save Adn in your react code

Valeriy Komlev 51 Dec 13, 2022
A reusable react hook that preserves a components semantic accessibility to create a visual block link.

useAccessibleBlockLink is a reusable react hook that preserves a components semantic accessibility to create a visual block link. This hook supports multiple links within the same block.

Wayfair Tech – Incubator 4 Nov 28, 2022
📋 useClipboardApi() is a React Hook that consumes Web Clipboard API.

?? use-clipboard-api useClipboardApi() is a React Hook that consumes Web Clipboard API. Table of Contents Motivation Usage Contributing Bugs and Suges

Helder B. Berto 22 Dec 15, 2022
A custom react hook to use a dialogs easily.

dialog-hook The dialog-hook is a custom react hook to use a dialog easily. First of all it is necessary to install the package in your project some co

Levy Mateus Macedo 2 Mar 29, 2022
Hello, world! :) Welcome to this project. This is a free web repository that you can use as a blog for your website. This project is dedicated to "Sina Sattari" as an honorary title.

QURNO News/Technology Web Application Greetings and courtesy to all of you dear colleagues and friends of the DarkDragons team. We came back with anot

DarkDragons Team 12 Sep 8, 2022
React tooltip is a React.JS Component that brings usefull UX and UI information in selected elements of your website.

React Tooltip ✅ React tooltip is a React.JS Component that brings usefull UX and UI information in elements of your website. Installation ⌨️ React Too

Marc Ramos 1 Dec 22, 2021
This hook allows you to isolate and manage the state within the component, reducing rendering operations and keeping the source code concise.

React Hook Component State This hook allows you to isolate and manage the state within the component, reducing rendering operations and keeping the so

Yongwoo Jung 2 May 15, 2022
A hook based project created during 20-Dec week ReactJS workshop

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

Nabendu 1 Dec 25, 2021
🌐 Unlimited free google translate component & hook

React Native Translator Preview Introduce Free translate component & hook Never need api key, This library is Unlimited free Support translators Googl

Hyun 34 Dec 18, 2022
Mirrors the functionality of Apollo client's useQuery hook, but with a "query" being any async function rather than GQL statement.

useAsyncQuery Mirrors the functionality of Apollo client's useQuery hook, but with a "query" being any async function rather than GQL statement. Usage

Alasdair McLeay 7 Nov 16, 2022
Script to remove unnecessary properties from package.json on prepublish hook

clean-pkg-json Script to remove unnecessary properties from package.json on prepublish hook. Support this project by ⭐️ starring and sharing it. Follo

hiroki osame 37 Oct 16, 2022
An easy hook to use with expo-calendar library!

useCalendar Hook ?? Updated to Expo SDK45 This is an easy hook to use expo-calendar. It allows you: Get access permission to calendar Open settings to

AtilaDev 12 Nov 1, 2022
Recoil is an experimental state management library for React apps. It provides several capabilities that are difficult to achieve with React alone, while being compatible with the newest features of React.

Recoil · Recoil is an experimental set of utilities for state management with React. Please see the website: https://recoiljs.org Installation The Rec

Facebook Experimental 18.2k Jan 8, 2023
A simple component for making elements draggable.

React Draggable Component A simple component for making elements draggable. Demo stackblitz Installation $ npm install drag-react # yarn add drag-reac

Haikel Fazzani 9 Sep 17, 2022
React Starter Kit — isomorphic web app boilerplate (Node.js, Express, GraphQL, React.js, Babel, PostCSS, Webpack, Browsersync)

React Starter Kit — "isomorphic" web app boilerplate React Starter Kit is an opinionated boilerplate for web development built on top of Node.js, Expr

Kriasoft 21.7k Dec 30, 2022
📋 React Hooks for forms validation (Web + React Native)

English | 繁中 | 简中 | 日本語 | 한국어 | Français | Italiano | Português | Español | Русский | Deutsch | Türkçe Features Built with performance and DX in mind

React Hook Form 32.4k Dec 29, 2022