A react-native module that can listen on orientation changing of device, get current orientation, lock to preferred orientation.
Installations
npm install react-native-orientation-locker
Developer
wonday
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
20.9.0
NPM Version
10.1.0
Statistics
762 Stars
297 Commits
274 Forks
10 Watching
1 Branches
46 Contributors
Updated on 27 Nov 2024
Languages
JavaScript (28.13%)
Java (28.03%)
C++ (21.05%)
Objective-C (17.39%)
Starlark (2.06%)
C (1.71%)
Ruby (1.63%)
Total Downloads
Cumulative downloads
Total Downloads
13,061,831
Last day
-6.3%
19,744
Compared to previous day
Last week
-1.5%
98,408
Compared to previous week
Last month
11.3%
420,693
Compared to previous month
Last year
30%
4,218,889
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Peer Dependencies
3
Dev Dependencies
3
react-native-orientation-locker
A react-native module that can listen on orientation changing of device, get current orientation, lock to preferred orientation. (cross-platform support)
Feature
- lock screen orientation to PORTRAIT|LANDSCAPE-LEFT|PORTRAIT-UPSIDEDOWN|LANDSCAPE-RIGHT.
- listen on orientation changing of device
- get the current orientation of device
ChangeLog
ChangeLog details
v1.7.0 1. Add: Add support for new iOS orientation API on newer OS versions 2. Fix: Fix reporting UI orientation upon unlockv1.6.0
- Add: Add vs2022 support. Remove unnecessary reference
- Fix: Change WindowsTargetPlatformVersion to 10.0
- Fix: added check for broadcast receiver being registered and unregistered to avoid IllegalArgumentException
- Fix: RN-0.73 change Android configs for RN 0.73 compatibility
- Fix: Add RECEIVER_EXPORTED/RECEIVER_NOT_EXPORTED to support Android 14
v1.5.0
- Fix: threading issue on Windows and fix android not triggering event in some cases
- Fix: add stub methods to remove warning while using listeners
v1.4.0
- Fix typescript types
- Fix some bugs
v1.3.1
- Fix require cycle problem
v1.3.0 BREAKING CHANGES
- Add support for Window
- Modify sample project
- Updates Tyescript define
v1.2.0 BREAKING CHANGES
- Add support for lockAllOrientationsButUpsideDown
- Ignore on web and desktop (#115)
- Fix for not finding @ReactModule (#125)
- Fix unlockAllOrientations on Android (#133)
- Implement ActivityLifecycleCallbacks on Android (#131)
Please be sure to add to onCreate
of your MainApplication
import org.wonday.orientation.OrientationActivityLifecycle;
@Override
public void onCreate() {
...
+ registerActivityLifecycleCallbacks(OrientationActivityLifecycle.getInstance());
}
v1.1.8
- Support FACE-UP and FACE-DOWN on iOS
v1.1.7
- Add lockToPortraitUpsideDown() to iOS
- Minor case corrections
v1.1.6
- Catch unknown device orientation value
- When calling unlockAllOrientations(), forcibly unlock whether locked or not
v1.1.5
- Add Orientation.isLocked() and Orientation.removeAllListeners()
v1.1.4
- Fix TypeScript declarations
v1.1.3
- Add
addLockListener
andremoveLockListener
- Improve Android orientation changed event sending condition
v1.1.2
- Improve Android orientation changed event timing
v1.1.1
- Fix show "supported event type for deviceOrientationDidChange..." error in debug
- Fix getAutoRotateState() code error
v1.1.0 BREAKING CHANGES
- Split
addOrientationListener(function(orientation, deviceOrientation))
toaddOrientationListener(function(orientation))
andaddDeviceOrientationListener(function(deviceOrientation))
- Make sure when lockToXXX and unlockAllOrientations resend UI orientation event
- remove setTimout from orientation listener
- Add getAutoRotateState() for Android
- Add TypeScript definitions
Notice
-
RN 0.58 + Android target SDK 27 maybe cause
Issue: java.lang.IllegalStateException: Only fullscreen activities can request orientation
problem, see [#55] for a solution. -
orientationDidChange will be delayed on iPads if we set upside down to true. Simply disable upside down for iPad and everything works like a charm ([#78] Thanks truongluong1314520)
-
If you get the following build error on iOS:
ld: library not found for -lRCTOrientation-tvOS
Just remove it from linked libraries and frameworks -
For Windows, locking to an orientation will only work on devices in tablet mode.
-
For Windows, getting information on device orientation and tracking its changes will only be possible on devices with an orientation sensor. If the device running your application does not have the appropriate hardware to support tracking device orientation,
getDeviceOrientation()
will return UNKNOWN.
To run example on Windows Tablet (This will allow one to view modules full functionality)
- Open example/windows/example.sln in Visual Studio.
- Go to Project > Publish > Create App Packages.
- Certificate password is "password".
- Select "Sideloading" and hit "Next".
- Hit "Yes, use the current certificate" and hit "Next".
- Choose desired output location and hit "Create".
- Got to output location and copy example_
_Test directory over to tablet device. - On tablet device open APPXBUNDLE File and hit "Install" (make sure tablet is in developer mode). App should launch after install is complete.
- Turn on tablet mode on device to see locking to a UI orientation functionality.
Installation
Using yarn (RN 0.60 and and above)
yarn add react-native-orientation-locker
Using yarn (RN 0.59 and and below)
yarn add react-native-orientation-locker
react-native link react-native-orientation-locker
Manual linking
For Windows, if you are using RNW v0.63.0 or higher, autolinking should link the module for you. Otherwise, you must follow the steps outlined here for linking module.
Add following to MainApplication.java (This will be added automatically by auto link. If not, please manually add the following )
1//... 2+import org.wonday.orientation.OrientationPackage; 3 @Override 4 protected List<ReactPackage> getPackages() { 5 @SuppressWarnings("UnnecessaryLocalVariable") 6 List<ReactPackage> packages = new PackageList(this).getPackages(); 7 // Packages that cannot be autolinked yet can be added manually here, for example: 8 // packages.add(new MyReactNativePackage()); 9+ packages.add(new OrientationPackage()); 10 return packages; 11 } 12//...
Using CocoaPods (iOS Only)
Run pod install
in the ios directory. Linking is not required in React Native 0.60 and above.
Configuration
iOS
Add the following to your project's AppDelegate.m
:
1+#import "Orientation.h" 2 3@implementation AppDelegate 4 5// ... 6 7+- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window { 8+ return [Orientation getOrientation]; 9+} 10 11@end
Android
Add following to android/app/src/main/AndroidManifest.xml
1 <activity 2 .... 3+ android:configChanges="keyboard|keyboardHidden|orientation|screenSize" 4 android:windowSoftInputMode="adjustResize"> 5 6 .... 7 8 </activity> 9
Implement onConfigurationChanged method (in MainActivity.java
)
1// ... 2 3+import android.content.Intent; 4+import android.content.res.Configuration; 5 6public class MainActivity extends ReactActivity { 7 8+ @Override 9+ public void onConfigurationChanged(Configuration newConfig) { 10+ super.onConfigurationChanged(newConfig); 11+ Intent intent = new Intent("onConfigurationChanged"); 12+ intent.putExtra("newConfig", newConfig); 13+ this.sendBroadcast(intent); 14+ } 15 16 // ...... 17}
Add following to MainApplication.java
1+import org.wonday.orientation.OrientationActivityLifecycle; 2 @Override 3 public void onCreate() { 4+ registerActivityLifecycleCallbacks(OrientationActivityLifecycle.getInstance()); 5 }
Usage
Imperative API
Whenever you want to use it within React Native code now you can:
import Orientation from 'react-native-orientation-locker';
1 2import Orientation from 'react-native-orientation-locker'; 3 4 5 _onOrientationDidChange = (orientation) => { 6 if (orientation == 'LANDSCAPE-LEFT') { 7 //do something with landscape left layout 8 } else { 9 //do something with portrait layout 10 } 11 }; 12 13 componentWillMount() { 14 //The getOrientation method is async. It happens sometimes that 15 //you need the orientation at the moment the js starts running on device. 16 //getInitialOrientation returns directly because its a constant set at the 17 //beginning of the js code. 18 var initial = Orientation.getInitialOrientation(); 19 if (initial === 'PORTRAIT') { 20 //do stuff 21 } else { 22 //do other stuff 23 } 24 }, 25 26 componentDidMount() { 27 28 Orientation.getAutoRotateState((rotationLock) => this.setState({rotationLock})); 29 //this allows to check if the system autolock is enabled or not. 30 31 Orientation.lockToPortrait(); //this will lock the view to Portrait 32 //Orientation.lockToLandscapeLeft(); //this will lock the view to Landscape 33 //Orientation.unlockAllOrientations(); //this will unlock the view to all Orientations 34 35 //get current UI orientation 36 /* 37 Orientation.getOrientation((orientation)=> { 38 console.log("Current UI Orientation: ", orientation); 39 }); 40 41 //get current device orientation 42 Orientation.getDeviceOrientation((deviceOrientation)=> { 43 console.log("Current Device Orientation: ", deviceOrientation); 44 }); 45 */ 46 47 Orientation.addOrientationListener(this._onOrientationDidChange); 48 }, 49 50 componentWillUnmount: function() { 51 Orientation.removeOrientationListener(this._onOrientationDidChange); 52 }
Reactive component <OrientationLocker>
It is possible to have multiple OrientationLocker
components mounted at the same time. The props will be merged in the order the OrientationLocker
components were mounted. This follows the same usability of <StatusBar>.
1import React, { useState } from "react"; 2import { Text, View } from "react-native"; 3import { OrientationLocker, PORTRAIT, LANDSCAPE } from "react-native-orientation-locker"; 4 5export default function App() { 6 const [showVideo, setShowVideo] = useState(true); 7 return ( 8 <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> 9 <OrientationLocker 10 orientation={PORTRAIT} 11 onChange={orientation => console.log('onChange', orientation)} 12 onDeviceChange={orientation => console.log('onDeviceChange', orientation)} 13 /> 14 <Button title="Toggle Video" onPress={() => setShowVideo(!showVideo)} /> 15 {showVideo && ( 16 <View> 17 <OrientationLocker orientation={LANDSCAPE} /> 18 <View style={{ width: 320, height: 180, backgroundColor: '#ccc' }}> 19 <Text>Landscape video goes here</Text> 20 </View> 21 </View> 22 )} 23 </View> 24 ); 25};
Hooks
useOrientationChange
: hook foraddOrientationListener
eventuseDeviceOrientationChange
: hook foraddDeviceOrientationListener
event
function SomeComponent() {
useOrientationChange((o) => {
// Handle orientation change
});
useDeviceOrientationChange((o) => {
// Handle device orientation change
});
}
Events
addOrientationListener(function(orientation))
When UI orientation changed, callback function will be called.
But if lockToXXX is called , callback function will be not called untill unlockAllOrientations.
It can return either PORTRAIT
LANDSCAPE-LEFT
LANDSCAPE-RIGHT
PORTRAIT-UPSIDEDOWN
UNKNOWN
When lockToXXX/unlockAllOrientations, it will force resend UI orientation changed event.
-
removeOrientationListener(function(orientation))
-
addDeviceOrientationListener(function(deviceOrientation))
When device orientation changed, callback function will be called.
When lockToXXX is called, callback function also can be called.
It can return either PORTRAIT
LANDSCAPE-LEFT
LANDSCAPE-RIGHT
PORTRAIT-UPSIDEDOWN
UNKNOWN
-
removeDeviceOrientationListener(function(deviceOrientation))
-
addLockListener(function(orientation))
When call lockToXXX/unlockAllOrientations, callback function will be called.
It can return either PORTRAIT
LANDSCAPE-LEFT
LANDSCAPE-RIGHT
UNKNOWN
UNKNOWN
means not be locked.
-
removeLockListener(function(orientation))
-
removeAllListeners()
Functions
configure({ disableFaceUpDown: boolean })
(ios only)lockToPortrait()
lockToLandscape()
lockToLandscapeLeft()
this will lock to camera left home button rightlockToLandscapeRight()
this will lock to camera right home button leftlockToPortraitUpsideDown
only support android and WindowslockToAllOrientationsButUpsideDown
only iosunlockAllOrientations()
getOrientation(function(orientation))
getDeviceOrientation(function(deviceOrientation))
getAutoRotateState(function(state))
(android only)isLocked()
(lock status by this library)
orientation can return one of:
PORTRAIT
LANDSCAPE-LEFT
camera left home button rightLANDSCAPE-RIGHT
camera right home button leftPORTRAIT-UPSIDEDOWN
FACE-UP
FACE-DOWN
UNKNOWN
Notice: PORTRAIT-UPSIDEDOWN is currently not supported on iOS at the moment. FACE-UP and FACE-DOWN are only supported on iOS and Windows.
No vulnerabilities found.
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
binaries present in source code
Details
- Warn: binary detected: example/android/app/gradle/wrapper/gradle-wrapper.jar:1
- Warn: binary detected: example/android/gradle/wrapper/gradle-wrapper.jar:1
Reason
Found 16/21 approved changesets -- score normalized to 7
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 25 are checked with a SAST tool
Score
3.7
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to react-native-orientation-locker
expo-screen-orientation
Expo universal module for managing device's screen orientation
@bugsnag/plugin-react-native-orientation-breadcrumbs
@bugsnag/js plugin to create breadcrumbs when the device orientation changes in a React Native app
react-native-orientation-locker-cn
A react-native module that can listen on orientation changing of device, get current orientation, lock to preferred orientation.
react-native-orientation
Listen to device orientation changes in React Native applications and programmatically set preferred orientations on a per screen basis