Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1952 from matrix-org/luke/context-menus-downwards…
Browse files Browse the repository at this point in the history
…-keep-vertically-on-screen

Keep context menus that extend downwards vertically on screen
  • Loading branch information
lukebarnard1 committed Jun 12, 2018
2 parents e858cf3 + ffcba94 commit b18a8c4
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/components/structures/ContextualMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ export default class ContextualMenu extends React.Component {
hasBackground: PropTypes.bool,
}

constructor() {
super();
this.state = {
contextMenuRect: null,
};

this.collectContextMenuRect = this.collectContextMenuRect.bind(this);
}

collectContextMenuRect(element) {
// We don't need to clean up when unmounting, so ignore
if (!element) return;

this.setState({
contextMenuRect: element.getBoundingClientRect(),
});
}

render() {
const position = {};
let chevronFace = null;
Expand All @@ -83,14 +101,29 @@ export default class ContextualMenu extends React.Component {
chevronFace = 'right';
}

const contextMenuRect = this.state.contextMenuRect || null;
const padding = 10;

const chevronOffset = {};
if (props.chevronFace) {
chevronFace = props.chevronFace;
}
if (chevronFace === 'top' || chevronFace === 'bottom') {
chevronOffset.left = props.chevronOffset;
} else {
chevronOffset.top = props.chevronOffset;
const target = position.top;

// By default, no adjustment is made
let adjusted = target;

// If we know the dimensions of the context menu, adjust its position
// such that it does not leave the (padded) window.
if (contextMenuRect) {
adjusted = Math.min(position.top, document.body.clientHeight - contextMenuRect.height - padding);
}

position.top = adjusted;
chevronOffset.top = Math.max(props.chevronOffset, props.chevronOffset + target - adjusted);
}

// To override the default chevron colour, if it's been set
Expand Down Expand Up @@ -154,7 +187,7 @@ export default class ContextualMenu extends React.Component {
// FIXME: If a menu uses getDefaultProps it clobbers the onFinished
// property set here so you can't close the menu from a button click!
return <div className={className} style={position}>
<div className={menuClasses} style={menuStyle}>
<div className={menuClasses} style={menuStyle} ref={this.collectContextMenuRect}>
{ chevron }
<ElementClass {...props} onFinished={props.closeMenu} onResize={props.windowResize} />
</div>
Expand Down

0 comments on commit b18a8c4

Please sign in to comment.