Repository navigation
fix(native): keep class, inline and !important styles as layers instead of copying or replacing them - #578
Merged
Conversation
… without copying the inline value A smaller alternative to nativewind#487, measured against it. On main: - an !important class on a single-key target other than style, such as contentContainerClassName, replaces both the class's other values and the inline value, because the !important pass merges that target with mergeDefinedProps; - the CSS-variable filter rebuilds every inline object, so an Animated.Value turns into a plain object nothing drives, and a Reanimated animated style's handle is copied with keys dropped; - the !important pass flattens the inline style into the class's, which folds an animated style's handle into a plain object, so Reanimated drops the class's values; - with the object form ({ target: "style" }), an inline [], { height: undefined } or vars() replaces the class's style; - a callback style beside a class becomes an array, where it never runs. Nativewind v4 gets each of these right. The filter now copies nothing until it finds a CSS variable and passes a class instance as it is; the !important pass layers the inline value instead of flattening it, on every single-key target; the object form keeps the class's value when the inline one filters to nothing; and nativewind#416's callback composition is carried over. flattenStyleArray loses its last callers and goes. On Hermes this allocates less per render than main: nothing for an inline style with no CSS variable, and a two-element array instead of a merged object under !important. Tests: nativewind#416's compiler/important.test.tsx, nativewind#487's rightIsInline and components changes, and style-layers-regressions.test.tsx, whose 9 cases all fail on main. Co-authored-by: Yevhenii <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
… merge
The ["style"] target merged props with { ...left, ...right }, which
copies an undefined prop such as style={undefined}. The merge then
deletes that key, and in Hermes deleting an existing property moves
the props object off its fast hidden class. mergeDefinedProps, which
every other target already uses, never copies it, so the later delete
is a no-op. It also stops an undefined inline prop from replacing one
the class computes, as on every other target.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
danstepanov
approved these changes
Nov 19, 2026
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.
Replaces #487 with a smaller, more targeted solution that also improves performance over the rewrite done in that branch.
That older PR also included the commits from #416; given the alternative approach taken here, that base PR will need to be updated to adapt.
Problem
A style prop's value is built in two merges: the class's style with the inline style, then that result with the class's
!importantstyle. Both merges lose values that Nativewind v4 kept:!importanton a target other thanstyle(contentContainerClassName="bg-red-500! p-4"with an inlinecontentContainerStyle): the!importantobject replaces everything, so the class's padding and the inline style are dropped.className:filterCssVariablescopies every inline object, so anAnimated.Valuebecomes a plain{ _value, … }thatsetValueno longer moves. A ReanimateduseAnimatedStylehandle loses its internals (it crashes on mount in jest). With an!importantclass, the second merge flattens the handle into a plain object and the class's values are lost.{ target: "style" }): an inlinevars()value is passed to the component instead of leaving the class's style. An inline[]or{ height: undefined }drops the class's style.Solution
All changes are in
src/native/styles/index.ts(+108/−131):filterCssVariablescopies nothing until it finds a CSS variable, and passes class instances such asAnimated.Valuethrough, so an inline style reaches the component as the same object.!importantmerge layers ([...layers, important]) instead of flattening;flattenStyleArrayis removed. This matches v4's cascade and the nitro prototype's[style, inline, importantStyle].!importantvalue on a single-key target other thanstylelayers over the class's and the inline value instead of replacing them.(state) => [left, right]). The callback helpers and the removal of the redundant inline branch are from feat(native): interop react-native-gesture-handler's Pressable and button family #416.styletarget merges props withmergeDefinedPropsrather than spreading and thendelete-ingstyle. Thedeletewas a measurable cost on Hermes for wrapped components without classes.Tests
style-layers-regressions.test.tsx(new, 9 cases, all fail onmain): thecontentContainerClassName!importantcase; an inlineAnimated.Valuestaying live, and inline object identity, withp-4andp-4 bg-red!; the object form with inline[],{ height: undefined }and a CSS variable object; a Pressable callback between a class and an!importantclass.compiler/important.test.tsx, from feat(native): interop react-native-gesture-handler's Pressable and button family #416, unchanged.components.test.tsx: an!importanttext-alignwins overTextInput'stextAlignprop.rightIsInline.test.tsx: the two!importantcases now compare the flattened style, since the inline style stays a separate layer.1683/1683 tests pass; typecheck and lint are clean.
Performance
Hermes, 16 rounds against
mainplus benchmark tooling (the raw React Native control is within ±1.4%):This minimal version is faster than the earlier
combineStyleLayersversion (in #487) in all 12 scenarios (−1.3% to −5.4%).Merge order
index.ts, so if this lands first, feat(native): interop react-native-gesture-handler's Pressable and button family #416 drops itsindex.tschange.getStyledProps; whichever lands second needs a rebase.