MCPcopy Index your code
hub / github.com/chartbrew/chartbrew / useChartSize

Function useChartSize

client/src/modules/useChartSize.js:4–38  ·  view source on GitHub ↗
(layouts)

Source from the content-addressed store, hash-verified

2import { widthSize } from "./layoutBreakpoints";
3
4const useChartSize = (layouts) => {
5 const [currentLayout, setCurrentLayout] = useState(null);
6
7 const calculateCurrentLayout = useCallback(() => {
8 const screenWidth = window.innerWidth;
9 let selectedKey = "xxs"; // Default to the smallest size
10
11 const orderedBreakpoints = Object.keys(widthSize).sort(
12 (a, b) => widthSize[a] - widthSize[b]
13 );
14
15 orderedBreakpoints.forEach((key) => {
16 if (screenWidth >= widthSize[key]) {
17 selectedKey = key;
18 }
19 });
20
21 if (!layouts?.[selectedKey]) {
22 return;
23 }
24
25 setCurrentLayout(layouts[selectedKey]);
26 }, [layouts]);
27
28 useEffect(() => {
29 calculateCurrentLayout();
30 // Update layout on window resize
31 window.addEventListener("resize", calculateCurrentLayout);
32 return () => {
33 window.removeEventListener("resize", calculateCurrentLayout);
34 };
35 }, [calculateCurrentLayout]);
36
37 return currentLayout;
38};
39
40export default useChartSize;

Callers 3

SharedChartFunction · 0.85
EmbeddedChartFunction · 0.85
ChartFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected