Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert json to media query #16 #18

Merged
merged 1 commit into from
Oct 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,51 @@ class App extends React.Component {

The `render` prop is never called if the query does not match.

query also accepts JSON(similar to jsx style) and is converted to media query via [json2mq](https://github.com/akiran/json2mq/blob/master/README.md#usage)

```js
import React from 'react'
import Media from 'react-media'

class App extends React.Component {
render() {
return (
<div>
<Media query={{maxWidth: 599}}>
{matches => matches ? (
<p>The document is less than 600px wide.</p>
) : (
<p>The document is at least 600px wide.</p>
)}
</Media>
</div>
)
}
}
```

Pixels are default type, can use em with string.


```js
import React from 'react'
import Media from 'react-media'

class App extends React.Component {
render() {
return (
<div>
<Media query={{minWidth: 100, maxWidth: '20em'}}>
{matches => matches ? (
<p>The document has max-width of 20em and min-width of 100px.</p>
) : (
<p>This document does not.</p>
)}
</Media>
</div>
)
}
}
```

That's it :) Enjoy!
12 changes: 10 additions & 2 deletions modules/Media.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { PropTypes } from 'react'
import json2mq from 'json2mq'

class Media extends React.Component {
state = {
Expand All @@ -9,8 +10,12 @@ class Media extends React.Component {
this.setState({ matches: this.mediaQueryList.matches })

componentWillMount() {
const { query } = this.props

if (typeof window === 'object') {
this.mediaQueryList = window.matchMedia(this.props.query)
this.mediaQueryList = window.matchMedia(
typeof query === 'object' ? json2mq(query) : query
)
this.mediaQueryList.addListener(this.updateMatches)
this.updateMatches()
}
Expand All @@ -36,7 +41,10 @@ class Media extends React.Component {

if (__DEV__) {
Media.propTypes = {
query: PropTypes.string.isRequired,
query: PropTypes.oneOfType([
PropTypes.string,
PropTypes.object
]).isRequired,
render: PropTypes.func,
children: PropTypes.oneOfType([
PropTypes.node,
Expand Down
32 changes: 32 additions & 0 deletions modules/__tests__/Media-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ describe('A <Media>', () => {
})
})
})

describe('and JSON input', () => {
it('renders its child', () => {
const jsonQuery = {maxWidth: window.innerWidth}
const element = (
<Media query={jsonQuery} render={() => (
<div>hello</div>
)}/>
)

render(element, node, () => {
expect(node.firstChild.innerHTML).toMatch(/hello/)
})
})
})

})

describe('with a query that does not match', () => {
Expand Down Expand Up @@ -118,6 +134,22 @@ describe('A <Media>', () => {
})
})
})

describe('and JSON input', () => {
it('does not render its child', () => {
const jsonQuery = {minWidth: window.innerWidth + 1}
const element = (
<Media query={jsonQuery} render={() => (
<div>hello</div>
)}/>
)

render(element, node, () => {
expect(node.firstChild.innerHTML).toNotMatch(/hello/)
})
})
})

})

describe('rendered on the server', () => {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"expect": "^1.20.2",
"gzip-size": "^3.0.0",
"in-publish": "^2.0.0",
"json2mq": "^0.2.0",
"karma": "^1.2.0",
"karma-browserstack-launcher": "^1.0.1",
"karma-chrome-launcher": "^1.0.1",
Expand Down