Add createAnimatedRestyleComponent and a guide for animating Restyle components (Reanimated 4.4+) - #358
Open
tothvoj-gl wants to merge 1 commit into
Conversation
Author
|
I have signed the CLA! |
Reanimated 4.4+'s default-on FORCE_REACT_RENDER_FOR_SETTLED_ANIMATIONS flag re-renders a wrapped component with its settled style values spread as top-level props. If that wrapped component is a Restyle component (Animated.createAnimatedComponent(createBox())), those raw values land in Restyle's theme-token prop namespace and getThemeValue throws (Shopify#355). Nesting the other way around - Restyle wrapping the already-animated component (createAnimatedRestyleComponent(Animated.createAnimatedComponent(View))) - avoids the collision entirely: the settled props land on the plain view Reanimated wraps internally, which Restyle's prop parsing never sees. No interception layer is needed; this is a design-spike finding, verified against real react-native-reanimated 4.5.1 in a scratch app (animated color, borderRadius and zIndex all settle correctly with zero errors) as well as with the unit tests added here. createAnimatedRestyleComponent is a thin wrapper around createBox that infers Props from the animated component handed to it, so its own prop types (style in particular) don't need to be redeclared by hand. Restyle still never imports an animation library - the caller constructs the animated component and hands it in. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
tothvoj-gl
force-pushed
the
feature/355-animated-restyle-component
branch
from
September 19, 2026 08:26
8d91308 to
8af8f0b
Compare
2 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
react-native-reanimated4.4+ ships a default-on flag,FORCE_REACT_RENDER_FOR_SETTLED_ANIMATIONS. Once an animation settles, Reanimated re-renders the component it wraps with the resolved style values spread on as top-level props, in addition tostyle. If that wrapped component is a Restyle component built asAnimated.createAnimatedComponent(createBox()), those raw values land as theme-key props on it, andgetThemeValuethrows:This is reported in #355 (two independent reports). #356 (@KAMRONBEK) fixes this at the
getThemeValuelevel, by passing raw numeric and color values through instead of throwing. This PR is a different, complementary fix that addresses the underlying ordering cause of the collision rather thangetThemeValue's strictness, and doesn't touchgetThemeValue.tsor anything else #356 touches.Cause
The crash is really an ordering problem. Given:
Reanimated's settle mechanism re-renders whatever it directly wraps — here, the Restyle component itself — spreading the settled props onto it, where Restyle's prop parsing sees and validates them.
Here Reanimated wraps a plain
Viewinside Restyle'sBaseComponentslot. The settled props are spread onto that innerView— a component Restyle's prop parsing never sees. I verified this empirically against realreact-native-reanimated4.5.1 in a scratch app: ordering A crashes exactly as in #355; ordering B, animating the sameborderRadius/backgroundColor/zIndexvalues, settles with zero errors.I also checked:
createRestyleComponentalready forwards refs (React.forwardRef→BaseComponent) — confirmed both by reading the source and empirically, withuseAnimatedRef+measure()returning the correct layout through the ref chain. No ref work was needed.createRestyleComponent.Fix
Since ordering B already works with Restyle's existing
createBox/createRestyleComponent(both already accept an arbitraryBaseComponent), no interception layer or runtime behavior change was needed. This PR adds:createAnimatedRestyleComponent: a small wrapper aroundcreateBoxthat infersPropsfrom the animated component you hand it (so itsstyleprop type doesn't need to be redeclared by hand), instead of requiring thecreateBox<Theme, Props>(BaseComponent)boilerplate directly. Restyle still never imports an animation library — the caller constructs the animated component and hands it in.What this deliberately does not change
getThemeValue.tsand anything else Pass through raw numeric and color style values instead of throwing when not found in theme #356 touches — untouched. I cherry-picked Pass through raw numeric and color style values instead of throwing when not found in theme #356's commit onto this branch to check: it applies with only a trivial conflict in the shared CHANGELOG "Next" section, and the combined test suite (53 + 7 = 60 tests) passes.react-native-reanimated(or any animation library) anywhere, including in this new file — it's generic over any already-animated component the caller supplies.BoxProps, theme typing, or any existing component's behavior —createAnimatedRestyleComponentis a new, additive export; existingcreateBox/createText/createRestyleComponentconsumers are unaffected (full existing suite is still green).backgroundColor="nonsense"(a genuine typo'd theme key) still fails at compile time and throws at runtime — this PR doesn't touch that constraint, and it's covered by a test.Test plan
New tests in
createAnimatedRestyleComponent.test.tsx(using a mock animated component that reproduces Reanimated's actual settle mechanism — an internalsetStatethat spreads resolved values onto its own rendered child — so the suite doesn't needreact-native-reanimatedas a dependency):borderRadius/zIndex→ no throwbackgroundColor="primary", etc.)createRestyleComponent(same as any custom component)Also manually verified end-to-end against real
react-native-reanimated4.5.1 +react-native-worklets0.10.4 on iOS (RN 0.86.3), using the actual proposed API — animatedbackgroundColor/borderRadiussettled correctly with zero errors.yarn test: 51/51 passing (44 existing + 7 new).yarn lint: clean.yarn build: clean.Refs #355, Refs #230
🤖 Generated with Claude Code