npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-units

v1.0.0

Published

A collection of useful units and a simple grid implementation for responsive layouts in React Native.

Downloads

37

Readme

react-native-units

A collection of useful units and a simple grid implementation for responsive layouts in React Native.

React Native uses density-independent pixels, or dp, as it's default unit. This will size elements so that they are roughly the same physical size on different devices. Whilst this is useful, I missed some of the units you have available in CSS.

Setup

Add the package from NPM. No react-native-link required.

yarn add 'react-native-units'
- or - 
npm install 'react-native-units'

Import the library where you need it:

import RNU from 'react-native-units'

Units

vw(x=1)

% of the screen width, e.g. RNU.vw(10) is equal to 10% of the screens width

vh(x=1)

% of the screen height, e.g. RNU.vh(10) is equal to 10% of the screen height

px(x=1)

Physical pixels based on device pixel ratio, e.g. RNU.px(1) is equal to 1 pixel on the device, handy for very thin lines!

su(x=1)

Scaled unit, similar to rem in CSS. You can set the scale using RNU.setScale(scale). This is useful for scaling fonts and layouts depending on the device e.g.

if(iPad) RNU.setScale(0.75) // RNU.su(10) > 7.5dp
if(iPhone5) RNU.setScale(1.5) // RNU.su(10) > 15dp

Grid

A simple way to create grids. First set your parameters:

RNU.setGrid({
	cols: 24, 
	padding: 20, 
	spacing: 10 
})

Then use the gr, gs & gp units to create your layout. I have made a snack here which will create the example below. enter image description here

gr(x=1)

This unit is equal to one column's width, however it will also include any spacing it encompasses. In this example, if RNU.gr(1) is equal to 12dp then RNU.gr(2) will equal 34dp ((12*2)+10)

gs(x=1)

The grid spacing. In this example RNU.gs() is equal to 10dp

gp(x=1)

The grid padding. In this example RNU.gp() is equal to 20dp

Screen Rotation

As this library depends on the screen width and height to calculate units, when the screen rotates you need to call RNU.update(). The easiest way is to add an onLayout to your main app component e.g.

<View onLayout={() => { RNU.update() }>
	...
</View>