» npm install react-bootstrap-modal
React Bootstrap modal
React-bootstrap Modal not opening on top of page
size - Specify/Set width and height on a react-boostrap modal - Stack Overflow
Triggering a Bootstrap Modal in React without npm react-bootstrap
Videos
In my nextjs project, I have been trying to implement a Bootstrap Modal as a component similar to this vertically centered code example.
The project structure:
components - modal.js pages - index.js - about.js
From index.js I call the modal component:
<Button variant="primary" onClick={() => setModalShow(true)}>
Launch vertically centered modal
</Button>
<ModalMailingList show={modalShow} onHide={() => setModalShow(false)} /> This results in the modal NOT opening in a window on top of my page, but rather toggle the modal as a div inside the page.
Here is the working code, of the unwanted behavior.
How can I implement the modal component and make it open ON TOP of the page? (as in the original react-bootstrap code example)
According to its documentation, you have to customize your own css class to achieve the style you want via modal's prop dialogClassName.
So we might have my.jsx code below:
<Modal dialogClassName="my-modal">
</Modal>
With my.css below:
.my-modal {
width: 90vw /* Occupy the 90% of the screen width */
max-width: 90vw;
}
Then you will have your custmized modal!
The other mentioned solution only works for setting the width.
For editing the height, you need to add your custom css class to the contentClassName attribute.
For Example:
<Modal contentClassName="modal-height"></Modal>
Css Class:
.modal-height {
height: 70%;
}
For editing the width you need to add your custom css class to the dialogClassName attribute.
For Example:
<Modal dialogClassName="modal-width"></Modal>
Css Class:
.modal-width {
width: 70%;
}
Possible Issues:
Sometimes you will have to use !important to over-ride bootstrap imposed CSS, so experiment with that as well.
Credits: Found the solution here.
You can trigger the Modal by set the "display" value instead of pass it to className directly. Bascially, replace
<div className={`modal ${modalTriggered ? "hide" : "show"}`}>
by
<div style={{ display: modalTriggered ? 'block' : 'none' }}>
will fix your issue
JS
const [quizStartModal, setQuizStartModal] = useState(false);
const showQuizModal = () => {
setQuizStartModal(true);
$('body').addClass('modal-open stop-scrolling');
};
const closeQuizModal = () => {
setQuizStartModal(false);
$('body').removeClass('modal-open stop-scrolling');
};
/* ... */
<div
className={`modal ${quizStartModal ? 'show' : ''}`}
style={{
display: quizStartModal ? 'block' : 'none',
background: 'rgba(0, 0, 0, .8)',
}}
id="quizStartModal"
data-bs-backdrop="static"
data-bs-keyboard="false"
tabIndex="-1"
aria-labelledby="quizStartModalLabel"
aria-hidden={quizStartModal ? undefined : 'true'}
aria-modal={quizStartModal ? 'true' : undefined}
role={quizStartModal ? 'dialog' : undefined}>
CSS
.stop-scrolling {
height: 100%;
overflow: hidden;
}