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

@zemd/react-modals

v1.0.1

Published

A lightweight React modal management library.

Downloads

164

Readme

React Modals

A lightweight React modal management library

npm version

This library gives you a simple way to manage modals in React. You define your modal components, register them with createModal, and open/close them from anywhere in your app — no context providers, no prop drilling. It uses useSyncExternalStore under the hood and works well with React 19 and Next.js.

Check out working examples in the examples folder, including a Next.js 16 demo.

Features

  • Simple createModal API — define once, use anywhere
  • Built-in support for lazy-loaded modals via React.lazy
  • Works with useSyncExternalStore — no extra providers needed
  • Full TypeScript support with typed modal props
  • SSR-friendly (renders nothing on the server)
  • Lifecycle callbacks (onOpen, onClose)
  • You can implement your own store for advanced use cases
  • Minimal bundle size, zero dependencies

Installation

npm install @zemd/react-modals
pnpm add @zemd/react-modals
yarn add @zemd/react-modals

Quick Start

1. Add ModalRoot to your layout

Place <ModalRoot /> somewhere near the root of your app. It renders active modals into a portal.

import { ModalRoot } from "@zemd/react-modals";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        {children}
        <ModalRoot />
      </body>
    </html>
  );
}

2. Create a modal component

Write a regular React component. Use useModalContext to get a close function.

"use client";

import { useEffect, useRef } from "react";
import { useModalContext } from "@zemd/react-modals";

type Props = {
  title: string;
  message: string;
};

export const AlertModal: React.FC<Props> = ({ title, message }) => {
  const { close } = useModalContext();
  const dialogRef = useRef<HTMLDialogElement>(null);

  useEffect(() => {
    dialogRef.current?.showModal();
  }, []);

  return (
    <dialog ref={dialogRef} onCancel={close}>
      <h2>{title}</h2>
      <p>{message}</p>
      <button onClick={close}>OK</button>
    </dialog>
  );
};

3. Register and use

import { createModal } from "@zemd/react-modals";
import { AlertModal } from "./AlertModal";

const alertModal = createModal({
  component: AlertModal,
});

// Open from anywhere — no hooks, no context
alertModal.open({ title: "Hello!", message: "This is a modal." });

// Close the last opened instance
alertModal.close();

Lazy loading

You can lazy-load modal components to keep your initial bundle small:

import { createModal } from "@zemd/react-modals";

const confirmModal = createModal({
  lazy: () => import("./ConfirmModal").then((m) => ({ default: m.ConfirmModal })),
});

confirmModal.open({ message: "Are you sure?" });

API Reference

createModal(options)

Creates a modal controller with open and close methods.

Options:

  • component — the React component to render as a modal
  • lazy — a function returning a dynamic import (alternative to component)
  • onOpen — callback fired when the modal opens
  • onClose — callback fired when the modal closes
  • store — custom store instance (optional)

Returns: { open, close }

  • open(props?) — opens the modal, returns a unique ID
  • close(id?) — closes a specific modal by ID, or the last opened one

<ModalRoot />

Renders all active modals into a portal. Place it once in your layout.

Props:

  • container — custom DOM element or function returning one (defaults to document.body)
  • store — custom store instance (optional)

useModalContext()

A hook available inside modal components. Returns { entry, close }.

  • entry — the current modal entry (id, component, props)
  • close() — closes this modal

useModals(options?)

A hook that returns the current list of active modal entries. Uses useSyncExternalStore.

createStore(options?)

Creates a standalone modal store. Useful when you need multiple independent modal stacks.

Options:

  • maxStackSize — maximum number of modals allowed in the stack (default: 100)

License

This project is licensed under the Apache License 2.0.

💙 💛 Donate

Support Ukraine