Skip to content

Commit

Permalink
fix: wrong position of context menu with browser scroll (fix #34) (#44)
Browse files Browse the repository at this point in the history
* fix: wrong position of context menu with browser scroll

* chore: change the method to get the mouse position to the utility function

chore: use pageX,Y of mouse event to get mouse position

* docs: add comment for location of wrapper element
  • Loading branch information
jajugoguma committed Jun 23, 2021
1 parent d0d9d1d commit 6b4df3a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/getting-started.md
Expand Up @@ -20,6 +20,7 @@ When you select each menu, or click outside of the area where the menu closes.
## Write wrapper elements

```html
<!-- Notice: The wrapper element must be located below the body. -->
<!-- Tag to create the context menu -->
<div id="tui-context-menu-container"></div>

Expand Down
7 changes: 2 additions & 5 deletions src/js/contextmenu.js
Expand Up @@ -6,7 +6,6 @@
import forEachArray from 'tui-code-snippet/collection/forEachArray';
import off from 'tui-code-snippet/domEvent/off';
import on from 'tui-code-snippet/domEvent/on';
import getMousePosition from 'tui-code-snippet/domEvent/getMousePosition';
import preventDefault from 'tui-code-snippet/domEvent/preventDefault';
import addClass from 'tui-code-snippet/domUtil/addClass';
import closest from 'tui-code-snippet/domUtil/closest';
Expand All @@ -19,7 +18,7 @@ import debounce from 'tui-code-snippet/tricks/debounce';

import FloatingLayer from './floatingLayer';
import Map from './Map';
import {sendHostName} from './util';
import {sendHostName, getMousePosition} from './util';
import tmpl from '../template/contextmenu';

const DEFAULT_ZINDEX = 999;
Expand Down Expand Up @@ -420,10 +419,8 @@ class ContextMenu {

this.activeLayer = relatedLayer;

const position = getMousePosition(clickEvent, this.activeLayer.container);
const {left, top} = getMousePosition(clickEvent, this.activeLayer.container);

/* clickEvent's clientX, clientY */
const [left, top] = position;
const debouncedMouseMove = debounce(mouseMoveEvent => this._onMouseMove(mouseMoveEvent), opt.delay);

this.cloneMouseMoveEvent = function(mouseMoveEvent) {
Expand Down
12 changes: 12 additions & 0 deletions src/js/util.js
Expand Up @@ -12,3 +12,15 @@ import sendHostname from 'tui-code-snippet/request/sendHostname';
export const sendHostName = () => {
sendHostname('context-menu', 'UA-129987462-1');
};

/**
* Get mouse postion
* @param {MouseEvent} clickEvent - mouse event object
* @returns {Object} object of mouse position contains left and top
* @private
*/
export const getMousePosition = clickEvent => {
const {pageX: left, pageY: top} = clickEvent;

return {left, top};
};

0 comments on commit 6b4df3a

Please sign in to comment.