Add useResizeObserver hook

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

No files matched your search

+1
View File
@@ -8,4 +8,5 @@
export * from "./useOnUnmount";
export * from "./useInterval";
export * from "./useMutationObserver";
export * from "./useResizeObserver";
export * from "./use-toggle";
+26
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]);
}