[Glitch] Place dropdown menu top if it is closer to the bottom of the viewport

Port f6a8d835d3 to glitch-soc
This commit is contained in:
Thibaut Girka 2018-03-30 12:31:29 +02:00
parent 1bd12cf8eb
commit 2871a82fcc

View file

@ -29,6 +29,10 @@ class DropdownMenu extends React.PureComponent {
placement: 'bottom', placement: 'bottom',
}; };
state = {
mounted: false,
};
handleDocumentClick = e => { handleDocumentClick = e => {
if (this.node && !this.node.contains(e.target)) { if (this.node && !this.node.contains(e.target)) {
this.props.onClose(); this.props.onClose();
@ -38,6 +42,7 @@ class DropdownMenu extends React.PureComponent {
componentDidMount () { componentDidMount () {
document.addEventListener('click', this.handleDocumentClick, false); document.addEventListener('click', this.handleDocumentClick, false);
document.addEventListener('touchend', this.handleDocumentClick, listenerOptions); document.addEventListener('touchend', this.handleDocumentClick, listenerOptions);
this.setState({ mounted: true });
} }
componentWillUnmount () { componentWillUnmount () {
@ -82,11 +87,15 @@ class DropdownMenu extends React.PureComponent {
render () { render () {
const { items, style, placement, arrowOffsetLeft, arrowOffsetTop } = this.props; const { items, style, placement, arrowOffsetLeft, arrowOffsetTop } = this.props;
const { mounted } = this.state;
return ( 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 }) }}> <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 }) => ( {({ 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 }} /> <div className={`dropdown-menu__arrow ${placement}`} style={{ left: arrowOffsetLeft, top: arrowOffsetTop }} />
<ul> <ul>
@ -124,11 +133,13 @@ export default class Dropdown extends React.PureComponent {
}; };
state = { state = {
expanded: false, placement: null,
}; };
handleClick = () => { handleClick = ({ target }) => {
if (!this.state.expanded && this.props.isUserTouching() && this.props.onModalOpen) { if (this.state.placement) {
this.setState({ placement: null });
} else if (this.props.isUserTouching() && this.props.onModalOpen) {
const { status, items } = this.props; const { status, items } = this.props;
this.props.onModalOpen({ this.props.onModalOpen({
@ -141,11 +152,10 @@ export default class Dropdown extends React.PureComponent {
} : null } : null
), ),
}); });
} else {
return; const { top } = target.getBoundingClientRect();
this.setState({ placement: top * 2 < innerHeight ? 'bottom' : 'top' });
} }
this.setState({ expanded: !this.state.expanded });
} }
handleClose = () => { handleClose = () => {
@ -153,7 +163,7 @@ export default class Dropdown extends React.PureComponent {
this.props.onModalClose(); this.props.onModalClose();
} }
this.setState({ expanded: false }); this.setState({ placement: null });
} }
handleKeyDown = e => { handleKeyDown = e => {
@ -191,21 +201,22 @@ export default class Dropdown extends React.PureComponent {
render () { render () {
const { icon, items, size, ariaLabel, disabled } = this.props; const { icon, items, size, ariaLabel, disabled } = this.props;
const { expanded } = this.state; const { placement } = this.state;
const show = placement !== null;
return ( return (
<div onKeyDown={this.handleKeyDown}> <div onKeyDown={this.handleKeyDown}>
<IconButton <IconButton
icon={icon} icon={icon}
title={ariaLabel} title={ariaLabel}
active={expanded} active={show}
disabled={disabled} disabled={disabled}
size={size} size={size}
ref={this.setTargetRef} ref={this.setTargetRef}
onClick={this.handleClick} 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} /> <DropdownMenu items={items} onClose={this.handleClose} />
</Overlay> </Overlay>
</div> </div>