Skip to content

Commit

Permalink
fix: fix scrolling in background behind the Modal (#620)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmcartaya authored and LeandroTorresSicilia committed Mar 12, 2019
1 parent e8173d2 commit 0bfee74
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/components/Modal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ESCAPE_KEY, TAB_KEY } from './../../libs/constants';
import Header from './header';
import CloseIcon from './closeIcon';
import manageTab from './manageTab';
import { disableScroll, enableScroll } from './scrollController';
import './styles.css';

/**
Expand All @@ -32,24 +33,24 @@ export default class Modal extends Component {
componentDidMount() {
const { isOpen } = this.props;
if (isOpen) {
document.body.style.overflow = 'hidden';
disableScroll();
this.modalTriggerElement = document.activeElement;
this.modalRef.current.focus();
}
}

componentDidUpdate() {
const { isOpen } = this.props;
document.body.style.overflow = 'inherit';
enableScroll();
if (isOpen) {
document.body.style.overflow = 'hidden';
disableScroll();
this.modalTriggerElement = document.activeElement;
this.modalRef.current.focus();
}
}

componentWillUnmount() {
document.body.style.overflow = 'inherit';
enableScroll();
}

getBackDropClassNames() {
Expand Down
13 changes: 13 additions & 0 deletions src/components/Modal/scrollController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function preventDefault(e) {
e.preventDefault();
}

export function disableScroll() {
document.body.addEventListener('touchmove', preventDefault, { passive: false });
document.body.style.overflow = 'hidden';
}

export function enableScroll() {
document.body.removeEventListener('touchmove', preventDefault, { passive: false });
document.body.style.overflow = 'inherit';
}

0 comments on commit 0bfee74

Please sign in to comment.