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

@soulpicks/react-spring-modal

v1.0.9

Published

Animatable and accessible modals built with react-spring

Downloads

3

Readme

react-spring-modal

A component library for animatable and accessible modals built with react-spring. Built to be composable to create any transition that matches your needs.

✅ Supports SSR ✅ Handles focus restoration ✅ Prevents focus on covered content via inert attribute; includes polyfill ✅ Animatable via react-spring's useTransition

Usage

Installation

When you install react-spring-modal you'll need to make sure that you have also installed react, react-dom and react-spring.

Note: this package uses React's hooks feature, so you'll need to have at least [email protected] or up.

pnpm i react-spring-modal react react-dom react-spring

# or

yarn add react-spring-modal react react-dom react-spring

Example

To use this package you'll need to choose a modal and import the CSS file.

import { useState } from 'react';
import { BottomModal } from 'react-spring-modal';
import 'react-spring-modal/dist/index.css';

function App() {
  const [isOpen, setOpen] = useState(false);
  return (
    <>
      <button onClick={() => setOpen(true)}>Open modal</button>
      <BottomModal isOpen={isOpen} onRequestClose={() => setOpen(false)}>
        <h1>The Modal</h1>
      </BottomModal>
    </>
  );
}

You might notice that <BottomModal> doesn't render to anything. Due to the use of React DOM's createPortal all modals that use <ModalPortal> (i.e. the ones packaged with this module) will render to #modal-root. You'll need something like this in your HTML:

<div id="root"></div> <!-- This was probably already here -->
<div id="modal-root"></div> <!-- This is where modals will render to -->

You can also create your own modal with it's own transition:

import { useState } from 'react';
import { useTransition, animated } from 'react-spring';
import { BaseModal } from 'react-spring-modal';

function MyModal() {
  const [isOpen, setOpen] = useState(false);
  const transition = useTransition(isOpen, null, {
    from: { transform: 'scale(0)' },
    enter: { transform: 'scale(1)' },
    leave: { transform: 'scale(0)' }
  });

  return (
    <BaseModal isOpen={isOpen} onRequestClose={() => setOpen(false)}>
      {transition.map(
        ({ item, key, props }) =>
          item && (
            <animated.div key={key} style={props}>
              <h1>My Modal</h1>
            </animated.div>
          )
      )}
    </BaseModal>
  );
}

API

<ModalPortal>

Requires that an element with the id modal-root exists.

| Name | Description | | -------- | ------------------------ | | children | Rendered into the portal |

<ModalBackdrop>

Any props allowed. This element basically just renders animated.div and passes the props to it.

| Name | Type | Description | | ------- | --------------- | ----------------------------------------------------------------- | | onClick | (Event) => void | Only fires when the backdrop itself is clicked, not its children. |

<BaseModal>

For custom modals <BaseModal> should be used as it handles accessibility via the inert attribute and handles restoring focus.

| Name | Type | Description | | -------------- | ---------- | ------------------------------------------------------------------------------ | | isOpen | Boolean | Used to determine open and closed state for useTransition | | onRequestClose | () => void | Used to close the modal when clicking on the backdrop or pressing ESC (escape) |

<BottomModal> and <CenterModal>

Built-in custom modals.

  • <BottomModal> slides in from the bottom of the screen and stays attached to the bottom.
  • <CenterModal> fades in and is positioned in the center.

Shares props with <BaseModal>

| Name | Type | Description | | --------------- | -------------------------------- | ------------------------------------------ | | modalTransition | ReturnType | Replaces the transition used for the modal |

FAQ

  • Why am I getting ReferenceError: globalThis is not defined?

    You're using this library in an environment that does not support the globalThis keyword out-of-the-box. You will need to use a polyfill for globalThis to fix the error. I recommend that you use @ungap/global-this

License

License MIT © Christopher H. Brown