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 🙏

© 2026 – Pkg Stats / Ryan Hefner

use-a11y-modal

v0.7.1

Published

Small, accessible React modal hook with focus and inert background management.

Readme

use-a11y-modal

npm version npm downloads Bun CI license

Create accessible React modals with managed portals, focus, inert backgrounds, and scroll locking with no runtime dependencies.

Installation

npm install use-a11y-modal

Other package managers:

yarn add use-a11y-modal
pnpm add use-a11y-modal
bun add use-a11y-modal

Usage

import * as React from "react";
import { useA11yModal } from "use-a11y-modal";

export function Example() {
  const [isOpen, setIsOpen] = React.useState(false);
  const { createPortal, modalProps, titleProps } = useA11yModal({
    id: "😎-id",
    isOpen,
    onClickOutside: () => setIsOpen(false),
    onEscapeKeyPress: () => setIsOpen(false),
  });

  return (
    <>
      <button type="button" onClick={() => setIsOpen(true)}>
        Open modal
      </button>

      {Array.from({ length: 50 }, (_, index) => (
        <p key={index}>Body Content</p>
      ))}

      {createPortal(
        <div className="overlay">
          <div className="modal" {...modalProps}>
            <h1 className="title" {...titleProps}>
              Modal Title
            </h1>
            <form>
              <label>
                Modal Field Label:
                <input />
              </label>
              <button type="button" onClick={() => setIsOpen(false)}>
                Close Modal
              </button>
              {Array.from({ length: 50 }, (_, index) => (
                <p key={index}>Modal Content</p>
              ))}
            </form>
          </div>
        </div>,
      )}
    </>
  );
}

When open, the hook creates a portal under document.body, makes its siblings inert, locks body scrolling, moves focus into the modal, and restores document state and focus when closed.

TypeScript declarations are included with the package.

API

useA11yModal(options)

Returns createPortal, modalProps, and titleProps for rendering and labeling the modal.

options

| Option | Default | Description | | ------------------ | ------- | ---------------------------------------------- | | id | — | Required unique ID applied to the dialog. | | isOpen | — | Controls portal creation and modal behavior. | | autoFocus | true | Focus the first focusable element when opened. | | onClickOutside | — | Handle mouse or touch interaction outside. | | onEscapeKeyPress | — | Handle the Escape key. |

The application controls whether the modal closes; the callbacks do not change isOpen themselves.

createPortal(children)

Renders children in the managed portal, or returns null while closed.

modalProps

Accessibility props for the dialog element.

titleProps

Props for the element that labels the dialog.

Runtime requirements

  • React and React DOM 16.8 or newer
  • An ESM-compatible runtime or bundler
  • A browser with native inert support

Server-side imports are safe; DOM work begins only after the hook mounts in a browser.

Legacy browsers

The library does not bundle an inert polyfill. If your supported browser set requires one, install it in your application and import it once in the browser entry point before rendering React:

npm install wicg-inert
import "wicg-inert";

Development

Run the test suite:

bun run test

Inspect the package contents before publishing:

bun pm pack --dry-run

License

MIT