Skip to content

fix(Modal): fix bug where closing modal removed wrong modal-open string in class#410

Merged
eddywashere merged 3 commits intoreactstrap:masterfrom
markogresak:master
May 14, 2017
Merged

fix(Modal): fix bug where closing modal removed wrong modal-open string in class#410
eddywashere merged 3 commits intoreactstrap:masterfrom
markogresak:master

Conversation

@markogresak
Copy link
Copy Markdown
Contributor

Initially, I wanted to just report the issue, but while writing it, I found out that it was an easy fix so I did it myself.
Here is what I wanted to report:

Issue description

  • version: tested on#4.5.0 but this issue can be found in the master branch
  • components: Modal

Steps to reproduce issue

  1. add a class to the body which containsmodal-open, e.g.: my-modal-opened
  2. add a Modal element
  3. show the modal, which will add the modal-open class to the body, the body class property is now my-modal-opened modal-open
  4. hide the modal, expecting to return body class to my-modal-opened, but instead, the class is now my-ed modal-open

Demo: http://codepen.io/anon/pen/pPWVyP

Comment thread src/Modal.js Outdated

const classes = document.body.className.replace('modal-open', '');
// Use regex to prevent matching `modal-open` as part of a different class, e.g. `my-modal-opened`
const classes = document.body.className.replace(/(^| )modal-open( |$)/, '');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best to use word boundaries \b because this will turn my-modal-opened modal-open modal-opened into my-modal-openedmodal-opened which is not wanted. This is because it removes the space from both sides.
/\bmodal-open\b/ would result in my-modal-opened modal-opened. There is double space, but that isn't going to break anything (technically, the current implementation would result in double spaces as well).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch!
This was adapted from Element.classList polyfill.
I guess I could also replace with ' ' instead of ''.
If you would opt for using \b, I would at least follow the .replace(/\bmodal-open\b/, '') with .replace(/\s{2,}/g, ' '), i.e. replace all occurrences of 2 or more spaces with a single space. Otherwise, it looks weird and could lead to a broken test case if someone would expect single space and would get 2 spaces.

I would also suggest using document.body.classList in the future. Bootstrap 4 supports only IE10+ and classList is already supported in IE10. With classList.remove, there would be no such problem in the first place 😄

…ng in class

Example:
initial body class: `no-modal-opened`
open and close modal, body class: `no-ed modal-open`
@markogresak
Copy link
Copy Markdown
Contributor Author

I pushed a change to fix the problem caught by @TheSharpieOne. I had also left a comment, but GitHub decided to hide it after the changes were pushed.

Instead of using word boundary match, I opted in for replacing current regex's match with a single space. If the match is at start/end, the next line uses classes.trim() so any extra spaces are going to be removed.
I had also suggested using Element.classList, but that would require a larger change and will not be necessary after this is merged.

@eddywashere
Copy link
Copy Markdown
Member

thanks @markogresak!

@eddywashere eddywashere merged commit 22d5c3f into reactstrap:master May 14, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants