2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-12-27 10:59:37 +01:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-05-16 16:01:11 +02:00
|
|
|
// TODO: semantic-ui-react exposes Portal component, use that
|
2017-07-17 18:37:33 +02:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2016-12-27 10:59:37 +01:00
|
|
|
import ReactDOM from 'react-dom';
|
2016-12-30 12:28:12 +01:00
|
|
|
import ReactPortal from 'react-portal';
|
2016-12-27 10:59:37 +01:00
|
|
|
import keycode from 'keycode';
|
2017-03-15 13:40:18 +01:00
|
|
|
import { noop } from 'lodash';
|
2016-12-27 10:59:37 +01:00
|
|
|
|
2017-05-09 12:01:44 +02:00
|
|
|
import { nodeOrStringProptype } from '@parity/shared/util/proptypes';
|
|
|
|
|
2017-07-21 15:46:53 +02:00
|
|
|
import { CloseIcon } from '../Icons';
|
|
|
|
import StackEventListener from '../StackEventListener';
|
|
|
|
import Title from '../Title';
|
2016-12-27 10:59:37 +01:00
|
|
|
|
|
|
|
import styles from './portal.css';
|
|
|
|
|
2016-12-30 12:28:12 +01:00
|
|
|
export default class Portal extends Component {
|
2016-12-27 10:59:37 +01:00
|
|
|
static propTypes = {
|
|
|
|
open: PropTypes.bool.isRequired,
|
2017-02-03 22:44:43 +01:00
|
|
|
activeStep: PropTypes.number,
|
|
|
|
busy: PropTypes.bool,
|
|
|
|
busySteps: PropTypes.array,
|
2017-02-07 08:46:17 +01:00
|
|
|
buttons: PropTypes.oneOfType([
|
|
|
|
PropTypes.array,
|
|
|
|
PropTypes.node,
|
|
|
|
PropTypes.object
|
|
|
|
]),
|
2016-12-27 10:59:37 +01:00
|
|
|
children: PropTypes.node,
|
|
|
|
className: PropTypes.string,
|
2017-02-03 22:44:43 +01:00
|
|
|
hideClose: PropTypes.bool,
|
2017-01-30 17:08:08 +01:00
|
|
|
isChildModal: PropTypes.bool,
|
2017-02-20 16:40:01 +01:00
|
|
|
isSmallModal: PropTypes.bool,
|
2017-03-03 14:38:40 +01:00
|
|
|
onClick: PropTypes.func,
|
2017-03-15 13:40:18 +01:00
|
|
|
onClose: PropTypes.func,
|
2017-02-03 22:44:43 +01:00
|
|
|
onKeyDown: PropTypes.func,
|
|
|
|
steps: PropTypes.array,
|
|
|
|
title: nodeOrStringProptype()
|
2016-12-27 10:59:37 +01:00
|
|
|
};
|
|
|
|
|
2017-03-15 13:40:18 +01:00
|
|
|
static defaultProps = {
|
|
|
|
onClose: noop
|
|
|
|
};
|
|
|
|
|
2017-02-03 22:44:43 +01:00
|
|
|
componentDidMount () {
|
|
|
|
this.setBodyOverflow(this.props.open);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
if (nextProps.open !== this.props.open) {
|
|
|
|
this.setBodyOverflow(nextProps.open);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount () {
|
|
|
|
this.setBodyOverflow(false);
|
|
|
|
}
|
|
|
|
|
2016-12-27 10:59:37 +01:00
|
|
|
render () {
|
2017-05-16 16:01:11 +02:00
|
|
|
const { activeStep, busy, busySteps, buttons, children, className, hideClose, isChildModal, isSmallModal, open, steps, title } = this.props;
|
2017-01-31 17:04:41 +01:00
|
|
|
|
|
|
|
if (!open) {
|
|
|
|
return null;
|
2016-12-27 10:59:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2017-02-22 15:26:58 +01:00
|
|
|
<ReactPortal isOpened>
|
2017-01-30 17:08:08 +01:00
|
|
|
<div
|
2017-01-31 17:04:41 +01:00
|
|
|
className={ styles.backOverlay }
|
2017-01-30 17:08:08 +01:00
|
|
|
onClick={ this.handleClose }
|
|
|
|
>
|
2016-12-31 14:28:16 +01:00
|
|
|
<div
|
2017-01-31 17:04:41 +01:00
|
|
|
className={
|
|
|
|
[
|
|
|
|
styles.overlay,
|
|
|
|
isChildModal
|
|
|
|
? styles.popover
|
|
|
|
: styles.modal,
|
2017-02-20 16:40:01 +01:00
|
|
|
isSmallModal
|
|
|
|
? styles.small
|
|
|
|
: null,
|
2017-01-31 17:04:41 +01:00
|
|
|
className
|
|
|
|
].join(' ')
|
|
|
|
}
|
2017-03-03 14:38:40 +01:00
|
|
|
onClick={ this.handleContainerClick }
|
2016-12-31 14:28:16 +01:00
|
|
|
onKeyDown={ this.handleKeyDown }
|
|
|
|
>
|
2017-03-03 19:49:36 +01:00
|
|
|
<StackEventListener onKeyUp={ this.handleKeyUp } />
|
2017-05-16 16:01:11 +02:00
|
|
|
{
|
|
|
|
!hideClose && (
|
|
|
|
<div
|
|
|
|
className={ styles.closeIcon }
|
|
|
|
onClick={ this.handleClose }
|
|
|
|
>
|
|
|
|
<CloseIcon />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2017-02-03 22:44:43 +01:00
|
|
|
<Title
|
|
|
|
activeStep={ activeStep }
|
|
|
|
busy={ busy }
|
|
|
|
busySteps={ busySteps }
|
|
|
|
className={ styles.titleRow }
|
|
|
|
steps={ steps }
|
|
|
|
title={ title }
|
|
|
|
/>
|
|
|
|
<div className={ styles.childContainer }>
|
|
|
|
{ children }
|
2017-01-31 17:04:41 +01:00
|
|
|
</div>
|
2017-05-16 16:01:11 +02:00
|
|
|
{
|
|
|
|
buttons && (
|
|
|
|
<div className={ styles.buttonRow }>
|
|
|
|
{ buttons }
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2016-12-31 14:28:16 +01:00
|
|
|
</div>
|
2016-12-27 10:59:37 +01:00
|
|
|
</div>
|
2016-12-30 12:28:12 +01:00
|
|
|
</ReactPortal>
|
2016-12-27 10:59:37 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-12-31 14:28:16 +01:00
|
|
|
stopEvent = (event) => {
|
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
|
2017-03-03 14:38:40 +01:00
|
|
|
handleContainerClick = (event) => {
|
|
|
|
const { onClick } = this.props;
|
|
|
|
|
|
|
|
if (!onClick) {
|
|
|
|
return this.stopEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
return onClick(event);
|
|
|
|
}
|
|
|
|
|
2016-12-27 10:59:37 +01:00
|
|
|
handleClose = () => {
|
2017-02-03 22:44:43 +01:00
|
|
|
const { hideClose, onClose } = this.props;
|
|
|
|
|
|
|
|
if (!hideClose) {
|
|
|
|
onClose();
|
|
|
|
}
|
2017-02-22 15:26:58 +01:00
|
|
|
|
|
|
|
this.stopEvent(event);
|
2016-12-27 10:59:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
handleKeyDown = (event) => {
|
2017-01-30 17:08:08 +01:00
|
|
|
const { onKeyDown } = this.props;
|
2017-01-31 11:38:53 +01:00
|
|
|
|
|
|
|
event.persist();
|
2017-02-03 22:44:43 +01:00
|
|
|
|
2017-01-31 11:38:53 +01:00
|
|
|
return onKeyDown
|
|
|
|
? onKeyDown(event)
|
|
|
|
: false;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleKeyUp = (event) => {
|
2016-12-27 10:59:37 +01:00
|
|
|
const codeName = keycode(event);
|
|
|
|
|
|
|
|
switch (codeName) {
|
|
|
|
case 'esc':
|
|
|
|
event.preventDefault();
|
2017-03-03 19:49:36 +01:00
|
|
|
return this.props.onClose();
|
2016-12-27 10:59:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleDOMAction = (ref, method) => {
|
2017-02-03 22:44:43 +01:00
|
|
|
const element = ReactDOM.findDOMNode(
|
|
|
|
typeof ref === 'string'
|
|
|
|
? this.refs[ref]
|
|
|
|
: ref
|
|
|
|
);
|
2016-12-27 10:59:37 +01:00
|
|
|
|
|
|
|
if (!element || typeof element[method] !== 'function') {
|
|
|
|
console.warn('could not find', ref, 'or method', method);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return element[method]();
|
|
|
|
}
|
2017-02-03 22:44:43 +01:00
|
|
|
|
|
|
|
setBodyOverflow (open) {
|
|
|
|
if (!this.props.isChildModal) {
|
|
|
|
document.body.style.overflow = open
|
|
|
|
? 'hidden'
|
|
|
|
: null;
|
|
|
|
}
|
|
|
|
}
|
2016-12-27 10:59:37 +01:00
|
|
|
}
|