(layouts)
| 2 | import { widthSize } from "./layoutBreakpoints"; |
| 3 | |
| 4 | const 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 | |
| 40 | export default useChartSize; |
no outgoing calls
no test coverage detected