Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle "outside" click on Dialog (Modal) with material-ui

My box closes when clicking outside of the box making me lose all the input. I want my box to close only when clicking on the cancel button. I am not sure what is making it close when clicking outside. Any help?

I am using @material-ui/core

  _close() {         DeviceCreationActions.close();     }  render() {         const actions = [             <Button                 id="device-create-dialog-close"                 key="device-create-dialog-close"                 onClick={this._close}             >               {this.context.intl.formatMessage({id: 'Cancel'})}             </Button>         ];          if (0 < this.state.stepIndex) {             actions.push(<Button                 id="device-create-dialog-back"                 key="device-create-dialog-back"                 onClick={this._previousStep.bind(this)}               >                 {this.context.intl.formatMessage({id: 'Back'})}               </Button>             );         }          if (             (1 >= this.state.stepIndex && 0 < this.state['formStep' + this.state.stepIndex].length) ||             (0 < this.state.stepIndex)         ) {             actions.push(<Button                 id="device-create-dialog-next"                 key="device-create-dialog-next"                 onClick={2 === this.state.stepIndex ? this._save.bind(this) : this._nextStep.bind(this)}               >                 {this.context.intl.formatMessage({id: 2 === this.state.stepIndex ? 'Create' : 'Next'})}               </Button>             );         } 
like image 264
rszaman Avatar asked Aug 02 '19 15:08

rszaman


People also ask

How do you close modal by clicking outside in react JS?

Simple solution. You need one touchableOpacity for clicking outside and another touchableOpacity for actual modal that will do nothing onPress. This solution was best for my purposes!

How do you prevent popup from closing when you click outside in angular?

By default, dialog can be closed by pressing Esc key and clicking the close icon on the right of dialog header. It can also be closed by clicking outside of the dialog using hide method. Set the closeOnEscape property value to false to prevent closing of the dialog when pressing Esc key.

How do I make material UI dialog full screen?

You just need to add fullScreen flag to modal component in order to achieve full screen. And if you don't want to use fullScreen, simply remove that fullScreen flag and don't need to use CSS here.


2 Answers

I think what you need is disableBackdropClick passed down to <Modal /> component

<Modal disableBackdropClick /> 

You can also disable close Dialog on Esc key press with disableEscapeKeyDown prop

like image 106
Adolfo Onrubia Avatar answered Sep 20 '22 13:09

Adolfo Onrubia


disableBackdropClick will not work in Material UI v5.

Dialog API (next)

You can use code from the migration guide to handle each closing source manually with the onClose prop by detecting the source of the closing event.

<Dialog onClose={handleClose} /> 

And use the handler to stop it

const handleClose = (event, reason) => {     if (reason && reason == "backdropClick")          return;     myCloseModal(); } 
like image 38
Vlad Pavlovski Avatar answered Sep 20 '22 13:09

Vlad Pavlovski