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

react-best-modal

v1.1.2

Published

Simple is best. Accessible out of the box, tiny api, bring your own styles.

Downloads

25

Readme

react-best-modal

NPM version NPM downloads Build Status codecov

Simple is best. Accessible out of the box, tiny api, bring your own styles. Don't let libraries you use get in the way of what you want, build up, not down. React 16+ only.

Usage

npm install react-focus-lock react-best-modal --save
import React from 'react';
import BestModal from 'react-best-modal';
import { render } from 'react-dom';

const appRoot = document.getElementById('root');

const App = ({ showModal, closeModal }) => (
  <div>
    Hi Mom!
    {showModal && (
      <BestModal onRequestClose={closeModal} appRoot={appRoot}>
        <button>Hello, World!</button>
      </BestModal>
    )}
  </div>
);

render(<App />, appRoot);

NOTE: Make sure you have an element inside your modal that is focusable. Essentially, any input, button, or element with tabIndex="0". This is to ensure focus is set correctly inside the modal - while this isn't enforced, it's strongly advised.

Styling

It's up to you to style and position your modal. Want to disable scrolling content behind your modal? You have to do it yourself. There's an example implementation here.

Transitions

react-best-modal works perfectly with ReactTransitionGroup.

Accessibility

If you're driving home for the ultimate accessible modal, make sure to use aria-labelledby and aria-describedby, for title and description of the modal, like so:

<BestModal
  onRequestClose={closeModal}
  appRoot={appRoot}
  aria-labelledby="modal-title"
  aria-describedby="modal-description"
>
  <button onClick={closeModal}>close</button>
  <h2 id="modal-title">Hello, World!</h2>
  <p id="modal-description">This is a modal, hello worlding!</p>
</BestModal>

Pro tips

  • Have an element that is focusable (for example there is a button above).
  • Make sure to embrace aria-labelledby and aria-describedby.

Focus

  • Focus will be trapped inside the modal when mounted.
  • Focus will automatically jump to the modal's first element when mounted, and will return to the previous focused element when unmounted.

Multiple modals (modals in modals)

This isn't supported. It's also bad practice to show many modals at a time.

API

BestModal

import BestModal from 'react-best-modal';

Props

| prop | type | required | description | | ---------------- | -------------------------- | -------- | ----------------------------------------------------------------------------------------------- | | children | ReactNode | yes | Your modal markup. | | onRequestClose | (e: KeyboardEvent) => void | yes | Callback when the modal wants to close. Up to you to action it. | | appRoot | HTMLElement | yes | Root of your application. The modal will add and remove accessible attributes when appropriate. | | className | string | no | | | disableFocusLock | boolean | no | Disables focus lock. Useful if something else wants to trap focus. |

All other props that are valid on a HTMLElement are passed through.