# react-responsive [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Support us][gittip-image]][gittip-url]
## Information
| Package | react-responsive |
| Description |
Media queries in react for responsive design |
| Browser Version |
>= IE6* |
| Demo |
The best supported, easiest to use react media query module.
This module is pretty straightforward: You specify a set of requirements, and the children will be rendered if they are met. Also handles changes so if you resize or flip or whatever it all just works.
## Install
```console
$ npm install react-responsive --save
```
## Usage
A MediaQuery element functions like any other React component, which means you can nest them and do all the normal jazz.
### Using CSS Media Queries
```jsx
import MediaQuery from 'react-responsive';
const Example = () => (
Device Test!
You are a desktop or laptop
You also have a huge screen
You are sized like a tablet or mobile phone though
You are a tablet or mobile phone
You are portrait
You are landscape
You are retina
);
```
### Using Properties
To make things more idiomatic to react, you can use camelcased shorthands to construct media queries.
For a list of all possible shorthands and value types see https://github.com/wearefractal/react-responsive/blob/master/src/mediaQuery.js#L9
Any numbers given as a shorthand will be expanded to px (`1234` will become `'1234px'`)
```jsx
import MediaQuery from 'react-responsive';
const Example = () => (
Device Test!
You are a desktop or laptop
You also have a huge screen
You are sized like a tablet or mobile phone though
You are a tablet or mobile phone
You are portrait
You are landscape
You are retina
);
```
### Rendering with a child function
You may also specify a function for the child of the MediaQuery component. When the component renders, it is passed whether or not the given media query matches. This function must return a single element or `null`.
```jsx
{(matches) => {
if (matches) {
return Media query matches!
;
} else {
return Media query does not match!
;
}
}}
```
### Component Property
You may specify an optional `component` property on the `MediaQuery` that indicates what component to wrap children with. Any additional props defined on the `MediaQuery` will be passed through to this "wrapper" component. If the `component` property is not defined and the `MediaQuery` has more than 1 child, a `div` will be used as the "wrapper" component by default. However, if the `component` prop is not defined and there is only 1 child, that child will be rendered alone without a component wrapping it.
**Specifying Wrapper Component**
```jsx
Item 1
Item 2
// renders the following when the media query condition is met
```
**Unwrapped Component**
```jsx
Unwrapped component
// renders the following when the media query condition is met
Unwrapped component
```
**Default div Wrapper Component**
```jsx
Wrapped
Content
// renders the following when the media query condition is met
```
### Server rendering
Server rendering can be done by passing static values through the `values` property.
The values property can contain `orientation`, `scan`, `aspectRatio`, `deviceAspectRatio`,
`height`, `deviceHeight`, `width`, `deviceWidth`, `color`, `colorIndex`, `monochrome`,
`resolution` and `type` to be matched against the media query.
`type` can be one of: `all`, `grid`, `aural`, `braille`, `handheld`, `print`, `projection`,
`screen`, `tty`, `tv` or `embossed`.
Note: The `values` property always takes precedence, even on the client where a `window` object exists and matchMedia can be used.
If you are using [redux](http://redux.js.org/) you can automatically pass `width` / `deviceWidth` values to your components with [react-responsive-redux](https://github.com/modosc/react-responsive-redux).
```jsx
import MediaQuery from 'react-responsive';
const Example = () => (
Device Test!
You are a desktop or laptop
You also have a huge screen
You are sized like a tablet or mobile phone though
You are a tablet or mobile phone
You are portrait
You are landscape
You are retina
);
```
### Common use cases
```javascript
import Responsive from 'react-responsive';
const Desktop = props => ;
const Tablet = props => ;
const Mobile = props => ;
const Default = props => ;
const Example = () => (
Desktop or laptop
Tablet
Mobile
Not mobile (desktop or laptop or tablet)
);
export default Example;
```
## Browser Support
### Out of the box
| Chrome |
9 |
| Firefox (Gecko) |
6 |
| MS Edge |
All |
| Internet Explorer |
10 |
| Opera |
12.1 |
| Safari |
5.1 |
### With Polyfills
Pretty much everything. Check out these polyfills:
- [matchMedia.js by Paul Irish](https://github.com/paulirish/matchMedia.js/)
- [media-match (faster, but larger and lacking some features)](https://github.com/weblinc/media-match)
[gittip-url]: https://www.gittip.com/WeAreFractal/
[gittip-image]: http://img.shields.io/gittip/WeAreFractal.svg
[downloads-image]: http://img.shields.io/npm/dm/react-responsive.svg
[npm-url]: https://npmjs.org/package/react-responsive
[npm-image]: http://img.shields.io/npm/v/react-responsive.svg