morne
morne

Reputation: 4179

Stop modal from closing on outside click

Is there anything that can be done to make keep a Modal open when clicking outside the container?

I have a password change screen and I need the Modal to ONLY close when clicking on the submitbutton. which only becomes active when certain conditions are met.

<div>
            <Modal show={this.state.show} onHide={this.handleClose}>
                <Modal.Header>
                    <Modal.Title>Change Password</Modal.Title>
                </Modal.Header>
                <Modal.Body>
                    <form className="form-horizontal" style={{margin:0}}>
                        <div className='password-heading'>This is the first time you have logged in.</div>
                        <div className='password-heading'>Please set a new password for your account.</div>
                        <br/>

                        <label className="password">Password
                            <input type={this.state.type} className="password__input" onChange={this.passwordStrength}/>
                            <span className="password__show" onClick={this.showHide}>{this.state.type === 'input' ? 'Hide' : 'Show'}</span>
                            <span className="password__strength" data-score={this.state.score} ><div className="strength_string">{this.state.strength}</div></span>
                        </label>
                        <br/>
                        <label className="password">Confirm Password
                            <input type={this.state.type} className="password__input" onChange={this.passwordStrength}/>
                        </label>

                    </form>
                    <br/>
                </Modal.Body>
                <Modal.Footer>
                    <Button onClick={this.submitPassword} disabled={this.state.isDisabled}>Submit</Button>
                </Modal.Footer>
            </Modal>
        </div>

UPDATE
Appart from adding backdrop={ 'static' } you will most likely still be able to close the modal by clicking the Escape key.
To prevent this add one more thing to your modal window: keyboard={ false }.

This should suffice in keeping the modal open.

Upvotes: 34

Views: 40304

Answers (6)

Remus.A
Remus.A

Reputation: 102

If you're using ReactModal like me you can use shouldCloseOnOverlayClick={false} like so

 <ReactModal
      open={open}
      onRequestClose={handleRequestClose}
      shouldCloseOnOverlayClick={false}
 >

ref: https://reactcommunity.org/react-modal/

Upvotes: 0

Raymond Liao
Raymond Liao

Reputation: 1795

In my case, I use React and Ant Design with the modal in my project. I found the anwser should be maskClosable={false}, please refer to the Modal document from Ant Design official website: enter image description here

Wish to help someone else.

Upvotes: 0

TobScada
TobScada

Reputation: 31

I also wanted to achieve the same thing and adding this <Modal keyboard={false} ...> worked for me.

Upvotes: 0

user19174705
user19174705

Reputation: 11

Don't pass the onHide or onClose props.

Upvotes: 1

Hammad Ali
Hammad Ali

Reputation: 39

Remove onHide from the Modal onHide={this.handleClose}

Upvotes: 3

Malcor
Malcor

Reputation: 2721

Set the modal's backdrop to static. The modal component has a prop of backdrop, set that to backdrop="static"

<div>
    <Modal show={this.state.show} onHide={this.handleClose} backdrop="static">
        <Modal.Header>
            <Modal.Title>Change Password</Modal.Title>
        </Modal.Header>
        <Modal.Body>
            <form className="form-horizontal" style={{margin:0}}>
                <div className='password-heading'>This is the first time you have logged in.</div>
                <div className='password-heading'>Please set a new password for your account.</div>
                <br/>

                <label className="password">Password
                    <input type={this.state.type} className="password__input" onChange={this.passwordStrength}/>
                    <span className="password__show" onClick={this.showHide}>{this.state.type === 'input' ? 'Hide' : 'Show'}</span>
                    <span className="password__strength" data-score={this.state.score}>
                        <div className="strength_string">{this.state.strength}</div>
                    </span>
                </label>
                <br/>
                <label className="password">Confirm Password
                    <input type={this.state.type} className="password__input" onChange={this.passwordStrength}/>
                </label>

            </form>
            <br/>
        </Modal.Body>
        <Modal.Footer>
            <Button onClick={this.submitPassword} disabled={this.state.isDisabled}>Submit</Button>
        </Modal.Footer>
    </Modal>
</div>

From the documentation:

Specify 'static' for a backdrop that doesn't trigger an "onHide" when clicked. react-bootstrap

Upvotes: 66

Related Questions