glitchier-soc/app/javascript/flavours/glitch/features/ui/components/modal_root.js

136 lines
4 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 FavouriteModal from './favourite_modal';
2017-10-13 19:01:14 +03:00
import DoodleModal from './doodle_modal';
import ConfirmationModal from './confirmation_modal';
import {
OnboardingModal,
MuteModal,
ReportModal,
2017-07-12 12:03:17 +03:00
SettingsModal,
EmbedModal,
ListEditor,
2017-12-04 09:26:40 +02:00
} from 'flavours/glitch/util/async-components';
2017-04-01 23:11:28 +03:00
const MODAL_COMPONENTS = {
'MEDIA': () => Promise.resolve({ default: MediaModal }),
'ONBOARDING': OnboardingModal,
'VIDEO': () => Promise.resolve({ default: VideoModal }),
'BOOST': () => Promise.resolve({ default: BoostModal }),
'FAVOURITE': () => Promise.resolve({ default: FavouriteModal }),
2017-10-13 19:01:14 +03:00
'DOODLE': () => Promise.resolve({ default: DoodleModal }),
'CONFIRM': () => Promise.resolve({ default: ConfirmationModal }),
'MUTE': MuteModal,
'REPORT': ReportModal,
2017-07-12 12:03:17 +03:00
'SETTINGS': SettingsModal,
'ACTIONS': () => Promise.resolve({ default: ActionsModal }),
'EMBED': EmbedModal,
'LIST_EDITOR': ListEditor,
2017-04-01 23:11:28 +03:00
};
export default class ModalRoot extends React.PureComponent {
2017-04-01 23:11:28 +03:00
static propTypes = {
type: PropTypes.string,
props: PropTypes.object,
onClose: PropTypes.func.isRequired,
};
2017-04-01 23:11:28 +03:00
state = {
revealed: false,
};
handleKeyUp = (e) => {
if ((e.key === 'Escape' || e.key === 'Esc' || e.keyCode === 27)
&& !!this.props.type && !this.props.props.noEsc) {
2017-04-01 23:11:28 +03:00
this.props.onClose();
}
}
2017-04-01 23:11:28 +03:00
componentDidMount () {
window.addEventListener('keyup', this.handleKeyUp, false);
}
2017-04-01 23:11:28 +03: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 23:11:28 +03:00
componentWillUnmount () {
window.removeEventListener('keyup', this.handleKeyUp);
}
2017-04-01 23:11:28 +03:00
getSiblings = () => {
return Array(...this.node.parentElement.childNodes).filter(node => node !== this.node);
}
setRef = ref => {
this.node = ref;
}
renderLoading = modalId => () => {
return ['MEDIA', 'VIDEO', 'BOOST', 'FAVOURITE', 'DOODLE', 'CONFIRM', 'ACTIONS'].indexOf(modalId) === -1 ? <ModalLoading /> : null;
}
renderError = (props) => {
const { onClose } = this.props;
return <BundleModalError {...props} onClose={onClose} />;
}
2017-04-01 23:11:28 +03:00
render () {
const { type, props, onClose } = this.props;
const { revealed } = this.state;
const visible = !!type;
2017-04-01 23:11:28 +03:00
if (!visible) {
return (
<div className='modal-root' ref={this.setRef} style={{ opacity: 0 }} />
);
2017-04-01 23:11:28 +03: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 23:11:28 +03:00
</div>
</div>
</div>
2017-04-01 23:11:28 +03:00
);
}
}