Skip to content

Instantly share code, notes, and snippets.

@jimthedev
Created January 30, 2017 19:10
Show Gist options
  • Save jimthedev/edcfe00d3f27da2a248b97559ce6e29f to your computer and use it in GitHub Desktop.
Save jimthedev/edcfe00d3f27da2a248b97559ce6e29f to your computer and use it in GitHub Desktop.
eventually will be react-native-dims
import React from 'react';
import {
Dimensions
} from 'react-native';
var {height: deviceOriginalHeight, width: deviceOriginalWidth} = Dimensions.get('window');
var deviceOriginalOrientation = orientation(deviceOriginalWidth, deviceOriginalHeight);
// Warning only tested on iOS so far
//
// Eventually will be react-native-dims
//
// USAGE:
//
// Set up state
// constructor(props) {
// super(props);
// this.state = {
// ...deviceInitialDimensions
// }
// }
// Listen for actual orientation changes
// <View onLayout={deviceOrientationDidChange.bind(this, () => {
// // Normalized for human consumption so in landscape
// // height is the short side, in portrait the long side
// console.log(
// this.state.deviceOrientation,
// this.state.deviceWidth,
// this.state.deviceHeight
// );
// })}
//
//
// These properties get added to the state
var deviceInitialDimensions = {
deviceOriginalWidth,
deviceOriginalHeight,
deviceOriginalOrientation,
deviceWidth: deviceOriginalWidth,
deviceHeight: deviceOriginalHeight,
deviceOrientation: deviceOriginalOrientation,
};
function orientation(width, height) {
return width > height ? 'landscape' : 'portrait';
}
function min(first, second) {
if(first > second) {
return second;
} else {
return first;
}
}
function max(first, second) {
if(first < second) {
return second;
} else {
return first;
}
}
// Internal function that determines if orientation has changed given
// a previous orientation.
function didDeviceOrientationChange(devicePreviousOrientation, deviceOrientationChangedCallback, deviceOrientationDidNotChangeCallback) {
if(!devicePreviousOrientation) { throw new Error('Missing parameter in devicePreviousOrientation. Provide the previous orientation.') }
var { height: deviceCurrentHeight, width: deviceCurrentWidth } = Dimensions.get('window')
var deviceCurrentOrientation = orientation(deviceCurrentWidth, deviceCurrentHeight);
if(devicePreviousOrientation !== deviceCurrentOrientation) {
deviceOrientationChangedCallback({deviceWidth:deviceCurrentWidth, deviceHeight:deviceCurrentHeight, deviceOrientation: deviceCurrentOrientation});
}
}
// This will mutate the state then trigger a callback.
// A user MUST bind this function explicitly as an onLayout
// handler. It uses this.setState so explicit binding is
// required.
//
function deviceOrientationDidChange(cb, evt) {
const {deviceOrientation} = this.state;
didDeviceOrientationChange(deviceOrientation, (newDimensions)=> {
const {deviceWidth, deviceHeight, deviceOrientation} = newDimensions;
this.setState({
deviceWidth,
deviceHeight,
deviceOrientation
}, cb);
})
}
export {
deviceInitialDimensions,
deviceOrientationDidChange,
orientation,
min,
max,
};
// Note: This is a very simple example
// Typically the deviceHeight would be most useful inside of a scrollview's contentContainerStyle
// or some other view whose parent has no height itself. minheight in react native needs a parent
// to have a height.
//
import React, {Component} from 'react';
import {
Text,
View
} from 'react-native';
import {deviceInitialDimensions, deviceOrientationDidChange} from './dims.js';
export default class MinHeightView extends Component {
constructor(props) {
super(props);
this.state = {
...deviceInitialDimensions
}
}
render() {
return (
<View onLayout={deviceOrientationDidChange.bind(this, () => {
// Orientation changed!
console.log(this.state);
})} style={{minHeight: this.state.deviceHeight}}>
<Text>
Original Device Dimensions
</Text>
<Text>
{this.state.deviceOriginalWidth}x{this.state.deviceOriginalHeight} @ {this.state.deviceOriginalOrientation}
</Text>
<Text>
Current Device Dimensions
</Text>
<Text>
{this.state.deviceWidth}x{this.state.deviceHeight} @ {this.state.deviceOrientation}
</Text>
</View>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment