Skip to content

fix(native): keep class, inline and !important styles as layers instead of copying or replacing them - #578

Merged
danstepanov merged 3 commits into
nativewind:mainfrom
sammoore:min/one-style-combine
Nov 19, 2026
Merged

danstepanov merged 3 commits into
nativewind:mainfrom
sammoore:min/one-style-combine

Conversation

@sammoore

Copy link
Copy Markdown
Contributor

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 !important style. Both merges lose values that Nativewind v4 kept:

  • !important on a target other than style (contentContainerClassName="bg-red-500! p-4" with an inline contentContainerStyle): the !important object replaces everything, so the class's padding and the inline style are dropped.
  • Animated inline styles beside a className: filterCssVariables copies every inline object, so an Animated.Value becomes a plain { _value, … } that setValue no longer moves. A Reanimated useAnimatedStyle handle loses its internals (it crashes on mount in jest). With an !important class, the second merge flattens the handle into a plain object and the class's values are lost.
  • Object-form mapping ({ target: "style" }): an inline vars() value is passed to the component instead of leaving the class's style. An inline [] or { height: undefined } drops the class's style.
  • Pressable callback styles are dropped when combined with a class.

Solution

All changes are in src/native/styles/index.ts (+108/−131):

  • filterCssVariables copies nothing until it finds a CSS variable, and passes class instances such as Animated.Value through, so an inline style reaches the component as the same object.
  • The !important merge layers ([...layers, important]) instead of flattening; flattenStyleArray is removed. This matches v4's cascade and the nitro prototype's [style, inline, importantStyle].
  • An !important value on a single-key target other than style layers over the class's and the inline value instead of replacing them.
  • The object form keeps the class's value when the inline value holds only CSS variables.
  • Callback styles compose ((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.
  • The style target merges props with mergeDefinedProps rather than spreading and then delete-ing style. The delete was a measurable cost on Hermes for wrapped components without classes.

Tests

  • style-layers-regressions.test.tsx (new, 9 cases, all fail on main): the contentContainerClassName !important case; an inline Animated.Value staying live, and inline object identity, with p-4 and p-4 bg-red!; the object form with inline [], { height: undefined } and a CSS variable object; a Pressable callback between a class and an !important class.
  • compiler/important.test.tsx, from feat(native): interop react-native-gesture-handler's Pressable and button family #416, unchanged.
  • components.test.tsx: an !important text-align wins over TextInput's textAlign prop.
  • rightIsInline.test.tsx: the two !important cases 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 main plus benchmark tooling (the raw React Native control is within ±1.4%):

  • Remount, wrapped component without classes: −3.4%
  • Inline style scenarios: −3.2% / −2.2%
  • Everything else: +0.5% to +2.3%, within run-to-run drift

This minimal version is faster than the earlier combineStyleLayers version (in #487) in all 12 scenarios (−1.3% to −5.4%).

Merge order

sammoore and others added 3 commits November 17, 2026 23:33
… 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
danstepanov merged commit 1695e40 into nativewind:main Nov 19, 2026
6 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.

2 participants