1.0.13 • Published 6 months ago

dynamic-modal v1.0.13

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
6 months ago

dynamic-modal

dynamic-modal is a TypeScript library for creating reusable modals in React and Next.js applications. It uses JSON objects to configure the modal structure, eliminating the need to write HTML. This approach simplifies modal creation and customization, allowing you to open and close modals using a custom hook.

Requirements

To use dynamic-modal properly, ensure you have the following dependencies installed:

  • React 18
  • React-DOM 18
  • react-hook-form
  • NextUI

Additionally, dynamic-modal is compatible with Next.js 14.

Installation

Install the library via npm:

npm install dynamic-modal

Setup for Next.js 14

In the main provider of your Next.js application, wrap your app with the NextUIProvider component to ensure dynamic-modal functions properly. Here’s an example:

import { NextUIProvider } from '@nextui-org/react';

function Provider({ children }: Readonly<{ children: ReactNode }>) {
  return (
    <NextUIProvider>
      <Component {...pageProps} />
    </NextUIProvider>
  );
}

export default Provider;

In the root layout define portal for modal (this component use react portal)

//imports...

export default function RootLayout ({
  children
}: Readonly<{
  children: ReactNode;
}>) {
  return (
    <html lang="en">
      <body className={inter.className}>
        <Provider>
          {children}
        </Provider>
        <div id='modal-portal'/>
      </body>
    </html>
  )
}

Setup for Next.js 13

Edit file named _document.tsx and define portal for modal (this component use react portal)

import { Html, Head, Main, NextScript } from 'next/document'

export default function Document () {
  return (
    <Html>
      <Head />
      <body>
        <Main />
        <div id='modal-portal'/>
        <NextScript />
      </body>
    </Html>
  )
}

Usage

To control the modal’s open and close states, use the useModalHandler custom hook and call openModal whenever you need to display the modal.

import { useModalHandler, DynamicModal } from 'dynamic-modal';
import { Button } from '@nextui-org/react';
//Create your modal, import and use
import testModal from '@modal-config/test';

function ExampleComponent() {
  const { openModal, modalProps } = useModalHandler();

  return (
    <>
      <Button
        onClick={() => {
          openModal(testModal.default({}, (data) => {
            console.log('modal data', data);
          }));
        }}
      >
        Open modal
      </Button>

      <DynamicModal {...modalProps} />
    </>
  );
}

export default ExampleComponent;

Examples

The examples folder in the repository contains different configuration modes to help you customize your modal.

1.0.13

6 months ago

1.0.12

6 months ago

1.0.11

6 months ago

1.0.9

7 months ago

1.0.8

7 months ago

1.0.7

7 months ago

1.0.10

7 months ago

1.0.6

8 months ago

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

9 months ago

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago