-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(component): new Modal component
- Loading branch information
Eszter Hofmann
committed
Jul 18, 2019
1 parent
468305a
commit c46b7b6
Showing
11 changed files
with
844 additions
and
741 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,78 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import bem from 'bem'; | ||
import ReactModal from 'react-modal'; | ||
import styles from './Modal.scss'; | ||
|
||
const { block, elem } = bem({ | ||
name: 'Modal', | ||
classnames: styles | ||
}); | ||
|
||
const Modal = props => { | ||
const { | ||
children, | ||
isOpen, | ||
contentLabel, | ||
onRequestClose, | ||
className, | ||
portalClassName, | ||
overlayClassName, | ||
...rest | ||
} = props; | ||
|
||
const { className: portalClass } = block(props); | ||
const { className: overlayClass } = elem('overlay', props); | ||
const { className: overlayEnterClass } = elem('overlayEnter', props); | ||
const { className: overlayExitClass } = elem('overlayExit', props); | ||
const { className: contentClass } = elem('content', props); | ||
const { className: contentEnterClass } = elem('contentEnter', props); | ||
const { className: contentExitClass } = elem('contentExit', props); | ||
|
||
const contentClassJoined = [contentClass, className].join(' '); | ||
|
||
return ( | ||
<ReactModal | ||
isOpen={isOpen} | ||
contentLabel={contentLabel} | ||
onRequestClose={onRequestClose} | ||
closeTimeoutMS={300} | ||
portalClassName={portalClass} | ||
overlayClassName={{ | ||
base: overlayClass, | ||
afterOpen: overlayEnterClass, | ||
beforeClose: overlayExitClass | ||
}} | ||
className={{ | ||
base: contentClassJoined, | ||
afterOpen: contentEnterClass, | ||
beforeClose: contentExitClass | ||
}} | ||
{...rest} | ||
> | ||
{children} | ||
</ReactModal> | ||
); | ||
}; | ||
|
||
Modal.displayName = 'Modal'; | ||
|
||
Modal.propTypes = { | ||
/** elements to be rendered within the modal */ | ||
children: PropTypes.node.isRequired, | ||
/** The state of the modal */ | ||
isOpen: PropTypes.bool.isRequired, | ||
/** A title for the modal that will be used by screenreaders */ | ||
contentLabel: PropTypes.string.isRequired, | ||
/** A function to be called when the modal is closed */ | ||
onRequestClose: PropTypes.func, | ||
/** Additional class to be applied to the content part */ | ||
className: PropTypes.string | ||
}; | ||
|
||
Modal.defaultProps = { | ||
onRequestClose: null, | ||
className: '' | ||
}; | ||
|
||
export default Modal; |
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,45 @@ | ||
.Modal { | ||
&__overlay { | ||
position: fixed; | ||
top: 0; | ||
left: 0; | ||
right: 0; | ||
bottom: 0; | ||
background-color: rgba(0, 0, 0, 0.5); | ||
opacity: 0; | ||
transition: opacity var(--transition-duration) ease-in-out; | ||
} | ||
|
||
&__overlayEnter { | ||
opacity: 1; | ||
} | ||
|
||
&__overlayExit { | ||
opacity: 0; | ||
} | ||
|
||
&__content { | ||
position: relative; | ||
top: 15vh; | ||
width: 65vw; | ||
height: 65vh; | ||
margin: auto; | ||
max-width: var(--site-container-max-size); | ||
border: 1px solid var(--border-color-strong); | ||
border-radius: var(--border-radius); | ||
background-color: var(--color-background); | ||
box-shadow: 0 1px 25px 0 rgba(0, 0, 0, 0.8); | ||
overflow: auto; | ||
outline: none; | ||
transform: translateY(50px); | ||
transition: transform var(--transition-duration); | ||
} | ||
|
||
&__contentEnter { | ||
transform: translateY(0); | ||
} | ||
|
||
&__contentExit { | ||
transform: translateY(50px); | ||
} | ||
} |
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,14 @@ | ||
import React from 'react'; | ||
import toJson from 'enzyme-to-json'; | ||
import Modal from '../Modal'; | ||
|
||
describe('Modal', () => { | ||
it('should render correctly', () => { | ||
const wrapper = shallow( | ||
<Modal isOpen contentLabel="Content label"> | ||
Some children | ||
</Modal> | ||
); | ||
expect(toJson(wrapper)).toMatchSnapshot(); | ||
}); | ||
}); |
35 changes: 35 additions & 0 deletions
35
src/components/Modal/__tests__/__snapshots__/Modal.spec.js.snap
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,35 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Modal should render correctly 1`] = ` | ||
<Modal | ||
ariaHideApp={true} | ||
bodyOpenClassName="ReactModal__Body--open" | ||
className={ | ||
Object { | ||
"afterOpen": "Modal__contentEnter", | ||
"base": "Modal__content ", | ||
"beforeClose": "Modal__contentExit", | ||
} | ||
} | ||
closeTimeoutMS={300} | ||
contentLabel="Content label" | ||
isOpen={true} | ||
onRequestClose={null} | ||
overlayClassName={ | ||
Object { | ||
"afterOpen": "Modal__overlayEnter", | ||
"base": "Modal__overlay", | ||
"beforeClose": "Modal__overlayExit", | ||
} | ||
} | ||
parentSelector={[Function]} | ||
portalClassName="ReactModalPortal" | ||
role="dialog" | ||
shouldCloseOnEsc={true} | ||
shouldCloseOnOverlayClick={true} | ||
shouldFocusAfterRender={true} | ||
shouldReturnFocusAfterClose={true} | ||
> | ||
Some children | ||
</Modal> | ||
`; |
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 @@ | ||
export { default } from './Modal'; |
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
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,49 @@ | ||
import React from 'react'; | ||
import { storiesOf } from '@storybook/react'; | ||
import { boolean, text, withKnobs } from '@storybook/addon-knobs'; | ||
import { withInfo } from '@storybook/addon-info'; | ||
import { Modal } from '@textkernel/oneui'; | ||
|
||
storiesOf('Modal', module) | ||
.addDecorator(withKnobs) | ||
.addDecorator(withInfo) | ||
.add( | ||
'Modal', | ||
() => { | ||
const onClose = () => console.log('Modal was requested to be closed.'); | ||
return ( | ||
<Modal | ||
isOpen={boolean('Open', false)} | ||
contentLabel={text( | ||
'Label for content visible to screenreaders', | ||
'My superb modal' | ||
)} | ||
onRequestClose={onClose} | ||
> | ||
Some content | ||
</Modal> | ||
); | ||
}, | ||
{ | ||
info: { | ||
text: ` | ||
## Usage information | ||
This component is a wrapper around [react-modal](http://reactcommunity.org/react-modal/#usage). | ||
* You can pass other props according to their definition, apart from classes. | ||
* For accessibility reasons you need to __initialise__ modal use in your app. | ||
For more info see [app element](http://reactcommunity.org/react-modal/accessibility/#app-element) in thier documentation. | ||
To do this: | ||
> import OneUI from '@textkernel/oneui'; | ||
> OneUI.__initModal__(appElementSelector); | ||
To see the modal in action here, change the Open prop to 'true' in the Knobs section. | ||
` | ||
} | ||
} | ||
); |
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