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

@wethegit/react-modal

v3.0.0

Published

A customizable, accessible modal window component for React projects.

Downloads

498

Readme

@wethegit/react-modal

Getting set up

Install

npm install @wethegit/react-modal

CSS

Import this wherever it makes sense to, based on your project structure:

import "@wethegit/react-modal/style.css"

Usage

The useModal hook provides mechanisms for hooking into the modal state. We'll need to pass said state to the <Modal> component, as shown below. This approach allows us to abstract away state management, which becomes especially useful when using a URL hash to manage the modal's state.

The snippet below demonstrates the minimum required setup. No focus management, no animations.

import {
  Modal,
  ModalBackdrop,
  ModalContent,
  useModal
} from "@wethegit/react-modal"

function MyModal() {
  const { isOpen, toggle } = useModal()

  return (
    <>
      <button onClick={toggle}>
        Open the modal window!
      </button>

      {isOpen && (
        <Modal>
          <ModalBackdrop onClick={toggle} />
          <ModalContent>
            <button onClick={toggle}>Close</button>
            <p>Voluptate Lorem ut minim excepteur sit fugiat anim magna aliquip.</p>
          </ModalContent>
        </Modal>
      )}
    </>
  )
}

Using a URL hash to manage the modal state

It's as easy as passing a hash prop to the useModal hook. The hook will then use the URL hash to manage the modal's state. This is useful for when you want to allow your users to share a link to a modal window, or when you want to allow your users to navigate to a modal window via a URL.

import {
  Modal,
  ModalContent,
  ModalBackdrop,
  useModal
} from "@wethegit/react-modal"

function MyModal() {
  const { isOpen, toggle } = useModal({
    hash: "my-modal-hash"
  })

  return (
    <>
      <button onClick={toggle}>
        Open the modal window!
      </button>

      {isOpen && (
        <Modal>
          <ModalBackdrop onClick={toggle} />
          <ModalContent>
            <button onClick={toggle}>Close</button>
            <p>Voluptate Lorem ut minim excepteur sit fugiat anim magna aliquip.</p>
          </ModalContent>
        </Modal>
      )}
    </>
  )
}

Full example

Custom transition, focus management and hash-based state management.

Use your favorite animation library, @wethegit/react-hooks provides a simple one for these cases.

import { useRef } from 'react'
import { useAnimatePresence } from '@wethegit/react-hooks'
import {
  Modal,
  ModalContent,
  ModalBackdrop,
  useModal
} from "@wethegit/react-modal"

function MyModal() {
  const triggerButton = useRef(null)
  const modalRootRef = useRef(null)

  const { isOpen, toggle } = useModal({
    // `triggerRef` allows the focus to shift to whatever triggered the modal
    triggerRef: triggerButton,
    // `hash` will automatically open the modal when hash changes and will also update the hash when the modal opens/closes
    hash: "modal-with-hash",
  })

  const { render, animate } = useAnimatePresence({
    isVisible: isOpen,
    duration: 800
  })

  return (
    <>
      <button ref={triggerButton} onClick={toggle}>
        Open the modal window!
      </button>
      <div ref={modalRootRef}></div>

      {render && modalRootRef.current && (
        <Modal 
          renderTo={modalRootRef.current}
          style={{
          transition: `opacity 800ms ease-in-out`,
          opacity: animate ? 1 : 0
        }}>
          <ModalBackdrop onClick={toggle} />
          <ModalContent>
            <button onClick={toggle}>
              Close
            </button>
            <p>Voluptate Lorem ut minim excepteur sit fugiat anim magna aliquip.</p>
          </ModalContent>
        </Modal>
      )}
    </>
  )
}

Props

<Modal>

| prop | type | default value | description | | -------------------- | --------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | renderTo | HTMLElement | null | If a valid HTMLElement is provided, the modal will be appended to that element. Otherwise will be rendered in place. | | className | String | null | | | children | ReactNode | null | |

<ModalContent>

A wrapper for the modal's content. This component is optional, but it's recommended to use it for styling purposes.

Any valid <div> props can be passed to this component.

<ModalBackdrop>

A wrapper for the modal's backdrop. This component is optional, but it's recommended to use it for styling purposes.

Any valid <div> props can be passed to this component.

useModal

| prop | type | default value | description | | -------------------- | --------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | hash | String | null | Optional. If provided, the hook will use the URL hash to manage the modal's state. This is useful for when you want to allow your users to share a link to a modal window, or when you want to allow your users to navigate to a modal window via a URL. | | triggerRef | RefObject | null | Optional. If provided, the hook will shift the user's focus to this element when the modal closes. |

Styling

The component comes with minimal and sensible defaults.

You can override these as you see fit and as your setup allows.

Accessibility

This component was built with focus accessibility best-practices at top-of-mind, and provides enough flexibility to allow you to create an accessible modal window.

Focus loop

There are hidden elements at the start and end of the modal component, which, on focus, shift the user's focus to either the end or start of the content, respectively.

Focus on close

The triggerRef prop of the useModal hook ensures that—on close of a modal—your user's focus is shifted back to where it was before opening the modal.