|
6 | 6 | */
|
7 | 7 |
|
8 | 8 | import { useCallback, useState, useRef } from 'react';
|
9 |
| -import { Layout, Layouts, Responsive, WidthProvider } from 'react-grid-layout'; |
| 9 | +import { Layout, Layouts, ItemCallback, Responsive, WidthProvider } from 'react-grid-layout'; |
10 | 10 | import { useDiagramModel } from './hooks/use-diagram-model';
|
11 | 11 | import { Diagram, DiagramParams, DiagramType } from './diagram.type';
|
12 | 12 | import { Box, useTheme } from '@mui/material';
|
@@ -156,6 +156,7 @@ function DiagramGridLayout({ studyUuid, showInSpreadsheet, showGrid, visible }:
|
156 | 156 | const theme = useTheme();
|
157 | 157 | const [layouts, setLayouts] = useState<Layouts>(initialLayouts);
|
158 | 158 | const [blinkingDiagrams, setBlinkingDiagrams] = useState<UUID[]>([]);
|
| 159 | + const responsiveGridLayoutRef = useRef<any>(null); |
159 | 160 | const currentBreakpointRef = useRef<string>('lg');
|
160 | 161 | const lastModifiedBreakpointRef = useRef<string>('lg'); // Track the last modified breakpoint
|
161 | 162 |
|
@@ -312,6 +313,19 @@ function DiagramGridLayout({ studyUuid, showInSpreadsheet, showGrid, visible }:
|
312 | 313 | setLayouts((prev) => ({ ...prev, [currentBreakpointRef.current]: currentLayout }));
|
313 | 314 | }, []);
|
314 | 315 |
|
| 316 | + const handleResize = useCallback<ItemCallback>((layout, oldItem, newItem, placeholder, event, _el) => { |
| 317 | + // We cannot use the ResponsiveGridLayout's innerRef prop (see https://github.com/react-grid-layout/react-grid-layout/issues/1444) |
| 318 | + // so we manually fetch its inner ref inside to get the HTMLDivElement. |
| 319 | + const container = responsiveGridLayoutRef.current?.elementRef?.current; |
| 320 | + if (!container) { |
| 321 | + return; |
| 322 | + } |
| 323 | + const { bottom } = container.getBoundingClientRect(); |
| 324 | + if (event.clientY > bottom) { |
| 325 | + container.scrollTop = container.scrollHeight; |
| 326 | + } |
| 327 | + }, []); |
| 328 | + |
315 | 329 | /**
|
316 | 330 | * Handle card resizing across all breakpoints
|
317 | 331 | * Maintains consistent card dimensions regardless of screen size
|
@@ -415,6 +429,9 @@ function DiagramGridLayout({ studyUuid, showInSpreadsheet, showGrid, visible }:
|
415 | 429 | onLayoutSave={debouncedGridLayoutSave}
|
416 | 430 | />
|
417 | 431 | <ResponsiveGridLayout
|
| 432 | + ref={responsiveGridLayoutRef} // the provided innerRef prop is bugged (see https://github.com/react-grid-layout/react-grid-layout/issues/1444) |
| 433 | + autoSize={false} // This props should do the resizing for us but is bugged too. We force it to false, otherwise the grid has strange behaviors. |
| 434 | + onResize={handleResize} // We manually scroll down when resizing downward. This is a workaround due to the bugs. |
418 | 435 | className="layout"
|
419 | 436 | breakpoints={GRID_CONFIG.breakpoints}
|
420 | 437 | cols={GRID_CONFIG.cols}
|
@@ -443,7 +460,6 @@ function DiagramGridLayout({ studyUuid, showInSpreadsheet, showGrid, visible }:
|
443 | 460 | }
|
444 | 461 | handleDragStop(layout);
|
445 | 462 | }}
|
446 |
| - autoSize={false} // otherwise the grid has strange behavior |
447 | 463 | resizeHandle={<CustomResizeHandle />}
|
448 | 464 | >
|
449 | 465 | {Object.values(diagrams).map((diagram) => {
|
|
0 commit comments