Skip to content

Commit bcae876

Browse files
authored
Merge pull request #155 from loktar00/feature/update-to-18
Feature/update to 18
2 parents 392ef54 + be2e20c commit bcae876

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+9444
-13091
lines changed

.babelrc

Lines changed: 0 additions & 3 deletions
This file was deleted.

.eslintrc

Lines changed: 0 additions & 47 deletions
This file was deleted.

.eslintrc.cjs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module.exports = {
2+
env: {
3+
browser: true,
4+
es2021: true
5+
},
6+
settings: {
7+
'import/resolver': {
8+
typescript: {}
9+
}
10+
},
11+
extends: [
12+
'plugin:react/recommended',
13+
'airbnb'
14+
],
15+
parser: '@typescript-eslint/parser',
16+
parserOptions: {
17+
ecmaFeatures: {
18+
jsx: true
19+
},
20+
ecmaVersion: 'latest',
21+
sourceType: 'module'
22+
},
23+
plugins: [
24+
'react',
25+
'@typescript-eslint'
26+
],
27+
rules: {
28+
'linebreak-style': 'off',
29+
'comma-dangle': ['error', 'never'],
30+
'import/extensions': [
31+
'error', {
32+
js: 'never',
33+
jsx: 'never',
34+
ts: 'never',
35+
tsx: 'never'
36+
}
37+
],
38+
'max-len': ['error', { code: 500 }],
39+
'arrow-parens': ['error', 'as-needed'],
40+
'react/function-component-definition': [1, { namedComponents: 'arrow-function' }],
41+
'react/jsx-filename-extension': [1, { extensions: ['.tsx', '.jsx'] }],
42+
'react/jsx-indent-props': [2, 4],
43+
indent: ['error', 4],
44+
'react/jsx-indent': [2, 4],
45+
'react/static-property-placement': [2, 'static public field'],
46+
'react/no-find-dom-node': 0,
47+
'jsx-a11y/media-has-caption': 0
48+
}
49+
};

README.md

Lines changed: 73 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,96 @@
11
React Lazy Load Component
22
=========================
33

4-
React Lazy Load is an easy-to-use React component which helps you defer loading content in predictable way. It's fast, works in IE8+, 6KB minified and uses debounce function by default. You can also use component inside scrolling container, such as div with scrollbar. It will be found automatically. Check out an example.
4+
React Lazy Load is an easy-to-use React component which helps you defer loading content in predictable way. It's fast, You can also use component inside scrolling container, such as div with scrollbar. It will be found automatically. Check out an example.
55

66
[![build status](https://img.shields.io/travis/loktar00/react-lazy-load.svg?style=flat-square)](https://travis-ci.org/loktar00/react-lazy-load)
7-
[![dependency status](https://david-dm.org/loktar00/react-lazy-load.svg?style=flat-square)](https://david-dm.org/loktar00/react-lazy-load)
87
[![npm downloads](https://img.shields.io/npm/dm/react-lazy-load.svg?style=flat-square)](https://www.npmjs.com/package/react-lazy-load)
98

9+
## 4.0 Update
10+
Converted to work with React 18, and updated to use the [Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API).
11+
12+
### Breaking changes
13+
* No more debunce, or throttle options as they aren't needed
14+
* Removed individual offset props, offset can be used just like css margin, eg. `offset={'0px 10px 200px 0px'}` or `offset={100}`
15+
16+
### Improvements
17+
Since we're now using the intersection observer we don't need to watch a user scroll, more importantly we don't have to manually calculate if a user can see the element or not.
18+
Also took this oppourtunity to convert over to TS.
19+
1020
## Installation
11-
React Lazy Load requires **React 0.14 or later.**
21+
React Lazy Load requires **React 17 or later.**
1222

1323
```
14-
npm install --save react-lazy-load
24+
npm i -S react-lazy-load
1525
```
1626

1727
## Examples
18-
* [Basic](https://github.com/loktar00/react-lazy-load/tree/master/examples/basic)
28+
* [In Repo](https://github.com/loktar00/react-lazy-load/tree/master/examples/basic)
1929

20-
## Usage
30+
## Examples
2131

32+
### Basic Usage
2233
```jsx
2334
import React from 'react';
2435
import LazyLoad from 'react-lazy-load';
2536

2637
const MyComponent = () => (
2738
<div>
28-
Scroll to load images.
29-
<div className="filler" />
30-
<LazyLoad height={762} offsetVertical={300}>
39+
<LazyLoad height={762}>
3140
<img src='http://apod.nasa.gov/apod/image/1502/HDR_MVMQ20Feb2015ouellet1024.jpg' />
3241
</LazyLoad>
33-
<div className="filler" />
34-
<LazyLoad height={683} offsetTop={200}>
35-
<img src='http://apod.nasa.gov/apod/image/1502/2015_02_20_conj_bourque1024.jpg' />
36-
</LazyLoad>
37-
<div className="filler" />
38-
<LazyLoad height={480} offsetHorizontal={50}>
39-
<img src='http://apod.nasa.gov/apod/image/1502/MarsPlume_jaeschke_480.gif' />
40-
</LazyLoad>
41-
<div className="filler" />
42-
<LazyLoad
43-
height={720}
44-
onContentVisible={() => console.log('look ma I have been lazyloaded!')}
45-
>
46-
<img src='http://apod.nasa.gov/apod/image/1502/ToadSky_Lane_1080_annotated.jpg' />
47-
</LazyLoad>
48-
<div className="filler" />
4942
</div>
50-
);
43+
)
5144
```
5245

53-
## Props
54-
55-
#### offset
56-
Type: `Number|String` Default: `0`
57-
58-
Aliases: `threshold`
59-
60-
The `offset` option allows you to specify how far below, above, to the left, and to the right of the viewport you want to _begin_ displaying your content. If you specify `0`, your content will be displayed as soon as it is visible in the viewport, if you want to load _1000px_ below or above the viewport, use `1000`.
61-
62-
#### offsetVertical
63-
Type: `Number|String` Default: `offset`'s value
64-
65-
The `offsetVertical` option allows you to specify how far above and below the viewport you want to _begin_ displaying your content.
66-
67-
#### offsetHorizontal
68-
Type: `Number|String` Default: `offset`'s value
69-
70-
The `offsetHorizontal` option allows you to specify how far to the left and right of the viewport you want to _begin_ displaying your contet.
46+
### Loading the image 300px prior to scroll
7147

72-
#### offsetTop
73-
Type: `Number|String` Default: `offsetVertical`'s value
74-
75-
The `offsetTop` option allows you to specify how far above the viewport you want to _begin_ displaying your content.
48+
```jsx
49+
const MyComponent = () => (
50+
<div>
51+
<LazyLoad height={762} offset={300}>
52+
<img src='http://apod.nasa.gov/apod/image/1502/HDR_MVMQ20Feb2015ouellet1024.jpg' />
53+
</LazyLoad>
54+
</div>
55+
)
56+
```
7657

77-
#### offsetBottom
78-
Type: `Number|String` Default: `offsetVertical`'s value
58+
### Loading image only when 95% of it is in the viewport. **note** a width must be specified.
7959

80-
The `offsetBottom` option allows you to specify how far below the viewport you want to _begin_ displaying your content.
60+
```jsx
61+
const MyComponent = () => (
62+
<div>
63+
<LazyLoad height={762} width={400} threshold={0.95}>
64+
<img src='http://apod.nasa.gov/apod/image/1502/HDR_MVMQ20Feb2015ouellet1024.jpg' />
65+
</LazyLoad>
66+
</div>
67+
)
68+
```
8169

82-
#### offsetLeft
83-
Type: `Number|String` Default: `offsetVertical`'s value
70+
### Performing a side effect once your image is loaded
8471

85-
The `offsetLeft` option allows you to specify how far to left of the viewport you want to _begin_ displaying your content.
72+
```jsx
73+
const MyComponent = () => (
74+
<div>
75+
<LazyLoad height={762} width={400} threshold={0.95} onContentVisible={() => {console.log('loaded!')}}>
76+
<img src='http://apod.nasa.gov/apod/image/1502/HDR_MVMQ20Feb2015ouellet1024.jpg' />
77+
</LazyLoad>
78+
</div>
79+
)
80+
```
8681

87-
#### offsetRight
88-
Type: `Number|String` Default: `offsetVertical`'s value
8982

90-
The `offsetRight` option allows you to specify how far to the right of the viewport you want to _begin_ displaying your content.
83+
## Props
9184

92-
#### throttle
93-
Type: `Number|String` Default: `250`
85+
#### offset
86+
Type: `Number|String` Default: `0`
9487

95-
The throttle is managed by an internal function that prevents performance issues from continuous firing of `scroll` events. Using a throttle will set a small timeout when the user scrolls and will keep throttling until the user stops. The default is `250` milliseconds.
88+
The `offset` option allows you to specify how far below, above, to the left, and to the right of the viewport you want to _begin_ displaying your content. If you specify `0`, your content will be displayed as soon as it is visible in the viewport, if you want to load _1000px_ below or above the viewport, use `1000`.
9689

97-
#### debounce
98-
Type: `Boolean` Default: `true`
90+
#### threshold
91+
Type: `Number` Default: `0`
9992

100-
By default the throttling function is actually a [debounce](https://lodash.com/docs#debounce) function so that the checking function is only triggered after a user stops scrolling. To use traditional throttling where it will only check the loadable content every `throttle` milliseconds, set `debounce` to `false`.
93+
This `threshold` option allows you to specify how much of the element must be shown on the screen prior to loading. This requires a _width_ and _height_ to be set on the `<LazyLoad>` component in order for the browser to calcualte the viewable area.
10194

10295
#### height
10396
Type: `String|Number`
@@ -113,3 +106,20 @@ The `width` option allows you to set the element's width even when it has no con
113106
Type `Function`
114107

115108
A callback function to execute when the content appears on the screen.
109+
110+
111+
## Building LazyLoad
112+
113+
```
114+
npm run build
115+
```
116+
117+
## Running examples
118+
119+
```
120+
cd examples/basic
121+
npm run dev
122+
```
123+
124+
125+

__tests__/inViewport-test.js

Lines changed: 0 additions & 74 deletions
This file was deleted.

examples/basic/.gitignore

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?

0 commit comments

Comments
 (0)