Skip to content

fix: handle boolean shorthand in nativeStyleMapping#349

Open
telanflow wants to merge 1 commit into
nativewind:mainfrom
telanflow:fix/native-style-mapping-boolean-shorthand
Open

fix: handle boolean shorthand in nativeStyleMapping#349
telanflow wants to merge 1 commit into
nativewind:mainfrom
telanflow:fix/native-style-mapping-boolean-shorthand

Conversation

@telanflow

Copy link
Copy Markdown

Summary

nativeStyleMapping() in src/native/styles/index.ts calls path.split(".") on every mapping value, but a value may be the boolean shorthand true — the same-name mapping permitted by the NativeStyleMapping type (true | DotNotation). When the mapped style is actually present, (true).split throws TypeError: undefined is not a function, crashing render.

Fixes #348 (same root cause as #232 / #288).

Affected built-in components

Components that ship the boolean shorthand crash as soon as the mapped style is applied:

  • components/TextInput.tsx{ textAlign: true } — triggered by text-right / text-center / text-left
  • components/ImageBackground.tsx{ backgroundColor: true } — triggered by any bg-*

(ActivityIndicator / Button use { color: "color" }, a string path, so they're unaffected.)

It only crashes when the mapped style is present: when absent, styleValue === undefined short-circuits via continue, which is why the crash looks intermittent.

Fix

Treat true as the same-name shorthand (map onto a prop named key):

const tokens = (typeof path === "string" ? path : key).split(".");

Test

Added a regression test in src/__tests__/native/components.test.tsx (using the target: "style" + { backgroundColor: true } shorthand). It fails on main with TypeError: path.split is not a function and passes with this change.

yarn test, yarn typecheck, and yarn lint all pass.

A nativeStyleMapping entry value may be `true` (the same-name shorthand
permitted by the NativeStyleMapping type: `true | DotNotation`), but the
runtime called path.split(".") unconditionally, throwing "true.split is
not a function" once the mapped style was present.

This crashed shipped components that use the shorthand - TextInput
(textAlign) and ImageBackground (backgroundColor) - as soon as a class
like text-right or bg-* was applied; the style was only skipped when
absent (the styleValue === undefined guard), making it look intermittent.

Map `true` onto a prop with the same name, and add a regression test.

Closes nativewind#348
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.

nativeStyleMapping crashes with "true.split is not a function" when mapping value is the boolean shorthand (e.g. TextInput textAlign)

1 participant