Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Place dropdown menu top if it is closer to the bottom of the viewport #6641

Merged
merged 1 commit into from
Mar 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions app/javascript/mastodon/components/dropdown_menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class DropdownMenu extends React.PureComponent {
placement: 'bottom',
};

state = {
mounted: false,
};

handleDocumentClick = e => {
if (this.node && !this.node.contains(e.target)) {
this.props.onClose();
Expand All @@ -38,6 +42,7 @@ class DropdownMenu extends React.PureComponent {
componentDidMount () {
document.addEventListener('click', this.handleDocumentClick, false);
document.addEventListener('touchend', this.handleDocumentClick, listenerOptions);
this.setState({ mounted: true });
}

componentWillUnmount () {
Expand Down Expand Up @@ -82,11 +87,15 @@ class DropdownMenu extends React.PureComponent {

render () {
const { items, style, placement, arrowOffsetLeft, arrowOffsetTop } = this.props;
const { mounted } = this.state;

return (
<Motion defaultStyle={{ opacity: 0, scaleX: 0.85, scaleY: 0.75 }} style={{ opacity: spring(1, { damping: 35, stiffness: 400 }), scaleX: spring(1, { damping: 35, stiffness: 400 }), scaleY: spring(1, { damping: 35, stiffness: 400 }) }}>
{({ opacity, scaleX, scaleY }) => (
<div className='dropdown-menu' style={{ ...style, opacity: opacity, transform: `scale(${scaleX}, ${scaleY})` }} ref={this.setRef}>
// It should not be transformed when mounting because the resulting
// size will be used to determine the coordinate of the menu by
// react-overlays
<div className='dropdown-menu' style={{ ...style, opacity: opacity, transform: mounted ? `scale(${scaleX}, ${scaleY})` : null }} ref={this.setRef}>
<div className={`dropdown-menu__arrow ${placement}`} style={{ left: arrowOffsetLeft, top: arrowOffsetTop }} />

<ul>
Expand Down Expand Up @@ -124,31 +133,32 @@ export default class Dropdown extends React.PureComponent {
};

state = {
expanded: false,
placement: null,
};

handleClick = () => {
if (!this.state.expanded && this.props.isUserTouching() && this.props.onModalOpen) {
handleClick = ({ target }) => {
if (this.state.placement) {
this.setState({ placement: null });
} else if (this.props.isUserTouching() && this.props.onModalOpen) {
const { status, items } = this.props;

this.props.onModalOpen({
status,
actions: items,
onClick: this.handleItemClick,
});

return;
} else {
const { top } = target.getBoundingClientRect();
this.setState({ placement: top * 2 < innerHeight ? 'bottom' : 'top' });
}

this.setState({ expanded: !this.state.expanded });
}

handleClose = () => {
if (this.props.onModalClose) {
this.props.onModalClose();
}

this.setState({ expanded: false });
this.setState({ placement: null });
}

handleKeyDown = e => {
Expand Down Expand Up @@ -187,21 +197,22 @@ export default class Dropdown extends React.PureComponent {

render () {
const { icon, items, size, title, disabled } = this.props;
const { expanded } = this.state;
const { placement } = this.state;
const show = placement !== null;

return (
<div onKeyDown={this.handleKeyDown}>
<IconButton
icon={icon}
title={title}
active={expanded}
active={show}
disabled={disabled}
size={size}
ref={this.setTargetRef}
onClick={this.handleClick}
/>

<Overlay show={expanded} placement='bottom' target={this.findTarget}>
<Overlay show={show} placement={placement} target={this.findTarget}>
<DropdownMenu items={items} onClose={this.handleClose} />
</Overlay>
</div>
Expand Down