31 - Modal Window for Alerts and Confirmations
Previous30 - Progress Bar for Loading or Task CompletionNext32 - Infinite Scroll for Loading More Content
Last updated
Last updated
import React from 'react';
import './Modal.css';
import ModalContent from './ModalContent';
import ModalFooter from './ModalFooter';
const Modal = ({
message,
buttons,
onClose,
onConfirm
}) => {
const [isOpen, setIsOpen] = React.useState(false);
const handleButtonClick = (button) => {
if (button === 'confirm') {
onConfirm();
} else {
onClose();
}
};
return (
<div className="modal" aria-hidden={!isOpen}>
<ModalContent>
<h5>{message}</h5>
</ModalContent>
<ModalFooter>
{buttons.map((button, index) => (
<button
key={index}
onClick={() => handleButtonClick(button)}
>
{button}
</button>
))}
</ModalFooter>
</div>
);
};
export default Modal;
import React from 'react';
const ModalContent = ({ children }) => {
return <div className="modal-content">{children}</div>;
};
export default ModalContent;import React from 'react';
const ModalFooter = ({ children }) => {
return <div className="modal-footer">{children}</div>;
};
export default ModalFooter;.modal {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
}
.modal-content {
background-color: #fefefe;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
width: 50%;
}
.modal-footer {
background-color: #f9f9f9;
padding: 10px;
border-top: 1px solid #888;
border-radius: 0 0 5px 5px;
}
.modal-footer button {
background-color: #007bff;
color: #fff;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
}
.modal-footer button:hover {
background-color: #0069d9;
}
.modal-content h5 {
margin-top: 0;
}
.modal-content p {
margin-bottom: 20px;
}
/* Media queries for responsive design */
@media (max-width: 768px) {
.modal-content {
width: 80%;
}
}
@media (max-width: 480px) {
.modal-content {
width: 100%;
}
.modal-footer {
flex-direction: column;
}
.modal-footer button {
width: 100%;
}
}