Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default unit of style in React Native?

I am contributing to an Open Source Project where I am developing Material design for React Native. I am blocked at work,I am unable to make some UI level enhancements w.r.t. padding, alignment etc.,

This is the Official Spec of Material Design for Drawer-

According to the spec of Material Design

In the above image, the UNIT of measurement is dp.

But, in my React Native code, I see there is no such units mentioned. Considering it is "react native" I am confused whether it is px or dp.

I even went over the Official Docs of React Native for Style component. I don't see a mention anywhere.

My Code looks like-

const styles = {     touchable: {         paddingHorizontal: 16,         marginVertical: 8,         height: 48     },     item: {         flex: 1,         flexDirection: 'row',         alignItems: 'center',     },     icon: {         position: 'relative',     },     value: {         flex: 1,         paddingLeft: 34,         top: 2     },     label: {         top: 2     } }, 

Please can you tell me, if this is pixels or dp? And also, is 1px = 1dp?

like image 400
bozzmob Avatar asked Dec 28 '15 12:12

bozzmob


People also ask

What is the unit of style in React Native?

From the docs: All dimensions in React Native are unitless, and represent density-independent pixels. Setting dimensions this way is common for components that should always render at exactly the same size, regardless of screen dimensions.

What is default position in React Native?

position in React Native is similar to regular CSS, but everything is set to relative by default, so absolute positioning is always relative to the parent. If you want to position a child using specific numbers of logical pixels relative to its parent, set the child to have absolute position.

Is the default unit of width and height?

Shapes from a stencil that uses metric units are measured in millimeters. Therefore, when you add a shape to a drawing, the Width and Height boxes in the Size & Position window display the values for that shape in the shape's default measurement units.

Is React Native style same as CSS?

Stying in React Native is not the same as normal CSS. For styling elements in React Native, JavaScript objects are used. Every core component in React Native accepts the style prop which accepts a JavaScript object containing CSS property names as key.


2 Answers

From the docs:

All dimensions in React Native are unitless, and represent density-independent pixels. Setting dimensions this way is common for components that should always render at exactly the same size, regardless of screen dimensions.

So yes, units in React Native are in dp. If you want to convert them to pixels, use PixelRatio.getPixelSizeForLayoutSize()

like image 89
Nick Avatar answered Sep 16 '22 21:09

Nick


I share your confusion somewhat, not being able to actively inspect with a developer console as we are used to in the browser.

I am not familiar with the 'dp' unit, but from what I gather width: 1 renders differently on each device depending on the pixel density of the screen (see link). The information in the react-native docs say that 1 would render thicker on screens with high pixel density. Which then sounds logical as you have more precision on high density screens than you would have on low density screens and react-native aims at being universal so it would not assume high dpi.

It is my understanding that you can use the below linked PixelRatio API to calculate sizes for detail elements (think borders, icons, etc), that way you can dynamically adjust the rendered size according to the device's screen density.

https://facebook.github.io/react-native/docs/pixelratio.html#content

like image 20
Mosselman Avatar answered Sep 18 '22 21:09

Mosselman