Skip to content

Commit 5401ceb

Browse files
committed
fix(board): use stable RefObject for gridstack wrapper with useElementSize
1 parent 13912b7 commit 5401ceb

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

apps/nextjs/src/components/board/sections/gridstack/use-gridstack.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { RefObject } from "react";
1+
import type { Ref, RefObject } from "react";
22
import { createRef, useCallback, useEffect, useRef } from "react";
33
import { useElementSize } from "@mantine/hooks";
44

@@ -13,7 +13,7 @@ import { useSectionActions } from "../section-actions";
1313
import { initializeGridstack } from "./init-gridstack";
1414

1515
export interface UseGridstackRefs {
16-
wrapper: RefObject<HTMLDivElement | null>;
16+
wrapper: Ref<HTMLDivElement | null>;
1717
items: RefObject<Record<string, RefObject<GridItemHTMLElement | null>>>;
1818
gridstack: RefObject<GridStack | null>;
1919
}
@@ -60,7 +60,15 @@ export const useGridstack = (section: Omit<Section, "items">, itemIds: string[])
6060
const { moveAndResizeInnerSection, moveInnerSectionToSection } = useSectionActions();
6161

6262
// define reference for wrapper - is used to calculate the width of the wrapper
63-
const { ref: wrapperRef, width, height } = useElementSize<HTMLDivElement>();
63+
const wrapperElementRef = useRef<HTMLDivElement | null>(null);
64+
const { ref: measureWrapperRef, width, height } = useElementSize<HTMLDivElement>();
65+
const wrapperRef = useCallback(
66+
(node: HTMLDivElement | null) => {
67+
wrapperElementRef.current = node;
68+
measureWrapperRef(node);
69+
},
70+
[measureWrapperRef],
71+
);
6472
// references to the diffrent items contained in the gridstack
6573
const itemRefs = useRef<Record<string, RefObject<GridItemHTMLElement | null>>>({});
6674
// reference of the gridstack object for modifications after initialization
@@ -193,7 +201,7 @@ export const useGridstack = (section: Omit<Section, "items">, itemIds: string[])
193201
itemIds,
194202
refs: {
195203
items: itemRefs,
196-
wrapper: wrapperRef,
204+
wrapper: wrapperElementRef,
197205
gridstack: gridRef,
198206
},
199207
sectionColumnCount: columnCount,
@@ -276,7 +284,7 @@ export const useGridstack = (section: Omit<Section, "items">, itemIds: string[])
276284
useCssVariableConfiguration({
277285
columnCount,
278286
gridRef,
279-
wrapperRef,
287+
wrapperRef: wrapperElementRef,
280288
width,
281289
height,
282290
isDynamic: section.kind === "dynamic",
@@ -293,7 +301,7 @@ export const useGridstack = (section: Omit<Section, "items">, itemIds: string[])
293301

294302
interface UseCssVariableConfiguration {
295303
gridRef: UseGridstackRefs["gridstack"];
296-
wrapperRef: UseGridstackRefs["wrapper"];
304+
wrapperRef: RefObject<HTMLDivElement | null>;
297305
width: number;
298306
height: number;
299307
columnCount: number;

0 commit comments

Comments
 (0)