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

react-modal_by_cl

v1.1.7

Published

React Modal Component

Downloads

12

Readme

React Modal Component

A reusable, accessible modal dialog implementation for React applications. This component supports features like focus trapping, keyboard accessibility, and overlay click handling.

Table of Contents

  1. Features
  2. Props
  3. Usage
  4. Accessibility
  5. Key Functions
  6. Styling
  7. Best Practices
  8. Troubleshooting
  9. License

Features

  • Close the modal using the Escape key.
  • Focus trapping within the modal when open.
  • Close the modal by clicking on the overlay (background).
  • Fully customizable through props for styling and behavior.

Props

|Prop Name|Type|Default Value|Description| |---------|----|-------------|-----------| |isOpen |boolean|undefined|Controls whether the modal is open. Pass true to open and false to close.| |onClose |function|undefined|Callback function executed when the modal is closed. Required for handling close actions| |children |node|undefined|Content to display inside the modal.| |overlayClassName |string|'overlay'|Class name for the overlay. Use for custom styling.| |modalClassName |string|'modal'|Class name for the modal container. Use for custom styling.| |buttonClassName |string|'closeButton'|Class name for the close button. Use for custom styling.|

Usage

Basic Example

import React, { useState } from 'react';
import Modal from './Modal';
import './style.css';

const App = () => {
  const [isModalOpen, setIsModalOpen] = useState(false);

  return (
    <div>
      <button onClick={() => setIsModalOpen(true)}>Open Modal</button>
      <Modal isOpen={isModalOpen} onClose={() => setIsModalOpen(false)}>
        <h1>Modal Content</h1>
        <p>This is an example of modal content.</p>
      </Modal>
    </div>
  );
};

export default App;

Custom Styling Example

<Modal
  isOpen={isModalOpen}
  onClose={() => setIsModalOpen(false)}
  overlayClassName="customOverlay"
  modalClassName="customModal"
  buttonClassName="customCloseButton"
>
  <h1>Custom Styled Modal</h1>
</Modal>

Accessibility

  1. Keyboard Navigation:
    • Press Escape to close the modal
    • Use Tab and Shift + Tab to navigate within the modal content.
  2. ARIA Attributes:
    • role="dialog" and aria-modal="true" are used to ensure compatibility with screen readers.

Key Functions

handleKeyDown Handles keyboard interactions, including:

  • Closing the modal with the Escape key.
  • Managing focus trapping with the Tab key.

trapFocus Ensures focus remains within the modal by cycling through focusable elements.

handleOverlayClick Closes the modal if a click occurs on the overlay (background).

Styling

The component is fully customizable through CSS class names provided via props.

Default Styles

overlay: {
    position: 'fixed',
    top: 0,
    left: 0,
    width: '100%',
    height: '100%',
    backgroundColor: 'rgba(0, 0, 0, 0.5)',
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center',
    zIndex: 1000,
  },
  modal: {
    position: 'relative',
  },
  closeButton: {
    position: 'absolute',
    top: '-10px',
    right: '-10px',
    borderRadius: '100%',
    border: 'none',
    cursor: 'pointer',
  },

Default CSS

.modal {
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  text-align: center;
  padding: 20px;
}

.closeButton {
  background: black;
  color: white;
  font-size: 20px;
}

Custom CSS

Use the overlayClassName, modalClassName, and buttonClassName props to apply your custom styles.

Best Practices

  1. Provide Required Props:
    • Ensure isOpen and onClose are passed to control modal behavior properly.
  2. Test Focus Management:
    • Verify focus trapping with Tab navigation in various scenarios.
  3. Customize Styles:
    • Match the modal's appearance to your application's theme using the class name props.
  4. Ensure Accessibility:
    • Validate ARIA attributes and focus trapping for assistive technologies.

Troubleshooting

  1. Modal Not Closing on Escape Key
    • Verify onClose is passed and correctly implemented
  2. Focus Not Trapped:
    • Check that all focusable elements inside the modal are correctly defined.
  3. Overlay Click Not Working:
    • Ensure handleOverlayClick is triggered by checking event propagation.

Notes

  • Manage the isOpen state in the parent component to avoid unintended behavior.

License

This component is provided under the MIT license. Feel free to use and modify it as needed.