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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@vegajs/modal-controller

v1.0.0-beta.31

Published

A flexible modal management system for React applications, featuring ModalContainer, ModalProvider, and ModalController components for advanced and intuitive modal workflows.

Downloads

3

Readme

@vegajs/modal-controller

npm version

Introduction

This documentation is intended to provide a comprehensive guide to the ModalContainer, ModalProvider, and ModalController components. These components form a system for managing modal windows in your application, offering a flexible, extensible, and intuitive API for both simple and advanced modal workflows.

Installation

Install the package via npm:

npm install @vegajs/storage

ModalContainer

ModalContainer is a wrapper component that is responsible for rendering the currently active modal window on the page. It listens to the ModalController to determine which modal to display.

Props

  • controller: (Required) An instance of ModalController that manages the modal windows. This prop determines which modal is currently active and handles its lifecycle.
  • BackdropComponent: (Optional) A component that wraps around the modal content to create a backdrop effect. If not provided, the modal content will be displayed without a backdrop.

Usage

import { modalController, ModalContainer } from '@vegajs/modal-controller';

export const App = () => (
  <div>
    <ModalContainer controller={modalController} />
    <ModalShow />
  </div>
);

In this example, ModalContainer listens to the modalController to determine which modal to render. You can also provide a custom BackdropComponent for added visual effects.

ModalProvider

ModalProvider is a wrapper component that provides modal management via React context. It works with the useModal hook, allowing you to easily manage modals without explicitly passing a controller.

Usage

import { ModalProvider } from '@vegajs/modal-controller';

export const App = () => (
  <div>
    <ModalProvider>
      <ModalShowWithContext />
    </ModalProvider>
  </div>
);

With ModalProvider, any nested component can use the useModal hook to show or hide modals without direct access to a modal controller instance.

ModalController

ModalController is a class responsible for managing the entire lifecycle of modal windows. It provides a rich set of methods for displaying, hiding, and confirming modals.

API Overview

The ModalController API provides the following core methods:

  • show: Displays a new modal.
  • onClose: Closes the current modal.
  • onResolve: Completes the current modal with a result.
  • subscribe: Listens for changes to the current modal.
  • closeAll: Closes all active modals.

Methods

subscribe(listener: Listener<ModalUnit | null>): () => void

Subscribes to changes in the currently active modal and notifies listeners when the active modal changes.

Parameters:

  • listener: A function that receives the current modal state (ModalUnit | null).

Returns:

  • A function to unsubscribe from the current modal state.

show(modal: ModalUnit): EventEmitter<ResolveObject<T>>

Displays a new modal. If the modal is already in the queue, it will be moved to the top.

Parameters:

  • modal: The modal data to be displayed.

Returns:

  • An EventEmitter that can be used to subscribe to the resolution of the modal.

Example:

const modal = modalController.show(myModalData);
modal.subscribe(({ status }) => console.log(status));

onClose(): void

Closes the currently active modal. If the modal has a confirmation step, it will display the confirmation modal instead.

Example:

<button onClick={controller.onClose}>Close Modal</button>

onResolve(status: boolean, data?: T): void

Completes the current modal by providing the result of its execution. If status is true, the modal will be closed.

Parameters:

  • status: A boolean indicating whether the modal was successfully completed.
  • data: Optional data to pass to the subscribers of the modal.

Example:

<button onClick={() => controller.onResolve(true)}>Confirm</button>
<button onClick={() => controller.onResolve(false)}>Cancel</button>

closeAll(): void

Closes all active modals and unsubscribes any listeners.

Example:

controller.closeAll();

Examples

Displaying a Basic Modal

Using modalController:

modalController.show({ id: 'baseModal', component: BaseModal });

Using useModal:

import { useModal } from '@vegajs/modal-controller';

const Component = () => {
  const { show } = useModal();
  const handleShowModal = () => {
    show({ id: 'baseModal', component: BaseModal });
  };
  return <button onClick={handleShowModal}>Show Modal</button>;
};

Displaying a Modal with Confirmation

const handleShowBaseWithConfirm = () => {
  modalController
    .show({
      id: 'baseModalWithConfirm',
      component: BaseModal,
      confirmComponent: ConfirmModal,
    })
    .subscribe(({ status }) => {
      console.log(`Modal closed with status: ${status}`);
    });
};

Displaying a Modal with an Inner Modal

Base Modal:

const handleShowModalWithInnerModal = () => {
  modalController
    .show({
      id: 'withInnerModal',
      component: (props) => {
        const handlerShowInner = () => {
          modalController
            .show({
              id: 'innerModal',
              component: InnerModal,
            })
            .subscribe(({ status }) => {
              console.log(`Modal closed with status: ${status}`);
            });
        };

        return (
          <BaseModal {...props}>
            <button onClick={handlerShowInner}>Show Inner Modal</button>
          </BaseModal>
        );
      },
    })
    .subscribe(({ status }) => {
      console.log(`Modal closed with status: ${status}`);
    });
};

Advanced Usage

Nested Modals

You can use the ModalController to manage nested modals by calling show inside a modal's component. This allows you to create complex workflows, such as step-by-step wizards or confirmation dialogs within modals.

Handling Modal Completion

Use the onResolve method to handle the result of a modal. This can be useful for confirmation dialogs, where the user must either confirm or cancel an action.

Example:

const handleConfirm = () => {
  modalController.show({
    id: 'confirmDelete',
    component: ConfirmDeleteModal,
  }).subscribe(({ status }) => {
    if (status) {
      // Perform the delete action
    }
  });
};

Conclusion

The ModalContainer, ModalProvider, and ModalController components provide a powerful system for managing modals in your application. By combining these components, you can create a variety of modal workflows, from simple alerts to complex multi-step wizards. The rich API of ModalController allows you to customize and extend modal behaviors to suit your application's needs.