1- ## react-native-orientation
2- Listen to device orientation changes in react-native and set preferred orientation on screen to screen basis.
1+ ## React Native Orientation
2+ [ ![ npm version ] ( https://badge.fury.io/js/ react-native- orientation.svg )] ( https://badge.fury.io/js/react-native-orientation )
33
4- ### Installation
4+ Listen to device orientation changes in React Native applications and set preferred orientation on a per screen basis. Works on both Android and iOS.
55
6- #### via rnpm
6+ ## NOTE - PLEASE READ
7+ ** The current version of this package on NPM's registry is not updated** . In order to use this package with RN apps on versions 0.40+ you must point your package to the ` master ` branch of this repository or install it like so:
78
8- Run ` rnpm install react-native-orientation `
9-
10- > Note: rnpm will install and link the library automatically.
11-
12- #### via npm
13-
14- Run ` npm install react-native-orientation --save `
15-
16- ### Linking
9+ ```
10+ npm install --save react-native-orientation@git+https://github.com/yamill/react-native-orientation.git
11+ ```
1712
18- #### Using rnpm (iOS + Android)
13+ ## Installing
1914
20- ` rnpm link react-native-orientation `
15+ ```
16+ npm install --save react-native-orientation@git+https://github.com/yamill/react-native-orientation.git
17+ ```
2118
22- #### Using [ CocoaPods ] ( https://cocoapods.org ) (iOS Only)
19+ ## Linking Native Dependencies
2320
24- ` pod 'react-native-orientation', :path => 'node_modules/react-native-orientation' `
21+ ### Automatic Linking
2522
26- Consult the React Native documentation on how to [ install React Native using CocoaPods] ( https://facebook.github.io/react-native/docs/embedded-app-ios.html#install-react-native-using-cocoapods ) .
23+ ```
24+ react-native link react-native-orientation
25+ ```
2726
28- #### Manually
27+ ### Manual Linking
2928
3029** iOS**
3130
@@ -53,9 +52,9 @@ Consult the React Native documentation on how to [install React Native using Coc
5352 }
5453 ```
5554
56- 3. Register module ( in MainApplication.java)
55+ 3. Register module in ` MainApplication.java`
5756
58- ```
57+ ```java
5958 import com.github.yamill.orientation.OrientationPackage; // <--- import
6059
6160 public class MainApplication extends Application implements ReactApplication {
@@ -76,7 +75,7 @@ Consult the React Native documentation on how to [install React Native using Coc
7675
7776### Configuration
7877
79- #### iOS
78+ ** iOS**
8079
8180Add the following to your project's `AppDelegate.m`:
8281
@@ -94,11 +93,11 @@ Add the following to your project's `AppDelegate.m`:
9493@end
9594```
9695
97- #### Android
96+ ** Android**
9897
99- Implement onConfigurationChanged method ( in MainActivity.java)
98+ Implement ` onConfigurationChanged ` method in ` MainActivity.java `
10099
101- ```
100+ ``` java
102101 import android.content.Intent ; // <--- import
103102 import android.content.res.Configuration ; // <--- import
104103
@@ -119,79 +118,107 @@ Implement onConfigurationChanged method (in MainActivity.java)
119118
120119## Usage
121120
122- Whenever you want to use it within React Native code now you can:
123- ` var Orientation = require('react-native-orientation'); `
121+ To use the ` react-native-orientation ` package in your codebase, you should use the Orientation module:
122+ ``` javascript
123+ import Orientation from ' react-native-orientation' ;
124+ ```
124125
125126``` javascript
126- _orientationDidChange : function (orientation ) {
127- if (orientation == ' LANDSCAPE' ) {
128- // do something with landscape layout
129- } else {
130- // do something with portrait layout
131- }
132- },
127+ export default class AppScreen extends Component {
128+ // ...
129+
130+ componentWillMount () {
131+ // The getOrientation method is async. It happens sometimes that
132+ // you need the orientation at the moment the JS runtime starts running on device.
133+ // `getInitialOrientation` returns directly because its a constant set at the
134+ // beginning of the JS runtime.
133135
134- componentWillMount : function () {
135- // The getOrientation method is async. It happens sometimes that
136- // you need the orientation at the moment the js starts running on device.
137- // getInitialOrientation returns directly because its a constant set at the
138- // beginning of the js code.
139- var initial = Orientation .getInitialOrientation ();
136+ const initial = Orientation .getInitialOrientation ();
140137 if (initial === ' PORTRAIT' ) {
141- // do stuff
138+ // do something
142139 } else {
143- // do other stuff
140+ // do something else
144141 }
145142 },
146143
147- componentDidMount : function () {
148- Orientation .lockToPortrait (); // this will lock the view to Portrait
149- // Orientation.lockToLandscape(); //this will lock the view to Landscape
150- // Orientation.unlockAllOrientations(); //this will unlock the view to all Orientations
144+ componentDidMount () {
145+ // this locks the view to Portrait Mode
146+ Orientation .lockToPortrait ();
147+
148+ // this locks the view to Landscape Mode
149+ // Orientation.lockToLandscape();
150+
151+ // this unlocks any previous locks to all Orientations
152+ // Orientation.unlockAllOrientations();
151153
152154 Orientation .addOrientationListener (this ._orientationDidChange );
153155 },
154156
155- componentWillUnmount : function () {
156- Orientation .getOrientation ((err ,orientation )=> {
157- console .log (" Current Device Orientation: " , orientation);
157+ _orientationDidChange = (orientation ) => {
158+ if (orientation === ' LANDSCAPE' ) {
159+ // do something with landscape layout
160+ } else {
161+ // do something with portrait layout
162+ }
163+ },
164+
165+ componentWillUnmount () {
166+ Orientation .getOrientation ((err , orientation ) => {
167+ console .log (` Current Device Orientation: ${ orientation} ` );
158168 });
169+
170+
171+ // Remember to remove listener
159172 Orientation .removeOrientationListener (this ._orientationDidChange );
160173 }
174+
175+ render () {
176+ // ...
177+
178+ return (
179+ // ...
180+ )
181+ }
182+ }
161183```
162184
163- ## Events
185+ ## Orientation Events
164186
165- - ` addOrientationListener(function(orientation)) `
187+ ``` javascript
188+ addOrientationListener ((orientation ) => {});
189+ ```
166190
167- orientation can return either ` LANDSCAPE ` ` PORTRAIT ` ` UNKNOWN `
168- also ` PORTRAITUPSIDEDOWN ` is now different from ` PORTRAIT `
191+ ` orientation ` will return one of the following values:
192+ - ` LANDSCAPE `
193+ - ` PORTRAIT `
194+ - ` PORTRAITUPSIDEDOWN `
195+ - ` UNKNOWN `
169196
170- - ` removeOrientationListener(function(orientation)) `
197+ ``` javascript
198+ removeOrientationListener ((orientation ) => {});
199+ ```
171200
172- - ` addSpecificOrientationListener(function(specificOrientation)) `
201+ ``` javascript
202+ addSpecificOrientationListener ((specificOrientation ) => {});
203+ ```
173204
174- specificOrientation can return either ` LANDSCAPE-LEFT ` ` LANDSCAPE-RIGHT ` ` PORTRAIT ` ` UNKNOWN ` ` PORTRAITUPSIDEDOWN `
205+ ` specificOrientation ` will return one of the following values:
206+ - ` LANDSCAPE-LEFT `
207+ - ` LANDSCAPE-RIGHT `
208+ - ` PORTRAIT `
209+ - ` PORTRAITUPSIDEDOWN `
210+ - ` UNKNOWN `
175211
176- - ` removeSpecificOrientationListener(function(specificOrientation)) `
212+ ``` javascript
213+ removeSpecificOrientationListener ((specificOrientation ) => {});
214+ ```
177215
178- ## Functions
216+ ## API
179217
180218- ` lockToPortrait() `
181219- ` lockToLandscape() `
182220- ` lockToLandscapeLeft() `
183221- ` lockToLandscapeRight() `
184222- ` unlockAllOrientations() `
185- - ` getOrientation(function(err, orientation) `
186-
187- orientation can return either ` LANDSCAPE ` ` PORTRAIT ` ` UNKNOWN ` ` PORTRAITUPSIDEDOWN `
188-
189- - ` getSpecificOrientation(function(err, specificOrientation) `
190-
191- specificOrientation can return either ` LANDSCAPE-LEFT ` ` LANDSCAPE-RIGHT ` ` PORTRAIT ` ` UNKNOWN ` ` PORTRAITUPSIDEDOWN `
192-
193- ## TODOS
194-
195- - [x] Add some way to allow setting a preferred orientation on a screen by screen basis.
196- - [x] Make API Cleaner to Orientation Locking
197- - [x] Android Support
223+ - ` getOrientation((err, orientation) => {}) `
224+ - ` getSpecificOrientation((err, specificOrientation) => {}) `
0 commit comments