This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
mastodon/app/javascript/flavours/glitch/features/ui/components/modal_root.js

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