Skip to content

Commit 852d976

Browse files
committed
fix: Added some fixes for React 18.
BREAKING CHANGE: The lib now takes "Element", not only "HTMLElement", to be consistent with ResizeObserver. fixes #90 fixes #91 fixes #92
1 parent 653b0b9 commit 852d976

File tree

10 files changed

+698
-855
lines changed

10 files changed

+698
-855
lines changed

karma.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ module.exports = function (karmaConfig) {
8989
);
9090

9191
let testFilePattern = "tests/*.tsx";
92+
// let testFilePattern = "tests/ssr.test.tsx";
9293
// let testFilePattern = "tests/basic.tsx";
9394
// let testFilePattern = "tests/testing-lib.tsx";
9495

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@
7070
"package.json"
7171
],
7272
"peerDependencies": {
73-
"react": ">=16.8.0",
74-
"react-dom": ">=16.8.0"
73+
"react": "16.8.0 - 18",
74+
"react-dom": "16.8.0 - 18"
7575
},
7676
"devDependencies": {
7777
"@babel/core": "^7.7.7",
@@ -88,11 +88,11 @@
8888
"@semantic-release/npm": "^7.0.6",
8989
"@semantic-release/release-notes-generator": "^9.0.1",
9090
"@size-limit/preset-small-lib": "^5.0.1",
91-
"@testing-library/react": "^12.0.0",
91+
"@testing-library/react": "^13.1.1",
9292
"@types/karma": "^6.3.1",
9393
"@types/karma-jasmine": "^4.0.1",
94-
"@types/react": "^17.0.15",
95-
"@types/react-dom": "^17.0.9",
94+
"@types/react": "^18.0.8",
95+
"@types/react-dom": "^18.0.3",
9696
"babel-loader": "^8.1.0",
9797
"delay": "^5.0.0",
9898
"husky": "^7.0.0",
@@ -107,9 +107,9 @@
107107
"lint-staged": "^11.1.1",
108108
"npm-run-all": "^4.1.5",
109109
"prettier": "^2.0.4",
110-
"react": "^17.0.2",
110+
"react": "^18.0.0",
111111
"react-app-polyfill": "^2.0.0",
112-
"react-dom": "^17.0.2",
112+
"react-dom": "^18.0.0",
113113
"rollup": "^2.6.1",
114114
"semantic-release": "^17.2.2",
115115
"size-limit": "^5.0.1",

src/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type SubscriberResponse = SubscriberCleanup | void;
1515
// refs, but then host hooks / components could not opt out of renders.
1616
// This could've been exported to its own module, but the current build doesn't
1717
// seem to work with module imports and I had no more time to spend on this...
18-
function useResolvedElement<T extends HTMLElement>(
18+
function useResolvedElement<T extends Element>(
1919
subscriber: (element: T) => SubscriberResponse,
2020
refOrElement?: T | RefObject<T> | null
2121
): RefCallback<T> {
@@ -26,12 +26,15 @@ function useResolvedElement<T extends HTMLElement>(
2626
} | null>(null);
2727
const cleanupRef = useRef<SubscriberResponse | null>();
2828

29+
// Resolving ".current" purely so that a new callSubscriber instance is created when needed.
30+
const refElement =
31+
refOrElement && "current" in refOrElement ? refOrElement.current : null;
2932
const callSubscriber = useCallback(() => {
3033
let element = null;
3134
if (callbackRefElement.current) {
3235
element = callbackRefElement.current;
3336
} else if (refOrElement) {
34-
if (refOrElement instanceof HTMLElement) {
37+
if (refOrElement instanceof Element) {
3538
element = refOrElement;
3639
} else {
3740
element = refOrElement.current;
@@ -60,7 +63,7 @@ function useResolvedElement<T extends HTMLElement>(
6063
if (element) {
6164
cleanupRef.current = subscriber(element);
6265
}
63-
}, [refOrElement, subscriber]);
66+
}, [refOrElement, refElement, subscriber]);
6467

6568
// On each render, we check whether a ref changed, or if we got a new raw
6669
// element.
@@ -88,7 +91,7 @@ type ObservedSize = {
8891

8992
type ResizeHandler = (size: ObservedSize) => void;
9093

91-
type HookResponse<T extends HTMLElement> = {
94+
type HookResponse<T extends Element> = {
9295
ref: RefCallback<T>;
9396
} & ObservedSize;
9497

@@ -159,7 +162,7 @@ const extractSize = (
159162

160163
type RoundingFunction = (n: number) => number;
161164

162-
function useResizeObserver<T extends HTMLElement>(
165+
function useResizeObserver<T extends Element>(
163166
opts: {
164167
ref?: RefObject<T> | T | null | undefined;
165168
onResize?: ResizeHandler;
@@ -194,6 +197,8 @@ function useResizeObserver<T extends HTMLElement>(
194197
// the component unmounted.
195198
const didUnmount = useRef(false);
196199
useEffect(() => {
200+
didUnmount.current = false;
201+
197202
return () => {
198203
didUnmount.current = true;
199204
};

0 commit comments

Comments
 (0)