Coder Social home page Coder Social logo

gaguirre / react-native-orientation Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yamill/react-native-orientation

0.0 1.0 1.0 30 KB

Listen to device orientation changes in react-native and set preferred orientation on screen to screen basis.

Home Page: https://www.npmjs.com/package/react-native-orientation

Objective-C 44.33% Java 43.88% JavaScript 11.78%

react-native-orientation's Introduction

react-native-orientation

Listen to device orientation changes in react-native and set preferred orientation on screen to screen basis.

Add it to your project

Run npm install react-native-orientation --save

iOS

  1. Open your project in XCode, right click on your project and click Add Files to "Your Project Name"
  2. Add RCTOrientation folder from your node_modules/react-native-orientation folder. Make sure you have 'Create Groups' selected

Android

  1. In android/setting.gradle

    ...
    include ':Orientation', ':app'
    project(':Orientation').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-orientation/android')
    
  2. In android/app/build.gradle

    ...
    dependencies {
        ...
        compile project(':Orientation')
    }
    
  3. Register module (in MainActivity.java)

    import com.github.yamill.orientation.OrientationPackage;  // <--- import
    
    public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
      ......
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mReactRootView = new ReactRootView(this);
    
        mReactInstanceManager = ReactInstanceManager.builder()
          .setApplication(getApplication())
          .setBundleAssetName("index.android.bundle")
          .setJSMainModuleName("index.android")
          .addPackage(new MainReactPackage())
          .addPackage(new OrientationPackage(this))              // <------ add here
          .setUseDeveloperSupport(BuildConfig.DEBUG)
          .setInitialLifecycleState(LifecycleState.RESUMED)
          .build();
    
        mReactRootView.startReactApplication(mReactInstanceManager, "ExampleRN", null);
    
        setContentView(mReactRootView);
      }
    
      ......
    
    }
    
  4. Implement onConfigurationChanged method (in MainActivity.java)

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Intent intent = new Intent("onConfigurationChanged");
        intent.putExtra("newConfig", newConfig);
        this.sendBroadcast(intent);
    }

Whenever you want to use it within React Native code now you can: var Orientation = require('react-native-orientation');

Usage

  _orientationDidChange: function(orientation) {
    if(orientation == 'LANDSCAPE'){
      //do something with landscape layout
    }else{
      //do something with portrait layout
    }
  },
  componentDidMount: function(){
    Orientation.lockToPortrait(); //this will lock the view to Portrait
    //Orientation.lockToLandscape(); //this will lock the view to Landscape
    //Orientation.unlockAllOrientations(); //this will unlock the view to all Orientations

    Orientation.addOrientationListener(this._orientationDidChange);
  },
  componentWillUnmount: function() {
	Orientation.getOrientation((err,orientation)=> {
		console.log("Current Device Orientation: ", orientation);
	});
    Orientation.removeOrientationListener(this._orientationDidChange);
  }

Events

  • addOrientationListener(function(orientation))
  • removeOrientationListener(function(orientation))

Functions

  • lockToPortrait()
  • lockToLandscape()
  • unlockAllOrientations()
  • getOrientation(function(err, orientation)

orientation can return either LANDSCAPE PORTRAIT UNKNOWN also 'PORTRAITUPSIDEDOWN' is now different from PORTRAIT

TODOS

  • Add some way to allow setting a preferred orientation on a screen by screen basis.
  • Make API Cleaner to Orientation Locking
  • [] Find a way to expose shouldAutoRotate (??? a PR would be great)

react-native-orientation's People

Contributors

albertwchang avatar brianjd avatar chetstone avatar daleljefferson avatar gaguirre avatar jaysoo avatar thoblr avatar vitaminwater avatar watert avatar yamill avatar

Watchers

 avatar

Forkers

jerocosta

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.