react-native-responsive-styles

React Native styles that respond to orientation change

Github星跟蹤圖


How-to

Installation:

npm i --save react-native-responsive-styles

This library depends on walmartlabs/react-native-orientation-listener, which contains native dependencies for iOS and Android. Link them with:

react-native link react-native-orientation-listener

You can now import StyleSheet and any React components from react-native-responsive-styles instead of react-native:

import {
  View,
  Image,
  ScrollView,
  StyleSheet
} from 'react-native-responsive-styles';

You can then define custom style properties for landscape and portrait orientations:

const styles = StyleSheet.create({
  container: {
    flex: 1,
    // define separate styles for portrait and landscape
    portrait: {
      flexDirection: 'column'
    },
    landscape: {
      flexDirection: 'row'
    }
  },
  mainContent: {
    flex: 8,
    // override a default value when orientation is landscape
    landscape: {
      flex: 1
    }
  },
  secondaryContent: {
    flex: 1,
    // override a default value when orientation is portrait
    portrait: {
      backgroundColor: '#ccc'
    }
  }
});

Usage of StyleSheet.create is not required. Plain style objects will work as well, as long as you use the React components from this package.

That's it!

Demo

Demo

Advanced features

Animate transitions

You can apply a LayoutAnimation to orientation changes. Valid animation values are spring, linear and easeInEaseOut.

StyleSheet.configureLayoutAnimation('spring');

The default behaviour is no animation, but configuring one is recommended to avoid visual glitches when rearranging elements.

Make your custom components responsive

To add support for orientation-specific styles to your own components or other third-party components, wrap them in a higher-order component with StyleSheet.makeResponsive:

import React from 'react';
import StyleSheet from 'react-native-responsive-styles';

class CustomComponent extends React.Component({
  //...
});

export default StyleSheet.makeResponsive(CustomComponent);

In most cases this should not be required. You can simply nest your component in a responsive View.

Hiding elements

A common use case is to hide an element entirely in one orientation. You can use StyleSheet.hidden for that:

const styles = StyleSheet.create({
  landscapeOnly: {
    flex: 1,
    portrait: StyleSheet.hidden
  }
})

Please note

This project is in a pre-release state. The API may be considered relatively stable, but changes may still occur.

MIT licensed

主要指標

概覽
名稱與所有者FormidableLabs/react-native-responsive-styles
主編程語言JavaScript
編程語言JavaScript (語言數: 1)
平台
許可證MIT License
所有者活动
創建於2016-09-27 20:24:37
推送於2022-02-18 22:24:30
最后一次提交2022-02-18 14:24:29
發布數2
最新版本名稱v0.2.1 (發布於 2016-09-28 14:20:23)
第一版名稱v0.2.0 (發布於 2016-09-28 13:40:09)
用户参与
星數169
關注者數56
派生數22
提交數23
已啟用問題?
問題數4
打開的問題數2
拉請求數2
打開的拉請求數5
關閉的拉請求數0
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?