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

Fix custom themed nested components props overwrite #241

Merged
merged 1 commit into from
Mar 27, 2023

Conversation

mattfrances
Copy link
Contributor

@mattfrances mattfrances commented Mar 17, 2023

Description

Note: This was a team effort with @ElviraBurchik!

The issue
When using nested components with different variant values and breakpoints, the child was receiving the parent's value.
For example with the following variant, if the child had mediumPadding and the parent had noPadding, the child ended up with a padding of 'none' / 0 rather than 'm' / 8:

spacingVariant: {
            defaults: {},
            noPadding: {
              phone: 'none',
              tablet: 'none',
            },
            mediumPadding: {
              phone: 'm',
              tablet: 'm',
            }
          }

Why this happened
This was happening because when memoizing properties that were objects, we were converting them to Strings resulting in a memoized hash key ending with "[object Object]". So the flow for nested components with variants was as follows:

  1. Parent's props get memoized into a hash key -> "1334x750-spacing-padding-[object Object]"
  2. The memoized has key now refers to the parent's props (in this case padding of 0)
  3. Child's props get memoized into a hash key -> "1334x750-spacing-padding-[object Object]"
  4. Since it's the same hash key as above, we return padding of 0

The fix
By stringifying properties that are objects, we create unique hash keys for each case. For the example above, this would result in two separate hash keys for each variant:

  1. "1334x750-spacing-padding-{"phone":"none","tablet":"none"}"
  2. "1334x750-spacing-padding-{"phone":"m","tablet":"m"}"

As a result, we wouldn't accidentally use the parent's hash key / styles.


The comment I added in the code is quite messy, and may be inaccurate as I'm still trying to completely understand how this works. I will update the code comment once I have some reviews and the chance to better understand how this fixes the issue.

Fixes #239

Reviewers’ hat-rack 🎩

  • CI
  • yarn test useRestyle - tests should all pass

Screenshots or videos (if needed)

Checklist

Copy link
Contributor

@fortmarek fortmarek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the PR and the thorough testing 💯 My only concern is JSON.stringify that we should address before merging.

src/createRestyleFunction.ts Outdated Show resolved Hide resolved
@fortmarek
Copy link
Contributor

I am onboard with the general direction of this PR, I will give this another thorough review once it's no longer a draft 🤞

@mattfrances mattfrances force-pushed the custom-themed-nested-components-props-overwrite branch from 5c3bf54 to 31ddfeb Compare March 23, 2023 12:42
@mattfrances mattfrances marked this pull request as ready for review March 23, 2023 12:42
@mattfrances mattfrances force-pushed the custom-themed-nested-components-props-overwrite branch from 31ddfeb to 2c4d095 Compare March 23, 2023 12:45
@mattfrances mattfrances requested a review from fortmarek March 23, 2023 19:32
Copy link
Contributor

@fortmarek fortmarek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit: 👏

@mattfrances mattfrances merged commit df187f3 into master Mar 27, 2023
@mattfrances mattfrances deleted the custom-themed-nested-components-props-overwrite branch March 27, 2023 19:02
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.

Custom themed components are losing spacing props after upgrade from 2.3.0 to 2.4.0
3 participants