mirror of https://github.com/mastodon/mastodon
Remove pointer events on the entire UI when a dropdown menu is open (#6648)
* Remove pointer events on the entire UI when a dropdown menu is open This prevents operations to change the location of the menu such as scrolling. * Fix mistake from merge
This commit is contained in:
parent
4847149b6e
commit
913a38111f
|
@ -0,0 +1,10 @@
|
||||||
|
export const DROPDOWN_MENU_OPEN = 'DROPDOWN_MENU_OPEN';
|
||||||
|
export const DROPDOWN_MENU_CLOSE = 'DROPDOWN_MENU_CLOSE';
|
||||||
|
|
||||||
|
export function openDropdownMenu(id, placement) {
|
||||||
|
return { type: DROPDOWN_MENU_OPEN, id, placement };
|
||||||
|
}
|
||||||
|
|
||||||
|
export function closeDropdownMenu(id) {
|
||||||
|
return { type: DROPDOWN_MENU_CLOSE, id };
|
||||||
|
}
|
|
@ -8,6 +8,7 @@ import spring from 'react-motion/lib/spring';
|
||||||
import detectPassiveEvents from 'detect-passive-events';
|
import detectPassiveEvents from 'detect-passive-events';
|
||||||
|
|
||||||
const listenerOptions = detectPassiveEvents.hasSupport ? { passive: true } : false;
|
const listenerOptions = detectPassiveEvents.hasSupport ? { passive: true } : false;
|
||||||
|
let id = 0;
|
||||||
|
|
||||||
class DropdownMenu extends React.PureComponent {
|
class DropdownMenu extends React.PureComponent {
|
||||||
|
|
||||||
|
@ -124,8 +125,10 @@ export default class Dropdown extends React.PureComponent {
|
||||||
status: ImmutablePropTypes.map,
|
status: ImmutablePropTypes.map,
|
||||||
isUserTouching: PropTypes.func,
|
isUserTouching: PropTypes.func,
|
||||||
isModalOpen: PropTypes.bool.isRequired,
|
isModalOpen: PropTypes.bool.isRequired,
|
||||||
onModalOpen: PropTypes.func,
|
onOpen: PropTypes.func.isRequired,
|
||||||
onModalClose: PropTypes.func,
|
onClose: PropTypes.func.isRequired,
|
||||||
|
dropdownPlacement: PropTypes.string,
|
||||||
|
openDropdownId: PropTypes.number,
|
||||||
};
|
};
|
||||||
|
|
||||||
static defaultProps = {
|
static defaultProps = {
|
||||||
|
@ -133,38 +136,28 @@ export default class Dropdown extends React.PureComponent {
|
||||||
};
|
};
|
||||||
|
|
||||||
state = {
|
state = {
|
||||||
placement: null,
|
id: id++,
|
||||||
};
|
};
|
||||||
|
|
||||||
handleClick = ({ target }) => {
|
handleClick = ({ target }) => {
|
||||||
if (this.state.placement) {
|
if (this.state.id === this.props.openDropdownId) {
|
||||||
this.setState({ placement: null });
|
this.handleClose();
|
||||||
} else if (this.props.isUserTouching() && this.props.onModalOpen) {
|
|
||||||
const { status, items } = this.props;
|
|
||||||
|
|
||||||
this.props.onModalOpen({
|
|
||||||
status,
|
|
||||||
actions: items,
|
|
||||||
onClick: this.handleItemClick,
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
const { top } = target.getBoundingClientRect();
|
const { top } = target.getBoundingClientRect();
|
||||||
this.setState({ placement: top * 2 < innerHeight ? 'bottom' : 'top' });
|
const placement = top * 2 < innerHeight ? 'bottom' : 'top';
|
||||||
|
|
||||||
|
this.props.onOpen(this.state.id, this.handleItemClick, placement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
handleClose = () => {
|
handleClose = () => {
|
||||||
if (this.props.onModalClose) {
|
this.props.onClose(this.state.id);
|
||||||
this.props.onModalClose();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.setState({ placement: null });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
handleKeyDown = e => {
|
handleKeyDown = e => {
|
||||||
switch(e.key) {
|
switch(e.key) {
|
||||||
case 'Enter':
|
case 'Enter':
|
||||||
this.handleClick();
|
this.handleClick(e);
|
||||||
break;
|
break;
|
||||||
case 'Escape':
|
case 'Escape':
|
||||||
this.handleClose();
|
this.handleClose();
|
||||||
|
@ -196,23 +189,22 @@ export default class Dropdown extends React.PureComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
render () {
|
render () {
|
||||||
const { icon, items, size, title, disabled } = this.props;
|
const { icon, items, size, title, disabled, dropdownPlacement, openDropdownId } = this.props;
|
||||||
const { placement } = this.state;
|
const open = this.state.id === openDropdownId;
|
||||||
const show = placement !== null;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div onKeyDown={this.handleKeyDown}>
|
<div onKeyDown={this.handleKeyDown}>
|
||||||
<IconButton
|
<IconButton
|
||||||
icon={icon}
|
icon={icon}
|
||||||
title={title}
|
title={title}
|
||||||
active={show}
|
active={open}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
size={size}
|
size={size}
|
||||||
ref={this.setTargetRef}
|
ref={this.setTargetRef}
|
||||||
onClick={this.handleClick}
|
onClick={this.handleClick}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Overlay show={show} placement={placement} target={this.findTarget}>
|
<Overlay show={open} placement={dropdownPlacement} target={this.findTarget}>
|
||||||
<DropdownMenu items={items} onClose={this.handleClose} />
|
<DropdownMenu items={items} onClose={this.handleClose} />
|
||||||
</Overlay>
|
</Overlay>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { openDropdownMenu, closeDropdownMenu } from '../actions/dropdown_menu';
|
||||||
import { openModal, closeModal } from '../actions/modal';
|
import { openModal, closeModal } from '../actions/modal';
|
||||||
import { connect } from 'react-redux';
|
import { connect } from 'react-redux';
|
||||||
import DropdownMenu from '../components/dropdown_menu';
|
import DropdownMenu from '../components/dropdown_menu';
|
||||||
|
@ -5,12 +6,22 @@ import { isUserTouching } from '../is_mobile';
|
||||||
|
|
||||||
const mapStateToProps = state => ({
|
const mapStateToProps = state => ({
|
||||||
isModalOpen: state.get('modal').modalType === 'ACTIONS',
|
isModalOpen: state.get('modal').modalType === 'ACTIONS',
|
||||||
|
dropdownPlacement: state.getIn(['dropdown_menu', 'placement']),
|
||||||
|
openDropdownId: state.getIn(['dropdown_menu', 'openId']),
|
||||||
});
|
});
|
||||||
|
|
||||||
const mapDispatchToProps = dispatch => ({
|
const mapDispatchToProps = (dispatch, { status, items }) => ({
|
||||||
isUserTouching,
|
onOpen(id, onItemClick, dropdownPlacement) {
|
||||||
onModalOpen: props => dispatch(openModal('ACTIONS', props)),
|
dispatch(isUserTouching() ? openModal('ACTIONS', {
|
||||||
onModalClose: () => dispatch(closeModal()),
|
status,
|
||||||
|
actions: items,
|
||||||
|
onClick: onItemClick,
|
||||||
|
}) : openDropdownMenu(id, dropdownPlacement));
|
||||||
|
},
|
||||||
|
onClose(id) {
|
||||||
|
dispatch(closeModal());
|
||||||
|
dispatch(closeDropdownMenu(id));
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export default connect(mapStateToProps, mapDispatchToProps)(DropdownMenu);
|
export default connect(mapStateToProps, mapDispatchToProps)(DropdownMenu);
|
||||||
|
|
|
@ -56,6 +56,7 @@ const messages = defineMessages({
|
||||||
const mapStateToProps = state => ({
|
const mapStateToProps = state => ({
|
||||||
isComposing: state.getIn(['compose', 'is_composing']),
|
isComposing: state.getIn(['compose', 'is_composing']),
|
||||||
hasComposingText: state.getIn(['compose', 'text']) !== '',
|
hasComposingText: state.getIn(['compose', 'text']) !== '',
|
||||||
|
dropdownMenuIsOpen: state.getIn(['dropdown_menu', 'openId']) !== null,
|
||||||
});
|
});
|
||||||
|
|
||||||
const keyMap = {
|
const keyMap = {
|
||||||
|
@ -184,6 +185,7 @@ export default class UI extends React.PureComponent {
|
||||||
hasComposingText: PropTypes.bool,
|
hasComposingText: PropTypes.bool,
|
||||||
location: PropTypes.object,
|
location: PropTypes.object,
|
||||||
intl: PropTypes.object.isRequired,
|
intl: PropTypes.object.isRequired,
|
||||||
|
dropdownMenuIsOpen: PropTypes.bool,
|
||||||
};
|
};
|
||||||
|
|
||||||
state = {
|
state = {
|
||||||
|
@ -405,7 +407,7 @@ export default class UI extends React.PureComponent {
|
||||||
|
|
||||||
render () {
|
render () {
|
||||||
const { draggingOver } = this.state;
|
const { draggingOver } = this.state;
|
||||||
const { children, isComposing, location } = this.props;
|
const { children, isComposing, location, dropdownMenuIsOpen } = this.props;
|
||||||
|
|
||||||
const handlers = {
|
const handlers = {
|
||||||
help: this.handleHotkeyToggleHelp,
|
help: this.handleHotkeyToggleHelp,
|
||||||
|
@ -428,7 +430,7 @@ export default class UI extends React.PureComponent {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<HotKeys keyMap={keyMap} handlers={handlers} ref={this.setHotkeysRef}>
|
<HotKeys keyMap={keyMap} handlers={handlers} ref={this.setHotkeysRef}>
|
||||||
<div className={classNames('ui', { 'is-composing': isComposing })} ref={this.setRef}>
|
<div className={classNames('ui', { 'is-composing': isComposing })} ref={this.setRef} style={{ pointerEvents: dropdownMenuIsOpen ? 'none' : null }}>
|
||||||
<TabsBar />
|
<TabsBar />
|
||||||
|
|
||||||
<SwitchingColumnsArea location={location} onLayoutChange={this.handleLayoutChange}>
|
<SwitchingColumnsArea location={location} onLayoutChange={this.handleLayoutChange}>
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
import Immutable from 'immutable';
|
||||||
|
import {
|
||||||
|
DROPDOWN_MENU_OPEN,
|
||||||
|
DROPDOWN_MENU_CLOSE,
|
||||||
|
} from '../actions/dropdown_menu';
|
||||||
|
|
||||||
|
const initialState = Immutable.Map({ openId: null, placement: null });
|
||||||
|
|
||||||
|
export default function dropdownMenu(state = initialState, action) {
|
||||||
|
switch (action.type) {
|
||||||
|
case DROPDOWN_MENU_OPEN:
|
||||||
|
return state.merge({ openId: action.id, placement: action.placement });
|
||||||
|
case DROPDOWN_MENU_CLOSE:
|
||||||
|
return state.get('openId') === action.id ? state.set('openId', null) : state;
|
||||||
|
default:
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
import { combineReducers } from 'redux-immutable';
|
import { combineReducers } from 'redux-immutable';
|
||||||
|
import dropdown_menu from './dropdown_menu';
|
||||||
import timelines from './timelines';
|
import timelines from './timelines';
|
||||||
import meta from './meta';
|
import meta from './meta';
|
||||||
import alerts from './alerts';
|
import alerts from './alerts';
|
||||||
|
@ -26,6 +27,7 @@ import lists from './lists';
|
||||||
import listEditor from './list_editor';
|
import listEditor from './list_editor';
|
||||||
|
|
||||||
const reducers = {
|
const reducers = {
|
||||||
|
dropdown_menu,
|
||||||
timelines,
|
timelines,
|
||||||
meta,
|
meta,
|
||||||
alerts,
|
alerts,
|
||||||
|
|
Loading…
Reference in New Issue