Skip to content

Commit

Permalink
refactor(hooks): use lazy function for useResize
Browse files Browse the repository at this point in the history
  • Loading branch information
xiejay97 committed Apr 23, 2023
1 parent 32fd556 commit ed26998
Showing 1 changed file with 38 additions and 24 deletions.
62 changes: 38 additions & 24 deletions packages/hooks/src/useResize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ import { isUndefined } from 'lodash';
import { useEffect, useRef } from 'react';
import { flushSync } from 'react-dom';

let checkResize = (cb1: () => void, cb2: () => void, entry: any) => {
if ('borderBoxSize' in entry) {
checkResize = (cb1: () => void) => {
cb1();
};
} else {
checkResize = (cb1: () => void, cb2: () => void) => {
cb2();
};
}
};

export function useResize(target: React.RefObject<Element | null>, cb?: ResizeObserverCallback, disabled = false, skipEmpty = true): void {
const dataRef = useRef<{
prevContentRect?: { width: number; height: number };
Expand All @@ -14,30 +26,32 @@ export function useResize(target: React.RefObject<Element | null>, cb?: ResizeOb
useEffect(() => {
if (target.current && !disabled) {
const observer = new ResizeObserver((entries, observer) => {
let entry = entries[0];

if ('borderBoxSize' in entry) {
if (
!isUndefined(dataRef.current.prevContentRect) &&
!(skipEmpty && entry.borderBoxSize[0].blockSize === 0 && entry.borderBoxSize[0].inlineSize === 0) &&
(dataRef.current.prevContentRect.width !== entry.borderBoxSize[0].inlineSize ||
dataRef.current.prevContentRect.height !== entry.borderBoxSize[0].blockSize)
) {
flushSync(() => cb?.(entries, observer));
}
dataRef.current.prevContentRect = { width: entry.borderBoxSize[0].inlineSize, height: entry.borderBoxSize[0].blockSize };
} else {
entry = entries[0];
if (
!isUndefined(dataRef.current.prevContentRect) &&
!(skipEmpty && entry.contentRect.width === 0 && entry.contentRect.height === 0) &&
(dataRef.current.prevContentRect.width !== entry.contentRect.width ||
dataRef.current.prevContentRect.height !== entry.contentRect.height)
) {
flushSync(() => cb?.(entries, observer));
}
dataRef.current.prevContentRect = { width: entry.contentRect.width, height: entry.contentRect.height };
}
const entry = entries[0];
checkResize(
() => {
if (
!isUndefined(dataRef.current.prevContentRect) &&
!(skipEmpty && entry.borderBoxSize[0].blockSize === 0 && entry.borderBoxSize[0].inlineSize === 0) &&
(dataRef.current.prevContentRect.width !== entry.borderBoxSize[0].inlineSize ||
dataRef.current.prevContentRect.height !== entry.borderBoxSize[0].blockSize)
) {
flushSync(() => cb?.(entries, observer));
}
dataRef.current.prevContentRect = { width: entry.borderBoxSize[0].inlineSize, height: entry.borderBoxSize[0].blockSize };
},
() => {
if (
!isUndefined(dataRef.current.prevContentRect) &&
!(skipEmpty && entry.contentRect.width === 0 && entry.contentRect.height === 0) &&
(dataRef.current.prevContentRect.width !== entry.contentRect.width ||
dataRef.current.prevContentRect.height !== entry.contentRect.height)
) {
flushSync(() => cb?.(entries, observer));
}
dataRef.current.prevContentRect = { width: entry.contentRect.width, height: entry.contentRect.height };
},
entry
);
});
observer.observe(target.current);
return () => {
Expand Down

0 comments on commit ed26998

Please sign in to comment.