1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Add useResizeObserver hook

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2022-03-14 13:00:27 +03:00
parent 1b2c866b45
commit 265d1076bd
2 changed files with 27 additions and 0 deletions

View File

@ -8,4 +8,5 @@
export * from "./useOnUnmount";
export * from "./useInterval";
export * from "./useMutationObserver";
export * from "./useResizeObserver";
export * from "./use-toggle";

View File

@ -0,0 +1,26 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { useEffect } from "react";
export function useResizeObserver(
element: Element,
callback: ResizeObserverCallback,
) {
useEffect(() => {
if (element) {
const observer = new ResizeObserver(callback);
observer.observe(element);
return () => {
observer.disconnect();
};
}
return undefined;
}, [callback]);
}