diff --git a/packages/asc-web-components/context-menu-button/index.js b/packages/asc-web-components/context-menu-button/index.js index 0531394cb1..e5ecac0062 100644 --- a/packages/asc-web-components/context-menu-button/index.js +++ b/packages/asc-web-components/context-menu-button/index.js @@ -29,6 +29,8 @@ class ContextMenuButton extends React.Component { isOpen: props.opened, data: props.data, displayType, + offsetX: props.manualX, + offsetY: props.manualY, }; this.throttledResize = throttle(this.resize, 300); } @@ -71,7 +73,7 @@ class ContextMenuButton extends React.Component { } if (this.props.manualX !== prevProps.manualX) { - this.onIconButtonClick(); + this.onContextClick(); } if (this.props.opened && this.state.displayType === "aside") { @@ -83,6 +85,20 @@ class ContextMenuButton extends React.Component { } } + onContextClick = () => { + if (this.props.isDisabled) { + this.stopAction; + return; + } + + this.setState({ + data: this.props.getData(), + isOpen: !this.state.isOpen, + offsetX: this.props.manualX, + offsetY: this.props.manualY, + }); + }; + onIconButtonClick = () => { if (this.props.isDisabled) { this.stopAction; @@ -93,6 +109,8 @@ class ContextMenuButton extends React.Component { { data: this.props.getData(), isOpen: !this.state.isOpen, + offsetX: "0px", + offsetY: "100%", }, () => !this.props.isDisabled && @@ -107,7 +125,7 @@ class ContextMenuButton extends React.Component { const dropDownItem = path ? path.find((x) => x === this.ref.current) : null; if (dropDownItem) return; - this.onIconButtonClick(); + this.onClose(); }; onDropDownItemClick = (item, e) => { @@ -151,11 +169,9 @@ class ContextMenuButton extends React.Component { style, isFill, // eslint-disable-line react/prop-types asideHeader, // eslint-disable-line react/prop-types - manualX, - manualY, } = this.props; - const { isOpen, displayType } = this.state; + const { isOpen, displayType, offsetX, offsetY } = this.state; const iconButtonName = isOpen && iconOpenName ? iconOpenName : iconName; return ( @@ -177,8 +193,9 @@ class ContextMenuButton extends React.Component { /> {displayType === "dropdown" ? ( { e.preventDefault(); - const cursorX = -(window.innerWidth - e.pageX) + "px"; - const cursorY = "-3px"; + const menu = document.getElementById("contextMenu"); + + const containerBounds = + this.container !== document && this.container.getBoundingClientRect(); + + const clickX = containerBounds.right - e.clientX; + const clickY = e.clientY - containerBounds.top; + const containerWidth = this.container.offsetWidth; + const containerHeight = this.container.offsetHeight; + const menuWidth = (menu && menu.offsetWidth) || 180; + const menuHeight = menu && menu.offsetHeight; + + const left = containerWidth - clickX > menuWidth && clickX < menuWidth; + const bottom = containerHeight - clickY < menuHeight && clickY > menuHeight; + + let newTop = `0px`; + let newRight = `0px`; + + newRight = !left ? `${clickX - menuWidth - 8}px` : `${clickX + 8}px`; + newTop = bottom ? `${clickY - menuHeight}px` : `${clickY}px`; this.setState({ - contextX: cursorX, - contextY: cursorY, contextOpened: !this.state.contextOpened, + contextX: newRight, + contextY: newTop, }); }; @@ -59,6 +81,8 @@ class Row extends React.Component { sectionWidth, } = this.props; + const { contextOpened, contextX, contextY } = this.state; + const renderCheckbox = Object.prototype.hasOwnProperty.call( this.props, "checked" @@ -107,13 +131,14 @@ class Row extends React.Component { )} {renderContext ? ( ) : (