Skip to content

Commit

Permalink
Merge pull request #1 from iVladyuser/modal_hooks
Browse files Browse the repository at this point in the history
modal on hooks
  • Loading branch information
iVladyuser authored Oct 31, 2023
2 parents 316e498 + 41ec504 commit df00d48
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 35 deletions.
1 change: 1 addition & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<div id="modal-root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
Expand Down
64 changes: 29 additions & 35 deletions src/components/Modal/Modal.jsx
Original file line number Diff line number Diff line change
@@ -1,45 +1,39 @@
import { Component } from 'react';
import { createPortal } from 'react-dom';
import { Overlay, ModalImage } from './Modal.styles';
import { Overlay, ModalImage } from './Modal.styled';
import { useEffect } from 'react';

const modalRoot = document.querySelector('#modal-root');
class Modal extends Component {


componentDidMount() {
window.addEventListener('keydown', this.handleKeyDown);
}

componentWillUnmount() {
window.removeEventListener('keydown', this.handleKeyDown);
}

handleKeyDown = e => {
if (e.code === 'Escape') {
this.props.onCloseModal();
}
};

handleBackdropClick = event => {
const Modal = ({ selectedPhoto: { largeImageURL, tags }, onCloseModal }) => {
useEffect(() => {
const handleKeyDown = e => {
if (e.code === 'Escape') {
onCloseModal();
}
};

window.addEventListener('keydown', handleKeyDown);
document.body.style.overflow = 'hidden';
return () => {
window.removeEventListener('keydown', handleKeyDown);
document.body.style.overflow = 'auto';
};
}, [onCloseModal]);

const handleBackdropClick = event => {
if (event.target === event.currentTarget) {
this.props.onCloseModal();
onCloseModal();
}
};

render() {
const {
selectedPhoto: { largeImageURL, tags },
} = this.props;

return createPortal(
<Overlay onClick={this.handleBackdropClick}>
<ModalImage>
<img src={largeImageURL} alt={tags} />
</ModalImage>
</Overlay>,
modalRoot
);
}
}
return createPortal(
<Overlay onClick={handleBackdropClick}>
<ModalImage>
<img src={largeImageURL} alt={tags} />
</ModalImage>
</Overlay>,
modalRoot
);
};

export default Modal;
File renamed without changes.

0 comments on commit df00d48

Please sign in to comment.