Skip to content

Allow unsupported length units to be used #83

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

Conversation

kristerkari
Copy link
Contributor

@kristerkari kristerkari commented May 1, 2018

This PR adds support to allow valid CSS length units to be passed through the parser.

The supported units are based on the list in MDN:
https://developer.mozilla.org/en-US/docs/Web/CSS/length

Why?

I'm planning on adding support for CSS viewport units to React Native. Viewport units need to be parsed to pixels at runtime, so I need the raw value from the parser, e.g. 20vw.

Currently css-to-react-native only supports parsing values with pixels and throws for any other unit. By removing that restriction we can allow valid CSS units and let React Native throw an error if the unit is not supported.

By allowing the use of other CSS length units, anyone who uses this parser can easily build custom support for other units in react-native, e.g. rem unit.

ping @jacobp100

@kristerkari kristerkari requested a review from jacobp100 May 1, 2018 12:33
@jacobp100
Copy link
Contributor

Is it easier to just modify length to allow the extra units?

@kristerkari
Copy link
Contributor Author

kristerkari commented May 2, 2018

Is it easier to just modify length to allow the extra units?

LENGTH token type converts the unit to number (LENGTH: regExpToken(lengthRe, Number)), I can not use the same token type.

I thought about using the same regex, but I decided to make a separate regex to make it clear in the code that those units are not supported and that the LENGTH token type is only used when parsing pixel values.

I can combine the regexes if you think that it's better that way.

@jacobp100
Copy link
Contributor

Cool! As an idea, we could extend the transform function for a token to pass in the entire match, then have length universally support all units.

But that also might be something we don’t want. Your call—I’m happy either way!

@kristerkari
Copy link
Contributor Author

We should probably go with what is easiest to understand for someone who reads the code.

The good thing about my current approach is that it makes a clear separation between what is supported by the parser and what is not. The good thing about your suggestion is that it would simplify the code as we would not have to pass the 2 token types around.

@jacobp100
Copy link
Contributor

Good points! Let’s do this!

@jacobp100 jacobp100 merged commit e301860 into styled-components:master May 5, 2018
@kristerkari kristerkari deleted the feature/allow-css-length-units-not-supported-by-react-native branch May 5, 2018 19:49
@kristerkari
Copy link
Contributor Author

@jacobp100 Thanks! Could you also do a new release with these changes?

@jacobp100
Copy link
Contributor

jacobp100 commented May 9, 2018

Published now. Sorry for the delay :)

Edit: Added you as a maintainer on NPM, so you should be able to publish too now!

@kristerkari
Copy link
Contributor Author

Thanks a lot!

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.

2 participants