Skip to content

Commit

Permalink
switch to module syntax in order to support future tree shaking. (#49)
Browse files Browse the repository at this point in the history
Unifies interface on all platforms
Module syntax for potential tree shaking
  • Loading branch information
EyMaddis authored and Sunhat committed Mar 20, 2019
1 parent 33503f2 commit 0d118b7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 43 deletions.
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ declare type Dimensions =

declare interface ExtraDimensions {
get: (dim: Dimensions) => number;
getRealWindowHeight: () => number;
getRealWindowWidth: () => number;
getStatusBarHeight: () => number;
getSoftMenuBarHeight: () => number;
getSmartBarHeight: () => number;
isSoftMenuBarEnabled: () => number;
}

declare module "react-native-extra-dimensions-android" {
Expand Down
97 changes: 54 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,58 @@
const React = require('react');
var { NativeModules, Platform } = require('react-native');

if (Platform.OS === 'android') {
module.exports = {
get(dim) {
try {
if(!NativeModules.ExtraDimensions) {
throw "ExtraDimensions not defined. Try rebuilding your project. e.g. react-native run-android";
}
const result = NativeModules.ExtraDimensions[dim];

if(typeof result !== 'number') {
return result;
}
import { NativeModules, Platform } from 'react-native';

export function get(dim) {
if (Platform.OS !== 'android') {

console.warn('react-native-extra-dimensions-android is only available on Android. Trying to access', dim);
return 0;
} else { // android
try {
if (!NativeModules.ExtraDimensions) {
throw "ExtraDimensions not defined. Try rebuilding your project. e.g. react-native run-android";
}
const result = NativeModules.ExtraDimensions[dim];

if (typeof result !== 'number') {
return result;
} catch (e) {
console.error(e);
}
},
getRealWindowHeight() {
return this.get('REAL_WINDOW_HEIGHT');
},
getRealWindowWidth() {
return this.get('REAL_WINDOW_WIDTH');
},
getStatusBarHeight() {
return this.get('STATUS_BAR_HEIGHT');
},
getSoftMenuBarHeight() {
return this.get('SOFT_MENU_BAR_HEIGHT');
},
getSmartBarHeight() {
return this.get('SMART_BAR_HEIGHT');
},
isSoftMenuBarEnabled() {
return this.get('SOFT_MENU_BAR_ENABLED');
}
};
} else {
module.exports = {
get(dim) {
console.warn('react-native-extra-dimensions-android is only available on Android');
return 0;
return result;
} catch (e) {
console.error(e);
}
};
}
}

export function getRealWindowHeight() {
return get('REAL_WINDOW_HEIGHT');
}

export function getRealWindowWidth() {
return get('REAL_WINDOW_WIDTH');
}

export function getStatusBarHeight() {
return get('STATUS_BAR_HEIGHT');
}

export function getSoftMenuBarHeight() {
return get('SOFT_MENU_BAR_HEIGHT');
}

export function getSmartBarHeight() {
return get('SMART_BAR_HEIGHT');
}

export function isSoftMenuBarEnabled() {
return get('SOFT_MENU_BAR_ENABLED');
}

// stay compatible with pre-es6 exports
export default {
get,
getRealWindowHeight,
getRealWindowWidth,
getStatusBarHeight,
getSoftMenuBarHeight,
getSmartBarHeight,
isSoftMenuBarEnabled
}

0 comments on commit 0d118b7

Please sign in to comment.