Skip to content

Commit

Permalink
Add getMargins as a option
Browse files Browse the repository at this point in the history
  • Loading branch information
brunnolou committed Feb 25, 2018
1 parent 0b26ab1 commit 5b186d7
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,27 @@ import { interpolateObject } from './util';

const { pipe } = transform;

const getBox = (elm, { margin = false } = {}) => {
const getBox = (elm, { getMargins = false } = {}) => {
const box = elm.getBoundingClientRect();
const styles = getComputedStyle(elm);

return {
top:
box.top - window.scrollY - (margin ? parseInt(styles.marginTop, 10) : 0),
box.top +
window.scrollY -
(getMargins ? parseInt(styles.marginTop, 10) : 0),
left:
box.left -
box.left +
window.scrollX -
(margin ? parseInt(styles.marginLeft, 10) : 0),
(getMargins ? parseInt(styles.marginLeft, 10) : 0),
width:
box.width +
(margin
(getMargins
? parseInt(styles.marginLeft, 10) + parseInt(styles.marginRight, 10)
: 0),
height:
box.height +
(margin
(getMargins
? parseInt(styles.marginTop, 10) + parseInt(styles.marginBottom, 10)
: 0),
};
Expand Down Expand Up @@ -118,7 +120,9 @@ class Morph extends Component {

componentWillUnmount() {
// Remove clones.
this.elementsCloned.forEach(node => this.props.portalElement.removeChild(node));
this.elementsCloned.forEach(node =>
this.props.portalElement.removeChild(node),
);
}

elementFrom = {};
Expand Down Expand Up @@ -215,12 +219,17 @@ class Morph extends Component {
morph = key => {
const {
element: original,
options: { zIndex, easing: optEasing = x => x, ...options } = {},
options: {
zIndex,
getMargins = true,
easing: optEasing = x => x,
...options
} = {},
} = this.elementFrom[key];
const { element: target } = this.elementTo[key];

const originalRect = getBox(original, { margin: true });
const targetRect = getBox(target);
const originalRect = getBox(original, { getMargins });
const targetRect = getBox(target, { getMargins });
const originalCloneContainer = document.createElement('div');
const originalClone = original.cloneNode(true);

Expand Down

0 comments on commit 5b186d7

Please sign in to comment.