This repository was archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathindex.ts
More file actions
135 lines (113 loc) · 3.67 KB
/
index.ts
File metadata and controls
135 lines (113 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { useState, useEffect, useRef } from "react";
import { Dimensions, ScaledSize } from "react-native";
const useDimensionsListener = () => {
const [screenDimension, setScreenDimension] = useState(
Dimensions.get("screen")
);
const [windowDimension, setWindowDimension] = useState(
Dimensions.get("window")
);
useEffect(() => {
function handleDimensionChange({
window,
screen
}: {
window: ScaledSize;
screen: ScaledSize;
}) {
setWindowDimension(window);
setScreenDimension(screen);
}
let handleDimensionChangeLinstener = Dimensions.addEventListener("change", handleDimensionChange);
return () => {
handleDimensionChangeLinstener.remove();
};
}, []);
return {
screen: screenDimension,
window: windowDimension
};
};
type EffectParams = {
screen: ScaledSize;
window: ScaledSize;
};
type EffectCallback =
| ((opts: EffectParams) => () => any)
| ((opts: EffectParams) => undefined)
| ((opts: EffectParams) => void);
const percentageCalculation = (max: number, val: number) => max * (val / 100);
const fontCalculation = (height: number, width: number, val: number) => {
const widthDimension = height > width ? width : height;
const aspectRatioBasedHeight = (16 / 9) * widthDimension;
return percentageCalculation(
Math.sqrt(
Math.pow(aspectRatioBasedHeight, 2) + Math.pow(widthDimension, 2)
),
val
);
};
export const useDimensionsChange = (effect: EffectCallback) => {
const hasMountRef = useRef(false);
const dimensions = useDimensionsListener();
useEffect(() => {
if (hasMountRef.current) {
const destroy = effect(dimensions);
let cleanUp: any = () => null;
if (typeof destroy === "function") {
cleanUp = destroy;
}
return () => cleanUp();
} else {
hasMountRef.current = true;
}
}, [dimensions, effect]);
};
export const responsiveHeight = (h: number) => {
const { height } = Dimensions.get("window");
return percentageCalculation(height, h);
};
export const responsiveWidth = (w: number) => {
const { width } = Dimensions.get("window");
return percentageCalculation(width, w);
};
export const responsiveFontSize = (f: number) => {
const { height, width } = Dimensions.get("window");
return fontCalculation(height, width, f);
};
export const responsiveScreenHeight = (h: number) => {
const { height } = Dimensions.get("screen");
return percentageCalculation(height, h);
};
export const responsiveScreenWidth = (w: number) => {
const { width } = Dimensions.get("screen");
return percentageCalculation(width, w);
};
export const responsiveScreenFontSize = (f: number) => {
const { height, width } = Dimensions.get("screen");
return fontCalculation(height, width, f);
};
export const useResponsiveHeight = (h: number) => {
const { height } = useDimensionsListener().window;
return percentageCalculation(height, h);
};
export const useResponsiveWidth = (w: number) => {
const { width } = useDimensionsListener().window;
return percentageCalculation(width, w);
};
export const useResponsiveFontSize = (f: number) => {
const { height, width } = useDimensionsListener().window;
return fontCalculation(height, width, f);
};
export const useResponsiveScreenHeight = (h: number) => {
const { height } = useDimensionsListener().screen;
return percentageCalculation(height, h);
};
export const useResponsiveScreenWidth = (w: number) => {
const { width } = useDimensionsListener().screen;
return percentageCalculation(width, w);
};
export const useResponsiveScreenFontSize = (f: number) => {
const { height, width } = useDimensionsListener().screen;
return fontCalculation(height, width, f);
};