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-light-dialog-modal

v1.0.9

Published

A lightweight and customizable React modal component

Downloads

20

Readme

ReactLightModal

Overview

The Modal component is a customizable and accessible modal dialog built with React.

Prérequis

  • Node.js : Version 20.16.0
  • Recommended Editor : WebStorm or any other editor compatible with JavaScript/React.

Installation

   npm i react-light-dialog-modal
   yarn add react-light-dialog-modal

Props

id (string, requis)

A unique identifier for the modal element. This ID is used to control accessibility and the behavior of the modal.

isOpen (bool, requis)

A boolean that determines whether the modal is open or closed. When true, the modal is displayed; when false, it is hidden.

children (node)

The content to display inside the modal. This can include any valid React element.

className (string)

Additional classes to apply for customizing the modal's styles.

onClose (func, requis)

A function called when the modal should close. This can be triggered by a click outside the modal, pressing the "Escape" key, or clicking the close button inside the modal.

ariaLabelledby (string, requis)

An ARIA attribute specifying the ID of the element that labels the modal. This improves accessibility for screen readers.

ariaDescribedby (string, requis)

An ARIA attribute specifying the ID of the element that describes the modal. This also enhances accessibility.

size (string or number)

Defines the width of the modal. It accepts percentage values, pixels, or any valid CSS width unit. The default size of the modal is set to 80%, meaning it occupies 80% of its container's width.

Behavior and Lifecycle

Effect Management

Le hook useEffect est utilisé pour ajouter des écouteurs d'événements qui gèrent les clics à l'extérieur de la modal et détectent lorsque la touche "Échap" est pressée. Ces écouteurs sont attachés lorsque la modal est ouverte et retirés lorsqu'elle est fermée ou que le composant est démonté.

  • Click Outside : If a user clicks outside the modal content, the onClose function is called to close the modal.
  • Escape Key : If the user presses the "Escape" key, the onClose function is triggered to close the modal.

Size Management

The determineSize function dynamically calculates the width of the modal based on the size prop:

  • If size is a number, it is converted to pixels.
  • If size is a string, it checks if the string is numeric and appends "px" if necessary.
  • If no valid size is provided, the modal defaults to 80% width.

Accessibility

  • The modal includes aria-modal, aria-labelledby, and aria-describedby attributes to ensure it is accessible to users with disabilities.
  • The modal is focusable (tabIndex="-1") to ensure it captures focus when opened.

Rendering

The content of the modal is rendered using ReactDOM.createPortal, allowing it to be placed outside the main React component hierarchy, typically at the root of the document body. This ensures that the modal appears above other content without disrupting the page layout.

How to use

import { Modal } from "react-light-dialog-modal";

const MyComponent = () => {
  const [isModalOpen, setIsModalOpen] = useState(false);
  
  const openModal = () => {
    setIsModalOpen(true);
  };

  const handleCloseModal = () => {
    setIsModalOpen(false);
  };
  
  return (
    <Modal
        id="modalId"
        className={"class"}
        isOpen={isModalOpen}
        onClose={handleCloseModal}
        size={70}
        ariaDescribedby={"modalDescribedby"}
        ariaLabelledby={"modalariaLabelledby"}
      >
        Modal Content
    </Modal>
  )
}