Giter Site home page Giter Site logo

pofoo / focus-trap Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 24 KB

React Focus Trap Hook for Refs. Lightweight and accessible focus trap react hook. Great for modals and dialog boxes.

License: MIT License

TypeScript 100.00%
react focus-trap react-hooks react-ref

focus-trap's Introduction

React Focus Trap Hook for Refs

Lightweight and accessible focus trap react hook. Great for modals and dialog boxes.

Install

yarn add @pofo/focus-trap
npm i @pofo/focus-trap

๐Ÿš€ Features ๐Ÿš€

  • Only enables tab movements within the focused target
  • Customize inital focus on the target
  • Resets initial focus after focused target goes away

Basic Usage

import { useRef, useState } from 'react';
import useFocusTrap from '@pofo/focus-trap';

const MyComponent = () => {
    
    const ref = useRef( null );
    const [ isActive, setIsActive ] = useState( false );

    const styles = {
        visibility: isActive ? 'visible' : 'hidden';
    }

    useFocusTrap( ref, isActive );

    return (
        <>
            <button onClick={ setIsActive( state => !state ) }>Toggle the div below!</button>
            <div style={styles} ref={ref}>
                Hi!
                <button>Try clicking Tab!</button>
                <button>har har har</button>
            </div>
            <button>You can only tab me when the div above is not active!</button>
        </>
    )
}

!IMPORTANT!

  • useFocusTrap only works if there is AT LEAST one focuable element within the target. That one focusable element should probably be a close button to close the target.
  • Your toggle button to mount and unmount the modal / dialog box needs to be an immediate sibling of your modal / dialog box (right before). This is important for both functionality and coherent HTML structure.
  • Your modal / dialog box component should have a focusable element as its last HTML element. As of now, if the user clicks onto a non focusable element within the your modal / dialog component and the next focusable element if off the target - THE FOCUS TRAP WILL BREAK.

Customizing Options

By default, nothing on the target is focused when activated. This can be customized through the third optional options parameter.

    useFocusTrap( ref, isActive, {
        initialFocus: 'first', // focus the first available focusable element on the target
    } );
}

API

useFocusTrap<T extends HTMLElement>(
    // requried
    ref: RefObject<T>,
    // required
    isActive: boolean,
    // optional
    options: {
        initialFocus?: 'first' | 'none' | number,
        tabbableElems: 'string',
    },
)
Parameter Default Type Description
ref REQUIRED RefObject<T> ref of the target
isActive REQUIRED boolean isActive state representing whether the target is on the screen
options { initialFocus: 'none', tabbableElems: '' } { initialFocus: 'first' | 'none' | number, tabbableElems: string } Optional options object.

initialFocus: When set to 'none', no element if focused on target mount. When set to 'first', the first tabbable element is given focus on the target. When set to a number, the corresponding order on the HTML tree structure within the target is given focus. So if you put 0, the first tabbable element will be given focus. 1, the next -> and so on.

tabbableElems: Specify additional tabbable elements you want to add to the query selector. For example, you can add ', object' to add the object element to the tabbable elements array. Always preface the string with , . By default, the following elements are tabbable:
'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), area[href], form, audio[controls], video[controls], [tabindex="0"]'

Extra

Plays well with useClickOutsideRef

LICENSE

MIT

focus-trap's People

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.