Skip to content

Commit efb5ee1

Browse files
committed
feat: add Responsive component
1 parent 1540673 commit efb5ee1

4 files changed

Lines changed: 54 additions & 3 deletions

File tree

src/components/Helpers/Hoverable.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export interface HoverableChildrenProps {
1010

1111
export interface HoverableProps {
1212
children: React.ReactNode | RenderPropType;
13-
onHoverIn: () => void;
14-
onHoverOut: () => void;
13+
onHoverIn?: () => void;
14+
onHoverOut?: () => void;
1515
}
1616

1717
export default class Hoverable extends React.Component<HoverableProps> {

src/components/Helpers/RefMeasure.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ class RefMeasure extends React.Component<RefMeasureProps, Measurements> {
7070
pageY,
7171
};
7272

73+
this.setState(() => nodeMeasurements);
74+
7375
if (onMeasure) {
7476
onMeasure(nodeMeasurements);
7577
}
76-
this.setState(() => nodeMeasurements);
7778
},
7879
);
7980
};
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import * as React from 'react';
2+
import { Dimensions } from 'react-native';
3+
4+
export type RenderPropType = (
5+
props: ResponsiveChildrenProps,
6+
) => React.ReactNode;
7+
8+
export interface ResponsiveChildrenProps {
9+
matches: boolean;
10+
}
11+
12+
export interface ResponsiveProps {
13+
children: React.ReactNode | RenderPropType;
14+
minWidth?: number;
15+
maxWidth?: number;
16+
}
17+
18+
export const isWindowAboveMinWidth = (minWidth: number) => {
19+
const scaledSize = Dimensions.get('window');
20+
21+
return scaledSize.width > minWidth;
22+
};
23+
24+
export const isWindowBelowMaxWidth = (maxWidth: number) => {
25+
const scaledSize = Dimensions.get('window');
26+
27+
return scaledSize.width < maxWidth;
28+
};
29+
30+
export const Responsive = (props: ResponsiveProps) => {
31+
const { children, minWidth, maxWidth } = props;
32+
33+
let isAboveMinWidth = true;
34+
let isBelowMaxWidth = true;
35+
36+
if (minWidth) {
37+
isAboveMinWidth = isWindowAboveMinWidth(minWidth);
38+
}
39+
40+
if (maxWidth) {
41+
isBelowMaxWidth = isWindowBelowMaxWidth(maxWidth);
42+
}
43+
44+
const isRenderProp = typeof children === 'function';
45+
const matches = isAboveMinWidth && isBelowMaxWidth;
46+
47+
// @ts-ignore
48+
return isRenderProp ? children({ matches }) : matches ? children : null;
49+
};

src/components/Helpers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export { default as ViewMeasure, ViewMeasureProps } from './ViewMeasure';
2+
export * from './Responsive';
23
export {
34
default as Hoverable,
45
HoverableProps,

0 commit comments

Comments
 (0)