Skip to content

[ResizeObserver 3/3] ResizeObserver Web API implementation - #57723

Open
paradowstack wants to merge 42 commits into
react:mainfrom
paradowstack:feat/resize-observer
Open

[ResizeObserver 3/3] ResizeObserver Web API implementation#57723
paradowstack wants to merge 42 commits into
react:mainfrom
paradowstack:feat/resize-observer

Conversation

@paradowstack

@paradowstack paradowstack commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Stack 3/3 — parent: feat/LayoutEventEmitter. Review the two parents first.

Summary:

Implements the ResizeObserver Web API for the New Architecture, behind the enableResizeObserverByDefault flag (off by default).

The motivation is Web compatibility, and it's more capable than onLayout: callers pick which box to observe (content-box, border-box, device-pixel-content-box) and the sizes for those boxes are delivered in the notification.

The design is as follows: JS ResizeObserver/ResizeObserverEntry/ResizeObserverSize on top of a manager singleton and the NativeResizeObserver TurboModule, using the same notify + takeRecords pull model. In C++, ResizeObserverManager collects observed targets whose layout changed at commit time (via shadowTreeDidCommit) and then computes and delivers observations in the event loop's "update the rendering" step (RuntimeSchedulerResizeObserverDelegate::runResizeObservations), as the spec requires. Requires bridgeless + the modern event loop; the legacy scheduler gets a no-op delegate.

Known deviations from the spec (each pinned by a test):

  • One gather/broadcast per tick. RN has no synchronous re-layout loop, so the ResizeObserver loop completed with undelivered notifications error is never emitted; a callback that triggers a resize is delivered on the next tick.
  • Re-observing a target with the same box is a no-op and does not re-deliver. This matches browsers, not the literal observe() algorithm.
  • Callback order across observers follows first-observe() order rather than construction order.

Changelog:

[INTERNAL] [ADDED] - Add the ResizeObserver API, behind the enableResizeObserverByDefault feature flag

Test Plan:

  • Fantom ResizeObserver-itest.js covers many test scenarios.
  • rn-tester has ResizeObserver examples (box sizes, text, visibility); verified initial, resize, and animation-driven delivery there.
  • RuntimeSchedulerTest.cpp new test scenarios for update rendering loop.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 28, 2026
@paradowstack
paradowstack force-pushed the feat/resize-observer branch from a7e8c72 to d9da0c1 Compare July 28, 2026 15:58
@paradowstack
paradowstack marked this pull request as ready for review July 28, 2026 16:00
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Jul 28, 2026
@meta-codesync

meta-codesync Bot commented Jul 29, 2026

Copy link
Copy Markdown

@rubennorte has imported this pull request. If you are a Meta employee, you can view this in D114045415.

@rubennorte

Copy link
Copy Markdown
Member

Are the CI failures legit?

@paradowstack
paradowstack force-pushed the feat/resize-observer branch from 795d2b1 to 027b820 Compare July 29, 2026 12:02
@paradowstack

Copy link
Copy Markdown
Contributor Author

Are the CI failures legit?

There were missing targets in Package.swift file - the last commit fixed that.

@rubennorte rubennorte left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thanks a lot!

Could you please address the comments in the PR?

Also, I think we should add more tests to see what would happen if a callback scheduled an update in a component. React DOM would probably batch those and run in a separate task, but they also provide flushSync to process them immediately. We don't provide that now in RN but I think we should at some point for use cases like this.

Comment thread packages/react-native/flow/bom.js.flow Outdated
...
};

declare type ResizeObserverEntry = {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both this and ResizeObserverSize should be declared as as classes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

// See
// https://w3c.github.io/csswg-drafts/resize-observer/#broadcast-resize-notifications-h
if (resizeObserverDelegate_ != nullptr) {
resizeObserverDelegate_->runResizeObservations();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is incomplete. This should be run in a loop to process updates coming callbacks themselves. Check resizeObserverDepth in https://html.spec.whatwg.org/multipage/webappapis.html#event-loop-processing-model

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is true, I misunderstood a bit flushSync issue.

I have changed it to use resize loop as in the spec.

_blockSize: number;

constructor(inlineSize: number, blockSize: number) {
this._inlineSize = inlineSize;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this constructor and the one in ResizeObserverEntry private using the same pattern we're using in other Web classes (see <Class>_public)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They already follow that pattern - take a look at the end of these files.

zeroContentRect);
}

auto layoutMetrics = LayoutableShadowNode::computeRelativeLayoutMetrics(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call is expensive and not necessary for ResizeObserver. In ResizeObserver, the position of the element in the viewport is not considered, only its size. For that, you can just look at the own layout of the element, and don't need the bounding client rect computed from that function.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced with cheaper version.

// the padding box (i.e. the paddings only, excluding borders).
// https://w3c.github.io/csswg-drafts/resize-observer/#dom-resizeobserverentry-contentrect
auto contentRect = Rect{
.origin =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is against the spec. The origin is 0,0 for the contentRect provided.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only if target is an SVG element, from spec:

If target is not an SVG element or target is an SVG element with an associated CSS layout box do these steps:

Set this.contentRect.top to target.padding top.
Set this.contentRect.left to target.padding left.

If target is an SVG element without an associated CSS layout box do these steps:

Set this.contentRect.top and this.contentRect.left to 0.

See 3.4.4. Create and populate a ResizeObserverEntry point 7.

@rubennorte

Copy link
Copy Markdown
Member

Also, re: the changelog, this should just be [internal] until we promote this to the experimental/canary channels

@javache
javache self-requested a review July 30, 2026 08:56
@paradowstack
paradowstack force-pushed the feat/resize-observer branch from bc50446 to 6192ecd Compare July 31, 2026 13:50
@paradowstack

Copy link
Copy Markdown
Contributor Author

This looks great, thanks a lot!

Could you please address the comments in the PR?

Also, I think we should add more tests to see what would happen if a callback scheduled an update in a component. React DOM would probably batch those and run in a separate task, but they also provide flushSync to process them immediately. We don't provide that now in RN but I think we should at some point for use cases like this.

Thanks for the review!

I have addressed all the comments, now implementation includes proper resize loop (gather / broadcast / depth), more tests (Fantom + RuntimeSchedulerTest), corrected bom.js.flow types, and not use computeRelativeLayoutMetrics function.

cc @javache

@paradowstack
paradowstack force-pushed the feat/resize-observer branch from 6192ecd to afea976 Compare July 31, 2026 14:09
@paradowstack
paradowstack requested a review from rubennorte July 31, 2026 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. p: Callstack Partner: Callstack Partner Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants