@vertly/dashboard-grid 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +72 -0
- package/dist/index.cjs +732 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +76 -0
- package/dist/index.d.ts +76 -0
- package/dist/index.js +691 -0
- package/dist/index.js.map +1 -0
- package/package.json +55 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,691 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
// src/dashboard-grid.tsx
|
|
4
|
+
import { Fragment, useCallback, useRef, useState } from "react";
|
|
5
|
+
import { DndProvider, useDrag, useDragLayer, useDrop } from "react-dnd";
|
|
6
|
+
import { HTML5Backend } from "react-dnd-html5-backend";
|
|
7
|
+
import { GripVerticalIcon } from "lucide-react";
|
|
8
|
+
|
|
9
|
+
// src/layout-engine.ts
|
|
10
|
+
var COLS = 12;
|
|
11
|
+
var ZONE_WIDTH_PX = 12;
|
|
12
|
+
var MIN_COL_SPAN = 1;
|
|
13
|
+
var MAX_ITEMS_PER_ROW = 4;
|
|
14
|
+
var DEFAULT_ROW_HEIGHT = 220;
|
|
15
|
+
var MIN_ROW_HEIGHT = 80;
|
|
16
|
+
var MAX_ROW_HEIGHT = 480;
|
|
17
|
+
function rebalanceRow(items, row) {
|
|
18
|
+
if (row.widgetIds.length === 0) return items;
|
|
19
|
+
const span = COLS / row.widgetIds.length;
|
|
20
|
+
const next = { ...items };
|
|
21
|
+
for (const id of row.widgetIds) {
|
|
22
|
+
const item = next[id];
|
|
23
|
+
if (item && item.colSpan !== span) next[id] = { ...item, colSpan: span };
|
|
24
|
+
}
|
|
25
|
+
return next;
|
|
26
|
+
}
|
|
27
|
+
function canPlaceInRow(row, isSameRowMove) {
|
|
28
|
+
if (isSameRowMove) return true;
|
|
29
|
+
return (row?.widgetIds.length ?? 0) < MAX_ITEMS_PER_ROW;
|
|
30
|
+
}
|
|
31
|
+
function placeItemAt(rows, items, itemId, fromRowId, targetRowId, index) {
|
|
32
|
+
const targetRowBefore = rows.find((row) => row.id === targetRowId);
|
|
33
|
+
const targetCountBefore = targetRowBefore?.widgetIds.length ?? 0;
|
|
34
|
+
const isSameRowMove = fromRowId === targetRowId;
|
|
35
|
+
if (!canPlaceInRow(targetRowBefore, isSameRowMove)) {
|
|
36
|
+
return { rows, items };
|
|
37
|
+
}
|
|
38
|
+
let nextRows = rows;
|
|
39
|
+
let nextItems = items;
|
|
40
|
+
let insertIndex = index;
|
|
41
|
+
let sourceCountBefore = 0;
|
|
42
|
+
if (fromRowId !== void 0) {
|
|
43
|
+
const fromRow = rows.find((row) => row.id === fromRowId);
|
|
44
|
+
sourceCountBefore = fromRow?.widgetIds.length ?? 0;
|
|
45
|
+
const fromIndex = fromRow?.widgetIds.indexOf(itemId) ?? -1;
|
|
46
|
+
if (fromRowId === targetRowId && fromIndex !== -1 && fromIndex < insertIndex) {
|
|
47
|
+
insertIndex -= 1;
|
|
48
|
+
}
|
|
49
|
+
nextRows = nextRows.map(
|
|
50
|
+
(row) => row.id === fromRowId ? { ...row, widgetIds: row.widgetIds.filter((id) => id !== itemId) } : row
|
|
51
|
+
);
|
|
52
|
+
nextRows = nextRows.filter((row) => row.widgetIds.length > 0 || row.id === targetRowId);
|
|
53
|
+
}
|
|
54
|
+
nextRows = nextRows.map((row) => {
|
|
55
|
+
if (row.id !== targetRowId) return row;
|
|
56
|
+
const next = [...row.widgetIds];
|
|
57
|
+
next.splice(Math.min(Math.max(insertIndex, 0), next.length), 0, itemId);
|
|
58
|
+
return { ...row, widgetIds: next };
|
|
59
|
+
});
|
|
60
|
+
const targetRowAfter = nextRows.find((row) => row.id === targetRowId);
|
|
61
|
+
if (targetRowAfter && targetRowAfter.widgetIds.length !== targetCountBefore) {
|
|
62
|
+
nextItems = rebalanceRow(nextItems, targetRowAfter);
|
|
63
|
+
}
|
|
64
|
+
if (fromRowId !== void 0 && fromRowId !== targetRowId) {
|
|
65
|
+
const sourceRowAfter = nextRows.find((row) => row.id === fromRowId);
|
|
66
|
+
if (sourceRowAfter && sourceRowAfter.widgetIds.length !== sourceCountBefore) {
|
|
67
|
+
nextItems = rebalanceRow(nextItems, sourceRowAfter);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return { rows: nextRows, items: nextItems };
|
|
71
|
+
}
|
|
72
|
+
function placeItemInNewRow(rows, items, itemId, fromRowId, newRowId, heightPx = DEFAULT_ROW_HEIGHT) {
|
|
73
|
+
let nextRows = rows;
|
|
74
|
+
let nextItems = items;
|
|
75
|
+
if (fromRowId !== void 0) {
|
|
76
|
+
const fromRow = rows.find((row) => row.id === fromRowId);
|
|
77
|
+
nextRows = nextRows.map(
|
|
78
|
+
(row) => row.id === fromRowId ? { ...row, widgetIds: row.widgetIds.filter((id) => id !== itemId) } : row
|
|
79
|
+
).filter((row) => row.widgetIds.length > 0);
|
|
80
|
+
const sourceRowAfter = nextRows.find((row) => row.id === fromRowId);
|
|
81
|
+
if (sourceRowAfter && sourceRowAfter.widgetIds.length !== fromRow?.widgetIds.length) {
|
|
82
|
+
nextItems = rebalanceRow(nextItems, sourceRowAfter);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const newRow = { id: newRowId, widgetIds: [itemId], heightPx };
|
|
86
|
+
nextItems = rebalanceRow(nextItems, newRow);
|
|
87
|
+
return { rows: [...nextRows, newRow], items: nextItems };
|
|
88
|
+
}
|
|
89
|
+
function removeItemFromRow(rows, items, itemId, rowId) {
|
|
90
|
+
const nextRows = rows.map(
|
|
91
|
+
(row) => row.id === rowId ? { ...row, widgetIds: row.widgetIds.filter((id) => id !== itemId) } : row
|
|
92
|
+
).filter((row) => row.widgetIds.length > 0);
|
|
93
|
+
const withoutItem = { ...items };
|
|
94
|
+
delete withoutItem[itemId];
|
|
95
|
+
const rowAfter = nextRows.find((row) => row.id === rowId);
|
|
96
|
+
const nextItems = rowAfter ? rebalanceRow(withoutItem, rowAfter) : withoutItem;
|
|
97
|
+
return { rows: nextRows, items: nextItems };
|
|
98
|
+
}
|
|
99
|
+
function resizeItemPair(items, leftId, rightId, nextLeftColSpan) {
|
|
100
|
+
const left = items[leftId];
|
|
101
|
+
const right = items[rightId];
|
|
102
|
+
if (!left || !right) return items;
|
|
103
|
+
const combined = left.colSpan + right.colSpan;
|
|
104
|
+
const clampedLeft = Math.min(combined - MIN_COL_SPAN, Math.max(MIN_COL_SPAN, nextLeftColSpan));
|
|
105
|
+
const nextRight = combined - clampedLeft;
|
|
106
|
+
return {
|
|
107
|
+
...items,
|
|
108
|
+
[leftId]: { ...left, colSpan: clampedLeft },
|
|
109
|
+
[rightId]: { ...right, colSpan: nextRight }
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function resizeRow(rows, rowId, heightPx) {
|
|
113
|
+
return rows.map((row) => row.id === rowId ? { ...row, heightPx } : row);
|
|
114
|
+
}
|
|
115
|
+
function moveRow(rows, from, to) {
|
|
116
|
+
const next = [...rows];
|
|
117
|
+
const [moved] = next.splice(from, 1);
|
|
118
|
+
next.splice(to, 0, moved);
|
|
119
|
+
return next;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// src/cn.ts
|
|
123
|
+
import { clsx } from "clsx";
|
|
124
|
+
import { twMerge } from "tailwind-merge";
|
|
125
|
+
function cn(...inputs) {
|
|
126
|
+
return twMerge(clsx(inputs));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// src/dashboard-grid.tsx
|
|
130
|
+
import { Fragment as Fragment2, jsx, jsxs } from "react/jsx-runtime";
|
|
131
|
+
var DEFAULT_SAVE_DEBOUNCE_MS = 600;
|
|
132
|
+
var ItemTypes = { WIDGET: "widget", ROW: "row" };
|
|
133
|
+
function DashboardGrid({
|
|
134
|
+
rows: initialRows,
|
|
135
|
+
widgets,
|
|
136
|
+
readOnly = false,
|
|
137
|
+
saveDebounceMs = DEFAULT_SAVE_DEBOUNCE_MS,
|
|
138
|
+
onUpdateLayout,
|
|
139
|
+
onDeleteWidget,
|
|
140
|
+
renderRowEdgeAdd,
|
|
141
|
+
renderWidgetMenu,
|
|
142
|
+
renderWidgetConfigDialog
|
|
143
|
+
}) {
|
|
144
|
+
const [{ rows, items }, setState] = useState(() => ({
|
|
145
|
+
rows: initialRows.map((row) => ({
|
|
146
|
+
id: row.id,
|
|
147
|
+
heightPx: row.heightPx,
|
|
148
|
+
widgetIds: row.widgetIds
|
|
149
|
+
})),
|
|
150
|
+
items: Object.fromEntries(
|
|
151
|
+
Object.values(widgets).map((w) => [w.id, { id: w.id, colSpan: w.colSpan }])
|
|
152
|
+
)
|
|
153
|
+
}));
|
|
154
|
+
const saveTimer = useRef(null);
|
|
155
|
+
const persist = useCallback(
|
|
156
|
+
(nextRows, nextItems) => {
|
|
157
|
+
if (readOnly) return;
|
|
158
|
+
if (saveTimer.current) clearTimeout(saveTimer.current);
|
|
159
|
+
saveTimer.current = setTimeout(() => {
|
|
160
|
+
onUpdateLayout?.(
|
|
161
|
+
nextRows.map((row) => ({ id: row.id, heightPx: row.heightPx, widgetIds: row.widgetIds })),
|
|
162
|
+
Object.values(nextItems).map((item) => ({ id: item.id, colSpan: item.colSpan }))
|
|
163
|
+
);
|
|
164
|
+
}, saveDebounceMs);
|
|
165
|
+
},
|
|
166
|
+
[readOnly, saveDebounceMs, onUpdateLayout]
|
|
167
|
+
);
|
|
168
|
+
const placeWidgetAt = useCallback(
|
|
169
|
+
(opts, targetRowId, index) => {
|
|
170
|
+
setState((prev) => {
|
|
171
|
+
const next = placeItemAt(
|
|
172
|
+
prev.rows,
|
|
173
|
+
prev.items,
|
|
174
|
+
opts.widgetId,
|
|
175
|
+
opts.fromRowId,
|
|
176
|
+
targetRowId,
|
|
177
|
+
index
|
|
178
|
+
);
|
|
179
|
+
persist(next.rows, next.items);
|
|
180
|
+
return next;
|
|
181
|
+
});
|
|
182
|
+
},
|
|
183
|
+
[persist]
|
|
184
|
+
);
|
|
185
|
+
const createRowWith = useCallback(
|
|
186
|
+
(opts) => {
|
|
187
|
+
setState((prev) => {
|
|
188
|
+
const next = placeItemInNewRow(
|
|
189
|
+
prev.rows,
|
|
190
|
+
prev.items,
|
|
191
|
+
opts.widgetId,
|
|
192
|
+
opts.fromRowId,
|
|
193
|
+
`row-${crypto.randomUUID()}`
|
|
194
|
+
);
|
|
195
|
+
persist(next.rows, next.items);
|
|
196
|
+
return next;
|
|
197
|
+
});
|
|
198
|
+
},
|
|
199
|
+
[persist]
|
|
200
|
+
);
|
|
201
|
+
const removeWidget = useCallback(
|
|
202
|
+
(widgetId, rowId) => {
|
|
203
|
+
setState((prev) => {
|
|
204
|
+
const next = removeItemFromRow(prev.rows, prev.items, widgetId, rowId);
|
|
205
|
+
persist(next.rows, next.items);
|
|
206
|
+
return next;
|
|
207
|
+
});
|
|
208
|
+
onDeleteWidget?.(widgetId);
|
|
209
|
+
},
|
|
210
|
+
[persist, onDeleteWidget]
|
|
211
|
+
);
|
|
212
|
+
const resizeWidgetPair = useCallback(
|
|
213
|
+
(leftId, rightId, nextLeftColSpan) => {
|
|
214
|
+
setState((prev) => {
|
|
215
|
+
const nextItems = resizeItemPair(prev.items, leftId, rightId, nextLeftColSpan);
|
|
216
|
+
persist(prev.rows, nextItems);
|
|
217
|
+
return { rows: prev.rows, items: nextItems };
|
|
218
|
+
});
|
|
219
|
+
},
|
|
220
|
+
[persist]
|
|
221
|
+
);
|
|
222
|
+
const resizeRowHeight = useCallback(
|
|
223
|
+
(rowId, heightPx) => {
|
|
224
|
+
setState((prev) => {
|
|
225
|
+
const nextRows = resizeRow(prev.rows, rowId, heightPx);
|
|
226
|
+
persist(nextRows, prev.items);
|
|
227
|
+
return { rows: nextRows, items: prev.items };
|
|
228
|
+
});
|
|
229
|
+
},
|
|
230
|
+
[persist]
|
|
231
|
+
);
|
|
232
|
+
const moveRowTo = useCallback(
|
|
233
|
+
(from, to) => {
|
|
234
|
+
setState((prev) => {
|
|
235
|
+
const nextRows = moveRow(prev.rows, from, to);
|
|
236
|
+
persist(nextRows, prev.items);
|
|
237
|
+
return { rows: nextRows, items: prev.items };
|
|
238
|
+
});
|
|
239
|
+
},
|
|
240
|
+
[persist]
|
|
241
|
+
);
|
|
242
|
+
return /* @__PURE__ */ jsx(DndProvider, { backend: HTML5Backend, children: /* @__PURE__ */ jsx(
|
|
243
|
+
Board,
|
|
244
|
+
{
|
|
245
|
+
rows,
|
|
246
|
+
items,
|
|
247
|
+
widgets,
|
|
248
|
+
readOnly,
|
|
249
|
+
onDropExisting: placeWidgetAt,
|
|
250
|
+
onCreateRowExisting: createRowWith,
|
|
251
|
+
onRemoveWidget: removeWidget,
|
|
252
|
+
onResizeWidget: resizeWidgetPair,
|
|
253
|
+
onResizeRow: resizeRowHeight,
|
|
254
|
+
onMoveRow: moveRowTo,
|
|
255
|
+
renderRowEdgeAdd,
|
|
256
|
+
renderWidgetMenu,
|
|
257
|
+
renderWidgetConfigDialog
|
|
258
|
+
}
|
|
259
|
+
) });
|
|
260
|
+
}
|
|
261
|
+
function Board({
|
|
262
|
+
rows,
|
|
263
|
+
items,
|
|
264
|
+
widgets,
|
|
265
|
+
readOnly,
|
|
266
|
+
onDropExisting,
|
|
267
|
+
onCreateRowExisting,
|
|
268
|
+
onRemoveWidget,
|
|
269
|
+
onResizeWidget,
|
|
270
|
+
onResizeRow,
|
|
271
|
+
onMoveRow,
|
|
272
|
+
renderRowEdgeAdd,
|
|
273
|
+
renderWidgetMenu,
|
|
274
|
+
renderWidgetConfigDialog
|
|
275
|
+
}) {
|
|
276
|
+
const dragActive = useDragLayer((monitor) => monitor.isDragging());
|
|
277
|
+
return /* @__PURE__ */ jsxs("div", { className: "flex w-full flex-col gap-4", children: [
|
|
278
|
+
rows.map((row, index) => /* @__PURE__ */ jsx(
|
|
279
|
+
SortableRow,
|
|
280
|
+
{
|
|
281
|
+
row,
|
|
282
|
+
index,
|
|
283
|
+
items,
|
|
284
|
+
widgets,
|
|
285
|
+
readOnly,
|
|
286
|
+
dragActive,
|
|
287
|
+
onDropExisting,
|
|
288
|
+
onRemoveWidget,
|
|
289
|
+
onResizeWidget,
|
|
290
|
+
onResizeRow,
|
|
291
|
+
onMoveRow,
|
|
292
|
+
renderRowEdgeAdd,
|
|
293
|
+
renderWidgetMenu,
|
|
294
|
+
renderWidgetConfigDialog
|
|
295
|
+
},
|
|
296
|
+
row.id
|
|
297
|
+
)),
|
|
298
|
+
!readOnly ? /* @__PURE__ */ jsx(NewRowDropZone, { onDropExisting: onCreateRowExisting }) : null
|
|
299
|
+
] });
|
|
300
|
+
}
|
|
301
|
+
function NewRowDropZone({
|
|
302
|
+
onDropExisting
|
|
303
|
+
}) {
|
|
304
|
+
const [{ isOver }, dropRef] = useDrop(() => ({
|
|
305
|
+
accept: ItemTypes.WIDGET,
|
|
306
|
+
drop: (item) => onDropExisting(item),
|
|
307
|
+
collect: (monitor) => ({ isOver: monitor.isOver() })
|
|
308
|
+
}));
|
|
309
|
+
return /* @__PURE__ */ jsx(
|
|
310
|
+
"div",
|
|
311
|
+
{
|
|
312
|
+
ref: (node) => {
|
|
313
|
+
dropRef(node);
|
|
314
|
+
},
|
|
315
|
+
className: cn(
|
|
316
|
+
"flex h-12 items-center justify-center rounded-lg border-2 border-dashed border-border text-xs text-muted-foreground transition-colors",
|
|
317
|
+
isOver && "border-primary bg-primary/5 text-primary"
|
|
318
|
+
),
|
|
319
|
+
children: "Drop here to start a new row"
|
|
320
|
+
}
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
function SortableRow({
|
|
324
|
+
row,
|
|
325
|
+
index,
|
|
326
|
+
items,
|
|
327
|
+
widgets,
|
|
328
|
+
readOnly,
|
|
329
|
+
dragActive,
|
|
330
|
+
onDropExisting,
|
|
331
|
+
onRemoveWidget,
|
|
332
|
+
onResizeWidget,
|
|
333
|
+
onResizeRow,
|
|
334
|
+
onMoveRow,
|
|
335
|
+
renderRowEdgeAdd,
|
|
336
|
+
renderWidgetMenu,
|
|
337
|
+
renderWidgetConfigDialog
|
|
338
|
+
}) {
|
|
339
|
+
const [{ isDragging }, dragRef, previewRef] = useDrag(
|
|
340
|
+
() => ({
|
|
341
|
+
type: ItemTypes.ROW,
|
|
342
|
+
item: { rowId: row.id, index },
|
|
343
|
+
canDrag: !readOnly,
|
|
344
|
+
collect: (monitor) => ({ isDragging: monitor.isDragging() })
|
|
345
|
+
}),
|
|
346
|
+
[row.id, index, readOnly]
|
|
347
|
+
);
|
|
348
|
+
const [, dropRef] = useDrop(
|
|
349
|
+
() => ({
|
|
350
|
+
accept: ItemTypes.ROW,
|
|
351
|
+
canDrop: () => !readOnly,
|
|
352
|
+
hover: (item) => {
|
|
353
|
+
if (readOnly || item.index === index) return;
|
|
354
|
+
onMoveRow(item.index, index);
|
|
355
|
+
item.index = index;
|
|
356
|
+
}
|
|
357
|
+
}),
|
|
358
|
+
[index, readOnly, onMoveRow]
|
|
359
|
+
);
|
|
360
|
+
const setRowRef = useCallback(
|
|
361
|
+
(node) => {
|
|
362
|
+
previewRef(node);
|
|
363
|
+
dropRef(node);
|
|
364
|
+
},
|
|
365
|
+
[previewRef, dropRef]
|
|
366
|
+
);
|
|
367
|
+
return /* @__PURE__ */ jsxs(
|
|
368
|
+
"div",
|
|
369
|
+
{
|
|
370
|
+
ref: setRowRef,
|
|
371
|
+
style: { height: row.heightPx },
|
|
372
|
+
className: cn(
|
|
373
|
+
"group/row relative flex items-stretch gap-2 rounded-lg bg-muted/30 p-2 transition-opacity",
|
|
374
|
+
isDragging && "opacity-40"
|
|
375
|
+
),
|
|
376
|
+
children: [
|
|
377
|
+
!readOnly ? /* @__PURE__ */ jsx(
|
|
378
|
+
"div",
|
|
379
|
+
{
|
|
380
|
+
ref: (node) => {
|
|
381
|
+
dragRef(node);
|
|
382
|
+
},
|
|
383
|
+
className: "flex w-6 shrink-0 cursor-grab items-center justify-center text-muted-foreground active:cursor-grabbing",
|
|
384
|
+
title: "Drag to reorder row",
|
|
385
|
+
children: /* @__PURE__ */ jsx(GripVerticalIcon, { className: "size-4" })
|
|
386
|
+
}
|
|
387
|
+
) : null,
|
|
388
|
+
!readOnly && row.widgetIds.length < MAX_ITEMS_PER_ROW ? /* @__PURE__ */ jsx(RowEdgeAddWidget, { rowId: row.id, edge: "start", render: renderRowEdgeAdd }) : null,
|
|
389
|
+
/* @__PURE__ */ jsx(
|
|
390
|
+
RowContent,
|
|
391
|
+
{
|
|
392
|
+
row,
|
|
393
|
+
items,
|
|
394
|
+
widgets,
|
|
395
|
+
readOnly,
|
|
396
|
+
dragActive,
|
|
397
|
+
onDropExisting,
|
|
398
|
+
onRemoveWidget,
|
|
399
|
+
onResizeWidget,
|
|
400
|
+
renderWidgetMenu,
|
|
401
|
+
renderWidgetConfigDialog
|
|
402
|
+
}
|
|
403
|
+
),
|
|
404
|
+
!readOnly && row.widgetIds.length < MAX_ITEMS_PER_ROW ? /* @__PURE__ */ jsx(RowEdgeAddWidget, { rowId: row.id, edge: "end", render: renderRowEdgeAdd }) : null,
|
|
405
|
+
!readOnly ? /* @__PURE__ */ jsx(RowResizeHandle, { rowId: row.id, heightPx: row.heightPx, onResize: onResizeRow }) : null
|
|
406
|
+
]
|
|
407
|
+
}
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
function RowEdgeAddWidget({
|
|
411
|
+
rowId,
|
|
412
|
+
edge,
|
|
413
|
+
render
|
|
414
|
+
}) {
|
|
415
|
+
if (!render) return null;
|
|
416
|
+
return /* @__PURE__ */ jsx(Fragment2, { children: render({ rowId, edge }) });
|
|
417
|
+
}
|
|
418
|
+
function RowContent({
|
|
419
|
+
row,
|
|
420
|
+
items,
|
|
421
|
+
widgets,
|
|
422
|
+
readOnly,
|
|
423
|
+
dragActive,
|
|
424
|
+
onDropExisting,
|
|
425
|
+
onRemoveWidget,
|
|
426
|
+
onResizeWidget,
|
|
427
|
+
renderWidgetMenu,
|
|
428
|
+
renderWidgetConfigDialog
|
|
429
|
+
}) {
|
|
430
|
+
const containerRef = useRef(null);
|
|
431
|
+
return /* @__PURE__ */ jsxs("div", { ref: containerRef, className: "flex h-full min-w-0 flex-1 items-stretch", children: [
|
|
432
|
+
!readOnly ? /* @__PURE__ */ jsx(
|
|
433
|
+
InsertionZone,
|
|
434
|
+
{
|
|
435
|
+
rowId: row.id,
|
|
436
|
+
index: 0,
|
|
437
|
+
widgetCount: row.widgetIds.length,
|
|
438
|
+
dragActive,
|
|
439
|
+
onDropExisting
|
|
440
|
+
}
|
|
441
|
+
) : /* @__PURE__ */ jsx(ZoneSpacer, {}),
|
|
442
|
+
row.widgetIds.map((widgetId, i) => {
|
|
443
|
+
const item = items[widgetId];
|
|
444
|
+
const widget = widgets[widgetId];
|
|
445
|
+
if (!item || !widget) return null;
|
|
446
|
+
return (
|
|
447
|
+
// A Fragment, not a wrapping div — PlacedWidget must be a direct
|
|
448
|
+
// flex child of the row container above, since its percentage
|
|
449
|
+
// width resolves against its immediate parent. A wrapper div here
|
|
450
|
+
// (with no definite width of its own) would make that percentage
|
|
451
|
+
// resolve against an indefinite size and collapse to content
|
|
452
|
+
// width instead.
|
|
453
|
+
/* @__PURE__ */ jsxs(Fragment, { children: [
|
|
454
|
+
/* @__PURE__ */ jsx(
|
|
455
|
+
PlacedWidget,
|
|
456
|
+
{
|
|
457
|
+
item,
|
|
458
|
+
widget,
|
|
459
|
+
rowId: row.id,
|
|
460
|
+
widgetCount: row.widgetIds.length,
|
|
461
|
+
readOnly,
|
|
462
|
+
onRemoveWidget,
|
|
463
|
+
renderWidgetMenu,
|
|
464
|
+
renderWidgetConfigDialog
|
|
465
|
+
}
|
|
466
|
+
),
|
|
467
|
+
!readOnly ? /* @__PURE__ */ jsx(
|
|
468
|
+
InsertionZone,
|
|
469
|
+
{
|
|
470
|
+
rowId: row.id,
|
|
471
|
+
index: i + 1,
|
|
472
|
+
widgetCount: row.widgetIds.length,
|
|
473
|
+
dragActive,
|
|
474
|
+
leftWidgetId: widgetId,
|
|
475
|
+
rightWidgetId: row.widgetIds[i + 1],
|
|
476
|
+
leftColSpan: item.colSpan,
|
|
477
|
+
containerRef,
|
|
478
|
+
onDropExisting,
|
|
479
|
+
onResizeWidget
|
|
480
|
+
}
|
|
481
|
+
) : /* @__PURE__ */ jsx(ZoneSpacer, {})
|
|
482
|
+
] }, widgetId)
|
|
483
|
+
);
|
|
484
|
+
})
|
|
485
|
+
] });
|
|
486
|
+
}
|
|
487
|
+
function ZoneSpacer() {
|
|
488
|
+
return /* @__PURE__ */ jsx("div", { style: { width: ZONE_WIDTH_PX }, className: "shrink-0" });
|
|
489
|
+
}
|
|
490
|
+
function InsertionZone({
|
|
491
|
+
rowId,
|
|
492
|
+
index,
|
|
493
|
+
widgetCount,
|
|
494
|
+
dragActive,
|
|
495
|
+
leftWidgetId,
|
|
496
|
+
rightWidgetId,
|
|
497
|
+
leftColSpan,
|
|
498
|
+
containerRef,
|
|
499
|
+
onDropExisting,
|
|
500
|
+
onResizeWidget
|
|
501
|
+
}) {
|
|
502
|
+
const [{ isOver, canDrop }, dropRef] = useDrop(
|
|
503
|
+
() => ({
|
|
504
|
+
accept: ItemTypes.WIDGET,
|
|
505
|
+
// A same-row reorder never grows the row, so it's always allowed —
|
|
506
|
+
// only a drop that would add a net-new member to an already-full row
|
|
507
|
+
// is rejected (mirrors the authoritative check in placeItemAt).
|
|
508
|
+
canDrop: (item) => {
|
|
509
|
+
if (widgetCount < MAX_ITEMS_PER_ROW) return true;
|
|
510
|
+
return item.fromRowId === rowId;
|
|
511
|
+
},
|
|
512
|
+
drop: (item) => onDropExisting(item, rowId, index),
|
|
513
|
+
collect: (monitor) => ({ isOver: monitor.isOver(), canDrop: monitor.canDrop() })
|
|
514
|
+
}),
|
|
515
|
+
[rowId, index, widgetCount, onDropExisting]
|
|
516
|
+
);
|
|
517
|
+
const isResizable = leftWidgetId !== void 0 && rightWidgetId !== void 0;
|
|
518
|
+
const handleMouseDown = useCallback(
|
|
519
|
+
(event) => {
|
|
520
|
+
if (leftWidgetId === void 0 || rightWidgetId === void 0 || leftColSpan === void 0 || !containerRef?.current || !onResizeWidget) {
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
event.preventDefault();
|
|
524
|
+
event.stopPropagation();
|
|
525
|
+
const containerWidth = containerRef.current.getBoundingClientRect().width;
|
|
526
|
+
if (!containerWidth) return;
|
|
527
|
+
const zonesWidthPx = (widgetCount + 1) * ZONE_WIDTH_PX;
|
|
528
|
+
const colWidthPx = (containerWidth - zonesWidthPx) / COLS;
|
|
529
|
+
const startX = event.clientX;
|
|
530
|
+
const startLeftColSpan = leftColSpan;
|
|
531
|
+
const resolvedOnResize = onResizeWidget;
|
|
532
|
+
const resolvedLeftId = leftWidgetId;
|
|
533
|
+
const resolvedRightId = rightWidgetId;
|
|
534
|
+
function handleMove(moveEvent) {
|
|
535
|
+
const deltaCols = Math.round((moveEvent.clientX - startX) / colWidthPx);
|
|
536
|
+
resolvedOnResize(resolvedLeftId, resolvedRightId, startLeftColSpan + deltaCols);
|
|
537
|
+
}
|
|
538
|
+
function handleUp() {
|
|
539
|
+
window.removeEventListener("mousemove", handleMove);
|
|
540
|
+
window.removeEventListener("mouseup", handleUp);
|
|
541
|
+
}
|
|
542
|
+
window.addEventListener("mousemove", handleMove);
|
|
543
|
+
window.addEventListener("mouseup", handleUp);
|
|
544
|
+
},
|
|
545
|
+
[leftWidgetId, rightWidgetId, leftColSpan, containerRef, widgetCount, onResizeWidget]
|
|
546
|
+
);
|
|
547
|
+
return /* @__PURE__ */ jsx(
|
|
548
|
+
"div",
|
|
549
|
+
{
|
|
550
|
+
ref: (node) => {
|
|
551
|
+
dropRef(node);
|
|
552
|
+
},
|
|
553
|
+
onMouseDown: handleMouseDown,
|
|
554
|
+
style: { width: ZONE_WIDTH_PX },
|
|
555
|
+
className: cn(
|
|
556
|
+
// Fixed footprint at all times — the "grows on hover" affordance
|
|
557
|
+
// comes from a scale transform on the inner pill, not a width
|
|
558
|
+
// change, so it can't perturb every other widget's calc()'d width
|
|
559
|
+
// mid-drag.
|
|
560
|
+
"flex h-full shrink-0 items-center justify-center",
|
|
561
|
+
isResizable && "cursor-ew-resize"
|
|
562
|
+
),
|
|
563
|
+
children: /* @__PURE__ */ jsx(
|
|
564
|
+
"div",
|
|
565
|
+
{
|
|
566
|
+
className: cn(
|
|
567
|
+
"h-full w-full rounded-full transition-all",
|
|
568
|
+
isOver && canDrop ? "scale-x-150 bg-primary/25 ring-2 ring-primary" : dragActive && canDrop ? "ring-dashed bg-muted-foreground/20 ring-1 ring-border" : isResizable ? "bg-transparent hover:bg-muted-foreground/20 hover:ring-1 hover:ring-border" : "bg-transparent"
|
|
569
|
+
)
|
|
570
|
+
}
|
|
571
|
+
)
|
|
572
|
+
}
|
|
573
|
+
);
|
|
574
|
+
}
|
|
575
|
+
function PlacedWidget({
|
|
576
|
+
item,
|
|
577
|
+
widget,
|
|
578
|
+
rowId,
|
|
579
|
+
widgetCount,
|
|
580
|
+
readOnly,
|
|
581
|
+
onRemoveWidget,
|
|
582
|
+
renderWidgetMenu,
|
|
583
|
+
renderWidgetConfigDialog
|
|
584
|
+
}) {
|
|
585
|
+
const zonesWidthPx = (widgetCount + 1) * ZONE_WIDTH_PX;
|
|
586
|
+
const widthCss = `calc((100% - ${zonesWidthPx}px) * ${item.colSpan} / ${COLS})`;
|
|
587
|
+
const [{ isDragging }, dragRef, previewRef] = useDrag(
|
|
588
|
+
() => ({
|
|
589
|
+
type: ItemTypes.WIDGET,
|
|
590
|
+
item: { widgetId: item.id, fromRowId: rowId },
|
|
591
|
+
canDrag: !readOnly,
|
|
592
|
+
collect: (monitor) => ({ isDragging: monitor.isDragging() })
|
|
593
|
+
}),
|
|
594
|
+
[item.id, rowId, readOnly]
|
|
595
|
+
);
|
|
596
|
+
const [configureOpen, setConfigureOpen] = useState(false);
|
|
597
|
+
return /* @__PURE__ */ jsxs(
|
|
598
|
+
"div",
|
|
599
|
+
{
|
|
600
|
+
ref: (node) => {
|
|
601
|
+
previewRef(node);
|
|
602
|
+
},
|
|
603
|
+
style: { width: widthCss },
|
|
604
|
+
className: cn(
|
|
605
|
+
"group relative h-full min-w-0 shrink-0 overflow-hidden transition-opacity",
|
|
606
|
+
isDragging && "opacity-40"
|
|
607
|
+
),
|
|
608
|
+
children: [
|
|
609
|
+
/* @__PURE__ */ jsx("div", { className: "h-full", children: widget.content }),
|
|
610
|
+
!readOnly ? /* @__PURE__ */ jsxs(Fragment2, { children: [
|
|
611
|
+
/* @__PURE__ */ jsx(
|
|
612
|
+
"span",
|
|
613
|
+
{
|
|
614
|
+
ref: (node) => {
|
|
615
|
+
dragRef(node);
|
|
616
|
+
},
|
|
617
|
+
className: "absolute top-2 left-2 z-10 cursor-grab rounded-md bg-background/80 p-1 text-muted-foreground opacity-0 backdrop-blur transition-opacity group-hover:opacity-100 focus-visible:opacity-100 active:cursor-grabbing",
|
|
618
|
+
title: "Drag to move",
|
|
619
|
+
children: /* @__PURE__ */ jsx(GripVerticalIcon, { className: "size-3.5" })
|
|
620
|
+
}
|
|
621
|
+
),
|
|
622
|
+
renderWidgetMenu?.(widget, {
|
|
623
|
+
onConfigure: () => setConfigureOpen(true),
|
|
624
|
+
onDelete: () => onRemoveWidget(widget.id, rowId)
|
|
625
|
+
}),
|
|
626
|
+
renderWidgetConfigDialog?.({
|
|
627
|
+
widget,
|
|
628
|
+
open: configureOpen,
|
|
629
|
+
onOpenChange: setConfigureOpen
|
|
630
|
+
})
|
|
631
|
+
] }) : null
|
|
632
|
+
]
|
|
633
|
+
}
|
|
634
|
+
);
|
|
635
|
+
}
|
|
636
|
+
function RowResizeHandle({
|
|
637
|
+
rowId,
|
|
638
|
+
heightPx,
|
|
639
|
+
onResize
|
|
640
|
+
}) {
|
|
641
|
+
const handleMouseDown = useCallback(
|
|
642
|
+
(event) => {
|
|
643
|
+
event.preventDefault();
|
|
644
|
+
event.stopPropagation();
|
|
645
|
+
const startY = event.clientY;
|
|
646
|
+
const startHeight = heightPx;
|
|
647
|
+
function handleMove(moveEvent) {
|
|
648
|
+
const nextHeight = Math.min(
|
|
649
|
+
MAX_ROW_HEIGHT,
|
|
650
|
+
Math.max(MIN_ROW_HEIGHT, startHeight + (moveEvent.clientY - startY))
|
|
651
|
+
);
|
|
652
|
+
onResize(rowId, nextHeight);
|
|
653
|
+
}
|
|
654
|
+
function handleUp() {
|
|
655
|
+
window.removeEventListener("mousemove", handleMove);
|
|
656
|
+
window.removeEventListener("mouseup", handleUp);
|
|
657
|
+
}
|
|
658
|
+
window.addEventListener("mousemove", handleMove);
|
|
659
|
+
window.addEventListener("mouseup", handleUp);
|
|
660
|
+
},
|
|
661
|
+
[rowId, heightPx, onResize]
|
|
662
|
+
);
|
|
663
|
+
return /* @__PURE__ */ jsx(
|
|
664
|
+
"div",
|
|
665
|
+
{
|
|
666
|
+
onMouseDown: handleMouseDown,
|
|
667
|
+
className: "absolute inset-x-6 -bottom-2 flex h-3 cursor-ns-resize items-center justify-center",
|
|
668
|
+
title: "Drag to resize row height",
|
|
669
|
+
children: /* @__PURE__ */ jsx("div", { className: "h-1 w-10 rounded-full bg-border" })
|
|
670
|
+
}
|
|
671
|
+
);
|
|
672
|
+
}
|
|
673
|
+
export {
|
|
674
|
+
COLS,
|
|
675
|
+
DEFAULT_ROW_HEIGHT,
|
|
676
|
+
DashboardGrid,
|
|
677
|
+
MAX_ITEMS_PER_ROW,
|
|
678
|
+
MAX_ROW_HEIGHT,
|
|
679
|
+
MIN_COL_SPAN,
|
|
680
|
+
MIN_ROW_HEIGHT,
|
|
681
|
+
ZONE_WIDTH_PX,
|
|
682
|
+
canPlaceInRow,
|
|
683
|
+
moveRow,
|
|
684
|
+
placeItemAt,
|
|
685
|
+
placeItemInNewRow,
|
|
686
|
+
rebalanceRow,
|
|
687
|
+
removeItemFromRow,
|
|
688
|
+
resizeItemPair,
|
|
689
|
+
resizeRow
|
|
690
|
+
};
|
|
691
|
+
//# sourceMappingURL=index.js.map
|