microsoft / react-native-macos

A framework for building native macOS apps with React.
https://microsoft.github.io/react-native-windows/
MIT License
3.37k stars 129 forks source link

Add getRectForCharRange to RCTTextView #2140

Open PPatBoyd opened 3 weeks ago

PPatBoyd commented 3 weeks ago

Summary:

Add a function to return the bounding rect of a character range in the string hosted by RCTTextView. Having such a function will enable referencing nested text runs for relative positioning of transient UI: callouts, tooltips, etc.

Saadnajmi commented 3 weeks ago

@acoates-ms any idea if there's potential to upstream this API in an x-plat / fabric context? The basic goal is to get the rect of a nested text run, aka:

<Text>Hello<Text ref={myRef}>World</Text></Text>

And then with a ref to the inner text, you can do stuff like anchor a Callout

acoates-ms commented 3 weeks ago

@acoates-ms any idea if there's potential to upstream this API in an x-plat / fabric context? The basic goal is to get the rect of a nested text run, aka:

<Text>Hello<Text ref={myRef}>World</Text></Text>

And then with a ref to the inner text, you can do stuff like anchor a Callout

Hmm.. the interesting thing about trying to get the rect of the text, is that layout information is stored in the shadownode on fabric, so you can normally get sync layout data of views on fabric.

However, I dont think that layout information exists currently for text runs, just the outer text component (ParagraphViewComponent). -- It could probably be added, but it'll be some work.

acoates-ms commented 3 weeks ago

Not quite the same, but similar functionality.

This is the change that added Text.onTextLayout to fabric: https://github.com/facebook/react-native/commit/8d6b41e9bcede07fb627d57cf6c11050ae590d57

Saadnajmi commented 3 weeks ago

@acoates-ms Looking into it more, I think this most closely translates to getBoundingClientRect, which Fabric aims to support as seen in this RFC: https://github.com/react-native-community/discussions-and-proposals/blob/main/proposals/0744-well-defined-event-loop.md

So there might be potential to upstream this for iOS at least.

PPatBoyd commented 2 weeks ago

@acoates-ms Saad's OOF this week but had unblocked my usage of this function downstream by merging the linked backport PR to 0.73-stable; any concerns with merging this change to main to stay consistent, and returning to iteration/discussion of upstreaming and Fabric support when Saad and I are both not OOF the second week of July?