Skip to content

Commit 4b4a0cf

Browse files
authored
Merge pull request #7 from ehellman/release/3.0.0
3.0.0
2 parents 5b66b64 + ff2a933 commit 4b4a0cf

27 files changed

+10438
-1432
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
.DS_Store
33
lib
44
es
5-
.vscode
5+
.vscode
6+
example/node_modules

.npmignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
modules
33
node_modules
44
README.md
5-
CHANGELOG.md
5+
CHANGELOG.md
6+
example

.prettierignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package.json
2+
package-lock.json
3+
.babelrc
4+
.npmignore
5+
.gitignore

.prettierrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"useTabs": false,
3+
"printWidth": 80,
4+
"tabWidth": 2,
5+
"singleQuote": true,
6+
"trailingComma": "all",
7+
"jsxBracketSameLine": false,
8+
"parser": "babylon",
9+
"semi": false,
10+
"arrowParens": "avoid",
11+
"bracketSpacing": true
12+
}

README.md

Lines changed: 73 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,18 @@
44

55
This library solves the problem that CSS media queries alone could not solve. Sometimes you want to create an application that looks a certain way on desktop and a certain way on mobile. Sometimes the components look too different for you to be able to just change the CSS, you have to make one component for desktop and another for mobile. This is bad, because the JavaScript for the hidden component is still running in the background even though you are not seeing it.
66

7-
This library solves that.
8-
97
`react-breakpoints` allows you to use the viewport width to load different components, opening up for building more complex responsive applications without suffering the performance problems of hidden desktop components on your mobile site and vice versa.
108

11-
This library is not dependent on Redux, see documentation for instructions on how to use.
9+
Version 2.0.0 was rewrite with the new context API that came in React `16.3.0`. A polyfill for older React versions is included in the library, so it is backwards compatible with `15.x.x` and `16.x.x`. However, version 4.0.0 will no longer support `15.x.x`.
1210

13-
Version 2.0.0 is a rewrite with the new context API that came in React `16.3.0`. A polyfill for older React versions is included in the library, so it is backwards compatible with `15.x.x` and `16.x.x`.
11+
Version 3.0.0 introduced `<Media>` with `renderProps` an alternative to the `withBreakpoints` HOC.
1412

1513
## Roadmap
1614

17-
**VERSION 3.0.0**
18-
- [ ] Test that `guessedBreakpoint` prop from server side rendering works, make changes if needed.
19-
- [ ] Example project
20-
- [ ] `debounceOptions` object passdown if needed.
15+
* [ ] `debounceOptions` object passdown if needed.
2116

2217
## Installation
18+
2319
`npm install --save react-breakpoints`
2420

2521
## Usage
@@ -44,65 +40,92 @@ const breakpoints = {
4440
ReactDOM.render(
4541
<ReactBreakpoints breakpoints={breakpoints}>
4642
<App />
47-
</ReactBreakpoints>,
48-
document.getElementById('root')
43+
</ReactBreakpoints>,
44+
document.getElementById('root'),
4945
)
5046
```
5147

5248
## Inside your components
5349

54-
When you want access to the current screen width inside a component you import the `withBreakpoints` function, wrapping your component when you export it. This will give you access to `props.screenWidth` which updates whenever you resize your window or your device orientation changes and `props.breakpoints` which is the original object which you supplied to the `ReactBreakpoints` component.
50+
When you want access to the current screen width inside a component you import the `withBreakpoints` function, wrapping your component when you export it. This will give you access to `props.currentBreakpoint` which updates whenever you resize your window to the point where it hits a new breakpoint, or your device orientation changes. It also adds `props.breakpoints` which is the original object which you supplied to the `ReactBreakpoints` component, so you can make comparisons with `props.currentBreakpoint`.
51+
52+
### Render Props
5553

5654
```js
57-
import { withBreakpoints } from 'react-breakpoints'
55+
import { Media } from 'react-breakpoints'
5856

5957
class Navigation extends React.Component {
6058
render() {
59+
const { breakpoints, currentBreakpoint } = this.props
60+
console.log(breakpoints) // { small: 320, medium: 768, ... }
61+
console.log(currentBreakpoint) // 'small'
6162
return (
62-
<div>
63-
{
64-
this.props.screenWidth > this.props.breakpoints.desktop
65-
? <DesktopNavigation />
66-
: <TouchNavigation />
63+
<Media>
64+
{({ breakpoints, currentBreakpoint }) =>
65+
breakpoints[currentBreakpoint] > breakpoints.desktop ? (
66+
<DesktopNavigation />
67+
) : (
68+
<TouchNavigation />
69+
)
6770
}
68-
</div>
71+
</Media>
6972
)
7073
}
7174
}
7275

73-
74-
export default withBreakpoints(Navigation)
76+
export default Navigation
7577
```
7678

77-
It works the exact same way in stateless components, here is a more extensive example:
79+
### HOC
7880

7981
```js
8082
import { withBreakpoints } from 'react-breakpoints'
8183

82-
const MyComponent = ({ screenWidth, breakpoints }) => {
83-
let renderList
84-
if (screenWidth < breakpoints.tablet) {
85-
renderList = <MobileList />
86-
}
87-
else if (screenWidth >= breakpoints.tablet && screenWidth < breakpoints.desktop) {
88-
renderList = <TabletList />
89-
}
90-
else if (screenWidth >= breakpoints.desktop) {
91-
renderList = <DesktopList />
84+
class Navigation extends React.Component {
85+
render() {
86+
const { breakpoints, currentBreakpoint } = this.props
87+
return (
88+
<div>
89+
{breakpoints[currentBreakpoint] > breakpoints.desktop ? (
90+
<DesktopNavigation />
91+
) : (
92+
<TouchNavigation />
93+
)}
94+
</div>
95+
)
9296
}
93-
return (
94-
<div>
95-
<label>Select from the list below:</label>
96-
{renderList}
97-
</div>
98-
)
9997
}
100-
101-
export default withBreakpoints(MyComponent)
98+
99+
export default withBreakpoints(Navigation)
100+
```
101+
102+
Here is a more extensive example with renderProps:
103+
104+
```js
105+
import { Media } from 'react-breakpoints'
106+
107+
const MyComponent = props => (
108+
<div>
109+
<h3>Select from the list below:</h3>
110+
<Media>
111+
{({ breakpoints, currentBreakpoint }) => {
112+
switch (currentBreakpoint) {
113+
case breakpoints.mobile:
114+
return <MobileList />
115+
case breakpoints.tablet:
116+
return <TabletList />
117+
case breakpoints.desktop:
118+
return <DesktopList />
119+
}
120+
}}
121+
</Media>
122+
</div>
123+
)
124+
125+
export default MyComponent
102126
```
103127

104128
## Server side rendering
105-
**WARNING:** This feature is experimental.
106129

107130
```js
108131
// server.js
@@ -119,35 +142,22 @@ const breakpoints = {
119142
desktopWide: 1920,
120143
}
121144

122-
const guessedBreakpoint = breakpoints.mobile // create your own logic to generate this number
145+
const guessedBreakpoint = breakpoints.mobile // create your own logic to generate this
123146

124147
const markup = renderToString(
125-
<ReactBreakpoints
126-
guessedBreakpoint={guessedBreakpoint}
148+
<ReactBreakpoints
149+
guessedBreakpoint={guessedBreakpoint}
127150
breakpoints={breakpoints}
128151
>
129152
<App/>
130153
<ReactBreakpoints />
131154
)
132155
```
133156

134-
## With ES7 decorators
135-
136-
```js
137-
import { withBreakpoints } from 'react-breakpoints'
138-
139-
@withBreakpoints
140-
class Navigation extends React.Component {
141-
render() {
142-
return this.props.screenWidth > this.props.breakpoints.desktop
143-
? <DesktopNavigation />
144-
: <TouchNavigation />
145-
}
146-
}
147-
```
148157
## Options
149158

150159
### `debounceResize: bool` option
160+
151161
By default, this library does NOT debounce the `resize` listener. However, by passing the `debounceResize` prop to the `ReactBreakpoints` component it will be enabled with a default delay.
152162

153163
```js
@@ -163,6 +173,7 @@ ReactDOM.render(
163173
```
164174

165175
### `debounceDelay: number[ms]` option
176+
166177
Set a custom delay in milliseconds for how the length of the debounce wait.
167178

168179
```js
@@ -179,10 +190,10 @@ ReactDOM.render(
179190
```
180191

181192
### `defaultBreakpoint: number` option
182-
In case you always want to default to a certain breakpoint, say you're building a mobile-only application - you can pass the mobile breakpoint here.
183193

184-
```js
194+
In case you always want to default to a certain breakpoint.
185195

196+
```js
186197
const breakpoints = {
187198
mobile: 320,
188199
tablet: 768,
@@ -195,7 +206,7 @@ ReactDOM.render(
195206
defaultBreakpoint={breakpoints.mobile}
196207
>
197208
<App />
198-
</ReactBreakpoints>
199-
, document.getElementById('root')
209+
</ReactBreakpoints>,
210+
document.getElementById('root'),
200211
)
201-
```
212+
```

example/.gitignore

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# See https://help.github.com/ignore-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
6+
# testing
7+
/coverage
8+
9+
# production
10+
/build
11+
12+
# misc
13+
.DS_Store
14+
.env.local
15+
.env.development.local
16+
.env.test.local
17+
.env.production.local
18+
19+
npm-debug.log*
20+
yarn-debug.log*
21+
yarn-error.log*

0 commit comments

Comments
 (0)