reactstrap icon indicating copy to clipboard operation
reactstrap copied to clipboard

Modal click events bubble up in certain cases

Open seungprk opened this issue 4 years ago • 1 comments

  • components: Modal
  • reactstrap version #9.0.1
  • react version #17.0.2
  • bootstrap version #5.1.3

Can repro in older versions also

What is happening?

Modal click events bubble up the react tree if you (1) click down within the modal and let go outside the modal or (2) click down within a button and let go outside of the initially clicked button

What should be happening?

Modal click events bubbling are handled consistently, i.e. do not bubble up

Steps to reproduce issue

  1. Setup clean project using reactstrap readme "Getting Started" instructions
  2. Use Modal example from reactstrap repo, copy over to the new project
  3. In the web browser, do one of the following: a. Click down from within the Modal and let go outside the Modal b. Click down from a button within the Modal and let go somewhere outside the initial button

Code

Stackblitz: https://stackblitz.com/edit/reactstrap-4tcn3n?file=Example.js

class Example extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      modal: false
    };

    this.toggle = this.toggle.bind(this);
  }

  toggle() {
    this.setState({
      modal: !this.state.modal
    });
  }

  render() {
    return (
      <div>
        <Button color="danger" onClick={this.toggle}>Open Modal</Button>
        <div onClick={e => alert('event bubbled')}>
          <Modal isOpen={this.state.modal} toggle={this.toggle} className={this.props.className}>
            <ModalHeader toggle={this.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={this.toggle}>Do Something</Button>{' '}
              <Button color="secondary" onClick={this.toggle}>Cancel</Button>
            </ModalFooter>
          </Modal>
        </div>
      </div>
    );
  }
}

Do step 3 with stackblitz and the alert will fire

seungprk avatar Dec 23 '21 01:12 seungprk

It looks like this is the relevant line of code. If I understand correctly, the Modal and its children are nested inside the Fade component, so any click events inside or outside the modal trigger handleBackdropClick function when the modal is active.

seungprk avatar Dec 23 '21 02:12 seungprk