mirror of
https://framagit.org/tykayn/mastodon.git
synced 2023-08-25 08:33:12 +02:00
Allow modals to be closed by pressing “back”
This commit is contained in:
parent
3eb3c21327
commit
5d060cb6e4
@ -1,7 +1,11 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
|
import createHistory from 'history/createBrowserHistory';
|
||||||
|
|
||||||
export default class ModalRoot extends React.PureComponent {
|
export default class ModalRoot extends React.PureComponent {
|
||||||
|
static contextTypes = {
|
||||||
|
router: PropTypes.object,
|
||||||
|
};
|
||||||
|
|
||||||
static propTypes = {
|
static propTypes = {
|
||||||
children: PropTypes.node,
|
children: PropTypes.node,
|
||||||
@ -24,6 +28,7 @@ export default class ModalRoot extends React.PureComponent {
|
|||||||
|
|
||||||
componentDidMount () {
|
componentDidMount () {
|
||||||
window.addEventListener('keyup', this.handleKeyUp, false);
|
window.addEventListener('keyup', this.handleKeyUp, false);
|
||||||
|
this.history = this.context.router ? this.context.router.history : createHistory();
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillReceiveProps (nextProps) {
|
componentWillReceiveProps (nextProps) {
|
||||||
@ -41,11 +46,13 @@ export default class ModalRoot extends React.PureComponent {
|
|||||||
this.getSiblings().forEach(sibling => sibling.removeAttribute('inert'));
|
this.getSiblings().forEach(sibling => sibling.removeAttribute('inert'));
|
||||||
this.activeElement.focus();
|
this.activeElement.focus();
|
||||||
this.activeElement = null;
|
this.activeElement = null;
|
||||||
|
this.handleModalClose();
|
||||||
}
|
}
|
||||||
if (this.props.children) {
|
if (this.props.children) {
|
||||||
requestAnimationFrame(() => {
|
requestAnimationFrame(() => {
|
||||||
this.setState({ revealed: true });
|
this.setState({ revealed: true });
|
||||||
});
|
});
|
||||||
|
if (!prevProps.children) this.handleModalOpen();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,6 +60,24 @@ export default class ModalRoot extends React.PureComponent {
|
|||||||
window.removeEventListener('keyup', this.handleKeyUp);
|
window.removeEventListener('keyup', this.handleKeyUp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handleModalClose () {
|
||||||
|
this.unlistenHistory();
|
||||||
|
|
||||||
|
const state = this.history.location.state;
|
||||||
|
if (state && state.mastodonModalOpen) {
|
||||||
|
this.history.goBack();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
handleModalOpen () {
|
||||||
|
const history = this.history;
|
||||||
|
const state = {...history.location.state, mastodonModalOpen: true};
|
||||||
|
history.push(history.location.pathname, state);
|
||||||
|
this.unlistenHistory = history.listen(() => {
|
||||||
|
this.props.onClose();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
getSiblings = () => {
|
getSiblings = () => {
|
||||||
return Array(...this.node.parentElement.childNodes).filter(node => node !== this.node);
|
return Array(...this.node.parentElement.childNodes).filter(node => node !== this.node);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user