Skip to content

android: size custom top-bar component buttons to content via a single bounded measure (Fabric)#8328

Open
israelko wants to merge 6 commits into
masterfrom
fix/android-topbar-button-remeasure-on-async-content
Open

android: size custom top-bar component buttons to content via a single bounded measure (Fabric)#8328
israelko wants to merge 6 commits into
masterfrom
fix/android-topbar-button-remeasure-on-async-content

Conversation

@israelko

@israelko israelko commented Jun 9, 2026

Copy link
Copy Markdown

Summary

  • Fixes custom React-component top bar buttons (topBar.leftButtons / rightButtons with { component }) collapsing to ~1px (invisible) on Android under the New Architecture (Fabric) when they don't declare explicit width/height. A content-hugging button (e.g. <View><Text/></View>) disappears entirely; when it's a left button it also takes the back-button slot, so the back affordance appears to vanish too.
  • Root cause: the two-pass measurement from android: fix custom top bar button measurement #8320/android: stabilize custom top bar button layout #8326 re-measured the hosted ReactSurfaceView with an EXACTLY box derived from the first (often still-empty) discovery pass. That forced box is pushed to Fabric via updateLayoutSpecs, so Fabric keeps laying the content out into the collapsed box and the button never recovers. The same forcing also stretched flex-content buttons to the full bar height.
  • Fix: replace the two-pass with a single bounded (AT_MOST) measure and let ReactSurfaceView size itself to its content (its documented AT_MOST = max-of-children behavior). When Fabric mounts/sizes the content it re-requests layout and onMeasure runs again against the real size. Explicit width/height still measure EXACTLY; vertical centering is preserved by the existing CENTER_VERTICAL gravity. Net change is a simplification (−108 / +26).

Validation

  • Unit tests: TitleBarReactButtonViewTest updated for the single-pass contract — 6/6 pass, including the centering test.
  • On-device (Pixel 7, newArchEnabled + hermesEnabled), RN 0.78.3 and 0.85.2:
    • Plain-text component button: was invisible (collapsed), now renders.
    • Flex component button (playground RoundedButton): renders correctly, pixel-identical to before this change.
  • Note: the collapse reproduces on both 0.78.3 and 0.85.2 — it was previously thought 0.78-only because flex-content buttons (which fill the constraint) masked it on 0.85.

…ports its size

On the New Architecture (Fabric), a custom React-component top bar button
without explicit width/height could collapse to ~1px. The hosted React
surface lays out asynchronously, off the native measure pass, so the first
onMeasure often observes a 0-sized child and freezes the button at the ~1px
floor with no subsequent re-measure.

Attach an OnLayoutChangeListener to the hosted child that re-requests layout
when the content's size changes (so onMeasure re-runs and sizes the button to
the content). The size-changed guard makes it converge once the button
matches the content. The explicit-dimensions check is done inside the listener
because onViewAdded fires from the superclass constructor (before `component`
is assigned) for the React surface view we need to observe.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@israelko israelko changed the title android: re-measure custom top bar button on async React content [Fix] android: re-measure custom top bar button on async React content Jun 9, 2026
israelko and others added 4 commits June 9, 2026 18:07
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The new test hardcoded the collapsed button width as 1px, which only holds at
mdpi (density 1.0). CI runs at a higher density, where resolveFinalWidth(0) =
ceil(dpToPx(1dp)) > 1. Derive the expected value from the same dpToPx formula
the production code uses instead of hardcoding it.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The re-measure assertion failed (expected 25 but was 1) because View.measure()
caches results: re-measuring the button with the same outer spec reused the
child's stale cached 0-width measurement, so resolveFinalWidth still saw 0.
In production RN re-measures the surface when it lays out new content; model
that with child.forceLayout() (after child.layout(), which clears FORCE_LAYOUT)
so the parent's super.onMeasure observes the real content width.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@israelko israelko requested a review from yedidyak June 10, 2026 11:15
@israelko israelko marked this pull request as draft June 11, 2026 09:41
…e bounded measure

Custom React-component top bar buttons without explicit width/height collapsed to
~1px (invisible) under the New Architecture (Fabric). The two-pass measurement from
#8320/#8326 re-measured the hosted ReactSurfaceView with an EXACTLY box derived from
the first (often still-empty) discovery pass; that forced box is pushed to Fabric via
updateLayoutSpecs, so Fabric keeps laying the content out into the collapsed box and
the button never recovers. The same forcing also stretched flex-content buttons to
the full bar height.

Replace the two-pass with a single bounded (AT_MOST) measure and let ReactSurfaceView
size itself to its content (its documented AT_MOST max-of-children behavior). When
Fabric mounts/sizes the content it re-requests layout and this measure runs again
against the real content size. Explicit width/height still measure EXACTLY; vertical
centering is preserved by the existing CENTER_VERTICAL gravity.

Verified on-device (Pixel 7, newArch+Hermes) on RN 0.78.3 and 0.85.2: content-hugging
and flex component buttons render correctly; flex buttons are pixel-identical to
before. Unit tests updated for the single-pass contract.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@israelko israelko changed the title [Fix] android: re-measure custom top bar button on async React content android: size custom top-bar component buttons to content via a single bounded measure (Fabric) Jun 11, 2026
@israelko israelko marked this pull request as ready for review June 11, 2026 11:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants