cybre-space/app/javascript/mastodon/features/ui/components/modal_root.js

126 lines
3.6 KiB
JavaScript
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
import BundleContainer from '../containers/bundle_container';
import BundleModalError from './bundle_modal_error';
import ModalLoading from './modal_loading';
import ActionsModal from './actions_modal';
import MediaModal from './media_modal';
import VideoModal from './video_modal';
import BoostModal from './boost_modal';
import ConfirmationModal from './confirmation_modal';
import {
OnboardingModal,
ReportModal,
EmbedModal,
} from '../../../features/ui/util/async-components';
2017-04-01 21:11:28 +01:00
const MODAL_COMPONENTS = {
'MEDIA': () => Promise.resolve({ default: MediaModal }),
'ONBOARDING': OnboardingModal,
'VIDEO': () => Promise.resolve({ default: VideoModal }),
'BOOST': () => Promise.resolve({ default: BoostModal }),
'CONFIRM': () => Promise.resolve({ default: ConfirmationModal }),
'REPORT': ReportModal,
'ACTIONS': () => Promise.resolve({ default: ActionsModal }),
'EMBED': EmbedModal,
2017-04-01 21:11:28 +01:00
};
export default class ModalRoot extends React.PureComponent {
2017-04-01 21:11:28 +01:00
static propTypes = {
type: PropTypes.string,
props: PropTypes.object,
onClose: PropTypes.func.isRequired,
};
2017-04-01 21:11:28 +01:00
state = {
revealed: false,
};
handleKeyUp = (e) => {
if ((e.key === 'Escape' || e.key === 'Esc' || e.keyCode === 27)
&& !!this.props.type) {
2017-04-01 21:11:28 +01:00
this.props.onClose();
}
}
2017-04-01 21:11:28 +01:00
componentDidMount () {
window.addEventListener('keyup', this.handleKeyUp, false);
}
2017-04-01 21:11:28 +01:00
componentWillReceiveProps (nextProps) {
if (!!nextProps.type && !this.props.type) {
this.activeElement = document.activeElement;
this.getSiblings().forEach(sibling => sibling.setAttribute('inert', true));
} else if (!nextProps.type) {
this.setState({ revealed: false });
}
}
componentDidUpdate (prevProps) {
if (!this.props.type && !!prevProps.type) {
this.getSiblings().forEach(sibling => sibling.removeAttribute('inert'));
this.activeElement.focus();
this.activeElement = null;
}
if (this.props.type) {
requestAnimationFrame(() => {
this.setState({ revealed: true });
});
}
}
2017-04-01 21:11:28 +01:00
componentWillUnmount () {
window.removeEventListener('keyup', this.handleKeyUp);
}
2017-04-01 21:11:28 +01:00
getSiblings = () => {
return Array(...this.node.parentElement.childNodes).filter(node => node !== this.node);
}
setRef = ref => {
this.node = ref;
}
renderLoading = modalId => () => {
return ['MEDIA', 'VIDEO', 'BOOST', 'CONFIRM', 'ACTIONS'].indexOf(modalId) === -1 ? <ModalLoading /> : null;
}
renderError = (props) => {
const { onClose } = this.props;
return <BundleModalError {...props} onClose={onClose} />;
}
2017-04-01 21:11:28 +01:00
render () {
const { type, props, onClose } = this.props;
const { revealed } = this.state;
const visible = !!type;
2017-04-01 21:11:28 +01:00
if (!visible) {
return (
<div className='modal-root' ref={this.setRef} style={{ opacity: 0 }} />
);
2017-04-01 21:11:28 +01:00
}
return (
<div className='modal-root' ref={this.setRef} style={{ opacity: revealed ? 1 : 0 }}>
<div style={{ pointerEvents: visible ? 'auto' : 'none' }}>
<div role='presentation' className='modal-root__overlay' onClick={onClose} />
<div role='dialog' className='modal-root__container'>
{
visible ?
(<BundleContainer fetchComponent={MODAL_COMPONENTS[type]} loading={this.renderLoading(type)} error={this.renderError} renderDelay={200}>
{(SpecificComponent) => <SpecificComponent {...props} onClose={onClose} />}
</BundleContainer>) :
null
}
2017-04-01 21:11:28 +01:00
</div>
</div>
</div>
2017-04-01 21:11:28 +01:00
);
}
}