import React, { useState } from 'react'; import ReactDOM from 'react-dom'; const Modal = ({ isOpen, onClose, children }) => { if (!isOpen) return null; return ReactDOM.createPortal( <div className="modal-overlay"> <div className="modal"> <button className="close-button" onClick={onClose}>Close</button> {children} </div> </div>, document.getElementById('modal-root') ); }; const App = () => { const [isModalOpen, setIsModalOpen] = useState(false); const openModal = () => { setIsModalOpen(true); }; const closeModal = () => { setIsModalOpen(false); }; return ( <div> <button onClick={openModal}>Open Modal</button> <Modal isOpen={isModalOpen} onClose={closeModal}> <h2>Modal Content</h2> <p>This is a simple modal</p> </Modal> </div> ); }; ReactDOM.render(<App />, document.getElementById('root'));
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter