From 0b1e3f2cf5b6d857396dd2d31d3b02b4ee32f377 Mon Sep 17 00:00:00 2001 From: ztplz Date: Sun, 17 Feb 2019 16:50:34 +0800 Subject: [PATCH] Recheck points after dom-align adjustment --- src/Align.jsx | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/Align.jsx b/src/Align.jsx index 2305652..9c23311 100644 --- a/src/Align.jsx +++ b/src/Align.jsx @@ -145,6 +145,37 @@ class Align extends Component { restoreFocus(activeElement, source); + // get correct points + // in dom-align, points will be reset after failed match + // https://github.com/ant-design/ant-design/issues/14582 + if (result.overflow.adjustX || result.overflow.adjustY) { + const sourceOffset = source.getBoundingClientRect(); + const targetOffset = element.getBoundingClientRect(); + const sourceCenter = { + left: sourceOffset.left + sourceOffset.width / 2, + top: sourceOffset.top + sourceOffset.height / 2, + }; + const targetCenter = { + left: targetOffset.left + targetOffset.width / 2, + top: targetOffset.top + targetOffset.height / 2, + } + const ratio = (sourceCenter.left - targetCenter.left) / (sourceCenter.top - targetCenter.top); + if (ratio >= -1 || ratio <= 1) { + if (sourceCenter.top < targetCenter.top) { + result.points = ["bc", "tc"]; + } else { + result.points = ["tc", "bc"]; + } + } else { + /* eslint-disable-next-line */ + if (sourceCenter.left < targetCenter.left) { + result.points = ["rc", "lc"]; + } else { + result.points = ["lc", "rc"]; + } + } + } + if (onAlign) { onAlign(source, result); }