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

feat: prevent flattening style arrays #7021

Merged
merged 12 commits into from
Feb 19, 2025
Merged

Conversation

jpudysz
Copy link
Contributor

@jpudysz jpudysz commented Feb 13, 2025

Summary

Why

While integrating Unistyles with Reanimated, there is one clash that I would love to resolve.
We encourage developers to merge styles using array syntax, as it makes it easier to detect the order of merging and persist the C++ state attached to styles.

Reanimated flattens arrays passed to the style prop, making it impossible to distinguish which props are animated and which are provided by Unistyles.

How

To fix this, we just need to remove two one occurrences of array flattening. I tested it locally with Unistyles, and I can correctly retrieve the style prop as-is.

Unistyles have to retrieve ref + style prop while borrowing ref.

Additionally, with this change, I can treat Reanimated styles as inline styles. Since inline styles are not processed by Unistyles algorithm, this can completely eliminate issues such as preventing animations when switching themes or changing device orientation.

Test plan

I tested it locally and I'm happy with the outcome. But I do understand that you have own test suites and regression tests, so I hope this won't break anything.

Copy link
Contributor Author

@jpudysz jpudysz left a comment

Choose a reason for hiding this comment

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

Added one more check for objects

@jeremybarbet
Copy link
Contributor

I tried to apply the changes locally and had some styles not being applied anymore. Since the flattenArray is recursively going through the array of styles, I believe I have more than one-depth level on my array.

However, I tried with the following:

// Kept as before
const styles = flattenArray<StyleProps>(styleProp ?? []);

// Only apply this change
props[key] = processedStyle;

And I didn't have any warning anymore, and the regression I saw previously didn't happen in this case. Did you try that too?

@jpudysz
Copy link
Contributor Author

jpudysz commented Feb 13, 2025

I believe I have more than one-depth level on my array

Yeah, but is array of arrays a valid style? Reanimated was safeguarding for it and that's all. Maybe we can call flat. I need to give it a try.

@jpudysz
Copy link
Contributor Author

jpudysz commented Feb 13, 2025

@jeremybarbet nevertheless you're right! It's just a one liner and I can still get Array on my side. Thanks for the verification 🙏🏼 I will update PR.

@jpudysz
Copy link
Contributor Author

jpudysz commented Feb 15, 2025

I updated few tests to indicate that we may have style arrays now

@tomekzaw tomekzaw requested review from tjzel and piaskowyk February 15, 2025 09:27
@jpudysz jpudysz requested a review from tjzel February 18, 2025 14:52
@jpudysz
Copy link
Contributor Author

jpudysz commented Feb 18, 2025

I think these warnings are not from my PR. I didn't use any or touch these files. 🤔

Copy link
Member

@piaskowyk piaskowyk left a comment

Choose a reason for hiding this comment

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

I've tested it, and it looks good 👍

@piaskowyk
Copy link
Member

To fix CI run:

cd packages/react-native-reanimated && yarn formatter

then we can merge it 😄

@piaskowyk
Copy link
Member

Dziękuję serdecznie 😊 🫡

@piaskowyk piaskowyk added this pull request to the merge queue Feb 19, 2025
Merged via the queue into software-mansion:main with commit 9b99fbd Feb 19, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants