Git Product home page Git Product logo

react-native-device-display's Introduction

๐Ÿ“ฒ React Native Device Display

This package is now deprecated and I am not actively maintaining it. In the meantime you can also use react-native-orientation.

A simple way to create dynamic views through device, display and orientation detection, allowing the creation of adaptable and universal apps. Currently only for React Native iOS, Android support in progress.

GitHub issues Version license Downloads

Example GIF Demo

Installation (iOS)

Simply install the package as shown below...

$ npm install react-native-device-display

Next you need to import the DisplayDeviceUtil classes into your project, these come bundled inside the NPM package.

Classes Installation Visual

Then require it in your project wherever you need it...

var Display = require('react-native-device-display');

Methods

Display.percentage(type, value);

Returns in pixels the percentage value of type width or height

Display.isTablet();

Returns true if the the device is a tablet (e.g iPad)

Display.isPhone();

Returns true if the the device is a phone (e.g iPhone)

Display.isPortrait();

Returns true if the the device is in a portrait position

Display.isLandscape();

Returns true if the the device is in a landscape position

Display.onOrientationDidChange(handler)

Triggers the handler call-back when the orientation changes

Properties

Display.width

Width in pixels of the display

Display.height

Height in pixels of the display

Display.verbose

Defaults to false, changing it to true enables console.log messages of orientation change events

Example

var Display = require('react-native-device-display');
var listener;

var testing = React.createClass({

  componentDidMount: function() {
    listener = Display.onOrientationDidChange(function() {
      //Change States, perform Magic, etc...
    });
  },

  componentWillUnmount: function() {
    //Unlisten the onOrientationChange...
    listener = null;
  },
  
  render: function() {
    if (Display.isPortrait()) {
      //Return portrait view...
    } else if (Display.isLandscape()) {
      //Return landscape view...
    } 
    //Add as many conditions and views as you see fit...
  }
  
});

react-native-device-display's People

Contributors

albertwchang avatar dsibiski avatar eliagrady avatar jdeal avatar kkjdaniel avatar natorojr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-native-device-display's Issues

Is orientation worked for a build product-apk

Hi all
I tried the same thing for a react-native android application. Its took the value of the screen width, height using Dimension when I reload the js otherwise this orientation (width, height changing) not worked. Can you help me to fix this issue

Thank you

Fixed Bugs and Made Changes

Hey, I know there is some official means of doing this but I'm not familiar with git really...

Anyway I made some changes I think are pretty good including the bug fix mentioned in previous

   UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
   NSDictionary *dimensions = @{ @"width": @(frameSize.width), @"height": @(frameSize.height),      @"orientation": @(deviceOrientation) };
   NSLog(@"%@", dimensions);
   [_bridge.eventDispatcher sendDeviceEventWithName:@"orientationDidChange" body:dimensions];

I now include the actual device orientation in the dictionary. I also fixed the issue with sending the wrong width / height (it was flipped around).

Then I receive the orientation in the event receiver and parse it. I also send the new data in the handler.

 onOrientationDidChange(handler) {
     var main = this;
     return RCTDeviceEventEmitter.addListener(
       'orientationDidChange',
       function(newDimensions) {
         main.updateProps(newDimensions.width, newDimensions.height);
         var orientation = main.updateOrientation(newDimensions.orientation);
         handler(newDimensions.width, newDimensions.height, orientation);
       }
     );
   }

I parse the orientation to return easier to work with object:

 updateOrientation(orientation) {
     var o = {};
     switch (orientation) {
       case 1:
         o = {
           current: 'portrait',
           idle: false,
           facing: ''
         };
         break;
       case 2:
         o = {
           current: 'portrait',
           idle: false,
           facing: ''
         };
        break;
       case 3:
         o = {
           current: 'landscape',
           idle: false,
           facing: ''
         };
         break;
       case 4:
         o = {
           current: 'landscape',
            idle: false,
           facing: ''
         };
         break;
       case 5:
         o = {
           current: this.orientation.current,
           idle: true,
           facing: 'up'
         };
         break;
       case 6:
         o = {
           current: this.orientation.current,
           idle: true,
           facing: 'down'
         };
         break;
     }
     console.log('Orientation Updated');
     console.log(o);
     this.orientation = o;
     return o;
   }

And now my handler receives:
NEW ORIENTATION DISCOVERED!
index.ios.js:22 Height: 716
index.ios.js:23 Width: 414
index.ios.js:24 Orientation:
index.ios.js:25 Object {current: "portrait", idle: true, facing: "up"}

Flipping Around Breaks This

This doesn't appear to work.

I start in portrait > 667 x 375

I rotate to landscape > 667 x375

I keep going in circle (rotate same way) to portrait > 375 x 667

I rotate again to landscape > 667 x 375

It is not reporting the proper values.

Submitting a pull request

Forgive me if this question is stupid, but I am wondering how I can update my forked version to the current version of this repo. My fork is currently @1.0.0, and I want to create a pull request from my base , and submit it to kkdaniel's master branch.

Please advise. Thanks. -Albert

Missing tag for master

currently the tag for the master is missing and if you follow the documentation and run

npm install react-native-device-display

You will get a different version from the latest release.

P.S: temporary solution for those who are facing this issue:
Use following in your package.json
"react-native-device-display": "kkjdaniel/react-native-device-display#master",

[UIScreen applicationFrame] is deprecated on iOS 9.0

Beside that using the screen dimensions is no longer proper to determine the application frame size since on iPad the app might be in split-screen mode. In order to determine the application frame one should get the frame of the application's key window.

React Native Android Support

Add support for React Native Android:

  • Percentage
  • isTablet
  • isPhone
  • isPortrait
  • isLandscape
  • onOrientationDidChange Event
  • Width & Height Properties

Seems to be incompatible with React Native 0.14.x and 0.13.x

I recently upgraded my app to React Native 0.14.x and react-native-device-display is causing the following errors to show up in the console:

2015-11-16 23:40:17.179 [error][tid:main][RCTBatchedBridge.m:436] Error while loading: Unable to resolve module Dimensions from /Users/XXX/XXX/XXX/node_modules/react-native-device-display/display.js: Invalid directory /Users/node_modules/Dimensions

screen shot 2015-11-16 at 11 40 25 pm

GPL - Incompatible with iOS App Store

The GPL is incompatible with the iOS App Store and proprietary software development. I'm not sure how many people are looking to build a GPL app and not distribute it on the App Store.

I would hazard to guess that the 500 users who have downloaded this library, the majority of them will be breaking the terms of your licence and putting themselves at risk of legal action.

I would suggest a less restrictive license like MIT, Apache or BSD.

Support newest versions of React Native

I've seen your last commit, but while using React Native 0.7.1 & 0..8.0.rc.2 are giving me errors installing your module. Could you alter your peerDepedencies less strict, while now I cannot use (without hassle) your module in the new updates of React Native. Could you update like react-native-icons has done for example:

"peerdependencies" : {
    "react-native": ">=0.6.0 <1.0.0"
}

Thanks!

Potential bug in the JS orientation predicates

The Display class will return false from both isPortrait and isLandscape in case the application frame is square (i.e. width == height).
Although application frame can not be square today, who know what new split-screen modes Apple is planning for future iOS releases.

Suggested solution: either isPortrait or isLandscape should change the condition to <=.

NPM Updates on Dimensions import issue

Hi, when i do npm install on react-native-device-display, i still get the old codes:

var { NativeModules } = require('react-native');

var DeviceUtil = NativeModules.DisplayDeviceUtil;
var Dimensions = require('Dimensions');
var RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');

Which displays the Module not found error. But i can see that this is fixed on Github copy but i'm not getting the update whenever i npm install.

FIX Code:


var {
Dimensions,
RCTDeviceEventEmitter,
NativeModules
} = require('react-native');

var DeviceUtil = NativeModules.DisplayDeviceUtil;

Thanks.

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.