Skip to content

Commit 7d38244

Browse files
committed
Updates react support to v18
Updates various dependencies to support React 18. Also updates to latest version of react-intersection-observer. This resolves fpapado#45 & fpapado#46
1 parent 2ec1f3e commit 7d38244

7 files changed

+6246
-4169
lines changed

.eslintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"parser": "typescript-eslint-parser",
2+
"parser": "@typescript-eslint/parser",
33
"plugins": [
44
"jsx-a11y"
55
],

package-lock.json

+6,209-4,073
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+14-13
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
"build": "npm-run-all clean bundle:prod copy-defs size",
1515
"bundle:prod": "microbundle build",
1616
"bundle:watch": "microbundle watch",
17-
"copy-defs": "tsc && cp -r ts-build/*.d.ts dist/",
18-
"storybook": "start-storybook -p 8080 -s ./stories/demo",
19-
"storybook:build": "npm run bundle:prod && build-storybook -c .storybook -s ./stories/demo -o .out",
17+
"copy-defs": "tsc --skipLibCheck && cp -r ts-build/*.d.ts dist/",
18+
"storybook": "start-storybook -p 8080",
19+
"storybook:build": "npm run bundle:prod && build-storybook -c .storybook -o .out",
2020
"storybook:deploy": "storybook-to-ghpages --existing-output-dir=.out",
2121
"test": "npm run build",
2222
"size": "bundlesize",
@@ -65,12 +65,12 @@
6565
},
6666
"homepage": "https://github.com/fpapado/react-lazy-images#readme",
6767
"dependencies": {
68-
"react-intersection-observer": "^6.1.0",
68+
"react-intersection-observer": "^9.4.0",
6969
"unionize": "^2.1.2"
7070
},
7171
"peerDependencies": {
72-
"react": "^15 || ^16",
73-
"react-dom": "^15 || ^16"
72+
"react": "^15 || ^16 || ^17 || ^18",
73+
"react-dom": "^15 || ^16 || ^17 || ^18"
7474
},
7575
"devDependencies": {
7676
"@storybook/addon-actions": "4.0.0-alpha.14",
@@ -79,28 +79,29 @@
7979
"@storybook/addons": "4.0.0-alpha.14",
8080
"@storybook/react": "4.0.0-alpha.14",
8181
"@storybook/storybook-deployer": "^2.3.0",
82-
"@types/react": "^16.4.6",
83-
"@types/react-dom": "^16.0.6",
82+
"@types/react": "^18.0.18",
83+
"@types/react-dom": "^18.0.6",
8484
"@types/storybook__addon-actions": "^3.0.3",
8585
"@types/storybook__addon-info": "^3.2.3",
8686
"@types/storybook__react": "^3.0.8",
87+
"@typescript-eslint/parser": "^5.36.2",
8788
"awesome-typescript-loader": "^5.2",
8889
"babel-core": "^6.26.3",
89-
"bundlesize": "^0.17.0",
90+
"bundlesize": "^0.18.1",
9091
"eslint": "^5.1.0",
9192
"eslint-plugin-jsx-a11y": "^6.1.1",
9293
"husky": "^1.0.0-rc.13",
9394
"lint-staged": "^7.2.0",
9495
"microbundle": "0.5.1",
9596
"npm-run-all": "^4.1.3",
9697
"prettier": "^1.13.7",
97-
"react": "^16.4.1",
98+
"react": "^18.2.0",
9899
"react-docgen-typescript": "^1.6.2",
99100
"react-docgen-typescript-loader": "^2.1.1",
100101
"react-docgen-typescript-webpack-plugin": "^1.1.0",
101-
"react-dom": "^16.4.1",
102+
"react-dom": "^18.2.0",
103+
"require-from-string": "^2.0.2",
102104
"tachyons": "^4.10.0",
103-
"typescript": "^3.0.1",
104-
"typescript-eslint-parser": "^17.0.1"
105+
"typescript": "^4.8.2"
105106
}
106107
}

src/LazyImage.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface LazyImageRenderPropArgs {
1515

1616
export interface RefArg {
1717
/** When not loading eagerly, a ref to bind to the DOM element. This is needed for the intersection calculation to work. */
18-
ref?: React.RefObject<any>;
18+
ref?: React.RefObject<any> | ((node?: Element | null) => void);
1919
}
2020

2121
export interface LazyImageProps extends CommonLazyImageProps {
@@ -45,7 +45,7 @@ export interface LazyImageProps extends CommonLazyImageProps {
4545
* and then swaps it in. Has predefined rendering logic, but the
4646
* specifics are up to the caller.
4747
*/
48-
export const LazyImage: React.StatelessComponent<LazyImageProps> = ({
48+
export const LazyImage: React.FC<LazyImageProps> = ({
4949
actual,
5050
placeholder,
5151
loading,

src/LazyImageFull.tsx

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import Observer from "react-intersection-observer";
2+
import { InView } from "react-intersection-observer";
33
import { unionize, ofType, UnionOf } from "unionize";
44

55
/**
@@ -48,7 +48,7 @@ export interface RenderCallbackArgs {
4848
imageState: ImageState;
4949
imageProps: ImageProps;
5050
/** When not loading eagerly, a ref to bind to the DOM element. This is needed for the intersection calculation to work. */
51-
ref?: React.RefObject<any>;
51+
ref?: React.RefObject<any> | ((node?: Element | null) => void);
5252
}
5353

5454
export interface ImageProps {
@@ -194,7 +194,7 @@ export class LazyImageFull extends React.Component<
194194
static displayName = "LazyImageFull";
195195

196196
/** A central place to store promises.
197-
* A bit silly, but passing promsises directly in the state
197+
* A bit silly, but passing promises directly in the state
198198
* was giving me weird timing issues. This way we can keep
199199
* the promises in check, and pick them up from the respective methods.
200200
* FUTURE: Could pass the relevant key in Buffering and Loading, so
@@ -300,7 +300,7 @@ export class LazyImageFull extends React.Component<
300300
// Clear the Promise Cache
301301
if (this.promiseCache.loading) {
302302
// NOTE: This does not cancel the request, only the callback.
303-
// We weould need fetch() and an AbortHandler for that.
303+
// We we would need fetch() and an AbortHandler for that.
304304
this.promiseCache.loading.cancel();
305305
}
306306
if (this.promiseCache.buffering) {
@@ -331,14 +331,16 @@ export class LazyImageFull extends React.Component<
331331
});
332332
} else {
333333
return (
334-
<Observer
334+
<InView
335335
rootMargin="50px 0px"
336336
// TODO: reconsider threshold
337337
threshold={0.01}
338338
{...observerProps}
339-
onChange={inView => this.update(Action.ViewChanged({ inView }))}
339+
onChange={(inView: boolean) =>
340+
this.update(Action.ViewChanged({ inView }))
341+
}
340342
>
341-
{({ ref }) =>
343+
{({ ref }: RenderProps) =>
342344
children({
343345
// We know that the state tags and the enum match up, apart
344346
// from Buffering not being exposed
@@ -350,12 +352,18 @@ export class LazyImageFull extends React.Component<
350352
ref
351353
})
352354
}
353-
</Observer>
355+
</InView>
354356
);
355357
}
356358
}
357359
}
358360

361+
interface RenderProps {
362+
inView: boolean;
363+
entry: IntersectionObserverEntry | undefined;
364+
ref: React.RefObject<any> | ((node?: Element | null) => void);
365+
}
366+
359367
///// Utilities /////
360368

361369
/** Promise constructor for loading an image */
@@ -384,7 +392,7 @@ const loadImage = (
384392
// TODO: consider writing the .decode() definition and sending a PR
385393
//@ts-ignore
386394
.decode()
387-
.then((image: HTMLImageElement) => resolve(image))
395+
.then(() => resolve(image))
388396
.catch((err: any) => reject(err))
389397
);
390398
}
@@ -398,7 +406,7 @@ const delayedPromise = (ms: number) =>
398406
new Promise(resolve => setTimeout(resolve, ms));
399407

400408
interface CancelablePromise {
401-
promise: Promise<{}>;
409+
promise: Promise<any>;
402410
cancel: () => void;
403411
}
404412

@@ -412,7 +420,7 @@ interface CancelablePromise {
412420
const makeCancelable = (promise: Promise<any>): CancelablePromise => {
413421
let hasCanceled_ = false;
414422

415-
const wrappedPromise = new Promise((resolve, reject) => {
423+
const wrappedPromise: Promise<any> = new Promise((resolve, reject) => {
416424
promise.then(
417425
(val: any) => (hasCanceled_ ? reject({ isCanceled: true }) : resolve(val))
418426
);

src/react-intersection-observer.d.ts

-65
This file was deleted.

tsconfig_base.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
"allowSyntheticDefaultImports": true,
1414
"esModuleInterop": true,
1515
"lib": ["es6", "dom"],
16-
"baseUrl": "./",
17-
"paths": {
18-
"react-intersection-observer": ["src/react-intersection-observer.d.ts"]
19-
}
16+
"baseUrl": "./"
2017
},
2118
"include": ["src/**/*"],
2219
"exclude": ["node_modules", "dist"]

0 commit comments

Comments
 (0)