Skip to content

Commit 58f9717

Browse files
committed
Add getClientRects to fabric fragment instance
We only have getBoundingClientRect available from RN currently. This should work as a substitute for this case because the equivalent of multi-rect elements in RN is a nested Text component. We only include the rects of top-level host components here so we can assume that calling getBoundingClientRect on each child is the same result.
1 parent b1c839e commit 58f9717

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

packages/react-native-renderer/src/ReactFiberConfigFabric.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,7 @@ export type FragmentInstanceType = {
641641
getRootNode(getRootNodeOptions?: {
642642
composed: boolean,
643643
}): Node | FragmentInstanceType,
644+
getClientRects: () => Array<DOMRect>,
644645
};
645646

646647
function FragmentInstance(this: FragmentInstanceType, fragmentFiber: Fiber) {
@@ -765,6 +766,27 @@ FragmentInstance.prototype.getRootNode = function (
765766
return rootNode;
766767
};
767768

769+
// $FlowFixMe[prop-missing]
770+
FragmentInstance.prototype.getClientRects = function (
771+
this: FragmentInstanceType,
772+
): Array<DOMRect> {
773+
const rects: Array<DOMRect> = [];
774+
traverseFragmentInstance(this._fragmentFiber, collectClientRects, rects);
775+
return rects;
776+
};
777+
function collectClientRects(child: Fiber, rects: Array<DOMRect>): boolean {
778+
const instance = getPublicInstanceFromHostFiber(child);
779+
780+
// getBoundingClientRect is available on Fabric instances while getClientRects is not.
781+
// This should work as a substitute in this case because the only equivalent of a multi-rect
782+
// element in RN would be a nested Text component.
783+
// Since we only use top-level nodes here, we can assume that getBoundingClientRect is sufficient.
784+
// $FlowFixMe[method-unbinding]
785+
// $FlowFixMe[incompatible-use] Fabric PublicInstance is opaque
786+
rects.push(instance.getBoundingClientRect());
787+
return false;
788+
}
789+
768790
export function createFragmentInstance(
769791
fragmentFiber: Fiber,
770792
): FragmentInstanceType {

0 commit comments

Comments
 (0)