-
Notifications
You must be signed in to change notification settings - Fork 82
Add aspect ratio support #123
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import transformCss from '..' | ||
|
||
it('handles regular aspect ratio values', () => { | ||
expect(transformCss([['aspect-ratio', '1.5']])).toEqual({ | ||
aspectRatio: 1.5, | ||
}) | ||
}) | ||
|
||
it('handles CSS-style aspect ratios', () => { | ||
expect(transformCss([['aspect-ratio', '3 / 2']])).toEqual({ | ||
aspectRatio: 1.5, | ||
}) | ||
}) | ||
|
||
it('handles CSS-style aspect ratios without spaces', () => { | ||
expect(transformCss([['aspect-ratio', '3/2']])).toEqual({ | ||
aspectRatio: 1.5, | ||
}) | ||
}) | ||
|
||
it('throws when omitting second value after slash', () => { | ||
expect(() => transformCss([['aspect-ratio', '3/']])).toThrow() | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { NUMBER, SLASH } from '../tokenTypes' | ||
|
||
export default tokenStream => { | ||
let aspectRatio = tokenStream.expect(NUMBER) | ||
|
||
if (tokenStream.hasTokens()) { | ||
tokenStream.expect(SLASH) | ||
aspectRatio /= tokenStream.expect(NUMBER) | ||
} | ||
|
||
return { aspectRatio } | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we actually want to support the CSS-style aspect ratios?
aspect-ratio
isn't a CSS prop, so we're actually extending a React Native prop with a "CSS-style" syntax, so this is something that only works when using this parser.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So there is a ratio type in CSS that's used in media queries (mdn), and this is shipped in browsers. This is the semantics this PR implements
There's also a proposal for a React Native style aspect-ratio property here. But that is just a proposal, it might never be implemented
I think we can be sure that if CSS did implement the proposal, it will use the ratio semantics. But this also isn't a feature anyone requested, so I don't mind if you think it's too premature to merge it in! I don't mind either way! 😃
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The property and the syntax is probably not going to change as it has a proposal, and the same syntax also being used in other CSS features, but there is a small chance that it will never implemented in browsers.
We can go forward with this, but it might mean that we have to do a breaking change in the future if things don't go as planned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I give it 50/50 of being implemented in browsers if that helps
Currently we allow
aspect-ratio: 1.5
(via the generic value transformer), which is not web compatible.We also have custom transforms for
shadowOffset
andtextShadowOffset
, and we indirectly supportshadowColor
andshadowRadius
etc. via the generic value transformer. But these properties will almost certainly never be supported on the webBut I don't know if we should stop people using styles that RN added on-top of CSS
The values for some of these additional styles are objects, so to support them, we need a custom transformer. There's also no proposed CSS spec for this, so we're really hoping the web doesn't add these in a breaking way
What do you think on these?
Aspect-ratio is kind of interesting because the spec pretty much copies what RN does (except for the syntax). But the generic value transformer (i.e.
1.5
) is not compatible with the proposed spec. The proposed spec only allows ratios like3 / 2
So say browsers do support this, we would need to do a breaking change to remove the
1.5
version - and we'd need to do this regardless of if we decide to merge this PRIf we do decide to merge this in, I'll actually need to make this stricter - since I think you can put stuff like
-3.2 / 8.4
- to avoid a breaking change later onBut if we decide not to merge this in (and while we're doing breaking changes with borders anyway), it might be an opportunity to reconsider the properties I put above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should definitely not do that, but what I see as a bit of a risk is to add syntax that is not supported in RN, and is only a proposal in Web.
If people know that their app is only going to be used with RN, then they should be able to use the RN specific styling props.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But currently
aspect-ratio
is a React Native-only property, so we could just think about it as something that the parser does not care about until it's implemented in CSS. When the CSS property is final, then we could allow both RN and Web way of giving the value, and people who want to render in both native/web could use 3/2 syntax.What we could do is to add docs about which props are treated as CSS properties and which are considered as React Native specific.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Let's do that
I wanna find some time to work on documenting what we have, as a lot of people ask for this!