Skip to content

Commit

Permalink
fix(web): fix gesture responder dimensions measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
msand committed Jan 4, 2020
1 parent f93bdde commit 36c20b3
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/ReactNativeSVG.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,52 @@ const prepare = <T extends BaseProps>(
return clean;
};

const getBoundingClientRect = (node: SVGElement) => {
if (node) {
// @ts-ignore
const isElement = node.nodeType === 1; /* Node.ELEMENT_NODE */
// @ts-ignore
if (isElement && typeof node.getBoundingClientRect === 'function') {
// @ts-ignore
return node.getBoundingClientRect();
}
}
};

const measureLayout = (
node: SVGElement,
callback: (
x: number,
y: number,
width: number,
height: number,
left: number,
top: number,
) => void,
) => {
// @ts-ignore
const relativeNode = node && node.parentNode;
if (node && relativeNode) {
setTimeout(() => {
const relativeRect = getBoundingClientRect(relativeNode);
const { height, left, top, width } = getBoundingClientRect(node);
const x = left - relativeRect.left;
const y = top - relativeRect.top;
callback(x, y, width, height, left, top);
}, 0);
}
};

function remeasure() {
// @ts-ignore
const tag = this.state.touchable.responderID;
if (tag == null) {
return;
}
// @ts-ignore
measureLayout(tag, this._handleQueryLayout);
}

export class WebShape<
P extends BaseProps = BaseProps,
C = {}
Expand All @@ -183,6 +229,7 @@ export class WebShape<
constructor(props: P, context: C) {
super(props, context);
SvgTouchableMixin(this);
this._remeasureMetricsOnActivation = remeasure.bind(this);
}
}

Expand Down

0 comments on commit 36c20b3

Please sign in to comment.