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

PlatformColor gets converted to undefined by parseStyle function #70

Closed
khagesh opened this issue Nov 10, 2023 · 1 comment
Closed

PlatformColor gets converted to undefined by parseStyle function #70

khagesh opened this issue Nov 10, 2023 · 1 comment

Comments

@khagesh
Copy link

khagesh commented Nov 10, 2023

Description

We are using PlatformColor for setting all colors inside our app. But when we pass a style object to createStyleSheet, then these PlatformColor gets converted to undefined. And hence the color does not gets applied to component. This is only happening for Android and iOS, web is working fine.

After digging into code, I found the reason. It was because inside parseStyle function we are checking if we see style properties value as object, then we proceed assuming that the value is for breakpoints, which is a valid assumption as per this library objective. But, this is where PlatformColor breaks. Since, PlatformColor value is also an object, the value gets converted to undefined by getValueForBreakpoint function in breakpoints.ts.

I am not sure what is the correct way to fix it. I can think of two ways to fix it.

First, we add checks for PlatformColor and return value as it is inside parseStyle function. This is the patch that I made for myself and patching this library locally.

diff --git a/node_modules/react-native-unistyles/src/utils/styles.ts b/node_modules/react-native-unistyles/src/utils/styles.ts
index c35c333..c44b242 100644
--- a/node_modules/react-native-unistyles/src/utils/styles.ts
+++ b/node_modules/react-native-unistyles/src/utils/styles.ts
@@ -97,6 +97,16 @@ export const parseStyle = <T, B extends Breakpoints>(
                     return [key, value]
                 }
 
+                // if the value is a PlatformColor Object, we don't want to parse it
+                if (value && typeof value === 'object' && value.hasOwnProperty('semantic')) {
+                    return [key, value]
+                }
+
+                // or on Android the property to check is resource_paths
+                if (value && typeof value === 'object' && value.hasOwnProperty('resource_paths')) {
+                    return [key, value]
+                }
+
                 return [
                     key,
                     getValueForBreakpoint<B>(

Second, we can return the value as it is, instead of returning undefined, inside breakpoints.ts file's getValueForBreakpoint function.

    return breakpointPairs.length > 0
        ? value[availableBreakpoints[availableBreakpoints.length - 1] as keyof B & string]
        : undefined

Steps to reproduce

  1. Define a PlatformColor
  2. Pass PlatformColor value to any style property
  3. console.log after useStyles hook to see that PlatformColor value is converted to undefined.

Snack or a link to a repository (optional)

No response

Unistyles version

1.1.6

React Native version

0.72.6

Platforms

Android, iOS

@jpudysz
Copy link
Owner

jpudysz commented Nov 10, 2023

Cool case and thanks for the hints about the patch. I will be happy to add support for PlatformColors 🙏

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

No branches or pull requests

2 participants