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.1.1

Published

A lightweight React modal management library.

Readme

@zemd/react-modals

A lightweight React modal management library

npm version source

This client-side library gives you a simple way to manage modals in React. Define modal components, register module-level controllers with createModal, and open or close them from anywhere in your client code. One ModalRoot renders the stack, without application-level provider wiring or prop drilling. The store uses useSyncExternalStore and is compatible 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 from client code
  • Built-in support for lazy-loaded modals via React.lazy
  • One ModalRoot, with no provider wiring required
  • Full TypeScript support with typed modal props
  • SSR-friendly empty server snapshot
  • Lifecycle callbacks (onOpen, onClose)
  • You can implement your own store for advanced use cases
  • ESM-only package with bundled TypeScript declarations
  • No runtime dependencies beyond the React and React DOM peers

Installation

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

Compatibility

@zemd/react-modals requires React 19 and React DOM 19 or newer. It is published as ESM with TypeScript declarations.

Server-side rendering requires Node.js 22 or newer.

The published entry is marked with "use client". A Next.js Server Component layout can render ModalRoot, but code that calls open, close, store methods, or hooks must run on the client.

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

Create the controller once at module scope:

// alert-modal-controller.ts
"use client";

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

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

Call it from client code, such as an event handler:

"use client";

import { alertModal } from "./alert-modal-controller";

export function ShowAlertButton() {
  return (
    <button
      onClick={() =>
        alertModal.open({
          title: "Hello!",
          message: "This is a modal.",
        })
      }
    >
      Show alert
    </button>
  );
}

open returns the new modal's ID. Pass that ID to close(id) to close a specific instance, or call close() to close the most recently opened instance created by this controller.

Lazy loading

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

"use client";

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

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

export function openConfirmModal() {
  return 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 with the modal props when the modal opens
  • onClose — callback fired with the modal props when the modal closes
  • store — custom store instance (optional)

Returns: { open, close }

  • open(props) — opens the modal and returns a unique ID; controllers for components without props expose open()
  • close(id?) — closes a specific modal by ID, or the last instance opened by this controller

<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)

When using a custom store, pass the same instance to ModalRoot and every controller that should render through it.

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)

The returned store exposes getSnapshot, getServerSnapshot, subscribe, append, remove, removeLatest, and removeAll. Most applications should manipulate it through modal controllers and render it through ModalRoot:

"use client";

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

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

export function CustomModalRoot() {
  return <ModalRoot store={store} />;
}

Lower-level exports

Portal, ModalInstance, ModalEntryContext, ModalEntryProvider, and the related entry, store, container, and UUID types are exported for custom renderers. Most consumers should prefer ModalRoot, createModal, useModalContext, and createStore.

License

This project is licensed under the Apache License 2.0.

💙 💛 Donate

Support Ukraine