Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit 0f1c267

Browse files
committed
created calculation utils
1 parent 986b8c9 commit 0f1c267

File tree

1 file changed

+25
-28
lines changed

1 file changed

+25
-28
lines changed

src/index.ts

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ type EffectCallback =
6767
| ((opts: EffectParams) => undefined)
6868
| ((opts: EffectParams) => void);
6969

70+
const percentageCalculation = (max: number, val: number) => max * (val / 100);
71+
72+
const fontCalculation = (height: number, width: number, val: number) => {
73+
const widthDimension = height > width ? width : height;
74+
const aspectRatioBasedHeight = (16 / 9) * widthDimension;
75+
return percentageCalculation(
76+
Math.sqrt(
77+
Math.pow(aspectRatioBasedHeight, 2) + Math.pow(widthDimension, 2)
78+
),
79+
val
80+
);
81+
};
82+
7083
export const useDimensionsChange = (effect: EffectCallback) => {
7184
const hasMountRef = useRef(false);
7285
const dimensions = useDimensionsListener();
@@ -87,76 +100,60 @@ export const useDimensionsChange = (effect: EffectCallback) => {
87100

88101
export const responsiveHeight = (h: number) => {
89102
const { height } = Dimensions.get("window");
90-
return height * (h / 100);
103+
return percentageCalculation(height, h);
91104
};
92105

93106
export const responsiveWidth = (w: number) => {
94107
const { width } = Dimensions.get("window");
95-
return width * (w / 100);
108+
return percentageCalculation(width, w);
96109
};
97110

98111
export const responsiveFontSize = (f: number) => {
99112
const { height, width } = Dimensions.get("window");
100-
const lowerSize = height > width ? width : height;
101-
const tempHeight = (16 / 9) * lowerSize;
102-
return (
103-
Math.sqrt(Math.pow(tempHeight, 2) + Math.pow(lowerSize, 2)) * (f / 100)
104-
);
113+
return fontCalculation(height, width, f);
105114
};
106115

107116
export const responsiveScreenHeight = (h: number) => {
108117
const { height } = Dimensions.get("screen");
109-
return height * (h / 100);
118+
return percentageCalculation(height, h);
110119
};
111120

112121
export const responsiveScreenWidth = (w: number) => {
113122
const { width } = Dimensions.get("screen");
114-
return width * (w / 100);
123+
return percentageCalculation(width, w);
115124
};
116125

117126
export const responsiveScreenFontSize = (f: number) => {
118127
const { height, width } = Dimensions.get("screen");
119-
const lowerSize = height > width ? width : height;
120-
const tempHeight = (16 / 9) * lowerSize;
121-
return (
122-
Math.sqrt(Math.pow(tempHeight, 2) + Math.pow(lowerSize, 2)) * (f / 100)
123-
);
128+
return fontCalculation(height, width, f);
124129
};
125130

126131
export const useResponsiveHeight = (h: number) => {
127132
const { height } = useDimensionsListener().window;
128-
return height * (h / 100);
133+
return percentageCalculation(height, h);
129134
};
130135

131136
export const useResponsiveWidth = (w: number) => {
132137
const { width } = useDimensionsListener().window;
133-
return width * (w / 100);
138+
return percentageCalculation(width, w);
134139
};
135140

136141
export const useResponsiveFontSize = (f: number) => {
137142
const { height, width } = useDimensionsListener().window;
138-
const lowerSize = height > width ? width : height;
139-
const tempHeight = (16 / 9) * lowerSize;
140-
return (
141-
Math.sqrt(Math.pow(tempHeight, 2) + Math.pow(lowerSize, 2)) * (f / 100)
142-
);
143+
return fontCalculation(height, width, f);
143144
};
144145

145146
export const useResponsiveScreenHeight = (h: number) => {
146147
const { height } = useDimensionsListener().screen;
147-
return height * (h / 100);
148+
return percentageCalculation(height, h);
148149
};
149150

150151
export const useResponsiveScreenWidth = (w: number) => {
151152
const { width } = useDimensionsListener().screen;
152-
return width * (w / 100);
153+
return percentageCalculation(width, w);
153154
};
154155

155156
export const useResponsiveScreenFontSize = (f: number) => {
156157
const { height, width } = useDimensionsListener().screen;
157-
const lowerSize = height > width ? width : height;
158-
const tempHeight = (16 / 9) * lowerSize;
159-
return (
160-
Math.sqrt(Math.pow(tempHeight, 2) + Math.pow(lowerSize, 2)) * (f / 100)
161-
);
158+
return fontCalculation(height, width, f);
162159
};

0 commit comments

Comments
 (0)