Skip to content

Commit

Permalink
Merge pull request #2256 from gluestack/fix/use-breakpoint
Browse files Browse the repository at this point in the history
fix: useBreakPointValue hook
  • Loading branch information
ankit-tailor authored Jun 26, 2024
2 parents 3fc8172 + eee9d8c commit 93abb7f
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions packages/nativewind/utils/useBreakpointValue/index.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Dimensions, useWindowDimensions } from 'react-native';
import { useEffect, useState } from 'react';
type BreakPointValue = Partial<{
// @ts-ignore
[key]: any;
}>;

import DefaultTheme from 'tailwindcss/defaultConfig';
import resolveConfig from 'tailwindcss/resolveConfig';

const TailwindTheme = resolveConfig(DefaultTheme);
const screenSize = TailwindTheme.theme.screens;

type breakpoints = keyof typeof screenSize | 'default';

type BreakPointValue = Partial<Record<breakpoints, any>>;

const resolveScreenWidth: any = {
default: 0,
};
Expand All @@ -23,7 +23,13 @@ export const getBreakPointValue = (values: any, width: any) => {
if (typeof values !== 'object') return values;

let finalBreakPointResolvedValue: any;
const mediaQueriesBreakpoints: any = [];
const mediaQueriesBreakpoints: any = [
{
key: 'default',
breakpoint: 0,
isValid: true,
},
];
Object.keys(resolveScreenWidth).forEach((key) => {
const isValid = isValidBreakpoint(resolveScreenWidth[key], width);

Expand All @@ -40,15 +46,16 @@ export const getBreakPointValue = (values: any, width: any) => {
breakpoint.value = values.hasOwnProperty(breakpoint.key)
? // @ts-ignore
values[breakpoint.key]
: mediaQueriesBreakpoints[index - 1].value;
: mediaQueriesBreakpoints[index - 1]?.value ||
mediaQueriesBreakpoints[0]?.value;
});

const lastValidObject = getLastValidObject(mediaQueriesBreakpoints);

if (!lastValidObject) {
finalBreakPointResolvedValue = values;
} else {
finalBreakPointResolvedValue = lastValidObject.value;
finalBreakPointResolvedValue = lastValidObject?.value;
}
return finalBreakPointResolvedValue;
};
Expand Down

0 comments on commit 93abb7f

Please sign in to comment.