Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: correct the offset of the mirror #455

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"helper": ["./scripts/test/helper.js"],
"shared/*": ["./src/shared/*"]
}
}
}
68 changes: 61 additions & 7 deletions src/Draggable/Plugins/Mirror/Mirror.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import AbstractPlugin from 'shared/AbstractPlugin';
import {browserInfo} from 'shared/utils';

import {
MirrorCreateEvent,
Expand Down Expand Up @@ -277,13 +278,14 @@ export default class Mirror extends AbstractPlugin {
[onMirrorCreated]({mirror, source, sensorEvent}) {
const mirrorClasses = this.draggable.getClassNamesFor('mirror');

const setState = ({mirrorOffset, initialX, initialY, ...args}) => {
const setState = ({mirrorOffset, correctOffset, initialX, initialY, ...args}) => {
this.mirrorOffset = mirrorOffset;
this.correctOffset = correctOffset;
this.initialX = initialX;
this.initialY = initialY;
this.lastMovedX = initialX;
this.lastMovedY = initialY;
return {mirrorOffset, initialX, initialY, ...args};
return {mirrorOffset, correctOffset, initialX, initialY, ...args};
};

mirror.style.display = 'none';
Expand All @@ -304,6 +306,7 @@ export default class Mirror extends AbstractPlugin {
// Fix reflow here
.then(computeMirrorDimensions)
.then(calculateMirrorOffset)
.then(calculateCorrectOffset)
.then(resetMirror)
.then(addMirrorClasses)
.then(positionMirror({initial: true}))
Expand Down Expand Up @@ -334,6 +337,7 @@ export default class Mirror extends AbstractPlugin {
mirror: mirrorEvent.mirror,
sensorEvent: mirrorEvent.sensorEvent,
mirrorOffset: this.mirrorOffset,
correctOffset: this.correctOffset,
options: this.options,
initialX: this.initialX,
initialY: this.initialY,
Expand Down Expand Up @@ -406,6 +410,50 @@ function calculateMirrorOffset({sensorEvent, sourceRect, options, ...args}) {
});
}

/**
* Calculates correct offset
* Adds correctOffset to state, because `position: fixed;` has some except
* See https://developer.mozilla.org/en-US/docs/Web/CSS/position
* @param {Object} state
* @param {HTMLElement} state.mirror
* @return {Promise}
* @private
*/
function calculateCorrectOffset({mirror, ...args}) {
return withPromise((resolve) => {
let x = 0;
let y = 0;
let container = mirror;

if (!browserInfo.IE11OrLess) {
do {
if (container && container.getBoundingClientRect) {
const containerStyle = getComputedStyle(container);
if (
(containerStyle.transform && containerStyle.transform !== 'none') ||
(containerStyle.perspective && containerStyle.perspective !== 'none') ||
(containerStyle.filter && containerStyle.filter !== 'none') ||
(containerStyle.backdropFilter && containerStyle.backdropFilter !== 'none')
) {
const containerRect = container.getBoundingClientRect();

// Set relative to edges of padding box of container
x = containerRect.top + parseInt(containerStyle['border-top-width'], 10);
y = containerRect.left + parseInt(containerStyle['border-left-width'], 10);

break;
}
}
/* jshint boss:true */
} while ((container = container.parentNode));
}

const correctOffset = {x, y};

resolve({mirror, correctOffset, ...args});
});
}

/**
* Applys mirror styles
* @param {Object} state
Expand All @@ -426,7 +474,7 @@ function resetMirror({mirror, source, options, ...args}) {
offsetWidth = computedSourceStyles.getPropertyValue('width');
}

mirror.style.display = null;
mirror.style.display = '';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A style declaration is reset by setting it to null or an empty string, e.g., elt.style.color = null. Internet Explorer requires setting it to an empty string, and does not do anything when setting it to null.

mirror.style.position = 'fixed';
mirror.style.pointerEvents = 'none';
mirror.style.top = 0;
Expand Down Expand Up @@ -478,6 +526,7 @@ function removeMirrorID({mirror, ...args}) {
* @param {HTMLElement} state.mirror
* @param {SensorEvent} state.sensorEvent
* @param {Object} state.mirrorOffset
* @param {Object} state.correctOffset
* @param {Number} state.initialY
* @param {Number} state.initialX
* @param {Object} state.options
Expand All @@ -489,6 +538,7 @@ function positionMirror({withFrame = false, initial = false} = {}) {
mirror,
sensorEvent,
mirrorOffset,
correctOffset,
initialY,
initialX,
scrollOffset,
Expand All @@ -505,18 +555,22 @@ function positionMirror({withFrame = false, initial = false} = {}) {
mirror,
sensorEvent,
mirrorOffset,
correctOffset,
options,
...args,
};

if (mirrorOffset) {
const thresholdX = options.thresholdX || 1;
const thresholdY = options.thresholdY || 1;

const x = passedThreshX
? Math.round((sensorEvent.clientX - mirrorOffset.left - scrollOffset.x) / (options.thresholdX || 1)) *
(options.thresholdX || 1)
? Math.round((sensorEvent.clientX - mirrorOffset.left - scrollOffset.x - correctOffset.x) / thresholdX) *
thresholdX
: Math.round(lastMovedX);
const y = passedThreshY
? Math.round((sensorEvent.clientY - mirrorOffset.top - scrollOffset.y) / (options.thresholdY || 1)) *
(options.thresholdY || 1)
? Math.round((sensorEvent.clientY - mirrorOffset.top - scrollOffset.y - correctOffset.y) / thresholdY) *
thresholdY
: Math.round(lastMovedY);

if ((options.xAxis && options.yAxis) || initial) {
Expand Down
12 changes: 12 additions & 0 deletions src/shared/utils/browserInfo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function userAgent(pattern) {
if (typeof window !== 'undefined' && window.navigator) {
return Boolean(navigator.userAgent.match(pattern));
}
return false;
}

const browserInfo = {
IE11OrLess: userAgent(/(?:Trident.*rv[ :]?11\.|msie|iemobile|Windows Phone)/i),
};

export default browserInfo;
1 change: 1 addition & 0 deletions src/shared/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export {default as closest} from './closest';
export {default as requestNextAnimationFrame} from './requestNextAnimationFrame';
export {default as distance} from './distance';
export {default as touchCoords} from './touchCoords';
export {default as browserInfo} from './browserInfo';