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

Don't recreate ResizeObserver on callback reference change #158

Conversation

leroykorterink
Copy link
Collaborator

@leroykorterink leroykorterink commented May 15, 2023

See title

@leroykorterink leroykorterink added documentation Improvements or additions to documentation enhancement New feature or request labels May 15, 2023
@leroykorterink leroykorterink self-assigned this May 15, 2023
@leroykorterink leroykorterink changed the title Don't recreate ResizeObserver on callback reference change and use Un… Don't recreate ResizeObserver on callback reference change May 15, 2023
* when the component unmounts.
*
* @param ref - The ref to observe
* @param callback - The callback to fire when the element resizes
*/
export function useResizeObserver(ref: RefObject<Element>, callback: ResizeObserverCallback): void {
export function useResizeObserver(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want a separate ticket to support unobserve, e.g. having a boolean arg that defaults to true, but can be set to false to unobserve ?

Copy link
Collaborator Author

@leroykorterink leroykorterink May 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function useResizeObserver(
  target: Unreffable<Element>,
  callback: ResizeObserverCallback,
  options?: ResizeObserverOptions,
  enabled = true,
): void

Implementation will be similar to this:

export function useResizeObserver(
  target: Unreffable<Element>,
  callback: ResizeObserverCallback,
  options?: ResizeObserverOptions,
  enabled = true,
): void {
  const memoizedOptions = useMemo(
    () => options,
    [JSON.stringify(options)],
  );

  const callbackRef = useRefValue(callback);
  const resizeObserver = useClientSideValue(
    () =>
      new ResizeObserver((entries, observer) => {
        callbackRef.current?.(entries, observer);
      }),
  );

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

    if (element === null) {
      return;
    }

    if (enabled) {
      resizeObserver?.observe(element, memoizedOptions);
    } else {
      resizeObserver?.unobserve(element);
    }

    return () => {
      resizeObserver?.unobserve(element);
    };
  }, [enabled, memoizedOptions, resizeObserver, target]);
}

@leroykorterink
Copy link
Collaborator Author

Feature is enhanced in #274

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants