1368ffe55d
* refactor: move data-table hooks/contexts/provider from features to web modules Co-Authored-By: eunjae@cal.com <hey@eunjae.dev> * fix: update broken useColumnResizing import path in DataTable.tsx Co-Authored-By: eunjae@cal.com <hey@eunjae.dev> * fix: update remaining broken import paths for moved hooks Co-Authored-By: eunjae@cal.com <hey@eunjae.dev> * docs: update GUIDE.md import paths and file references for moved modules Co-Authored-By: eunjae@cal.com <hey@eunjae.dev> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
33 lines
753 B
TypeScript
33 lines
753 B
TypeScript
"use client";
|
|
|
|
import debounce from "lodash/debounce";
|
|
import { useState, useEffect } from "react";
|
|
|
|
export function useDebouncedWidth(ref: React.RefObject<HTMLDivElement>, debounceMs = 100) {
|
|
const [width, setWidth] = useState<number>(0);
|
|
|
|
useEffect(() => {
|
|
if (!ref.current) return;
|
|
|
|
const element = ref.current;
|
|
setWidth(element.clientWidth);
|
|
|
|
const debouncedSetWidth = debounce((width: number) => {
|
|
setWidth(width);
|
|
}, debounceMs);
|
|
|
|
const resizeObserver = new ResizeObserver(([entry]) => {
|
|
debouncedSetWidth(entry.target.clientWidth);
|
|
});
|
|
|
|
resizeObserver.observe(element);
|
|
|
|
return () => {
|
|
resizeObserver.disconnect();
|
|
debouncedSetWidth.cancel();
|
|
};
|
|
}, [ref]);
|
|
|
|
return width;
|
|
}
|