Skip to content

Commit 9f8092f

Browse files
committed
feat: use-break-point-value hook
1 parent 442a398 commit 9f8092f

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { Dimensions, useWindowDimensions } from 'react-native';
2+
import { useEffect, useState } from 'react';
3+
4+
import DefaultTheme from 'tailwindcss/defaultConfig';
5+
import resolveConfig from 'tailwindcss/resolveConfig';
6+
7+
const TailwindTheme = resolveConfig(DefaultTheme);
8+
const screenSize = TailwindTheme.theme.screens;
9+
10+
type breakpoints = keyof typeof screenSize | 'default';
11+
12+
type BreakPointValue = Partial<Record<breakpoints, any>>;
13+
14+
const resolveScreenWidth: any = {
15+
default: 0,
16+
};
17+
18+
Object.entries(screenSize).forEach(([key, value]) => {
19+
resolveScreenWidth[key] = parseInt(value.replace('px', ''));
20+
});
21+
22+
export const getBreakPointValue = (values: any, width: any) => {
23+
if (typeof values !== 'object') return values;
24+
25+
let finalBreakPointResolvedValue: any;
26+
const mediaQueriesBreakpoints: any = [
27+
{
28+
key: 'default',
29+
breakpoint: 0,
30+
isValid: true,
31+
},
32+
];
33+
Object.keys(resolveScreenWidth).forEach((key) => {
34+
const isValid = isValidBreakpoint(resolveScreenWidth[key], width);
35+
36+
mediaQueriesBreakpoints.push({
37+
key: key,
38+
breakpoint: resolveScreenWidth[key],
39+
isValid: isValid,
40+
});
41+
});
42+
43+
mediaQueriesBreakpoints.sort((a: any, b: any) => a.breakpoint - b.breakpoint);
44+
45+
mediaQueriesBreakpoints.forEach((breakpoint: any, index: any) => {
46+
breakpoint.value = values.hasOwnProperty(breakpoint.key)
47+
? // @ts-ignore
48+
values[breakpoint.key]
49+
: mediaQueriesBreakpoints[index - 1]?.value ||
50+
mediaQueriesBreakpoints[0]?.value;
51+
});
52+
53+
const lastValidObject = getLastValidObject(mediaQueriesBreakpoints);
54+
55+
if (!lastValidObject) {
56+
finalBreakPointResolvedValue = values;
57+
} else {
58+
finalBreakPointResolvedValue = lastValidObject?.value;
59+
}
60+
return finalBreakPointResolvedValue;
61+
};
62+
63+
export function useBreakpointValue(values: BreakPointValue) {
64+
const { width } = useWindowDimensions();
65+
66+
const [currentBreakPointValue, setCurrentBreakPointValue] = useState(null);
67+
68+
useEffect(() => {
69+
if (typeof values === 'object') {
70+
const finalBreakPointResolvedValue = getBreakPointValue(values, width);
71+
setCurrentBreakPointValue(finalBreakPointResolvedValue);
72+
}
73+
}, [values, width]);
74+
75+
if (typeof values !== 'object') return values;
76+
77+
return currentBreakPointValue;
78+
}
79+
80+
export function isValidBreakpoint(
81+
breakPointWidth: any,
82+
width: any = Dimensions.get('window')?.width
83+
) {
84+
const windowWidth = width;
85+
86+
if (windowWidth >= breakPointWidth) {
87+
return true;
88+
}
89+
return false;
90+
}
91+
92+
function getLastValidObject(mediaQueries: any) {
93+
for (let i = mediaQueries.length - 1; i >= 0; i--) {
94+
if (mediaQueries[i].isValid) {
95+
return mediaQueries[i];
96+
}
97+
}
98+
return null; // No valid object found
99+
}

0 commit comments

Comments
 (0)