Skip to content

Commit

Permalink
fix: Added some fixes for React 18.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The lib now takes "Element", not only "HTMLElement", to
be consistent with ResizeObserver.

fixes #90
fixes #91
fixes #92
  • Loading branch information
ZeeCoder committed May 15, 2022
1 parent 653b0b9 commit 852d976
Show file tree
Hide file tree
Showing 10 changed files with 698 additions and 855 deletions.
1 change: 1 addition & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ module.exports = function (karmaConfig) {
);

let testFilePattern = "tests/*.tsx";
// let testFilePattern = "tests/ssr.test.tsx";
// let testFilePattern = "tests/basic.tsx";
// let testFilePattern = "tests/testing-lib.tsx";

Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@
"package.json"
],
"peerDependencies": {
"react": ">=16.8.0",
"react-dom": ">=16.8.0"
"react": "16.8.0 - 18",
"react-dom": "16.8.0 - 18"
},
"devDependencies": {
"@babel/core": "^7.7.7",
Expand All @@ -88,11 +88,11 @@
"@semantic-release/npm": "^7.0.6",
"@semantic-release/release-notes-generator": "^9.0.1",
"@size-limit/preset-small-lib": "^5.0.1",
"@testing-library/react": "^12.0.0",
"@testing-library/react": "^13.1.1",
"@types/karma": "^6.3.1",
"@types/karma-jasmine": "^4.0.1",
"@types/react": "^17.0.15",
"@types/react-dom": "^17.0.9",
"@types/react": "^18.0.8",
"@types/react-dom": "^18.0.3",
"babel-loader": "^8.1.0",
"delay": "^5.0.0",
"husky": "^7.0.0",
Expand All @@ -107,9 +107,9 @@
"lint-staged": "^11.1.1",
"npm-run-all": "^4.1.5",
"prettier": "^2.0.4",
"react": "^17.0.2",
"react": "^18.0.0",
"react-app-polyfill": "^2.0.0",
"react-dom": "^17.0.2",
"react-dom": "^18.0.0",
"rollup": "^2.6.1",
"semantic-release": "^17.2.2",
"size-limit": "^5.0.1",
Expand Down
15 changes: 10 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type SubscriberResponse = SubscriberCleanup | void;
// refs, but then host hooks / components could not opt out of renders.
// This could've been exported to its own module, but the current build doesn't
// seem to work with module imports and I had no more time to spend on this...
function useResolvedElement<T extends HTMLElement>(
function useResolvedElement<T extends Element>(
subscriber: (element: T) => SubscriberResponse,
refOrElement?: T | RefObject<T> | null
): RefCallback<T> {
Expand All @@ -26,12 +26,15 @@ function useResolvedElement<T extends HTMLElement>(
} | null>(null);
const cleanupRef = useRef<SubscriberResponse | null>();

// Resolving ".current" purely so that a new callSubscriber instance is created when needed.
const refElement =
refOrElement && "current" in refOrElement ? refOrElement.current : null;
const callSubscriber = useCallback(() => {
let element = null;
if (callbackRefElement.current) {
element = callbackRefElement.current;
} else if (refOrElement) {
if (refOrElement instanceof HTMLElement) {
if (refOrElement instanceof Element) {
element = refOrElement;
} else {
element = refOrElement.current;
Expand Down Expand Up @@ -60,7 +63,7 @@ function useResolvedElement<T extends HTMLElement>(
if (element) {
cleanupRef.current = subscriber(element);
}
}, [refOrElement, subscriber]);
}, [refOrElement, refElement, subscriber]);

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

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

type HookResponse<T extends HTMLElement> = {
type HookResponse<T extends Element> = {
ref: RefCallback<T>;
} & ObservedSize;

Expand Down Expand Up @@ -159,7 +162,7 @@ const extractSize = (

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

function useResizeObserver<T extends HTMLElement>(
function useResizeObserver<T extends Element>(
opts: {
ref?: RefObject<T> | T | null | undefined;
onResize?: ResizeHandler;
Expand Down Expand Up @@ -194,6 +197,8 @@ function useResizeObserver<T extends HTMLElement>(
// the component unmounted.
const didUnmount = useRef(false);
useEffect(() => {
didUnmount.current = false;

return () => {
didUnmount.current = true;
};
Expand Down
Loading

0 comments on commit 852d976

Please sign in to comment.