Skip to content

Commit

Permalink
Create synchronous method to get bounding client rect
Browse files Browse the repository at this point in the history
Summary: Changelog: [internal] Added internal method in Fabric to synchronously get layout information

Reviewed By: sammy-SC

Differential Revision: D43080208

fbshipit-source-id: 8af9fe8ef9dbfe37c4b70101cc13e12905ee5b69
  • Loading branch information
rubennorte authored and facebook-github-bot committed Feb 9, 2023
1 parent 6018c19 commit 97d90c6
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ jsi::Value UIManagerBinding::get(
*shadowNodeFromValue(runtime, arguments[0]),
nullptr,
{/* .includeTransform = */ true,
/* includeViewportOffset = */ true});
/* .includeViewportOffset = */ true});

auto onSuccessFunction =
arguments[1].getObject(runtime).getFunction(runtime);
Expand All @@ -641,6 +641,40 @@ jsi::Value UIManagerBinding::get(
});
}

if (methodName == "getBoundingClientRect") {
// This is similar to `measureInWindow`, except it's explicitly synchronous
// (returns the result instead of passing it to a callback).
// The behavior is similar to `Element.prototype.getBoundingClientRect` from
// Web.
return jsi::Function::createFromHostFunction(
runtime,
name,
1,
[uiManager](
jsi::Runtime &runtime,
jsi::Value const & /*thisValue*/,
jsi::Value const *arguments,
size_t /*count*/) noexcept -> jsi::Value {
auto layoutMetrics = uiManager->getRelativeLayoutMetrics(
*shadowNodeFromValue(runtime, arguments[0]),
nullptr,
{/* .includeTransform = */ true,
/* .includeViewportOffset = */ true});

if (layoutMetrics == EmptyLayoutMetrics) {
return jsi::Value::undefined();
}

auto frame = layoutMetrics.frame;
return jsi::Array::createWithElements(
runtime,
jsi::Value{runtime, (double)frame.origin.x},
jsi::Value{runtime, (double)frame.origin.y},
jsi::Value{runtime, (double)frame.size.width},
jsi::Value{runtime, (double)frame.size.height});
});
}

if (methodName == "sendAccessibilityEvent") {
return jsi::Function::createFromHostFunction(
runtime,
Expand Down

0 comments on commit 97d90c6

Please sign in to comment.