-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0dfc736
commit 32d316b
Showing
7 changed files
with
90 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# @gluestack-ui/nativewind-utils | ||
|
||
## 1.0.20 | ||
|
||
### Patch Changes | ||
|
||
- - useBreakPointValue hook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
type BreakPointValue = Partial<{}>; | ||
export declare const getBreakPointValue: (values: any, width: any) => any; | ||
export declare function useBreakpointValue(values: BreakPointValue): null; | ||
export declare function isValidBreakpoint( | ||
breakPointWidth: any, | ||
width?: any | ||
): boolean; | ||
export {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Dimensions, useWindowDimensions } from 'react-native'; | ||
import { useEffect, useState } from 'react'; | ||
import DefaultTheme from 'tailwindcss/defaultConfig'; | ||
import resolveConfig from 'tailwindcss/resolveConfig'; | ||
const TailwindTheme = resolveConfig(DefaultTheme); | ||
const screenSize = TailwindTheme.theme.screens; | ||
const resolveScreenWidth = { | ||
default: 0, | ||
}; | ||
Object.entries(screenSize).forEach(([key, value]) => { | ||
resolveScreenWidth[key] = parseInt(value.replace('px', '')); | ||
}); | ||
export const getBreakPointValue = (values, width) => { | ||
if (typeof values !== 'object') return values; | ||
let finalBreakPointResolvedValue; | ||
const mediaQueriesBreakpoints = []; | ||
Object.keys(resolveScreenWidth).forEach((key) => { | ||
const isValid = isValidBreakpoint(resolveScreenWidth[key], width); | ||
mediaQueriesBreakpoints.push({ | ||
key: key, | ||
breakpoint: resolveScreenWidth[key], | ||
isValid: isValid, | ||
}); | ||
}); | ||
mediaQueriesBreakpoints.sort((a, b) => a.breakpoint - b.breakpoint); | ||
mediaQueriesBreakpoints.forEach((breakpoint, index) => { | ||
breakpoint.value = values.hasOwnProperty(breakpoint.key) | ||
? // @ts-ignore | ||
values[breakpoint.key] | ||
: mediaQueriesBreakpoints[index - 1].value; | ||
}); | ||
const lastValidObject = getLastValidObject(mediaQueriesBreakpoints); | ||
if (!lastValidObject) { | ||
finalBreakPointResolvedValue = values; | ||
} else { | ||
finalBreakPointResolvedValue = lastValidObject.value; | ||
} | ||
return finalBreakPointResolvedValue; | ||
}; | ||
export function useBreakpointValue(values) { | ||
const { width } = useWindowDimensions(); | ||
const [currentBreakPointValue, setCurrentBreakPointValue] = useState(null); | ||
useEffect(() => { | ||
if (typeof values === 'object') { | ||
const finalBreakPointResolvedValue = getBreakPointValue(values, width); | ||
setCurrentBreakPointValue(finalBreakPointResolvedValue); | ||
} | ||
}, [values, width]); | ||
if (typeof values !== 'object') return values; | ||
return currentBreakPointValue; | ||
} | ||
export function isValidBreakpoint( | ||
breakPointWidth, | ||
width = Dimensions.get('window')?.width | ||
) { | ||
const windowWidth = width; | ||
if (windowWidth >= breakPointWidth) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
function getLastValidObject(mediaQueries) { | ||
for (let i = mediaQueries.length - 1; i >= 0; i--) { | ||
if (mediaQueries[i].isValid) { | ||
return mediaQueries[i]; | ||
} | ||
} | ||
return null; // No valid object found | ||
} |
File renamed without changes.