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(react-native): border widths in the theme don't throw runtime errors #4227

Merged
merged 4 commits into from
Jul 12, 2023

Conversation

dbanksdesign
Copy link
Contributor

@dbanksdesign dbanksdesign commented Jul 11, 2023

Description of changes

  • Fixed a typing issue where the theme would only accept strings as borderWidths
  • Made sure borderWidths and space values turn into numbers even without 'rem' or 'px'

Issue #, if available

Description of how you validated changes

Checklist

  • Have read the Pull Request Guidelines
  • PR description included
  • Relevant documentation is changed or added (and PR referenced)
  • yarn test passes and tests are updated/added
  • No side effects or sideEffects field updated

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@dbanksdesign dbanksdesign requested a review from a team as a code owner July 11, 2023 05:55
@changeset-bot
Copy link

changeset-bot bot commented Jul 11, 2023

🦋 Changeset detected

Latest commit: 82e9251

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 10 packages
Name Type
@aws-amplify/ui-react-native Patch
@aws-amplify/ui-react Patch
@aws-amplify/ui Patch
@aws-amplify/ui-react-liveness Patch
@aws-amplify/ui-react-notifications Patch
@aws-amplify/ui-react-storage Patch
@aws-amplify/ui-react-core-notifications Patch
@aws-amplify/ui-react-core Patch
@aws-amplify/ui-vue Patch
@aws-amplify/ui-angular Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@dbanksdesign dbanksdesign temporarily deployed to ci July 11, 2023 06:11 — with GitHub Actions Inactive
@dbanksdesign dbanksdesign temporarily deployed to ci July 11, 2023 06:11 — with GitHub Actions Inactive
@dbanksdesign dbanksdesign temporarily deployed to ci July 11, 2023 06:11 — with GitHub Actions Inactive
@dbanksdesign dbanksdesign temporarily deployed to ci July 11, 2023 06:11 — with GitHub Actions Inactive
@dbanksdesign dbanksdesign temporarily deployed to ci July 11, 2023 06:11 — with GitHub Actions Inactive
@dbanksdesign dbanksdesign temporarily deployed to ci July 11, 2023 06:11 — with GitHub Actions Inactive
Comment on lines 54 to 56
if (path[0] === 'borderWidths' || path[0] === 'space') {
return parseFloat(value);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

To make the logic in setupToken a bit easier to reason about in the future, what do you think abt combining the conditional block here the block immediately below and the block on line 43 and abstracting a util for checking whether the value should be passed to parseFloat ? e.g.

// contrived name
const shouldParseFloatValue = (pathKey: string) => pathKey === 'space' || pathKey === 'borderWidths' || pathKey === 'opacities';

const setupToken = (
  // ...setupToken params
) {
  // ...other setupToken logic

  if (shouldParseFloat(path[0])) {
    if (value.includes('rem')) {
      return Math.floor(parseFloat(value) * 16 * spaceModifier);
    }
  
    return parseFloat(value);
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

love it

borderWidths: {
small: '4',
medium: '1rem',
large: 6,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this something specific in RN where this works without a unit? Or do we just not want it to break completely if people try to do it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Specific to RN because when we use the theme we use it in a RN StyleSheet which is typed rather than outputting to a CSS string which is untyped. Most numeric values (space, borderWidth, fontSize, etc.) are expected to be a number rather than a string. In CSS its all just strings and numbers can be cast to strings but not the other way around.

@dbanksdesign dbanksdesign temporarily deployed to ci July 11, 2023 19:37 — with GitHub Actions Inactive
@dbanksdesign dbanksdesign temporarily deployed to ci July 11, 2023 19:37 — with GitHub Actions Inactive
@dbanksdesign dbanksdesign temporarily deployed to ci July 11, 2023 19:37 — with GitHub Actions Inactive
@dbanksdesign dbanksdesign temporarily deployed to ci July 11, 2023 19:37 — with GitHub Actions Inactive
Copy link
Contributor

@reesscot reesscot left a comment

Choose a reason for hiding this comment

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

LGTM

@dbanksdesign dbanksdesign merged commit d3ee054 into main Jul 12, 2023
32 checks passed
@dbanksdesign dbanksdesign deleted the react-native-border-widths branch July 12, 2023 17:12
@github-actions github-actions bot mentioned this pull request Jul 12, 2023
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.

None yet

4 participants