Skip to content

[Fabric] Implement onContentSizeChange in TextInput #14785

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

anupriya13
Copy link
Contributor

@anupriya13 anupriya13 commented Jun 16, 2025

Description

Type of Change

  • New feature (non-breaking change which adds functionality)

Why

Implementation for onContentSizeChange property for TextInput for fabric
This property was available in RNW Paper via TextInputViewManager.

See https://reactnative.dev/docs/textinput#oncontentsizechange for details.

Resolves #13125

What

Implemented onContentSizeChange property for TextInput for fabric
Refer https://learn.microsoft.com/en-us/windows/win32/api/textserv/nf-textserv-itextservices-txgetnaturalsize

Screenshots

android:

android.content.size.mp4

windows:

contentsize.mp4

Testing

Tested using playground-composition App,

E2ETestApp already has this test case:
https://github.com/microsoft/react-native-windows/blob/main/packages/@react-native-windows/tester/src/js/examples/TextInput/TextInputExample.windows.js

Changelog

Should this change be included in the release notes: YES

[Fabric] Implemented onContentSizeChange in TextInput

@anupriya13 anupriya13 added Area: TextInput Area: Fabric Support Facebook Fabric New Architecture Broad category for issues that apply to the RN "new" architecture of Turbo Modules + Fabric Parity: Fabric vs. Paper RNW Fabric does not look or behave like RNW Paper Workstream: Component Parity Close the parity gap between RNW and RN for core RN components and their supporting APIs. labels Jun 16, 2025
@anupriya13 anupriya13 marked this pull request as ready for review June 16, 2025 11:31
@anupriya13 anupriya13 requested a review from a team as a code owner June 16, 2025 11:31
@anupriya13 anupriya13 enabled auto-merge (squash) June 16, 2025 13:21
@anupriya13 anupriya13 requested a review from acoates-ms June 16, 2025 15:18
@@ -1247,6 +1247,12 @@ void WindowsTextInputComponentView::OnTextUpdated() noexcept {
onChangeArgs.text = GetTextFromRichEdit();
onChangeArgs.eventCount = ++m_nativeEventCount;
emitter->onChange(onChangeArgs);
if (windowsTextInputProps().multiline) {
facebook::react::WindowsTextInputEventEmitter::OnContentSizeChange onContentSizeChangeArgs;
onContentSizeChangeArgs.contentSize.width = m_layoutMetrics.frame.size.width * m_layoutMetrics.pointScaleFactor;
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't what content size changed is supposed to report. m_layoutMetrics.frame.size is the size of the TextInput, onContentSizeChanged should be reporting the size of the text content within the textinput.

So if we have a textinput which scrolls, then onContentSizeChanged should include the whole scrollable content, not just the size of the textinput.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@anupriya13 anupriya13 marked this pull request as draft June 16, 2025 16:08
auto-merge was automatically disabled June 16, 2025 16:08

Pull request was converted to draft

@anupriya13 anupriya13 marked this pull request as ready for review June 17, 2025 04:46
@anupriya13 anupriya13 requested a review from Copilot June 17, 2025 04:46
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for the onContentSizeChange property in the Fabric TextInput, allowing consumers to receive updates on the rendered content’s dimensions.

  • Added the GetContentSize method declaration and implementation in WindowsTextInputComponentView.
  • Modified OnTextUpdated to emit onContentSizeChange events for multiline text inputs.
  • Updated the playground sample to demonstrate usage of onContentSizeChange.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
vnext/Microsoft.ReactNative/Fabric/Composition/TextInput/WindowsTextInputComponentView.h Declaration of GetContentSize added
vnext/Microsoft.ReactNative/Fabric/Composition/TextInput/WindowsTextInputComponentView.cpp Implementation of GetContentSize and modification of OnTextUpdated to emit onContentSizeChange events
packages/playground/Samples/textinput.tsx New sample demonstrating usage of onContentSizeChange
change/react-native-windows-f1078f0f-499f-408a-acd3-e969096b6dea.json Change metadata for the release note
Comments suppressed due to low confidence (2)

vnext/Microsoft.ReactNative/Fabric/Composition/TextInput/WindowsTextInputComponentView.cpp:1294

  • [nitpick] Consider adding a comment here clarifying that the onContentSizeChange event is only emitted for multiline text inputs.
if (windowsTextInputProps().multiline) {

packages/playground/Samples/textinput.tsx:68

  • [nitpick] Consider adding JSDoc comments to the getHeightAndWidth function to describe its purpose and parameters for improved code clarity.
getHeightAndWidth = (height: number, width: number) => {

Comment on lines 1245 to 1248
constexpr float HIMETRIC_PER_INCH = 2540.0f;

SIZE extentHimetric = {
static_cast<LONG>(availableWidth * scale * HIMETRIC_PER_INCH / dpi),
Copy link
Preview

Copilot AI Jun 17, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider adding inline comments to explain the conversion logic to HIMETRIC units and the use of std::numeric_limits::max() for clarity to future maintainers.

Suggested change
constexpr float HIMETRIC_PER_INCH = 2540.0f;
SIZE extentHimetric = {
static_cast<LONG>(availableWidth * scale * HIMETRIC_PER_INCH / dpi),
constexpr float HIMETRIC_PER_INCH = 2540.0f; // HIMETRIC units per inch, used for conversion.
SIZE extentHimetric = {
// Convert available width to HIMETRIC units using the formula:
// HIMETRIC = (width in pixels) * (scale factor) * (HIMETRIC_PER_INCH) / (DPI)
static_cast<LONG>(availableWidth * scale * HIMETRIC_PER_INCH / dpi),
// Use the maximum value of LONG for height to indicate no vertical constraint.
// This ensures the text layout engine calculates the natural height needed.

Copilot uses AI. Check for mistakes.

@anupriya13 anupriya13 requested a review from acoates-ms June 17, 2025 14:37
hdc,
nullptr,
nullptr,
static_cast<DWORD>(TXTNS_FITTOCONTENTWSP),
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I'm not sure on this vs TXTNS_FITTOCONTENT3. -- I dont know what "indented content" really means.

@@ -1247,6 +1290,13 @@ void WindowsTextInputComponentView::OnTextUpdated() noexcept {
onChangeArgs.text = GetTextFromRichEdit();
onChangeArgs.eventCount = ++m_nativeEventCount;
emitter->onChange(onChangeArgs);
if (windowsTextInputProps().multiline) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we verify what single line textinputs do on the other platforms? Does it not fire at all? Is it just return the same as onLayout?

Copy link
Contributor Author

@anupriya13 anupriya13 Jun 17, 2025

Choose a reason for hiding this comment

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

As per this https://reactnative.dev/docs/textinput#oncontentsizechange
It's only called for multiline

@acoates-ms
Copy link
Contributor

It would be good to add an automation test for this event too.

@anupriya13 anupriya13 merged commit dee65eb into microsoft:main Jun 17, 2025
58 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Fabric Support Facebook Fabric Area: TextInput New Architecture Broad category for issues that apply to the RN "new" architecture of Turbo Modules + Fabric Parity: Fabric vs. Paper RNW Fabric does not look or behave like RNW Paper Workstream: Component Parity Close the parity gap between RNW and RN for core RN components and their supporting APIs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement onContentSizeChange property for TextInput for fabric
2 participants