Skip to content

Commit 97d90c6

Browse files
rubennortefacebook-github-bot
authored andcommitted
Create synchronous method to get bounding client rect
Summary: Changelog: [internal] Added internal method in Fabric to synchronously get layout information Reviewed By: sammy-SC Differential Revision: D43080208 fbshipit-source-id: 8af9fe8ef9dbfe37c4b70101cc13e12905ee5b69
1 parent 6018c19 commit 97d90c6

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ jsi::Value UIManagerBinding::get(
620620
*shadowNodeFromValue(runtime, arguments[0]),
621621
nullptr,
622622
{/* .includeTransform = */ true,
623-
/* includeViewportOffset = */ true});
623+
/* .includeViewportOffset = */ true});
624624

625625
auto onSuccessFunction =
626626
arguments[1].getObject(runtime).getFunction(runtime);
@@ -641,6 +641,40 @@ jsi::Value UIManagerBinding::get(
641641
});
642642
}
643643

644+
if (methodName == "getBoundingClientRect") {
645+
// This is similar to `measureInWindow`, except it's explicitly synchronous
646+
// (returns the result instead of passing it to a callback).
647+
// The behavior is similar to `Element.prototype.getBoundingClientRect` from
648+
// Web.
649+
return jsi::Function::createFromHostFunction(
650+
runtime,
651+
name,
652+
1,
653+
[uiManager](
654+
jsi::Runtime &runtime,
655+
jsi::Value const & /*thisValue*/,
656+
jsi::Value const *arguments,
657+
size_t /*count*/) noexcept -> jsi::Value {
658+
auto layoutMetrics = uiManager->getRelativeLayoutMetrics(
659+
*shadowNodeFromValue(runtime, arguments[0]),
660+
nullptr,
661+
{/* .includeTransform = */ true,
662+
/* .includeViewportOffset = */ true});
663+
664+
if (layoutMetrics == EmptyLayoutMetrics) {
665+
return jsi::Value::undefined();
666+
}
667+
668+
auto frame = layoutMetrics.frame;
669+
return jsi::Array::createWithElements(
670+
runtime,
671+
jsi::Value{runtime, (double)frame.origin.x},
672+
jsi::Value{runtime, (double)frame.origin.y},
673+
jsi::Value{runtime, (double)frame.size.width},
674+
jsi::Value{runtime, (double)frame.size.height});
675+
});
676+
}
677+
644678
if (methodName == "sendAccessibilityEvent") {
645679
return jsi::Function::createFromHostFunction(
646680
runtime,

0 commit comments

Comments
 (0)