lazy-react 2.0.5 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,34 +1,17 @@
1
- import React from 'react' // eslint-disable-line no-unused-vars
2
- import CheckIfRender from './baseClass'
1
+ import React, { useRef } from 'react' // eslint-disable-line no-unused-vars
2
+ import useIsInViewPort from './baseClass'
3
3
 
4
- export default class LazyImage extends CheckIfRender {
5
- constructor(props) {
6
- super(props)
7
- }
8
- render() {
9
- return (
10
- <img
11
- src={this.state.link}
12
- alt={this.props.alt}
13
- style={this.style}
14
- className={this.props.className}
15
- ref={node => (this.domNode = node)}
16
- />
17
- )
18
- }
19
- componentWillMount() {
20
- if (this.props.className === '' && this.props.preserveAspect === false) {
21
- this.style = {
22
- minHeight: '300px',
23
- minWidth: '300px',
24
- ...this.props.style
25
- }
26
- }
27
- }
28
- }
4
+ export default function LazyImage(props) {
5
+ const ref = useRef()
6
+ const isViewable = useIsInViewPort(ref, props)
29
7
 
30
- LazyImage.defaultProps = {
31
- style: {},
32
- className: '',
33
- preserveAspect: true
8
+ return (
9
+ <img
10
+ src={isViewable.link}
11
+ alt={props.alt}
12
+ style={props.style}
13
+ className={props.className}
14
+ ref={ref}
15
+ />
16
+ )
34
17
  }
@@ -1,21 +1,12 @@
1
1
  var path = require('path')
2
- var webpack = require('webpack')
3
2
 
4
3
  module.exports = {
4
+ mode:'development',
5
5
  entry: './demo/demo.js',
6
6
  output: {
7
7
  path: path.join(__dirname),
8
8
  filename: './demo/index.js'
9
9
  },
10
- plugins: [
11
- new webpack.DefinePlugin({
12
- 'process.env': {
13
- NODE_ENV: '"production"'
14
- },
15
- __DEVELOPMENT__: false
16
- }),
17
- new webpack.optimize.UglifyJsPlugin({ comments: false })
18
- ],
19
10
  module: {
20
11
  rules: [
21
12
  {
@@ -25,4 +16,4 @@ module.exports = {
25
16
  }
26
17
  ]
27
18
  }
28
- }
19
+ }
package/webpack.config.js CHANGED
@@ -2,6 +2,7 @@ var path = require('path')
2
2
  var webpack = require('webpack')
3
3
 
4
4
  module.exports = {
5
+ mode:'production',
5
6
  entry: './src/index.js',
6
7
  devtool: 'source-map',
7
8
  output: {
@@ -10,6 +11,40 @@ module.exports = {
10
11
  path: path.join(__dirname),
11
12
  filename: './dist/index.js'
12
13
  },
14
+ // optimization: {
15
+ // minimizer: [
16
+ // new UglifyJsPlugin({
17
+ // sourceMap: true,
18
+ // uglifyOptions: {
19
+ // compress: {
20
+ // inline: false
21
+ // },
22
+ // output: {
23
+ // comments: false
24
+ // }
25
+ // }
26
+ // })
27
+ // ],
28
+ // runtimeChunk: false,
29
+ // splitChunks: {
30
+ // cacheGroups: {
31
+ // default: false,
32
+ // commons: {
33
+ // test: /[\\/]node_modules[\\/]/,
34
+ // name: 'vendor_app',
35
+ // chunks: 'all',
36
+ // minChunks: 2
37
+ // }
38
+ // }
39
+ // }
40
+ // },
41
+ plugins: [
42
+ new webpack.DefinePlugin({
43
+ 'process.env': {
44
+ NODE_ENV: '"production"'
45
+ },
46
+ __DEVELOPMENT__: false
47
+ })],
13
48
  externals: [
14
49
  {
15
50
  react: {
@@ -28,15 +63,6 @@ module.exports = {
28
63
  }
29
64
  }
30
65
  ],
31
- plugins: [
32
- new webpack.DefinePlugin({
33
- 'process.env': {
34
- NODE_ENV: '"production"'
35
- },
36
- __DEVELOPMENT__: false
37
- }),
38
- new webpack.optimize.UglifyJsPlugin({ comments: false })
39
- ],
40
66
  module: {
41
67
  rules: [
42
68
  {
@@ -1,75 +0,0 @@
1
- //this file it's here just for reference on how build size differs with webpack and babel when there is one or more files involved
2
-
3
- import React from 'react' // eslint-disable-line no-unused-vars
4
- import ReactDom from 'react-dom'
5
-
6
- class CheckIfRender extends React.Component {
7
- constructor(props) {
8
- super(props)
9
- this.state = {
10
- link: ''
11
- }
12
- }
13
- listener() {
14
- if (window.scrollY + window.innerWidth + 100 > this.state.top) {
15
- this.setState({ link: this.props.link })
16
- this.removeListener()
17
- }
18
- }
19
- removeListener() {
20
- window.removeEventListener('scroll', this.listener)
21
- }
22
- componentDidMount() {
23
- const element = ReactDom.findDOMNode(this)
24
- const { top } = element.getBoundingClientRect()
25
- this.setState({ top })
26
- window.addEventListener('scroll', this.listener)
27
- }
28
- componentWillUnmount() {
29
- this.removeListener()
30
- }
31
- }
32
-
33
- class LazyFrame extends CheckIfRender {
34
- constructor(props) {
35
- super(props)
36
- }
37
- render() {
38
- return (
39
- <iframe
40
- height={this.props.height || '500'}
41
- scrolling={this.props.scrolling || 'no'}
42
- src={this.state.link}
43
- frameBorder={this.props.frameBorder || 'no'}
44
- allowTransparency={this.props.allowTransparency || 'true'}
45
- allowFullScreen={this.props.allowFullScreen || 'true'}
46
- style={this.props.style || { width: '100%' }}
47
- />
48
- )
49
- }
50
- }
51
-
52
- class LazyBackgroundImage extends CheckIfRender {
53
- constructor(props) {
54
- super(props)
55
- }
56
- render() {
57
- return (
58
- <div
59
- className={this.props.className}
60
- style={{ backgroundImage: `url(${this.state.link})` }}
61
- />
62
- )
63
- }
64
- }
65
-
66
- class LazyImage extends CheckIfRender {
67
- constructor(props) {
68
- super(props)
69
- }
70
- render() {
71
- return <img src={this.state.link} alt={this.props.alt} />
72
- }
73
- }
74
-
75
- export { LazyBackgroundImage, LazyImage, LazyFrame }