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-modals-store

v1.0.3

Published

A lightweight, type-safe hooks & functions for managing multiple modals in React

Downloads

216

Readme

react-modals-store

A tiny, type-safe, and performant global state manager for React modals.

This library provides a set of hooks and functions to manage your application's modal state without coupling your components. It uses useSyncExternalStore to ensure components only re-render when the specific modal they care about changes.

🌟 Key Features

  • Type-Safe: Link modal IDs to specific data payloads using TypeScript.
  • Performant: Uses useSyncExternalStore to prevent unnecessary re-renders.
  • Decoupled: Manages state only. Bring your own UI components.
  • Lightweight: Tiny and has no dependencies (other than React).

📦 Installation

npm install react-modals-store
yarn add react-modals-store

Peer Dependencies

This package has peer dependencies on react and use-sync-external-store.

  • If you are on React 18+: useSyncExternalStore is built-in. You are done.
  • If you are on React 16.8+ or 17: You must install the shim:
npm install use-sync-external-store
yarn add use-sync-external-store

🚀 Usage

1. Define Your Modals

Create a central type file to define all modals and the data they require.

// src/types/modals.ts

// This is the generic you will pass to the hooks
export type AppModalConfig = {
  confirmation: {
    title: string;
    onConfirm: () => void;
  };
  userProfile: {
    userId: string;
  };
  settings: undefined; // A modal with no data
};

2. Connect Your Modal Component

In your modal component, use useModalOpen and useModalData to subscribe to its state.

// src/components/UserProfileModal.tsx
import { useModalOpen, useModalData, closeModal } from "react-modals-store";
import type { AppModalConfig } from "../types/modals";

export function UserProfileModal() {
  // These hooks subscribe *only* to 'userProfile' state.
  const isOpen = useModalOpen<AppModalConfig>("userProfile");
  const data = useModalData<AppModalConfig>("userProfile");

  if (!isOpen) {
    return null;
  }

  return (
    <div className="modal-backdrop">
      <div className="modal-content">
        <h2>User Profile: {data?.userId}</h2>
        <button onClick={() => closeModal("userProfile")}>Close</button>
      </div>
    </div>
  );
}

3. Render Modals

// src/App.tsx
import { UserProfileModal } from "./components/UserProfileModal";
import { ConfirmationModal } from "./components/ConfirmationModal";

export function App() {
  return (
    <div>
      {/* Your regular app content */}
      <h1>My App</h1>
      <MyPage />

      {/* Modals are invisible by default. */}
      <UserProfileModal />
      <ConfirmationModal />
    </div>
  );
}

4. Open Modals

Call openModal function to open a modal.

// src/components/SomeButton.tsx
import { openModal } from "react-modals-store";
import type { AppModalConfig } from "../types/modals";

export function SomeButton() {
  const handleOpenProfile = () => {
    // Type-safe: TypeScript ensures the data matches 'userProfile'
    openModal<AppModalConfig>("userProfile", { userId: "abc-123" });
  };

  const handleOpenSettings = () => {
    // Works for modals with no data
    openModal<AppModalConfig>("settings");
  };

  return (
    <>
      <button onClick={handleOpenProfile}>View Profile</button>
      <button onClick={handleOpenSettings}>Open Settings</button>
    </>
  );
}

📚 API Reference

Actions

openModal<T extends ModalConfig>(modalId: keyof T, data?: T[keyof T])
Opens a modal and optionally sets its data.

closeModal(modalId: keyof T)
Closes a modal and clears its data.

Hooks

useModalOpen<T extends ModalConfig>(modalId: keyof T): boolean
Returns true or false. Subscribes the component to the modal's open state.

useModalData<T extends ModalConfig>(modalId: keyof T): T[keyof T] | undefined
Returns the modal's data. Subscribes the component to the modal's data state.

Types

ModalConfig
The base type to extend for your application's modal configuration.

📄 License

MIT