dynamic-modal
v1.0.5
Published
The dynamic-modal is a solution of creation differents modals into project using a json configuration file
Downloads
410
Maintainers
Readme
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.