Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to module syntax, support React Native for Web #49

Merged
merged 1 commit into from Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions index.d.ts
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
@@ -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
}