|
| 1 | +"use client"; |
| 2 | +// gridstack-item.tsx |
| 3 | + |
| 4 | +import type { |
| 5 | + GridItemHTMLElement, |
| 6 | + |
| 7 | + GridStackWidget, |
| 8 | +} from "gridstack"; |
| 9 | +import * as React from "react"; |
| 10 | +import {useGridstackContext} from "./use-gridstack-context"; |
| 11 | + |
| 12 | + |
| 13 | + |
| 14 | +interface GridstackItemComponentProps { |
| 15 | + initOptions?: GridStackWidget; |
| 16 | + id: string; |
| 17 | + children: React.ReactNode; |
| 18 | + className?: string; |
| 19 | +} |
| 20 | + |
| 21 | +export type ItemRefType = React.MutableRefObject<GridItemHTMLElement | null>; |
| 22 | + |
| 23 | +/** |
| 24 | + * Component for rendering a grid item in a Gridstack layout. |
| 25 | + * |
| 26 | + * @component |
| 27 | + * @param {GridstackItemComponentProps} props - The component props. |
| 28 | + * @param {GridStackWidget} props.initOptions - The initial options for the grid item. |
| 29 | + * @param {string} props.id - The unique identifier for the grid item. |
| 30 | + * @param {ReactNode} props.children - The content to be rendered inside the grid item. |
| 31 | + * @param {string} props.className - The CSS class name for the grid item. |
| 32 | + * @returns {JSX.Element} The rendered grid item component. |
| 33 | + */ |
| 34 | +export const GridstackItemComponent = ({ |
| 35 | + initOptions, |
| 36 | + id, |
| 37 | + children, |
| 38 | + className, |
| 39 | +}: GridstackItemComponentProps) => { |
| 40 | + const containerRef = React.useRef<HTMLDivElement>(null); |
| 41 | + const optionsRef = React.useRef<GridStackWidget | undefined>(initOptions); |
| 42 | + const { grid, addItemRefToList, removeItemRefFromList } = useGridstackContext(); |
| 43 | + const itemRef = React.useRef<GridItemHTMLElement | null>(null); |
| 44 | + |
| 45 | + // Update the optionsRef when initOptions changes |
| 46 | + React.useEffect(() => { |
| 47 | + optionsRef.current = initOptions; |
| 48 | + }, [initOptions]); |
| 49 | + |
| 50 | + // biome-ignore lint/correctness/useExhaustiveDependencies: <explanation> |
| 51 | + React.useLayoutEffect(() => { |
| 52 | + if (!grid || !containerRef.current) return; |
| 53 | + if (grid && containerRef.current) { |
| 54 | + // Initialize the widget |
| 55 | + |
| 56 | + grid.batchUpdate(true); |
| 57 | + itemRef.current = grid.addWidget(containerRef.current, optionsRef.current); |
| 58 | + grid.batchUpdate(false); |
| 59 | + |
| 60 | + addItemRefToList(id, itemRef); |
| 61 | + |
| 62 | + |
| 63 | + // Cleanup function to remove the widget |
| 64 | + return () => { |
| 65 | + if (grid && itemRef.current) { |
| 66 | + try { |
| 67 | + grid.batchUpdate(true); |
| 68 | + grid.removeWidget(itemRef.current, false); |
| 69 | + grid.batchUpdate(false); |
| 70 | + |
| 71 | + removeItemRefFromList(id); |
| 72 | + |
| 73 | + |
| 74 | + } catch (error) { |
| 75 | + console.error("Error removing widget", error); |
| 76 | + //! Doing nothing here is a bad practice, but we don't want to crash the app (Temporary fix) |
| 77 | + // Do nothing |
| 78 | + } |
| 79 | + } |
| 80 | + }; |
| 81 | + } |
| 82 | + |
| 83 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 84 | + }, [grid]); |
| 85 | + |
| 86 | + return ( |
| 87 | + <div ref={containerRef} id=""> |
| 88 | + <div className={`w-full h-full ${className}`}>{children}</div> |
| 89 | + </div> |
| 90 | + ); |
| 91 | +}; |
0 commit comments