vt-social/app/javascript/mastodon/features/ui/components/modal_root.js

131 lines
3.6 KiB
JavaScript
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
import TransitionMotion from 'react-motion/lib/TransitionMotion';
import spring from 'react-motion/lib/spring';
import BundleContainer from '../containers/bundle_container';
import BundleModalError from './bundle_modal_error';
import ModalLoading from './modal_loading';
import ActionsModal from '../components/actions_modal';
import {
MediaModal,
OnboardingModal,
VideoModal,
BoostModal,
ConfirmationModal,
ReportModal,
} from '../../../features/ui/util/async-components';
2017-04-01 20:11:28 +00:00
const MODAL_COMPONENTS = {
2017-04-11 02:28:52 +00:00
'MEDIA': MediaModal,
'ONBOARDING': OnboardingModal,
'VIDEO': VideoModal,
'BOOST': BoostModal,
'CONFIRM': ConfirmationModal,
'REPORT': ReportModal,
'ACTIONS': () => Promise.resolve({ default: ActionsModal }),
2017-04-01 20:11:28 +00:00
};
export default class ModalRoot extends React.PureComponent {
2017-04-01 20:11:28 +00:00
static propTypes = {
type: PropTypes.string,
props: PropTypes.object,
onClose: PropTypes.func.isRequired,
};
2017-04-01 20:11:28 +00:00
handleKeyUp = (e) => {
if ((e.key === 'Escape' || e.key === 'Esc' || e.keyCode === 27)
&& !!this.props.type) {
2017-04-01 20:11:28 +00:00
this.props.onClose();
}
}
2017-04-01 20:11:28 +00:00
componentDidMount () {
window.addEventListener('keyup', this.handleKeyUp, false);
}
2017-04-01 20:11:28 +00:00
componentWillReceiveProps (nextProps) {
if (!!nextProps.type && !this.props.type) {
this.activeElement = document.activeElement;
this.getSiblings().forEach(sibling => sibling.setAttribute('inert', true));
}
}
componentDidUpdate (prevProps) {
if (!this.type && !!prevProps.type) {
this.getSiblings().forEach(sibling => sibling.removeAttribute('inert'));
this.activeElement.focus();
this.activeElement = null;
}
}
2017-04-01 20:11:28 +00:00
componentWillUnmount () {
window.removeEventListener('keyup', this.handleKeyUp);
}
2017-04-01 20:11:28 +00:00
getSiblings = () => {
return Array(...this.node.parentElement.childNodes).filter(node => node !== this.node);
}
setRef = ref => {
this.node = ref;
}
2017-04-01 20:11:28 +00:00
willEnter () {
return { opacity: 0, scale: 0.98 };
}
2017-04-01 20:11:28 +00:00
willLeave () {
return { opacity: spring(0), scale: spring(0.98) };
}
2017-04-01 20:11:28 +00:00
renderLoading = () => {
return <ModalLoading />;
}
renderError = (props) => {
const { onClose } = this.props;
return <BundleModalError {...props} onClose={onClose} />;
}
2017-04-01 20:11:28 +00:00
render () {
const { type, props, onClose } = this.props;
const visible = !!type;
2017-04-01 20:11:28 +00:00
const items = [];
if (visible) {
2017-04-01 20:11:28 +00:00
items.push({
key: type,
data: { type, props },
style: { opacity: spring(1), scale: spring(1, { stiffness: 120, damping: 14 }) },
2017-04-01 20:11:28 +00:00
});
}
return (
<TransitionMotion
styles={items}
willEnter={this.willEnter}
willLeave={this.willLeave}
>
2017-04-01 20:11:28 +00:00
{interpolatedStyles =>
<div className='modal-root' ref={this.setRef}>
{interpolatedStyles.map(({ key, data: { type, props }, style }) => (
<div key={key} style={{ pointerEvents: visible ? 'auto' : 'none' }}>
<div role='presentation' className='modal-root__overlay' style={{ opacity: style.opacity }} onClick={onClose} />
<div role='dialog' className='modal-root__container' style={{ opacity: style.opacity, transform: `translateZ(0px) scale(${style.scale})` }}>
<BundleContainer fetchComponent={MODAL_COMPONENTS[type]} loading={this.renderLoading} error={this.renderError} renderDelay={200}>
{(SpecificComponent) => <SpecificComponent {...props} onClose={onClose} />}
</BundleContainer>
2017-04-01 20:11:28 +00:00
</div>
</div>
))}
2017-04-01 20:11:28 +00:00
</div>
}
</TransitionMotion>
);
}
}