-
Notifications
You must be signed in to change notification settings - Fork 24.4k
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
Add substring measurements for Text #37397
Conversation
Hi @azimgd! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
I have been first trying to solve the problem by adding a new prop called textCodeBlockStyle to . This prop accepts a background color, border radius, etc. Text renders a background rect natively using
implementation: Expensify#43 Would you be willing to help me validate my idea @NickGerleman? |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
Are you looking for this? https://reactnative.dev/docs/text#nested-text |
Nested Text won't let you draw rounded borders with a background, margins don't work as well. I would like to implement exactly this in RN: |
Base commit: 1671247 |
Fully functioning |
@NickGerleman may I ask for a code-review please. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't be adding a new API for marking spans of text. If we want to be able to do more with spans of text, we should built it into the existing facilities.
Note that we do also allow Views inline in text already.
This particular PR enables tracking layout metrics for a substring of text, by extending
I have previously tried to implement that on a native layer here: Expensify#43. Main problem was that you can't go beyond Text component's line-height with styling. That said drawing a background layer with 30px is not possible on a line which has 20px. I would appreciate any help and suggestions on what would be the best way to move forward. @NickGerleman @cortinico |
To reiterate, the API in its current form is not something I think we are interested in. I think it might be possible to reconcile this with the existing nested text APIs, to expand the APIs React Native already offer in a way that is cohesive. The path there I think would be to start with a proposal of the new API and get feedback on that first. I think the faster way to achieve the goal you are looking for might be a custom native component. |
To better explain what I mean by reconciling the APIs, we already have a way for users to segment texts, like a span. So adding a “regions” prop for this one particular separate use case is not cohesive with the current API. As a start of an API to explore, what if “onTextLayout” was set on a specific nested text span to get its information? But these sorts of major APIs usually take some back and forth to refine. If there gets to be agreement on an API (The Discussions and Proposals Repo would be a great place for this), the next steps would be a a series of smaller, reviewable PRs, building it out in increment. I.e. not all the platforms and logic at once, to help make the change reviewable. |
I think it's a great opportunity to add onLayout support for nested text. Expensify#56 (comment) I would like to help with implementing it. Pretty sure I know how to do it. |
This PR is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days. |
This PR was closed because it has been stalled for 7 days with no activity. |
Summary:
I would like to apply styling to a substring of text, such as a background color, padding, and rounded corners. This functionality could be used for:
@username
is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen
book.The problem is that the Text element in RN behaves like a block element rather than an inline element when it is multiline. Additionally, it does not allow you to have borders or rounded corners. This makes it unsuitable for implementing a multiline inline code block.
Adding a new prop called textLayoutRegions which accepts an array of regions (substring positions) could be used to solve the problem. Passing this prop will calculate the regions [[start, end], [start, end] ...] position and pass the response into onTextLayout callback. Knowing exact substring position and dimensions will allow implementing custom styled View behind the text.
Changelog:
[GENERAL] [ADDED] - New
textLayoutRegions
prop for Text component that accepts an array of substring regions. Each region's layout position will be appended intoonTextLayout
event callback.[GENERAL] [ADDED] - New
regions
props foronTextLayout
event callback.Test Plan: