Skip to content

Commit 8839213

Browse files
Scrolls the grid layout to the bottom when resizing cards below the c… (#3191)
Scrolls the grid layout to the bottom when resizing cards below the container. --------- Signed-off-by: BOUTIER Charly <[email protected]>
1 parent ad26c62 commit 8839213

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/components/diagrams/diagram-grid-layout.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
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';
1010
import { useDiagramModel } from './hooks/use-diagram-model';
1111
import { Diagram, DiagramParams, DiagramType } from './diagram.type';
1212
import { Box, useTheme } from '@mui/material';
@@ -156,6 +156,7 @@ function DiagramGridLayout({ studyUuid, showInSpreadsheet, showGrid, visible }:
156156
const theme = useTheme();
157157
const [layouts, setLayouts] = useState<Layouts>(initialLayouts);
158158
const [blinkingDiagrams, setBlinkingDiagrams] = useState<UUID[]>([]);
159+
const responsiveGridLayoutRef = useRef<any>(null);
159160
const currentBreakpointRef = useRef<string>('lg');
160161
const lastModifiedBreakpointRef = useRef<string>('lg'); // Track the last modified breakpoint
161162

@@ -312,6 +313,19 @@ function DiagramGridLayout({ studyUuid, showInSpreadsheet, showGrid, visible }:
312313
setLayouts((prev) => ({ ...prev, [currentBreakpointRef.current]: currentLayout }));
313314
}, []);
314315

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+
315329
/**
316330
* Handle card resizing across all breakpoints
317331
* Maintains consistent card dimensions regardless of screen size
@@ -415,6 +429,9 @@ function DiagramGridLayout({ studyUuid, showInSpreadsheet, showGrid, visible }:
415429
onLayoutSave={debouncedGridLayoutSave}
416430
/>
417431
<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.
418435
className="layout"
419436
breakpoints={GRID_CONFIG.breakpoints}
420437
cols={GRID_CONFIG.cols}
@@ -443,7 +460,6 @@ function DiagramGridLayout({ studyUuid, showInSpreadsheet, showGrid, visible }:
443460
}
444461
handleDragStop(layout);
445462
}}
446-
autoSize={false} // otherwise the grid has strange behavior
447463
resizeHandle={<CustomResizeHandle />}
448464
>
449465
{Object.values(diagrams).map((diagram) => {

0 commit comments

Comments
 (0)