npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@pofo/focus-trap

v1.0.0

Published

React Focus Trap Hook for Refs

Downloads

287

Readme

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 🚀

  • [x] Only enables tab movements within the focused target
  • [x] Customize inital focus on the target
  • [x] 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