Manage react popups, Modals, Lightboxes, Notifications, etc. easily
npm install react-popup-managerManage react popups, Modals, Lightboxes, Notifications, etc. easily.
render function.isOpen stateComponent should be written.An example of how using this library will simplify your code
The Old Way | The react-popup-manager Way
:-------------------------:|:-------------------------:
!image | !image
``bash`
npm install react-popup-manageror
yarn add react-popup-manager
#### Basic Example
`jsx
import React from 'react';
import { PopupProvider, usePopupManager } from 'react-popup-manager';
import Modal from 'any-modal-library';
// Your modal component
const MyModal = ({ isOpen, onClose }) => (
My Modal
);
// Component that uses the modal
const MyComponent = () => {
const popupManager = usePopupManager();
const handleOpenModal = async () => {
const { response } = popupManager.open(MyModal);
const result = await response;
console.log(result); // { success: true }
};
return (
// Root component with provider
const App = () => (
);
export default App;
`
`jsx
import React, { useState } from 'react';
import { usePopupManager } from 'react-popup-manager';
import Modal from 'any-modal-library';
const ConfirmationModal = ({ isOpen, onClose, message }) => (
{message}
);
const TodoList = () => {
const [todos, setTodos] = useState(['Task 1', 'Task 2']);
const popupManager = usePopupManager();
const handleDeleteTodo = async (index) => {
const { response } = popupManager.open(ConfirmationModal, {
message: 'Are you sure you want to delete this task?'
});
const result = await response;
if (result.confirmed) {
setTodos(todos.filter((_, i) => i !== index));
}
};
return (
export default TodoList;
`
#### usePopupManager()
Returns the popup manager instance with methods to control popups.
`jsx`
const popupManager = usePopupManager();
#### PopupProvider
A React context provider that should wrap your application.
`jsx`
#### open(componentClass, popupProps)
Opens a popup and renders the popup component.
Parameters:
* componentClass: Component to renderpopupProps
* (optional): Props passed to the popup componentonClose
* (deprecated): Legacy callback method, use response insteadisOpen
> Note: prop is not allowed and will be managed internally
Returns:
`typescript`
{
close: (...args: any[]) => void; // Closes the popup
unmount: () => void; // Removes popup from DOM
response: Promise
}
Response Resolution:
The response promise resolves with:`jsx
// In your modal:
onClose({ data: 'success' });
// In your component:
const { response } = popupManager.open(MyModal);
const result = await response;
console.log(result); // { data: 'success' }
`
#### closeAll()
Closes all open popups.
#### Before (Deprecated)
`jsx
const MyComponent = () => {
const popupManager = usePopupManager();
const handleOpenModal = () => {
popupManager.open(MyModal, {
onClose: (result) => console.log(result)
});
};
};
`
#### After (Recommended)
`jsx
const MyComponent = () => {
const popupManager = usePopupManager();
const handleOpenModal = async () => {
const { response } = popupManager.open(MyModal);
const result = await response;
console.log(result);
};
};
``
MIT