aravind_reddy
aravind_reddy

Reputation: 5466

stop modal from closing on clicking outside or pressing Esc key

Stop modal from closing on clicking outside or pressing Esc key

<Modal
    id='VideoPlayer'
    modalOptions={{ dismissible: false }}
    trigger={
           <VideoPlay
        className='modal-close'
        id='myBtn3'> PLAY VIDEO
      </VideoPlay>
      }
  >
    <div id='overlay' className='modal-close modal-action' data-toggle='VideoPlayer' onClick={this.handleClose}>
      <i className='material-icons close'>close</i>
    </div>
    <div className='flowplayer'>
      <video id='Player1'>
        <source type='video/webm' src='//edge.flowplayer.org/bauhaus.webm' />
        <source type='video/mp4' src='//edge.flowplayer.org/bauhaus.mp4' />
      </video>
    </div>
  </Modal>

I am using react-materialize modal http://react-materialize.github.io/#/modals

I think backdrop and keyboard are set to true by Default. So my question is how to make backdrop to static and keyboard to false. I tried various ways like using jquery but nothing is working.

Upvotes: 6

Views: 5827

Answers (3)

seebham
seebham

Reputation: 81

For someone who is struggling with this in Ant Design version >= 4, use closable for controlling close by clicking outside the modal, and use keyboard for controlling close by pressing the ESC key.

an example disabling close when pressing ESC or clicking outside the modal -

<Modal {...props} closable={false} keyboard={false} />

Check - API Docs for more detail

Upvotes: 2

aravind_reddy
aravind_reddy

Reputation: 5466

Actually with react -materialize version 1.1.1 you should have react version 16 otherwise it is causing problems so either you have to upgrade your react version to 16 then use dissmissible:false .or else modal options prop is not working with previous versions of react-materalize. If you are forced to have previous version of react then you can have the event listeners using the npm module react-onclickoutside with those i did what is required when clicked outside the react-materailze modal or when ESC key is pressed

Upvotes: 1

FacundoGFlores
FacundoGFlores

Reputation: 8108

You need to pass

dismissible: false

into the modalOptions property.

If you need to setup more options, you can check the source code:

https://github.com/react-materialize/react-materialize/blob/master/src/Modal.js

I tried it in my localhost (for some strange reason, I can't make it work on codesandbox).

Fow now:

app.js

import React, { Component } from "react";

import { Button, Modal } from "react-materialize";

class App extends Component {
  constructor() {
    super();
  }

  render() {
    return (
      <div>
        <Modal
          modalOptions={{ dismissible: false }}
          header={"header"}
          trigger={<Button>trigger</Button>}
        >
          content
        </Modal>
      </div>
    );
  }
}

export default App;

package.json

{
  "name": "test",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^16.0.0",
    "react-dom": "^16.0.0",
    "react-materialize": "^1.1.1",
    "react-scripts": "1.0.17"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

index.html

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="theme-color" content="#000000">
  <link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/css/materialize.min.css" rel="stylesheet">
  <!--
      manifest.json provides metadata used when your web app is added to the
      homescreen on Android. See https://developers.google.com/web/fundamentals/engage-and-retain/web-app-manifest/
    -->
  <link rel="manifest" href="%PUBLIC_URL%/manifest.json">
  <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
  <!--
      Notice the use of %PUBLIC_URL% in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
  <title>React App</title>
</head>

<body>
  <noscript>
    You need to enable JavaScript to run this app.
  </noscript>
  <div id="root"></div>
  <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.0/js/materialize.min.js"></script>
</body>

</html>

Upvotes: 3

Related Questions