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

feat: Add string support for aspectRatio #34629

Conversation

gabrieldonadel
Copy link
Collaborator

@gabrieldonadel gabrieldonadel commented Sep 8, 2022

Summary

This updates aspectRatio to support string values and ratio formats, i.e., '16 / 9', thus aligning it with the CSS Box Sizing Module Level 4 specification as requested on #34425. This also adds unit tests to the processAspectRatio function ensuring the style processing works as expected.

Changelog

[General] [Added] - Add string support for aspectRatio

Test Plan

This can be tested either through processAspectRatio-tests or by using the following code:

 <View
   style={{
     backgroundColor: '#527FE4', 
     aspectRatio: '16 / 9',
  }} />
Screen.Recording.2022-09-08.at.00.47.42.mov

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Sep 8, 2022
@analysis-bot
Copy link

analysis-bot commented Sep 8, 2022

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 7,727,589 +22
android hermes armeabi-v7a 7,130,459 +6
android hermes x86 8,034,135 +7
android hermes x86_64 8,007,425 +18
android jsc arm64-v8a 9,597,018 +100
android jsc armeabi-v7a 8,363,335 +106
android jsc x86 9,541,246 +95
android jsc x86_64 10,133,651 +104

Base commit: 8cdc9e7
Branch: main

@react-native-bot react-native-bot added the Type: Enhancement A new feature or enhancement of an existing feature. label Sep 8, 2022
@analysis-bot
Copy link

analysis-bot commented Sep 8, 2022

Platform Engine Arch Size (bytes) Diff
ios - universal n/a --

Base commit: 8cdc9e7
Branch: main

@facebook-github-bot
Copy link
Contributor

@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

}

const match = new RegExp(
/(^\s*[+]?\d+([.]\d+)?\s*$)|(^\s*([+]?\d+([.]\d+)?)\s*\/\s*([+]?\d+([.]\d+)?)\s*$)/,
Copy link
Contributor

Choose a reason for hiding this comment

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

Quoting @jacdebug:

This regex is not easily maintainable also fails on values like auto 4 / 3, can we follow spec and check simple splitting by space will work?
https://css-tricks.com/almanac/properties/a/aspect-ratio/#aa-values
Also this transforms will add up and have some runtime cost, we should do static transforms at build time using babel

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@cipolleschi and @jacdebug react native does not really support auto as a value for aspectRatio, any ideas on how we should handle that? Should we default auto to some specific value?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The main problem I can think of about using splitting by space is that it wouldn't be able to handle cases like '4/3', '4 /3' or '4/ 3'

Copy link
Contributor

@necolas necolas Sep 23, 2022

Choose a reason for hiding this comment

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

auto can just do nothing since it's the default. And because there is no concept of a "replaced element" in RN, auto <ratio> should probably not be allowed. You can split on auto (to print warning), split on /, trim white space if needed, and parse the floats

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated

@gabrieldonadel gabrieldonadel requested review from cipolleschi and necolas and removed request for cipolleschi September 23, 2022 21:01
return aspectRatio;
}

const matches = aspectRatio.split('/').map(s => s.trim());
Copy link
Contributor

Choose a reason for hiding this comment

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

It actually looks like we don't need to trim, as Number apparently does that internally

@facebook-github-bot
Copy link
Contributor

@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@cipolleschi
Copy link
Contributor

Hi @gabrieldonadel, thanks for all your work with these PRs. We recently started to maintain the TypeScript types and it looks like that this PR touches the Flow types in the StyleSheetTypes.js. Could you update also the TypeScript definitions in this file?

@gabrieldonadel
Copy link
Collaborator Author

Hi @gabrieldonadel, thanks for all your work with these PRs. We recently started to maintain the TypeScript types and it looks like that this PR touches the Flow types in the StyleSheetTypes.js. Could you update also the TypeScript definitions in this file?

Hi @cipolleschi, thanks for reviewing this, I've just pushed a commit updating TS types

@facebook-github-bot
Copy link
Contributor

@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@cipolleschi
Copy link
Contributor

cipolleschi commented Sep 27, 2022

Hi @gabrieldonadel. We are almost there, many thanks for your patience.

@facebook-github-bot
Copy link
Contributor

@cipolleschi has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@react-native-bot
Copy link
Collaborator

This pull request was successfully merged by @gabrieldonadel in 14c91cd.

When will my fix make it into a release? | Upcoming Releases

@react-native-bot react-native-bot added the Merged This PR has been merged. label Sep 27, 2022
@gabrieldonadel gabrieldonadel deleted the feat/aspect-ratio-string-support branch September 27, 2022 12:06
OlimpiaZurek pushed a commit to OlimpiaZurek/react-native that referenced this pull request May 22, 2023
Summary:
This updates `aspectRatio` to support string values and ratio formats, i.e., `'16 / 9'`, thus aligning it with the [CSS Box Sizing Module Level 4](https://drafts.csswg.org/css-sizing-4/#aspect-ratio) specification as requested on facebook#34425. This also adds unit tests to the `processAspectRatio` function ensuring the style processing works as expected.

## Changelog

[General] [Added] - Add string support for aspectRatio

Pull Request resolved: facebook#34629

Test Plan:
This can be tested either through `processAspectRatio-tests` or by using the following code:

```js
 <View
   style={{
     backgroundColor: '#527FE4',
     aspectRatio: '16 / 9',
  }} />
```

https://user-images.githubusercontent.com/11707729/189029904-da1dc0a6-85de-46aa-8ec2-3567802c8719.mov

Reviewed By: jacdebug

Differential Revision: D39423304

Pulled By: cipolleschi

fbshipit-source-id: d323de93d6524e411e7ab9943335a8ca323b6e61
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. Type: Enhancement A new feature or enhancement of an existing feature.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants