The Challenge
You're working with reactstrap (bootstrap + react), that good old trusty css library which lets you style your views with components as against the traditional classes. From the docs, you get an example like this:
import React, {useState} from 'react';
import {Modal, ModalHeader, ModalBody, ModalFooter} from 'reactstrap';
const ModalExample = (props) => {
const {
buttonLabel,
className
} = props;
const [modal, setModal] = useState(false);
const toggle = () => setModal(!modal);
return (
<div>
<Button color="danger" onClick={toggle}>{buttonLabel}</Button>
<Modal isOpen={modal} toggle={toggle} className={className}>
<ModalHeader toggle={toggle}>Modal title</ModalHeader>
<ModalBody>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</ModalBody>
<ModalFooter>
<Button color="primary" onClick={toggle}>Do Something</Button>{' '}
<Button color="secondary" onClick={toggle}>Cancel</Button>
</ModalFooter>
</Modal>
</div>
);
}
export default ModalExample;
The example above works perfectly for most use cases, you can add the needed classes and style components as needed and boom! works like magic---until you have to resize the width and height of the modal, that is.
First attempt at resizing.
This is what I personally tried to do at first.
Step 1: Create a class with my custom width and height requirements in my stylesheet.
.custom-modal-style {
width: 897px;
height: 802px;
}
Step 2: Add style to Modal component.
import React, {useState} from 'react';
import {Modal, ModalHeader, ModalBody, ModalFooter} from 'reactstrap';
import "./styles.css";
const ModalExample = (props) => {
const [modal, setModal] = useState(false);
const toggle = () => setModal(!modal);
return (
<div>
<Button color="danger" onClick={toggle}>{buttonLabel}</Button>
<Modal isOpen={modal} toggle={toggle} className="custom-modal-style">
<ModalHeader toggle={toggle}>Modal title</ModalHeader>
<ModalBody>
...
</ModalBody>
<ModalFooter>
<Button color="primary" onClick={toggle}>Do Something</Button>{' '}
<Button color="secondary" onClick={toggle}>Cancel</Button>
</ModalFooter>
</Modal>
</div>
);
}
export default ModalExample;
But you'll notice with me that this doesn't work. Go ahead, try it(for width and height)...I'll wait.
Following my curiosity...
Upon further inspection with the Chrome dev tools, I realized that the Modal component rendered down to three nested tags:
The first 3 divs just before the div with a class of 'modal-header' show what the modal component renders down to. And then on further fiddling, I saw that the only way I could get the required change I was looking for was by adding my custom class to the div with the class of 'modal-content'.
This is not in reactstrap's docs, and there's no stated way of accessing that particular div tag.
So like every developer who wants to deliver fast, I went to stack-overflow to find hacks...long story short, they didn't work. So I did the next sane thing....
Diving into reactstrap's github repo.
On a brief perusal of the Modal component's props on Reactstrap's GitHub repo, this is what you see from lines 21-61:
const propTypes = {
isOpen: PropTypes.bool,
autoFocus: PropTypes.bool,
centered: PropTypes.bool,
scrollable: PropTypes.bool,
size: PropTypes.string,
toggle: PropTypes.func,
keyboard: PropTypes.bool,
role: PropTypes.string,
labelledBy: PropTypes.string,
backdrop: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.oneOf(['static'])
]),
onEnter: PropTypes.func,
onExit: PropTypes.func,
onOpened: PropTypes.func,
onClosed: PropTypes.func,
children: PropTypes.node,
className: PropTypes.string,
wrapClassName: PropTypes.string,
modalClassName: PropTypes.string,
backdropClassName: PropTypes.string,
contentClassName: PropTypes.string, //voila!!!!!! Got you!
external: PropTypes.node,
fade: PropTypes.bool,
cssModule: PropTypes.object,
zIndex: PropTypes.oneOfType([
PropTypes.number,
PropTypes.string,
]),
backdropTransition: FadePropTypes,
modalTransition: FadePropTypes,
innerRef: PropTypes.oneOfType([
PropTypes.object,
PropTypes.string,
PropTypes.func,
]),
unmountOnClose: PropTypes.bool,
returnFocusAfterClose: PropTypes.bool
};
These are all the props that are to be passed to the Modal component. I won't go into details about the other props because that's not what you're here for.
I saw the contentClassName prop and thought "hey, this could work..", and work it did. So my code now looked like this:
import React, {useState} from 'react';
import {Modal, ModalHeader, ModalBody, ModalFooter} from 'reactstrap';
import "./styles.css";
const ModalExample = (props) => {
const [modal, setModal] = useState(false);
const toggle = () => setModal(!modal);
return (
<div>
<Button color="danger" onClick={toggle}>{buttonLabel}</Button>
<Modal isOpen={modal} toggle={toggle} contentClassName="custom-modal-style">
<ModalHeader toggle={toggle}>Modal title</ModalHeader>
<ModalBody>
...
</ModalBody>
<ModalFooter>
<Button color="primary" onClick={toggle}>Do Something</Button>{' '}
<Button color="secondary" onClick={toggle}>Cancel</Button>
</ModalFooter>
</Modal>
</div>
);
}
export default ModalExample;
Now our Chrome dev tools look like this:
And I got the desired result!
To Summarize:
Just add contentClassName="your-custom-class" to the Modal component and you'll get your result!
Please share and like to save someone the stress!
Top comments (16)
I literally have been struggling with this for two days! Thanks!
You're welcome!
so nice , worked smoothly as butter
Just one more question, now that I increased the width
modal is no longer , centered, how to achieve that
attaching image for reference
Okay so i tried EVERYTHING to try and resize it, this example wouldn't work for me^^
I was trying to override the .modal-dialog class and nothing would work, I realised the syntax had to be the exact same as it was in my developer tools.
so i added @media (min-width: 576px) {
.modal-dialog {
max-width: fit-content;
margin: 1.75rem auto;
}
}
and this worked !!
Love this post!
Thank you! Glad you like it.
Amazing. Nice approach on finding the solution as well!
Thanks man! Helped me a lot :)
HY! thanks for this amazing editorial. One question:- I want to change the font-size of the react header. I tried using contentClassName but that's not working. Please help me with this
Great post.
For my case, I had to set modalClassName instead of contentClassName. But, this post gave me idea where to check the expected variable.
If this doesn't work for someone then please check if this css item (width) is being overridden by other css. In this case you may give preference to your css item by adding !important in your css item as below:
.custom-modal-style {
width: 600px !important;
height: 802px !important;
}
nicee
This is not working if I want to decrease the width of the modal. Any fix or I am going wrong somewhere?