-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement <WidthQuery> component
- Loading branch information
Showing
2 changed files
with
26 additions
and
1 deletion.
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,25 @@ | ||
export interface IWidthQueryProps { | ||
width: number, | ||
} | ||
|
||
export const WidthQuery: React.SFC<IWidthQueryProps> = ({width, children}) => { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (!Array.isArray(children)) { | ||
throw new TypeError('<WidthQuery/> expects multiple <View/> children.'); | ||
} | ||
} | ||
|
||
for (const child of children as any) { | ||
const {maxWidth = Infinity, minWidth = 0} = child.props; | ||
|
||
if (maxWidth > width && minWidth <= width) { | ||
return child; | ||
} | ||
|
||
if (maxWidth === Infinity && width === Infinity) { | ||
return child; | ||
} | ||
} | ||
|
||
return null; | ||
}; |