Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

License

NotificationsYou must be signed in to change notification settings

AlixH/react-native-responsive-stylesheet

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 theDimensions 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 theonLayout event to trigger a re-render.

Install

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

API

Importing

importResponsiveStylesheetfrom"react-native-responsive-stylesheet"

ResponsiveStylesheet.create()

Same asStyleSheet.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} wheresize is an integer representing pixels, andobj 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 thesize represents thewidth orheight, and whether you should look forsize values higher or greater than the current screen resolution.

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

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 forlandscape andportrait orientations

map is an object literal with the optional keyslandscape andportrait 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

importResponsiveStylesheetfrom"react-native-responsive-stylesheet"import{View}from"react-native";importReactfrom"react";varnormalStyles=ResponsiveStylesheet.create({item:{flex:1,backgroundColor:"red",alignSelf:"stretch"}});varresponsiveStyles=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 changesexportdefaultfunctionOrientedList(){varitemStyle=[responsiveStyles.item,normalStyles.item];return(<Viewstyle={styles.container}><Viewstyle={item}/><Viewstyle={item}/><Viewstyle={item}/></View>)}

Responsive grid

importResponsiveStylesheetfrom"react-native-responsive-stylesheet"import{View,ScrollView}from"react-native";importReactfrom"react";varnormalStyles=ResponsiveStylesheet.create({container:{flexDirection:"row",// Make grid items align horizontally and wrap onto new linesflexWrap:"wrap",justifyContent:"space-around"},item:{backgroundColor:"red",// Default on small screenswidth:64,height:64}});varresponsiveStyles=ResponsiveStylesheet.createSized("min-width",{// Make them bigger on tablets768:{// Make sure you specify the style name and definition you wantitem:{width:128,height:128}},// Make them even bigger on wide screens (desktop)1224:{item:{width:128,height:128}},});exportdefaultfunctionResponsiveGrid(){// Make a flat array of styles since `responsiveStyles.item` will return an arrayvaritemStyle=[normalStyles.item].concat(responsiveStyles.item);return(<ScrollView><Viewstyle={normalStyles.container}><Viewstyle={itemStyle}/><Viewstyle={itemStyle}/><Viewstyle={itemStyle}/><Viewstyle={itemStyle}/><Viewstyle={itemStyle}/><Viewstyle={itemStyle}/><Viewstyle={itemStyle}/><Viewstyle={itemStyle}/><Viewstyle={itemStyle}/><Viewstyle={itemStyle}/><Viewstyle={itemStyle}/><Viewstyle={itemStyle}/><Viewstyle={itemStyle}/><Viewstyle={itemStyle}/><Viewstyle={itemStyle}/><Viewstyle={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 anyreact-native-web project without any other configuration (other than setting up an alias forreact-native)

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript100.0%

[8]ページ先頭

©2009-2025 Movatter.jp