Files
Web-Frontend/client/src/components/layout/Canvas/views/DashboardCanvasNav/WidgetCanvas.jsx
T

50 lines
2.0 KiB
React
Raw Normal View History

2025-07-21 14:27:53 -07:00
// This component defines a generic Widget that can be dynamically resized and
// dragged inside the canvas
// Content not included, must be populated using custom charts plots and notifications
import { useState } from 'react';
2025-07-22 11:58:10 -07:00
import GridLayout, { WidthProvider } from 'react-grid-layout';
2025-07-24 08:52:20 -07:00
import Widget from '../../components/Widget.jsx';
2025-07-23 13:44:24 -07:00
import { useSlotRecipe, chakra, Flex } from '@chakra-ui/react';
2025-07-21 14:27:53 -07:00
2025-07-24 12:08:10 -07:00
export default function WidgetCanvas({layout, onLayoutChange}) {
2025-07-21 14:27:53 -07:00
// Define widgets initial position and size
// TODO Add dynamic adding/deleting of widgets via options button
2025-07-22 11:58:10 -07:00
const recipe = useSlotRecipe({ key: "widget" });
2025-07-23 13:44:24 -07:00
const styles = recipe(); // using root recipe for widget below
const ResponsiveGridLayout = WidthProvider(GridLayout); // canvas width now adjusts to window size
2025-07-22 11:58:10 -07:00
2025-07-22 13:41:32 -07:00
// don't allow text highlighting when dragging
const handleDragStart = () => {
document.body.style.userSelect = "none";
};
// allow text highlighting all other times
const handleDragStop = () => {
document.body.style.userSelect = "";
};
2025-07-21 14:27:53 -07:00
return (
2025-07-22 14:55:28 -07:00
<chakra.div width="100%">
2025-07-22 12:56:44 -07:00
<ResponsiveGridLayout
2025-07-24 16:05:42 -07:00
className="layout"
layout={layout}
cols={12} // Total columns in grid
rowHeight={30} //Height of a single row in px
isBounded={true} // Total width of the grid
margin={[10, 10]} // [horizontal, vertical] gap
onLayoutChange={onLayoutChange}
draggableHandle=".widget-handle" // Make only the header draggable
onDragStart={handleDragStart}
onDragStop={handleDragStop}
2025-07-22 12:56:44 -07:00
>
{layout.map((item) => (
2025-07-23 13:44:24 -07:00
// this flex wrapper allows the widget to shrink down to the header size, no less
<Flex key={item.i} css={styles.root} height="100%" >
2025-07-22 12:56:44 -07:00
<Widget title="VDAP Widget" />
2025-07-23 13:44:24 -07:00
</Flex>
2025-07-22 12:56:44 -07:00
))}
</ResponsiveGridLayout>
</chakra.div>
2025-07-21 14:27:53 -07:00
);
}