From f18bb73eaaa8f5d6de701bfe1b6c03febad3c7dd Mon Sep 17 00:00:00 2001 From: Vadim Dalecky Date: Sun, 4 Mar 2018 02:03:51 +0000 Subject: [PATCH] feat: implement component --- package.json | 2 +- src/WidthQuery/index.tsx | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/WidthQuery/index.tsx diff --git a/package.json b/package.json index 7e8f2db5..9a4fde5f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "libreact", - "version": "0.14.0", + "version": "0.15.0", "description": "React standard library", "main": "lib/index.js", "repository": { diff --git a/src/WidthQuery/index.tsx b/src/WidthQuery/index.tsx new file mode 100644 index 00000000..54233f93 --- /dev/null +++ b/src/WidthQuery/index.tsx @@ -0,0 +1,25 @@ +export interface IWidthQueryProps { + width: number, +} + +export const WidthQuery: React.SFC = ({width, children}) => { + if (process.env.NODE_ENV !== 'production') { + if (!Array.isArray(children)) { + throw new TypeError(' expects multiple 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; +};