Skip to content

Commit 665bfb4

Browse files
danielr18renatorib
authored andcommitted
Upgrade dev tools & use new context API (#33)
* Upgrade dev tools and prepare support for React new Context * Use React new Context * Remove componentWillReceiveProps for getDerivedStateFromProps * fix getDerivedStateFromProps
1 parent 0df6318 commit 665bfb4

15 files changed

+6532
-3123
lines changed

.babelrc

-14
This file was deleted.

.eslintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"import/no-unresolved": ["error", { "ignore": ["^react$"] }],
2626
"comma-dangle": ["error", "always-multiline"],
2727
"semi": ["error", "never"],
28-
"no-trailing-spaces": ["error", { "skipBlankLines": true }]
28+
"no-trailing-spaces": ["error", { "skipBlankLines": true }],
29+
"no-unused-vars": ["error", { "ignoreRestSiblings": true }]
2930
},
3031
"plugins": [
3132
"import",

.storybook/.babelrc

-3
This file was deleted.

.storybook/components/Code.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React from 'react'
22
import SyntaxHighlighter from 'react-syntax-highlighter'
3-
import { github as theme } from 'react-syntax-highlighter/dist/styles'
3+
import { dark } from 'react-syntax-highlighter/dist/styles/prism';
44
import Block from './Block'
55

66
const Code = ({ children }) => (
77
<Block title="Code">
8-
<SyntaxHighlighter language="jsx" style={theme} customStyle={{ margin: 0 }}>
8+
<SyntaxHighlighter language="jsx" style={dark} customStyle={{ margin: 0 }}>
99
{children}
1010
</SyntaxHighlighter>
1111
</Block>

.storybook/components/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
export Block from './Block'
2-
export Code from './Code'
3-
export Result from './Result'
1+
export { default as Block } from './Block'
2+
export { default as Code } from './Code'
3+
export { default as Result } from './Result'

.storybook/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { configure } from '@kadira/storybook';
1+
import { configure } from '@storybook/react';
22

33
function loadStories() {
44
require('./stories');

.storybook/stories/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react'
2-
import { storiesOf, action, linkTo } from '@kadira/storybook'
2+
import { storiesOf } from '@storybook/react'
33
import { Code, Result } from '../components'
44
import MobileBreakpoint from '../components/MobileBreakpoint'
55
import withSizes from '../../src/withSizes'

babel.config.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = api => {
2+
api.cache(true)
3+
return {
4+
plugins: ['@babel/plugin-proposal-class-properties'],
5+
env: {
6+
es: {
7+
presets: ['@babel/preset-env', '@babel/preset-react'],
8+
},
9+
commonjs: {
10+
presets: ['@babel/preset-env', '@babel/preset-react'],
11+
},
12+
rollup: {
13+
presets: [
14+
['@babel/preset-env', { modules: false }],
15+
'@babel/preset-react',
16+
],
17+
},
18+
},
19+
}
20+
}

package.json

+25-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-sizes",
3-
"version": "1.0.4",
3+
"version": "2.0.0-alpha.1",
44
"description": "Hoc to easily map window sizes to props.",
55
"main": "lib/index.js",
66
"module": "es/index.js",
@@ -52,33 +52,35 @@
5252
"prop-types": "^15.6.0"
5353
},
5454
"devDependencies": {
55-
"@kadira/storybook": "^2.21.0",
56-
"babel-cli": "^6.24.1",
57-
"babel-eslint": "^8.0.0",
58-
"babel-plugin-external-helpers": "^6.22.0",
59-
"babel-plugin-transform-export-extensions": "^6.22.0",
60-
"babel-preset-es2015": "^6.24.1",
61-
"babel-preset-react": "^6.24.1",
62-
"babel-preset-stage-1": "^6.24.1",
55+
"@babel/cli": "^7.2.3",
56+
"@babel/core": "^7.2.2",
57+
"@babel/plugin-proposal-class-properties": "^7.3.0",
58+
"@babel/preset-env": "^7.3.1",
59+
"@babel/preset-react": "^7.0.0",
60+
"@storybook/react": "^4.1.11",
61+
"babel-eslint": "^10.0.1",
62+
"babel-loader": "^8.0.5",
6363
"chokidar-cli": "^1.2.0",
64-
"cross-env": "^5.0.5",
65-
"del-cli": "^0.2.1",
66-
"eslint": "^4.6.1",
64+
"cross-env": "^5.2.0",
65+
"del-cli": "^1.1.0",
66+
"eslint": "^4.0.0",
6767
"eslint-plugin-import": "^2.7.0",
6868
"eslint-plugin-react": "^7.3.0",
69-
"jest": "^21.2.1",
70-
"np": "^2.13.1",
71-
"react": "^15.6.1",
72-
"react-dom": "^15.5.3",
73-
"react-syntax-highlighter": "^5.3.0",
74-
"rollup": "^0.52.0",
75-
"rollup-plugin-babel": "^3.0.2",
76-
"rollup-plugin-commonjs": "^8.2.6",
77-
"rollup-plugin-node-resolve": "^3.0.0",
69+
"jest": "^24.0.0",
70+
"np": "^4.0.2",
71+
"react": "^16.7.0",
72+
"react-dom": "^16.7.0",
73+
"react-syntax-highlighter": "^10.1.2",
74+
"rollup": "^1.1.2",
75+
"rollup-plugin-babel": "^4.3.2",
76+
"rollup-plugin-commonjs": "^9.2.0",
77+
"rollup-plugin-node-resolve": "^4.0.0",
7878
"rollup-plugin-replace": "^2.0.0",
79-
"rollup-plugin-uglify": "^2.0.1"
79+
"rollup-plugin-terser": "^4.0.3",
80+
"rollup-plugin-uglify": "^6.0.2",
81+
"webpack": "^4.29.0"
8082
},
8183
"peerDependencies": {
82-
"react": "^0.14.0 || ^15.0.0-0 || ^16.0.0-0"
84+
"react": "^16.3.0"
8385
}
8486
}

rollup.config.js

+7-15
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ import nodeResolve from 'rollup-plugin-node-resolve'
22
import babel from 'rollup-plugin-babel'
33
import replace from 'rollup-plugin-replace'
44
import commonjs from 'rollup-plugin-commonjs'
5-
import uglify from 'rollup-plugin-uglify'
5+
import { terser } from 'rollup-plugin-terser'
66

77
const env = process.env.NODE_ENV
88

99
const config = {
1010
input: 'src/index.js',
1111
output: {
1212
format: 'umd',
13+
name: 'ReactSizes',
14+
exports: 'named',
15+
globals: {
16+
react: 'React',
17+
},
1318
},
1419
external: ['react'],
15-
globals: {
16-
react: 'React',
17-
},
18-
name: 'ReactSizes',
1920
plugins: [
2021
nodeResolve(),
2122
babel({
@@ -29,16 +30,7 @@ const config = {
2930
}
3031

3132
if (env === 'production') {
32-
config.plugins.push(
33-
uglify({
34-
compress: {
35-
pure_getters: true,
36-
unsafe: true,
37-
unsafe_comps: true,
38-
warnings: false,
39-
},
40-
})
41-
)
33+
config.plugins.push(terser())
4234
}
4335

4436
export default config

src/SizesContext.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from 'react'
2+
3+
const SizesContext = React.createContext({
4+
fallbackWidth: null,
5+
fallbackHeight: null,
6+
forceFallback: false,
7+
throttle: 200,
8+
})
9+
10+
SizesContext.displayName = 'SizesContext'
11+
12+
export default SizesContext

src/SizesProvider.js

+5-21
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
1-
import { Component } from 'react'
2-
import contextTypes, { contextKey } from './contextTypes'
1+
import React from 'react'
2+
import SizesContext from './SizesContext'
33

4-
class SizesProvider extends Component {
5-
static childContextTypes = contextTypes
6-
7-
getChildContext() {
8-
const { config = {} } = this.props
9-
10-
return {
11-
[contextKey]: {
12-
fallbackWidth: config.fallbackWidth || null,
13-
fallbackHeight: config.fallbackHeight || null,
14-
throttle: config.throttle || 200,
15-
},
16-
}
17-
}
18-
19-
render() {
20-
return this.props.children
21-
}
22-
}
4+
const SizesProvider = ({ config, children }) => (
5+
<SizesContext.Provider value={config}>{children}</SizesContext.Provider>
6+
)
237

248
export default SizesProvider

src/contextTypes.js

-11
This file was deleted.

src/withSizes.js

+36-22
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,39 @@
11
/* eslint-disable no-console */
22

3-
import React, { Component } from 'react'
3+
import React, { PureComponent } from 'react'
44
import throttle from 'lodash.throttle'
55

66
import getDisplayName from './utils/getDisplayName'
77
import shallowDiff from './utils/shallowDiff'
88
import getWindowSizes from './utils/getWindowSizes'
9-
10-
import contextTypes, { contextKey } from './contextTypes'
9+
import SizesContext from './SizesContext'
1110
import * as presets from './presets'
1211

12+
const getWindowSizesWithFallback = props => {
13+
const { fallbackHeight, fallbackWidth } = props
14+
return getWindowSizes({ fallbackHeight, fallbackWidth })
15+
}
16+
1317
const withSizes = (...mappedSizesToProps) => WrappedComponent => {
1418
const parseMappedSizesToProps = (dimensions, props) =>
1519
mappedSizesToProps
1620
.map(check => check(dimensions, props))
1721
.reduce((acc, props) => ({ ...acc, ...props }), {})
1822

19-
return class ComponentWithSizes extends Component {
23+
class ComponentWithSizes extends PureComponent {
2024
static displayName = `withSizes(${getDisplayName(WrappedComponent)})`
21-
static contextTypes = contextTypes
2225

23-
constructor(props, context) {
24-
super(props, context)
25-
26-
this.getWindowSizesWithFallback = () => {
27-
const config = this.context[contextKey] || {}
28-
const { fallbackHeight = null, fallbackWidth = null } = config
29-
return getWindowSizes({ fallbackHeight, fallbackWidth })
30-
}
26+
constructor(props) {
27+
super(props)
3128

3229
this.getPropsToPass = () => {
3330
return parseMappedSizesToProps(
34-
this.getWindowSizesWithFallback(),
31+
getWindowSizesWithFallback(this.props),
3532
this.props
3633
)
3734
}
3835

3936
this.state = {
40-
initialSizes: this.getWindowSizesWithFallback(),
4137
propsToPass: this.getPropsToPass(),
4238
}
4339
}
@@ -52,15 +48,19 @@ const withSizes = (...mappedSizesToProps) => WrappedComponent => {
5248
}
5349
}
5450

55-
throttledDispatchSizes = throttle(
56-
this.dispatchSizes,
57-
(this.context[contextKey] || {}).throttle || 200
58-
)
51+
throttledDispatchSizes = throttle(this.dispatchSizes, this.props.throttle)
5952

6053
/* Lifecycles */
54+
static getDerivedStateFromProps(props, state) {
55+
const propsToPass = parseMappedSizesToProps(
56+
getWindowSizesWithFallback(props),
57+
props
58+
)
59+
if (shallowDiff(propsToPass, state.propsToPass)) {
60+
return { propsToPass }
61+
}
6162

62-
componentWillReceiveProps() {
63-
this.dispatchSizes()
63+
return null
6464
}
6565

6666
componentDidMount() {
@@ -73,9 +73,23 @@ const withSizes = (...mappedSizesToProps) => WrappedComponent => {
7373
}
7474

7575
render() {
76-
return <WrappedComponent {...this.props} {...this.state.propsToPass} />
76+
const {
77+
fallbackHeight,
78+
fallbackWidth,
79+
...otherProps
80+
} = this.props
81+
82+
return <WrappedComponent {...otherProps} {...this.state.propsToPass} />
7783
}
7884
}
85+
86+
const WithSizes = props => (
87+
<SizesContext.Consumer>
88+
{config => <ComponentWithSizes {...config} {...props} />}
89+
</SizesContext.Consumer>
90+
)
91+
92+
return WithSizes
7993
}
8094

8195
export default Object.assign(withSizes, { ...presets })

0 commit comments

Comments
 (0)