Skip to content

Commit 2c46802

Browse files
committed
Add Preact compat
1 parent 2d899e6 commit 2c46802

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

modules/Media.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import React, { PropTypes } from 'react'
22
import json2mq from 'json2mq'
33

4+
/**
5+
* Conditionally renders based on whether or not a media query matches.
6+
*/
47
class Media extends React.Component {
58
static propTypes = {
69
query: PropTypes.oneOfType([
@@ -43,13 +46,21 @@ class Media extends React.Component {
4346
const { children, render } = this.props
4447
const { matches } = this.state
4548

46-
if (matches && render)
47-
return render()
48-
49-
if (typeof children === 'function')
50-
return children(matches)
51-
52-
return matches ? React.Children.only(children) : null
49+
return (
50+
render ? (
51+
matches ? render() : null
52+
) : children ? (
53+
typeof children === 'function' ? (
54+
children(matches)
55+
) : !Array.isArray(children) || children.length ? ( // Preact defaults to empty children array
56+
matches ? React.Children.only(children) : null
57+
) : (
58+
null
59+
)
60+
) : (
61+
null
62+
)
63+
)
5364
}
5465
}
5566

0 commit comments

Comments
 (0)