
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
react-hook-modal
Advanced tools
A custom modal, with a custom hook for modal handling in React App
A custom modal, with a custom hook for handling modals in React applications. This package allows us to centralize the logic for rendering modal components. Through a hook, we can dynamically render any coponent in this modal. This gives us flexibility in our components since we would not have to handle local states for modal rendering. For that we use the hook useModal
npm install --save react-hook-modal
The "react-hook-modal" api consists of a hook: useModal, a component containing the modals: Modal, and a HOC ModalDataContextProvider. react-hook-modal, uses the react Context api to exchange states and information to be rendered in the modal through all the components of our application.
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App/App';
////////////////////////////////////
import { ModalDataContextProvider } from 'react-hook-modal';
////////////////////////////////////
ReactDOM.render(
////////////////////////////////////
<ModalDataContextProvider>
<App />
</ModalDataContextProvider>,
////////////////////////////////////
document.getElementById('root')
);
...
import { Modal, useModal } from 'react-hook-modal';
import 'react-hook-modal/dist/index.css';
...
return (
<div className='App'>
<Header />
{/* Modal section here */}
<Modal />
</div>
);
...
import { Modal, useModal } from 'react-hook-modal';
...
const { setComponentToRender, closeModal } = useModal(onCloseModal);
function onCloseModal(data){
console.log("Modal closed",data);
}
return (
<button
style={{ color: '#fff' }}
type="button"
onClick={() => {
setComponentToRender(<AccountCustomComponent />, {
title: 'My Account',
animation: true,
closeOnBackgroundOrEsc: false,
});
}}
>
Open Modal
</button>
)
The Modal component has props to customize the styles of the container, header and body sections. here an example of setting different styles:
<Modal
styles={{
container: {
backgroundColor: '#1C1C1C',
color: '#fff',
width: '55rem',
maxHeight: '100vh',
maxWidth: '100vw'
},
header: { backgroundColor: '#2C2C2C', color: '#fff' },
body: { maxHeight: '80vh', padding: '0rem' }
}}
/>
The Modal component has a prop called classContainer which defines a custom container Modal class but it is recommended include the following css styles.
.app-modal {
position: fixed;
max-height: 100vh;
max-width: 100vw;
z-index: 999;
top: 50%;
left: 50%;
box-shadow: 0px 0px 4px rgba(0, 0, 0, 0.2);
transform: translate(-50%, -50%);
background-color: white;
color: inherit;
width: 50rem;
}
The setComponentToRender(element: JSX.Element, options?: ModalOptions) function takes a JSX.Element which can be a component or just an 'html' and renders it inside the body of the Modal.
options allow you to dynamically set the attributes and properties of the modal to be represented.
export interface ModalOptions {
title?: string; // A title to display in the header
dataProps?: any; // An object passed to the Modal state to share data: we can access **const {dataToProps} = useModal()
width?: string | number; // Set the width for the modal
height?: string | number; // Set the height for the modal
closeOnBackgroundOrEsc?: boolean | undefined; // Closes the modal if we click outside the modal context or if we press the Esc key.
resizable?: boolean; // Make a resizable Modal
fullScreen?: boolean; // Complete viewport size
animation?: boolean; // Make a nice animation on open the modal
footer?: JSX.Element | null | undefined; // A component for the footer
darkMode?: boolean; // Enable dark mode
}
MIT © josealejandro2928
FAQs
A custom modal, with a custom hook for modal handling in React App
The npm package react-hook-modal receives a total of 11 weekly downloads. As such, react-hook-modal popularity was classified as not popular.
We found that react-hook-modal demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.