Skip to content
This repository was archived by the owner on Nov 3, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export function isIphoneX(): boolean;
export function hasIsland(): boolean;
export function ifIphoneX<T, U>(iphoneXVal: T, regularVal: U): T | U;
export function ifIphoneX<T>(iphoneXVal: T): T;
export function getStatusBarHeight(safe?: boolean): number;
Expand Down
33 changes: 25 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
import { Dimensions, Platform, StatusBar } from 'react-native';

function isIphone() {
return Platform.OS === 'ios' && !Platform.isPad && !Platform.isTV;
}

export function isIphoneX() {
const dimen = Dimensions.get('window');
return (
Platform.OS === 'ios' &&
!Platform.isPad &&
!Platform.isTVOS &&
isIphone() &&
((dimen.height === 780 || dimen.width === 780)
|| (dimen.height === 812 || dimen.width === 812)
|| (dimen.height === 844 || dimen.width === 844)
|| (dimen.height === 896 || dimen.width === 896)
|| (dimen.height === 926 || dimen.width === 926))
|| (dimen.height === 812 || dimen.width === 812)
|| (dimen.height === 844 || dimen.width === 844)
|| (dimen.height === 896 || dimen.width === 896)
|| (dimen.height === 926 || dimen.width === 926)
|| (dimen.height === 852 || dimen.width === 852) // 14 Pro
|| (dimen.height === 932 || dimen.width === 932)) // 14 Pro Max
);
}

export function hasIsland() {
const dimen = Dimensions.get('window');
return (
isIphone() &&
((dimen.height === 852 || dimen.width === 852) // 14 Pro
|| (dimen.height === 932 || dimen.width === 932)) // 14 Pro Max
);
}

Expand All @@ -22,8 +35,12 @@ export function ifIphoneX(iphoneXStyle, regularStyle) {
}

export function getStatusBarHeight(safe) {
function safeHeight(hasIsland) {
// FIXME: There are more height values depending on the model (iPhone 12/13 -> 47, 13 mini -> 50, ..)
return hasIsland ? 59 : 44
}
return Platform.select({
ios: ifIphoneX(safe ? 44 : 30, 20),
ios: ifIphoneX(safe ? safeHeight(hasIsland()) : 30, 20),
android: StatusBar.currentHeight,
default: 0
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-iphone-x-helper",
"version": "1.3.1",
"version": "1.4.0",
"description": "A library to help you design your react-native app for the iPhone X",
"main": "index.js",
"types": "index.d.ts",
Expand Down