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-modal-state

v1.1.1

Published

This is a tiny library that manages modal in react with minimal dependency.

Downloads

34

Readme

React Modal State

This is a tiny library that manages modal in react with minimal dependency.

[!IMPORTANT]
This library is in an early stage. Any issue or feature request would be very helpful to develop this library further!

Installation

npm i react-modal-state

Usage

Place ModalProvider and ModalRenderer

import { ModalProvider, ModalRenderer } from "react-modal-state";

...

// Optionally provide modals prop to open modal by id or use self-nested modals
<ModalProvider modals={[
        ["users/:id", UserModal],
      ]}
    >
    <App />
    <ModalRenderer Component={NewUserModal} />
    <ModalRenderer Component={UserModal} />
</ModalProvider>

[!NOTE]
<ModalRenderer /> does not have to be the direct child of the <ModalProvider />. You can place <ModalRenderer /> in any node under <ModalProvider /> component tree if needed.

Open from anywhere

import { useModal } from "react-modal-state";

function Content() {
  const { open: openNewUser } = useModal(NewUserModal);
  const { open: openUser } = useModal(UserModal);
  // or alternatively, you can open modal by id defined in ModalProvider
  // const { open: openUser } = useModal("users/:id");

  return (
    <>
      <button onClick={() => openNewUser()}>New User</button>
      <button onClick={() => openUser({ id: 1, name: "Steve" })}>User 1</button>
    </>
  );
}

Declare modal component

import { useModalInstance } from "react-modal-state";

const UserModal = () => {
  const { data, isOpen, close } = useModalInstance<{
    id: number;
    name: string;
  }>();

  return (
    <Dialog open={isOpen} onClose={close}>
      <DialogTitle>User {data.id}</DialogTitle>
      <DialogContent>
        {data.name}'s profile
      </DialogContent>
    </Dialog>
  );
};

Features

  • Very small bundle footprint
  • Allow to write modal component with caller interface ignorant manner.
  • UI Framework independant. Can be used with any UI framework such as MUI or Headless UI
  • Supports nested modal of the same component using parameterized path(users/:id). for example, users/1 can be opened over users/2

Backgrounds

There are many awkward and repetitive points when using modals naively and this library tries to solve these problems.

Typical scenarios are as below.

  • Many modals require extra data to be passed when opening modal. If you start to manage more and more modals, component state which opens modal soon become very reptitive with declarations of modal state managing its open state and extra data.

  • When you need to open a parent modal from a deeply nested component, you typically have choices below.

    • Pass props to open modal down to the child component. which usually causes a problem known as a prop drilling.
    • Provide methods to manage a modal via Context API or other state management libraries. which also became tedious if you need to manage multiple modals.

Using this library, you can solve above situations elegantly.

Below is an minimal example code using this library (with dialog component from MUI)

const NewUserModal = () => {
  const { isOpen, close } = useModalInstance();

  return (
    <Dialog open={isOpen} onClose={close}>
      <DialogTitle>New User</DialogTitle>
      <DialogContent>
        <Box>Create new user</Box>
      </DialogContent>
    </Dialog>
  );
};

const UserModal = () => {
  const { data, isOpen, close } = useModalInstance<{
    id: number;
    name: string;
  }>();

  const { open: openUser } = useModal(`users/:id`);

  return (
    <Dialog open={isOpen} onClose={close}>
      <DialogTitle>User {data.id}</DialogTitle>
      <DialogContent>
        {data.name}'s profile
      </DialogContent>
    </Dialog>
  );
};


function Content() {
  const { open: openNewUser } = useModal("new-user");
  const { open: openUser } = useModal("users/:id");

  return (
    <Box>
      <button onClick={() => openNewUser()}>New User</button>
      <button onClick={() => openUser({ id: 1, name: "Steve" })}>User 1</button>
    </Box>
  );
}

function App() {
  return (
    <ModalProvider
      modals={[
        ["new-user", NewUserModal],
        ["users/:id", UserModal],
      ]}
    >
      <Content />
      <ModalRenderer Component={NewUserModal} />
      <ModalRenderer Component={UserModal} />
    </ModalProvider>
  );
}

In the above code, you can see benefits like

  • A component which is responsible to open modal does not need to manage any extra state which is needed in modal. It only calls open (and close if needed)
  • Modal component can access methods and custom properties need to render modal by itself. No need to manage props to provide data, which enables modals to be declared and implemented fully indepenently without worrying about the interface it has to provide to opening components.

Demo and Development

This is a typical vite project.

npm install
npm run dev

to run demo page.