MCPcopy
hub / github.com/react-grid-layout/react-grid-layout / useGridLayout

Function useGridLayout

src/react/hooks/useGridLayout.ts:153–471  ·  view source on GitHub ↗
(
  options: UseGridLayoutOptions
)

Source from the content-addressed store, hash-verified

151 * ```
152 */
153export 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)) {

Callers 1

hooks-test.tsxFile · 0.85

Calls 7

correctBoundsFunction · 0.85
cloneLayoutFunction · 0.85
getLayoutItemFunction · 0.85
cloneLayoutItemFunction · 0.85
moveElementFunction · 0.85
bottomFunction · 0.85
compactMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…