Created
January 26, 2018 09:29
-
-
Save adamduncan/b1b52d815f3ad904f948bb5c3103a1fb to your computer and use it in GitHub Desktop.
Container component for AriaModal
This file contains hidden or 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
import React, { Fragment } from 'react'; | |
import PropTypes from 'prop-types'; | |
import AriaModal from 'react-aria-modal'; | |
import classNames from 'utils/namespace-classnames'; | |
import './Modal.css'; | |
class Modal extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
modalActive: false | |
} | |
} | |
activateModal = () => { | |
this.setState({ modalActive: true }); | |
} | |
deactivateModal = () => { | |
this.setState({ modalActive: false }); | |
} | |
render() { | |
const { | |
id, | |
title, | |
render, | |
className, | |
children | |
} = this.props | |
const Modal = this.state.modalActive ? ( | |
<AriaModal | |
titleText={title} | |
onExit={this.deactivateModal} | |
initialFocus={`#${id}`} | |
verticallyCenter={true} | |
> | |
<div id={id} className={classNames('modal', className)} tabIndex="0"> | |
{children()} | |
<button className={classNames('modal__close')} onClick={this.deactivateModal}> | |
Close | |
</button> | |
</div> | |
</AriaModal> | |
) : false | |
return ( | |
<Fragment> | |
{render(this.activateModal)} | |
{Modal} | |
</Fragment> | |
); | |
} | |
} | |
Modal.propTypes = { | |
id: PropTypes.string.isRequired, | |
title: PropTypes.string.isRequired, | |
render: PropTypes.func.isRequired, | |
className: PropTypes.string, | |
children: PropTypes.func.isRequired, | |
} | |
export default Modal; | |
// usage | |
// <Modal | |
// id="modal-id" | |
// title="Modal title" | |
// render={(activate) => ( | |
// <button onClick={activate}> | |
// Show modal | |
// </button> | |
// )} | |
// > | |
// {() => ( | |
// // modal content inline | |
// // or <Component /> to render | |
// )} | |
// </Modal> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment