You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+22-45Lines changed: 22 additions & 45 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,25 +7,22 @@
7
7
8
8
[`react-media`](https://www.npmjs.com/package/react-media) is a CSS media query component for React.
9
9
10
-
A `<Media>` component listens for matches to a
11
-
[CSS media query](https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries) and renders stuff
12
-
based on whether the query matches or not.
10
+
A `<Media>` component listens for matches to a [CSS media query](https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries) and renders stuff based on whether the query matches or not.
13
11
14
12
## Installation
15
13
16
-
Using [npm](https://www.npmjs.com/):
14
+
Using npm:
17
15
18
16
$ npm install --save react-media
19
17
20
-
Then with a module bundler like [webpack](https://webpack.github.io/), use as you would anything
21
-
else:
18
+
Then, use as you would anything else:
22
19
23
20
```js
24
21
// using ES modules
25
-
importMediafrom"react-media"
22
+
importMediafrom"react-media";
26
23
27
24
// using CommonJS modules
28
-
var Media =require("react-media")
25
+
var Media =require("react-media");
29
26
```
30
27
31
28
The UMD build is also available on [unpkg](https://unpkg.com):
@@ -38,14 +35,11 @@ You can find the library on `window.ReactMedia`.
38
35
39
36
## Usage
40
37
41
-
Render a `<Media>` component with a `query` prop whose value is a valid
42
-
[CSS media query](https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries). The `children`
43
-
prop should be a function whose only argument will be a boolean flag that indicates whether the
44
-
media query matches or not.
38
+
Render a `<Media>` component with a `query` prop whose value is a valid [CSS media query](https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries). The `children` prop should be a function whose only argument will be a boolean flag that indicates whether the media query matches or not.
45
39
46
40
```jsx
47
-
importReactfrom"react"
48
-
importMediafrom"react-media"
41
+
importReactfrom"react";
42
+
importMediafrom"react-media";
49
43
50
44
classAppextendsReact.Component {
51
45
render() {
@@ -61,25 +55,20 @@ class App extends React.Component {
61
55
}
62
56
</Media>
63
57
</div>
64
-
)
58
+
);
65
59
}
66
60
}
67
61
```
68
62
69
63
If you render a `<Media>` component on the server, it always matches.
70
64
71
-
If you use a regular React element as `children` (i.e. `<Media><SomethingHere/></Media>`) it will be
72
-
rendered if the query matches. However, _you may end up creating a bunch of elements that won't ever
73
-
actually be rendered to the page_ (i.e. you'll do a lot of unnecessary `createElement`s on each
74
-
`render`). Thus, a `children`**function** (i.e. `<Media>{matches => ...}</Media>`) is the preferred
75
-
API. Then you can decide in the callback which elements to create based on the result of the query.
65
+
If you use a regular React element as `children` (i.e. `<Media><SomethingHere/></Media>`) it will be rendered if the query matches. However, _you may end up creating a bunch of elements that won't ever actually be rendered to the page_ (i.e. you'll do a lot of unnecessary `createElement`s on each `render`). Thus, a `children`**function** (i.e. `<Media>{matches => ...}</Media>`) is the preferred API. Then you can decide in the callback which elements to create based on the result of the query.
76
66
77
-
For the common case of "only render something when the media query matches", you can use a `render`
78
-
prop that is only called if the query matches.
67
+
For the common case of "only render something when the media query matches", you can use a `render` prop that is only called if the query matches.
79
68
80
69
```jsx
81
-
importReactfrom"react"
82
-
importMediafrom"react-media"
70
+
importReactfrom"react";
71
+
importMediafrom"react-media";
83
72
84
73
classAppextendsReact.Component {
85
74
render() {
@@ -90,21 +79,18 @@ class App extends React.Component {
90
79
render={() =><p>The document is less than 600px wide.</p>}
91
80
/>
92
81
</div>
93
-
)
82
+
);
94
83
}
95
84
}
96
85
```
97
86
98
87
The `render` prop is never called if the query does not match.
99
88
100
-
`<Media query>` also accepts an object, similar to
101
-
[React's built-in support for inline style objects](https://facebook.github.io/react/tips/inline-styles.html)
102
-
in e.g. `<div style>`. These objects are converted to CSS media queries via
`<Media query>` also accepts an object, similar to [React's built-in support for inline style objects](https://facebook.github.io/react/tips/inline-styles.html) in e.g. `<div style>`. These objects are converted to CSS media queries via [json2mq](https://github.com/akiran/json2mq/blob/master/README.md#usage).
104
90
105
91
```jsx
106
-
importReactfrom"react"
107
-
importMediafrom"react-media"
92
+
importReactfrom"react";
93
+
importMediafrom"react-media";
108
94
109
95
classAppextendsReact.Component {
110
96
render() {
@@ -120,28 +106,19 @@ class App extends React.Component {
120
106
}
121
107
</Media>
122
108
</div>
123
-
)
109
+
);
124
110
}
125
111
}
126
112
```
127
113
128
-
Keys of media query objects are camel-cased and numeric values automatically get the `px` suffix.
129
-
See the [json2mq docs](https://github.com/akiran/json2mq/blob/master/README.md#usage) for more
130
-
examples of queries you can construct using objects.
114
+
Keys of media query objects are camel-cased and numeric values automatically get the `px` suffix. See the [json2mq docs](https://github.com/akiran/json2mq/blob/master/README.md#usage) for more examples of queries you can construct using objects.
131
115
132
-
An optional `targetWindow` prop can be specified if you want the `query` to be
133
-
evaluated against a different window object than the one the code is running in.
134
-
This can be useful for example if you are rendering part of your component tree
135
-
to an iframe or [a popup window](https://hackernoon.com/using-a-react-16-portal-to-do-something-cool-2a2d627b0202).
116
+
An optional `targetWindow` prop can be specified if you want the `query` to be evaluated against a different window object than the one the code is running in. This can be useful for example if you are rendering part of your component tree to an iframe or [a popup window](https://hackernoon.com/using-a-react-16-portal-to-do-something-cool-2a2d627b0202).
136
117
137
-
If you're curious about how react-media differs from
138
-
[react-responsive](https://github.com/contra/react-responsive), please see
If you're curious about how react-media differs from [react-responsive](https://github.com/contra/react-responsive), please see [this comment](https://github.com/ReactTraining/react-media/issues/70#issuecomment-347774260).
140
119
141
120
Enjoy!
142
121
143
122
## About
144
123
145
-
`react-media` is developed and maintained by [React Training](https://reacttraining.com). If you're
146
-
interested in learning more about what React can do for your company, please
`react-media` is developed and maintained by [React Training](https://reacttraining.com). If you're interested in learning more about what React can do for your company, please [get in touch](mailto:[email protected])!
0 commit comments