generated from goitacademy/react-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from 001elijah/Modal-component
Modal component
- Loading branch information
Showing
5 changed files
with
159 additions
and
3 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import PropTypes from 'prop-types'; | ||
import { useSelector } from 'react-redux'; | ||
import React, { useEffect, useCallback, useState } from 'react'; | ||
import { BackdropModal } from '../BackdropMain/BackdropMain'; | ||
import { selectorTheme } from 'redux/Auth/authSelectors'; | ||
import s from './Modal.module.scss'; | ||
import svg from 'assets/icons/sprite.svg'; | ||
|
||
export const Modal = ({ title, onClose, children }) => { | ||
const theme = useSelector(selectorTheme); | ||
const [, setThemeClass] = useState(''); | ||
|
||
useEffect(() => { | ||
setThemeClass(s[theme]); | ||
}, [theme]); | ||
|
||
useEffect(() => { | ||
const handleKeyDown = event => { | ||
if (event.key === 'Escape') { | ||
handleClose(); | ||
} | ||
}; | ||
|
||
document.addEventListener('keydown', handleKeyDown); | ||
|
||
return () => { | ||
document.removeEventListener('keydown', handleKeyDown); | ||
}; | ||
}, []); | ||
|
||
const handleClose = useCallback(() => { | ||
onClose(); | ||
}, [onClose]); | ||
|
||
return ( | ||
<BackdropModal closeModal={onClose}> | ||
<div className={`${s.modal} ${s[theme]}`}> | ||
<div className={`${s.modalHeader} ${s[theme]}`}> | ||
<h3 className={`${s.title} ${s[theme]}`}>{title}</h3> | ||
<div onClick={handleClose} className={`${s.close} ${s[theme]}`}> | ||
<svg width="18" height="18" className={`${s.closeSvg} ${s[theme]}`}> | ||
<use xlinkHref={`${svg}#icon-x-close`} /> | ||
</svg> | ||
</div> | ||
</div> | ||
<div className={s.modalContent}>{children}</div> | ||
</div> | ||
</BackdropModal> | ||
); | ||
}; | ||
|
||
Modal.propTypes = { | ||
title: PropTypes.string.isRequired, | ||
onClose: PropTypes.func.isRequired, | ||
children: PropTypes.node.isRequired, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
@import '../../assets/styles/vars'; | ||
@import '../../assets/styles/mixins'; | ||
|
||
.modalBackdrop { | ||
position: fixed; | ||
top: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 100%; | ||
background-color: rgba(0, 0, 0, 0.5); | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
} | ||
|
||
.title { | ||
font-family: 'Poppins-Medium'; | ||
font-style: normal; | ||
font-weight: 500; | ||
font-size: 18px; | ||
line-height: 1.5em; | ||
letter-spacing: -0.02em; | ||
margin-bottom: 24px; | ||
margin-top: 24px; | ||
|
||
&.dark { | ||
color: $white-text-color; | ||
} | ||
|
||
&.light, | ||
&.colorful { | ||
color: $black-text-color; | ||
} | ||
} | ||
|
||
.modal { | ||
padding: 0 0 24px 24px; | ||
border-radius: 4px; | ||
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3); | ||
width: 100%; | ||
max-width: 335px; | ||
border-radius: 8px; | ||
|
||
&.dark { | ||
background-color: $black-text-color; | ||
} | ||
|
||
&.light, | ||
&.colorful { | ||
background-color: $white-background-modal; | ||
} | ||
|
||
@include tablet { | ||
max-width: 350px; | ||
} | ||
|
||
@include desktop { | ||
max-width: 350px; | ||
} | ||
} | ||
|
||
.modalHeader { | ||
display: flex; | ||
justify-content: space-between; | ||
} | ||
|
||
.closeSvg { | ||
background: none; | ||
border: none; | ||
cursor: pointer; | ||
margin: 14px; | ||
&.dark { | ||
stroke: $white-text-color; | ||
} | ||
|
||
&.light, | ||
&.colorful { | ||
stroke: $black-text-color; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters