-
Notifications
You must be signed in to change notification settings - Fork 20
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
Is it possible to wrap pixel value with adaptive function? #26
Comments
Hi @5468sun! There is no support for custom functions on runtime, but you can always propose such feature to be added. What you can do now instead is use CSS for other styles and set the height with javascript: <View className={styles.myClass} style={{ height: pxtoDp(100) }} /> |
@kristerkari beside viewport unit, is there any way to do screen size adaption? here is the code. what do you think? import {PixelRatio,Dimensions}} from 'react-native';
const dp2px = dp=>PixelRatio.getPixelSizeForLayoutSize(dp);
const px2dp = px=>PixelRatio.roundToNearestPixel(px);
let designSize = {width:750,height:1336};
let pxRatio = PixelRatio.get();
let {win_width,win_height} = Dimensions.get("window");
let width = dp2px(win_width);
let height = dp2px(win_height);
let design_scale = designSize.width/width;
height = height*design_scale
let scale = 1/pxRatio/design_scale;
const com = props=>(
<View sytle={styles.container}>
<View style={{width:100,height:200,backgroundColor:"red"}}/>
</View>)
const styles={
container: {
width:width,
height:height,
transform:[{translateX:-width*.5},
{translateY:-height*.5},
{scale:scale},
{translateX:width*.5},
{translateY:height*.5}]
},
} |
Good question, the goal for the this project is to bring a set of browser CSS features to React Native. That means that if you want to use only CSS, then viewport units and media queries are the available options to scale your UI. Looking at the code you have, it seems like it would be a good idea to create a library from that and use Javascript to do the scaling. I'm open for new features to be added to React Native CSS modules, but they should always have a matching Web feature.
Usually a combination of viewport units and media queries is enough, sometimes I need to do something with Javascript too. |
Hi , @kristerkari |
That would not really work as PostCSS does not run at runtime, it's executed when the Metro packager is creating a bundle of your app.
You could do that, it could work the same way as https://github.com/kristerkari/babel-plugin-react-native-classname-to-dynamic-style, but call your px2dp function instead. It might be a good idea in the future to create an API which would allow the user to run their own library or functions during runtime. |
Im completely new to babel plugin. Now im comparing react-native-classname-to-style and react-native-classname-to-dynamic-style to see where is the dynamic funciton. Hopefully i can figure it out. |
It's these two things, another generates the |
function viewportUnitsTransform(obj, matchObject) {
const hasViewportUnits = "__viewportUnits" in obj;
if (!hasViewportUnits) {
return obj;
}
return transform(omitMemoized(obj, "__viewportUnits"), matchObject);
} where do this '__viewportUnits' come from? |
never mind, i find it in css-to-react-native-transform. |
@kristerkari after days of researching, I finally add px2dp dynamic sizing function to style. But now this module only support design width 750. I dont know how to pass design width as param or like you said add an API to let user run there own sizing function. here is the code. |
css
.footer{
height: 100px;
}
output
.footer{
height:pxtoDp(100)
}
and the pxtoDp function run at runtime.
The text was updated successfully, but these errors were encountered: