Skip to content
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

Nested text can not be styled with reanimated - backgroundColor, color and etc #1455

Closed
Strate opened this issue Nov 18, 2020 · 9 comments
Closed
Assignees
Labels

Comments

@Strate
Copy link

Strate commented Nov 18, 2020

Description

Hello, I'm trying to stylize <Text> which is nested to other <Text>, but in this case styling is not applied.

Steps To Reproduce

See snack example

Expected behavior

Nested text's backgroundColor, color and other styles which can be applied to nested text support animation

Actual behavior

Nested text's backgroundColor, color and other styles which can be applied to nested text don't support animation

Snack or minimal code example

https://snack.expo.io/ZrEZ2NTsc

Package versions

  • React:
  • React Native: 0.63.3
  • React Native Reanimated: 1.13.0
@Strate Strate added the Bug label Nov 18, 2020
@hoangtrucit
Copy link

Why do you want to put the Animated.Text in Animated.Text

<Animated.Text style={{ marginTop: 20 }}>
        <Animated.Text
          style={{
            fontSize: 20,
            backgroundColor: Animated.interpolateColors(pressedState, {
              inputRange: [0, 1],
              outputColorRange: ['transparent', '#bbb'],
            }),
          }}>
          This text can not be animated
        </Animated.Text>
</Animated.Text>

I think we can resolve it with View

<View style={{ marginTop: 20 }}>
        <Animated.Text
          style={{
            fontSize: 20,
            backgroundColor: Animated.interpolateColors(pressedState, {
              inputRange: [0, 1],
              outputColorRange: ['transparent', '#bbb'],
            }),
          }}>
          This text can not be animated
        </Animated.Text>
</View>

@Strate
Copy link
Author

Strate commented Nov 23, 2020

@hoangtrucit I do it because I want to stylize only part of the text (in real life structure look like this: <Text><Text>this need to be stylized</Text><Text> and this is not</Text></Text>

@piaskowyk
Copy link
Member

Hey @Strate
I investigated this issue. If you write somethings like this:

<Text>
  string1
  <Text>
    string2
  </Text>
</Text>

The React Native change the structure to:

<RCTTextView>
  string1
  <RCTVirtualText>
    string2
  </RCTVirtualText>
</RCTTextView>

The RCTTextView is an native component but the RCTVirtualText is not and that is the reason why you can’t animate inner <Text> because in Reanimated you can animate only native nodes on layout. This can be a feature to implement in future, but now you could use solution by @hoangtrucit - change parent Text component to View

// this
<Text>
  <Text>this need to be stylized</Text>
  <Text> and this is not</Text>
</Text>

// to
<View>
  <Text>this need to be stylized</Text>
  <Text> and this is not</Text>
</View>

piaskowyk added a commit that referenced this issue Dec 8, 2020
## Description
I added to documentation information about the impossibility of animating a virtual component.
For example we can’t animate nested `<Text>` components because React Native change 
  ```
  <Text>
     string1
     <Text>string2</Text>
  </Text>
  ``` 
  to 
  ```
  <RCTTextView>
     string1
     <RCTVirtualText>string2</RCTVirtualText>
  </RCTTextView>
  ``` 
  and the `RCTVirtualText` is a virtual component.
It is related with issue: #1455

## Changes
- Updated `about.md` docs

## Checklist
- [x] Updated documentation
@yuriyyakym
Copy link

@piaskowyk This issue should be re-opened, because using View wrapper does not work with diacritics that are treated as a separate symbol:

2024-01-09.09.57.47.mp4

@yuriyyakym
Copy link

I've got it working by wrapping Animated.Text into an additional Text:

<Text style={styles.word}>
  {value.slice(0, highlightSymbolAt)}
  <Text>
    <Animated.Text style={[animatedStyle]}>{value[highlightSymbolAt]}</Animated.Text>
  </Text>
  {value.slice(highlightSymbolAt + 1)}
</Text>
2024-01-09.12.26.04.mp4

@mlazari
Copy link

mlazari commented Feb 27, 2024

It would be nice if this could work. I don't think it is possible to show text inline like this without nesting a Text in another Text:
IMG_0334
Changing the wrapper to a View (and thus not using nested Text) fixes the animations, but the animated text is not inline anymore with the rest of the text:
IMG_0335

@audrius-savickas
Copy link

@mlazari have you anyone else figured out if this is possible? @yuriyyakym for me your solution doesn't work, nested text still isn't animatable

@mlazari
Copy link

mlazari commented Nov 26, 2024

@audrius-savickas I have not.

@biw
Copy link

biw commented Jan 22, 2025

It might be worth opening this issue back up since this is no longer a note about this in the 3.x / 4.x docs 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants