Skip to content

Commit

Permalink
fix(*): relative positioning
Browse files Browse the repository at this point in the history
fix wrong position when contextmenu is located in absolute or
fixed positioned div
  • Loading branch information
DanielSchuech committed Nov 15, 2017
1 parent da8bf7b commit 41d0417
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/contextmenu/contextmenu.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
styles: [
`:host(.contextmenu-container) {
display: none;
position: fixed;
position: absolute;
z-index: 1300;
}`,
`:host(.contextmenu-container.show) {
Expand All @@ -40,9 +40,20 @@ export class ContextmenuComponent {
}

show(x: number, y: number) {
this.renderer.setElementStyle(this.element.nativeElement, 'left', `${x}px`);
this.renderer.setElementStyle(this.element.nativeElement, 'top', `${y}px`);
this.isVisible = true;

// timeout required that menu is visible, otherwise offsetParent is always null
setTimeout(() => {
let offsetParent = this.element.nativeElement.offsetParent;
if (offsetParent && offsetParent !== document.body) {
// compute position relative to offset parent
let bb = offsetParent.getBoundingClientRect();
x -= bb.left;
y -= bb.top;
}
this.renderer.setElementStyle(this.element.nativeElement, 'left', x + 'px');
this.renderer.setElementStyle(this.element.nativeElement, 'top', y + 'px');
});
}

hide() {
Expand Down

0 comments on commit 41d0417

Please sign in to comment.