Skip to content

Commit

Permalink
Merge pull request #155 from loktar00/feature/update-to-18
Browse files Browse the repository at this point in the history
Feature/update to 18
  • Loading branch information
loktar00 authored Aug 29, 2022
2 parents 392ef54 + be2e20c commit bcae876
Show file tree
Hide file tree
Showing 43 changed files with 9,444 additions and 13,091 deletions.
3 changes: 0 additions & 3 deletions .babelrc

This file was deleted.

47 changes: 0 additions & 47 deletions .eslintrc

This file was deleted.

49 changes: 49 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module.exports = {
env: {
browser: true,
es2021: true
},
settings: {
'import/resolver': {
typescript: {}
}
},
extends: [
'plugin:react/recommended',
'airbnb'
],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaFeatures: {
jsx: true
},
ecmaVersion: 'latest',
sourceType: 'module'
},
plugins: [
'react',
'@typescript-eslint'
],
rules: {
'linebreak-style': 'off',
'comma-dangle': ['error', 'never'],
'import/extensions': [
'error', {
js: 'never',
jsx: 'never',
ts: 'never',
tsx: 'never'
}
],
'max-len': ['error', { code: 500 }],
'arrow-parens': ['error', 'as-needed'],
'react/function-component-definition': [1, { namedComponents: 'arrow-function' }],
'react/jsx-filename-extension': [1, { extensions: ['.tsx', '.jsx'] }],
'react/jsx-indent-props': [2, 4],
indent: ['error', 4],
'react/jsx-indent': [2, 4],
'react/static-property-placement': [2, 'static public field'],
'react/no-find-dom-node': 0,
'jsx-a11y/media-has-caption': 0
}
};
136 changes: 73 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,103 +1,96 @@
React Lazy Load Component
=========================

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.
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.

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

## 4.0 Update
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).

### Breaking changes
* No more debunce, or throttle options as they aren't needed
* Removed individual offset props, offset can be used just like css margin, eg. `offset={'0px 10px 200px 0px'}` or `offset={100}`

### Improvements
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.
Also took this oppourtunity to convert over to TS.

## Installation
React Lazy Load requires **React 0.14 or later.**
React Lazy Load requires **React 17 or later.**

```
npm install --save react-lazy-load
npm i -S react-lazy-load
```

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

## Usage
## Examples

### Basic Usage
```jsx
import React from 'react';
import LazyLoad from 'react-lazy-load';

const MyComponent = () => (
<div>
Scroll to load images.
<div className="filler" />
<LazyLoad height={762} offsetVertical={300}>
<LazyLoad height={762}>
<img src='http://apod.nasa.gov/apod/image/1502/HDR_MVMQ20Feb2015ouellet1024.jpg' />
</LazyLoad>
<div className="filler" />
<LazyLoad height={683} offsetTop={200}>
<img src='http://apod.nasa.gov/apod/image/1502/2015_02_20_conj_bourque1024.jpg' />
</LazyLoad>
<div className="filler" />
<LazyLoad height={480} offsetHorizontal={50}>
<img src='http://apod.nasa.gov/apod/image/1502/MarsPlume_jaeschke_480.gif' />
</LazyLoad>
<div className="filler" />
<LazyLoad
height={720}
onContentVisible={() => console.log('look ma I have been lazyloaded!')}
>
<img src='http://apod.nasa.gov/apod/image/1502/ToadSky_Lane_1080_annotated.jpg' />
</LazyLoad>
<div className="filler" />
</div>
);
)
```

## Props

#### offset
Type: `Number|String` Default: `0`

Aliases: `threshold`

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`.

#### offsetVertical
Type: `Number|String` Default: `offset`'s value

The `offsetVertical` option allows you to specify how far above and below the viewport you want to _begin_ displaying your content.

#### offsetHorizontal
Type: `Number|String` Default: `offset`'s value

The `offsetHorizontal` option allows you to specify how far to the left and right of the viewport you want to _begin_ displaying your contet.
### Loading the image 300px prior to scroll

#### offsetTop
Type: `Number|String` Default: `offsetVertical`'s value

The `offsetTop` option allows you to specify how far above the viewport you want to _begin_ displaying your content.
```jsx
const MyComponent = () => (
<div>
<LazyLoad height={762} offset={300}>
<img src='http://apod.nasa.gov/apod/image/1502/HDR_MVMQ20Feb2015ouellet1024.jpg' />
</LazyLoad>
</div>
)
```

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

The `offsetBottom` option allows you to specify how far below the viewport you want to _begin_ displaying your content.
```jsx
const MyComponent = () => (
<div>
<LazyLoad height={762} width={400} threshold={0.95}>
<img src='http://apod.nasa.gov/apod/image/1502/HDR_MVMQ20Feb2015ouellet1024.jpg' />
</LazyLoad>
</div>
)
```

#### offsetLeft
Type: `Number|String` Default: `offsetVertical`'s value
### Performing a side effect once your image is loaded

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

#### offsetRight
Type: `Number|String` Default: `offsetVertical`'s value

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

#### throttle
Type: `Number|String` Default: `250`
#### offset
Type: `Number|String` Default: `0`

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.
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`.

#### debounce
Type: `Boolean` Default: `true`
#### threshold
Type: `Number` Default: `0`

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`.
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.

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

A callback function to execute when the content appears on the screen.


## Building LazyLoad

```
npm run build
```

## Running examples

```
cd examples/basic
npm run dev
```



74 changes: 0 additions & 74 deletions __tests__/inViewport-test.js

This file was deleted.

24 changes: 24 additions & 0 deletions examples/basic/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
Loading

0 comments on commit bcae876

Please sign in to comment.