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-responsive-stylesheet

v1.1.0

Published

React-native library for creating stylesheets that return different styles based on screen size

Downloads

145

Readme

react-native-responsive-stylesheet

React-native library for creating stylesheets that return different styles based on screen size. It does this by dynamically checking the current screen width and height using the Dimensions API whenever you render a component.

** NOTE: ** This library does not re-render on screen size changes, if you want to handle those cases, you must listen to the onLayout event to trigger a re-render.

Install

npm install --save react-native-responsive-stylesheet

API

Importing

import ResponsiveStylesheet from "react-native-responsive-stylesheet"

ResponsiveStylesheet.create()

Same as StyleSheet.create, creates a regular StyleSheet with your styles in it

ResponsiveStylesheet.createSized(direction, map) stylesheet

Creates a responsive stylesheet based size increments or decrements (like media queries)

map is an object literal mapping of {size: obj} where size is an integer representing pixels, and obj is a style definition to use with .create(obj)

direction is one of "min-width", "max-width", "min-height", "max-height", "min-direction" or "max-direction". It's purpose is to determine whether the size represents the width or height, and whether you should look for size values higher or greater than the current screen resolution.

  • min/max-direction uses the smallest direction value, height or width.

The return value is a special kind of stylesheet where when you try to get a style, you will get back an array of all styles that are valid for the current resolution. Check the examples for details.

The values are in the proper order to overwrite each other as they progress.

ResponsiveStylesheet.createOriented(map)

Creates a responsive stylesheet based on styles for landscape and portrait orientations

map is an object literal with the optional keys landscape and portrait pointing to a stylesheet definition used for .create().

The return value is a stylesheet which will dynamically look up the style definition based on whether you're in landscape or portrait mode. See the examples for details

Examples

Change direction and margin based on orientation

import ResponsiveStylesheet from "react-native-responsive-stylesheet"
import {View} from "react-native";
import React from "react";

var normalStyles = ResponsiveStylesheet.create({
	item: {
		flex: 1,
		backgroundColor: "red",
		alignSelf: "stretch"
	}
});

var responsiveStyles = ResponsiveStylesheet.createOriented({
	landscape: {
		item: {
			marginHorizontal: 16
		},
		container: {
			flexDirection: "row"
		}
	},
	portrait: {
		item: {
			marginVertical: 16
		},
		container: {
			flexDirection: "column"
		}
	}
});

// Stateless component
// Have parent re-render with the `onLayout` event to see changes
export default function OrientedList(){
	var itemStyle = [responsiveStyles.item, normalStyles.item];

	return (
		<View style={styles.container}>
			<View style={item} />
			<View style={item} />
			<View style={item} />
		</View>
	)
}

Responsive grid

import ResponsiveStylesheet from "react-native-responsive-stylesheet"
import {View, ScrollView} from "react-native";
import React from "react";

var normalStyles = ResponsiveStylesheet.create({
	container: {
		flexDirection: "row",
		// Make grid items align horizontally and wrap onto new lines
		flexWrap: "wrap",
		justifyContent: "space-around"
	},
	item: {
		backgroundColor: "red",
		// Default on small screens
		width: 64,
		height: 64
	}
});

var responsiveStyles = ResponsiveStylesheet.createSized("min-width", {
	// Make them bigger on tablets
	768: {
		// Make sure you specify the style name and definition you want
		item: {
			width: 128,
			height: 128
		}
	},
	// Make them even bigger on wide screens (desktop)
	1224: {
		item: {
			width: 128,
			height: 128
		}
	},
});


export default function ResponsiveGrid(){
	// Make a flat array of styles since `responsiveStyles.item` will return an array
	var itemStyle = [normalStyles.item].concat(responsiveStyles.item);

	return (
		<ScrollView>
			<View style={normalStyles.container}>
				<View style={itemStyle} />
				<View style={itemStyle} />
				<View style={itemStyle} />
				<View style={itemStyle} />
				<View style={itemStyle} />
				<View style={itemStyle} />
				<View style={itemStyle} />
				<View style={itemStyle} />
				<View style={itemStyle} />
				<View style={itemStyle} />
				<View style={itemStyle} />
				<View style={itemStyle} />
				<View style={itemStyle} />
				<View style={itemStyle} />
				<View style={itemStyle} />
				<View style={itemStyle} />
			</View>
		</ScrollView>
	);
}

react-native-web support

Since this module doesn't rely on any native APIs uses ES5 and CommonJS, you should be able to include it in any react-native-web project without any other configuration (other than setting up an alias for react-native)