@whop-core/modal-manager
TypeScript icon, indicating that this package has built-in type declarations

0.0.1-canary.60 • Public • Published

@whop-core/modal-manager

A simple utility for managing modals in React. Based on nice-modal-react

Setup

ModalManager must be mounted at the root of the application to work correctly.

// layout.tsx

import { ModalManager } from "@whop-core/modal-manager";

interface RootLayoutProps {
  children: ReactNode;
}

export default function RootLayout({ children }: RootLayoutProps) {
  return (
    <html lang="en">
      <body>
        <main>
          <ModalManager>{children}</ModalManager>
        </main>
      </body>
    </html>
  );
}

This helper file has to be created due to the way pnpm module resolution works and how while @whop-core/modal-manager and the next app use the same frosted version they could be different instances and thus not share the same context.

// create-dialog.ts

import { Dialog } from "frosted-ui";

import { create } from "@whop-core/modal-manager";

export const createDialog = create(Dialog.Root);

Declaration

// example-modal.tsx
import { Dialog } from "frosted-ui";
import { createDialog } from "./create-dialog";
import { useModal } from "@whop-core/modal-manager";

interface ExampleModalProps {
  id: string;
}

export const ExampleModal = createDialog<ExampleModalProps>(({ id }) => {
  const modal = useModal();

  return (
    <Dialog.Content>
      <Dialog.Title>{id}</Dialog.Title>
      <Button onClick={modal.remove}>Close</Button>
    </Dialog.Content>
  );
});

Usage

This is the programmatic way to show a modal

import { showModal } from "@whop-core/modal-manager";
import { ExampleModal } from "./example-modal";

const ExamplePage = () => {
  return (
    <Button onClick={() => showModal(ExampleModal, { id: "example" })}>
      Show Modal
    </Button>
  );
};

This is the declarative way to show a modal which will automatically show the modal when the component is mounted

import { ExampleModal } from "./example-modal";

const OtherPage = () => {
  return <ExampleModal id="example" modalId="example-modal" defaultVisible />;
};

Readme

Keywords

none

Package Sidebar

Install

npm i @whop-core/modal-manager

Weekly Downloads

58

Version

0.0.1-canary.60

License

ISC

Unpacked Size

48 kB

Total Files

6

Last publish

Collaborators

  • jjantschulev
  • baked-developer