Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memoize resize callback #307

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/hooks/useContentRect/useContentRect.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRef, type RefObject } from 'react';
import { useRef, type RefObject, useCallback } from 'react';
import { useMount } from '../../lifecycle/hooks/useMount/useMount.js';
import { unref, type Unreffable } from '../../utils/unref/unref.js';
import { useResizeObserver } from '../useResizeObserver/useResizeObserver.js';
Expand All @@ -12,9 +12,11 @@ export function useContentRect(
): RefObject<DOMRectReadOnly | null> {
const contentRectRef = useRef<DOMRectReadOnly | null>(null);

useResizeObserver(target, (entries): void => {
const onResize = useCallback<ResizeObserverCallback>((entries): void => {
contentRectRef.current = entries.at(0)?.contentRect ?? null;
});
}, []);

useResizeObserver(target, onResize);

useMount(() => {
contentRectRef.current = unref(target)?.getBoundingClientRect() ?? null;
Expand Down
8 changes: 5 additions & 3 deletions src/hooks/useContentRectState/useContentRectState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRef, useState } from 'react';
import { useCallback, useRef, useState } from 'react';
import { useMount } from '../../index.js';
import { unref, type Unreffable } from '../../utils/unref/unref.js';
import { useResizeObserver } from '../useResizeObserver/useResizeObserver.js';
Expand All @@ -11,12 +11,14 @@ export function useContentRectState(target: Unreffable<Element | null>): DOMRect
const [contentRect, setContentRect] = useState<DOMRectReadOnly | null>(null);
const rafRef = useRef(0);

useResizeObserver(target, (entries) => {
const onResize = useCallback<ResizeObserverCallback>((entries) => {
cancelAnimationFrame(rafRef.current);
rafRef.current = requestAnimationFrame(() => {
setContentRect(entries.at(0)?.contentRect ?? null);
});
});
}, []);

useResizeObserver(target, onResize);

useMount(() => {
setContentRect(unref(target)?.getBoundingClientRect() ?? null);
Expand Down
10 changes: 8 additions & 2 deletions src/hooks/useResizeObserver/useResizeObserver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useEffect } from 'react';
import { unref, type Unreffable } from '../../utils/unref/unref.js';
import { useRefValue } from '../useRefValue/useRefValue.js';

/**
* This hook allows you to add a ResizeObserver for an element and remove it
Expand All @@ -14,19 +15,24 @@ export function useResizeObserver(
callback?: ResizeObserverCallback | undefined,
options?: ResizeObserverOptions,
): void {
const callbackRef = useRefValue(callback);

useEffect(() => {
const element = unref(target);

if (element === null || callback === undefined) {
return;
}

const resizeObserver = new ResizeObserver(callback);
const resizeObserver = new ResizeObserver((..._arguments) => {
callbackRef.current?.(..._arguments);
});

resizeObserver.observe(element, options);

return () => {
resizeObserver.disconnect();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [target, callback, ...Object.values(options ?? {})]);
}, [target, ...Object.values(options ?? {})]);
}
Loading