UNPKG

2.75 kBJavaScriptView Raw
1/*
2Copyright 2013-2015 ASIAL CORPORATION
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15
16*/
17
18// This object should not be exposed to users. Please keep this private.
19const iPhoneXPatch = {};
20
21iPhoneXPatch.isIPhoneXPortraitPatchActive = () => {
22 return document.documentElement.getAttribute('onsflag-iphonex-portrait') != null && window.innerWidth < window.innerHeight;
23};
24
25iPhoneXPatch.isIPhoneXLandscapePatchActive = () => {
26 // If width === height, treat it as landscape
27 return document.documentElement.getAttribute('onsflag-iphonex-landscape') != null && window.innerWidth >= window.innerHeight;
28};
29
30/**
31 * Returns the safe area lengths based on the current state of the safe areas.
32 */
33iPhoneXPatch.getSafeAreaLengths = () => {
34 let safeAreaLengths;
35 if (iPhoneXPatch.isIPhoneXPortraitPatchActive()) {
36 safeAreaLengths = {
37 top: 44,
38 right: 0,
39 bottom: 34,
40 left: 0
41 };
42 } else if (iPhoneXPatch.isIPhoneXLandscapePatchActive()) {
43 safeAreaLengths = {
44 top: 0,
45 right: 44,
46 bottom: 21,
47 left: 44
48 };
49 } else {
50 safeAreaLengths = {
51 top: 0,
52 right: 0,
53 bottom: 0,
54 left: 0
55 };
56 }
57
58 return safeAreaLengths;
59};
60
61/**
62 * Returns the safe area rect based on the current state of the safe areas.
63 */
64iPhoneXPatch.getSafeAreaDOMRect = () => {
65 let safeAreaRect;
66 if (iPhoneXPatch.isIPhoneXPortraitPatchActive()) {
67 safeAreaRect = {
68 x: 0,
69 y: 44, /* 0 + 44 (top safe area) */
70 width: window.innerWidth,
71 height: window.innerHeight - 78 /* height - 44 (top safe area) - 34 (bottom safe area) */
72 };
73 } else if (iPhoneXPatch.isIPhoneXLandscapePatchActive()) {
74 safeAreaRect = {
75 x: 44, /* 0 + 44 (left safe area) */
76 y: 0,
77 width: window.innerWidth - 88, /* width - 44 (left safe area) - 34 (right safe area) */
78 height: window.innerHeight - 21 /* height - 21 (bottom safe area) */
79 };
80 } else {
81 safeAreaRect = {
82 x: 0,
83 y: 0,
84 width: window.innerWidth,
85 height: window.innerHeight
86 };
87 }
88
89 return {
90 ...safeAreaRect,
91 left: safeAreaRect.x,
92 top: safeAreaRect.y,
93 right: safeAreaRect.x + safeAreaRect.width,
94 bottom: safeAreaRect.y + safeAreaRect.height
95 };
96};
97
98export default iPhoneXPatch;