( options: UseGridLayoutOptions )
| 151 | * ``` |
| 152 | */ |
| 153 | export function useGridLayout( |
| 154 | options: UseGridLayoutOptions |
| 155 | ): UseGridLayoutResult { |
| 156 | const { |
| 157 | layout: propsLayout, |
| 158 | cols, |
| 159 | preventCollision = false, |
| 160 | onLayoutChange, |
| 161 | compactor = verticalCompactor |
| 162 | } = options; |
| 163 | |
| 164 | // Track if we're currently dragging to block prop updates |
| 165 | const isDraggingRef = useRef(false); |
| 166 | |
| 167 | // Initialize layout with compaction using the compactor |
| 168 | const [layout, setLayoutState] = useState<Layout>(() => { |
| 169 | const corrected = correctBounds(cloneLayout(propsLayout), { cols }); |
| 170 | return compactor.compact(corrected, cols); |
| 171 | }); |
| 172 | |
| 173 | // Drag state |
| 174 | const [dragState, setDragState] = useState<DragState>({ |
| 175 | activeDrag: null, |
| 176 | oldDragItem: null, |
| 177 | oldLayout: null |
| 178 | }); |
| 179 | |
| 180 | // Resize state |
| 181 | const [resizeState, setResizeState] = useState<ResizeState>({ |
| 182 | resizing: false, |
| 183 | oldResizeItem: null, |
| 184 | oldLayout: null |
| 185 | }); |
| 186 | |
| 187 | // Drop state |
| 188 | const [dropState, setDropState] = useState<DropState>({ |
| 189 | droppingDOMNode: null, |
| 190 | droppingPosition: null |
| 191 | }); |
| 192 | |
| 193 | // Track previous layout for change detection |
| 194 | const prevLayoutRef = useRef<Layout>(layout); |
| 195 | |
| 196 | // Set layout with optional compaction - use compactor.compact() (#2213) |
| 197 | const setLayout = useCallback( |
| 198 | (newLayout: Layout) => { |
| 199 | const corrected = correctBounds(cloneLayout(newLayout), { cols }); |
| 200 | const compacted = compactor.compact(corrected, cols); |
| 201 | setLayoutState(compacted); |
| 202 | }, |
| 203 | [cols, compactor] |
| 204 | ); |
| 205 | |
| 206 | // Sync layout from props when not dragging |
| 207 | useEffect(() => { |
| 208 | if (isDraggingRef.current) return; |
| 209 | |
| 210 | if (!deepEqual(propsLayout, prevLayoutRef.current)) { |
no test coverage detected
searching dependent graphs…