mantine-gantt 0.2.0 → 0.4.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.
Files changed (52) hide show
  1. package/README.md +57 -17
  2. package/dist/cjs/Gantt/DependencyLinks.cjs +104 -34
  3. package/dist/cjs/Gantt/DependencyLinks.cjs.map +1 -1
  4. package/dist/cjs/Gantt/Gantt.cjs +207 -76
  5. package/dist/cjs/Gantt/Gantt.cjs.map +1 -1
  6. package/dist/cjs/Gantt/Gantt.module.css.cjs +1 -1
  7. package/dist/cjs/Gantt/TaskBar.cjs +35 -19
  8. package/dist/cjs/Gantt/TaskBar.cjs.map +1 -1
  9. package/dist/cjs/Gantt/TaskList.cjs +57 -14
  10. package/dist/cjs/Gantt/TaskList.cjs.map +1 -1
  11. package/dist/cjs/Gantt/TimelineGrid.cjs +4 -3
  12. package/dist/cjs/Gantt/TimelineGrid.cjs.map +1 -1
  13. package/dist/cjs/Gantt/TimelineHeader.cjs +6 -2
  14. package/dist/cjs/Gantt/TimelineHeader.cjs.map +1 -1
  15. package/dist/cjs/Gantt/use-gantt-drag.cjs +96 -53
  16. package/dist/cjs/Gantt/use-gantt-drag.cjs.map +1 -1
  17. package/dist/cjs/Gantt/utils.cjs +305 -12
  18. package/dist/cjs/Gantt/utils.cjs.map +1 -1
  19. package/dist/cjs/index.cjs +3 -0
  20. package/dist/cjs/index.cjs.map +1 -1
  21. package/dist/esm/Gantt/DependencyLinks.mjs +106 -36
  22. package/dist/esm/Gantt/DependencyLinks.mjs.map +1 -1
  23. package/dist/esm/Gantt/Gantt.mjs +211 -80
  24. package/dist/esm/Gantt/Gantt.mjs.map +1 -1
  25. package/dist/esm/Gantt/Gantt.module.css.mjs +1 -1
  26. package/dist/esm/Gantt/TaskBar.mjs +37 -21
  27. package/dist/esm/Gantt/TaskBar.mjs.map +1 -1
  28. package/dist/esm/Gantt/TaskList.mjs +58 -16
  29. package/dist/esm/Gantt/TaskList.mjs.map +1 -1
  30. package/dist/esm/Gantt/TimelineGrid.mjs +4 -3
  31. package/dist/esm/Gantt/TimelineGrid.mjs.map +1 -1
  32. package/dist/esm/Gantt/TimelineHeader.mjs +6 -2
  33. package/dist/esm/Gantt/TimelineHeader.mjs.map +1 -1
  34. package/dist/esm/Gantt/use-gantt-drag.mjs +98 -55
  35. package/dist/esm/Gantt/use-gantt-drag.mjs.map +1 -1
  36. package/dist/esm/Gantt/utils.mjs +298 -13
  37. package/dist/esm/Gantt/utils.mjs.map +1 -1
  38. package/dist/esm/index.mjs +1 -1
  39. package/dist/styles.css +5 -1
  40. package/dist/styles.layer.css +5 -1
  41. package/dist/types/Gantt/DependencyLinks.d.ts +11 -3
  42. package/dist/types/Gantt/TaskBar.d.ts +8 -2
  43. package/dist/types/Gantt/TaskList.d.ts +11 -3
  44. package/dist/types/Gantt/TimelineGrid.d.ts +2 -1
  45. package/dist/types/Gantt/TimelineHeader.d.ts +2 -1
  46. package/dist/types/Gantt/index.d.ts +1 -1
  47. package/dist/types/Gantt/types.d.ts +80 -8
  48. package/dist/types/Gantt/use-gantt-drag.d.ts +8 -4
  49. package/dist/types/Gantt/utils.d.ts +58 -3
  50. package/dist/types/index.d.mts +1 -1
  51. package/dist/types/index.d.ts +1 -1
  52. package/package.json +1 -1
@@ -4,6 +4,7 @@
4
4
  var dayjs = require('dayjs');
5
5
  var React = require('react');
6
6
  var core = require('@mantine/core');
7
+ var hooks = require('@mantine/hooks');
7
8
  var DependencyLinks = require('./DependencyLinks.cjs');
8
9
  var TaskBar = require('./TaskBar.cjs');
9
10
  var TaskList = require('./TaskList.cjs');
@@ -14,21 +15,31 @@ var utils = require('./utils.cjs');
14
15
  var Gantt_module = require('./Gantt.module.css.cjs');
15
16
 
16
17
  const DRAG_BUFFER_DAYS = 30;
18
+ const EMPTY_CRITICAL = /* @__PURE__ */ new Set();
19
+ const FLEX_COLUMN_WIDTH = 200;
20
+ function autoTaskListWidth(columns = TaskList.defaultColumns) {
21
+ return columns.reduce((sum, col) => sum + (col.width ?? FLEX_COLUMN_WIDTH), 0);
22
+ }
17
23
  const defaultProps = {
18
24
  columnWidth: 40,
19
25
  rowHeight: 44,
20
- taskListWidth: 320,
21
26
  showTitle: false,
22
27
  showTodayMarker: true,
23
- viewMode: "day"
28
+ viewMode: "day",
29
+ weekStart: 1,
30
+ highlightCriticalPath: false,
31
+ criticalPathColor: "red",
32
+ showBaselines: true,
33
+ autoSchedule: false
24
34
  };
25
35
  const varsResolver = core.createVarsResolver(
26
- (_, { columnWidth, rowHeight, taskListWidth }) => ({
36
+ (theme, { columnWidth, rowHeight, taskListWidth, columns, criticalPathColor }) => ({
27
37
  root: {
28
38
  "--gantt-column-width": `${columnWidth}px`,
29
39
  "--gantt-row-height": `${rowHeight}px`,
30
40
  "--gantt-header-height": "50px",
31
- "--gantt-task-list-width": `${taskListWidth}px`
41
+ "--gantt-task-list-width": `${taskListWidth ?? autoTaskListWidth(columns)}px`,
42
+ "--gantt-critical-color": core.getThemeColor(criticalPathColor, theme)
32
43
  }
33
44
  })
34
45
  );
@@ -42,19 +53,29 @@ const Gantt = core.factory((_props, ref) => {
42
53
  unstyled,
43
54
  vars,
44
55
  attributes,
45
- tasks: initialTasks,
56
+ tasks: tasksProp,
57
+ defaultTasks,
58
+ onTasksChange,
46
59
  columns,
47
60
  onTaskUpdate,
48
61
  onTaskClick,
49
62
  onLinkCreate,
50
- columnWidth,
51
- rowHeight,
63
+ onLinkDelete,
64
+ columnWidth = 40,
65
+ rowHeight = 44,
52
66
  taskListWidth,
53
67
  showTitle,
54
68
  showTodayMarker,
69
+ autoSchedule,
55
70
  startDate,
56
71
  endDate,
57
- viewMode,
72
+ viewMode = "day",
73
+ weekStart = 1,
74
+ highlightCriticalPath,
75
+ criticalPathColor,
76
+ showBaselines,
77
+ defaultExpandedIds,
78
+ onToggleExpand,
58
79
  ...others
59
80
  } = props;
60
81
  const getStyles = core.useStyles({
@@ -80,58 +101,134 @@ const Gantt = core.factory((_props, ref) => {
80
101
  // Each day is 1/2nd, min 14px
81
102
  case "day":
82
103
  default:
83
- return columnWidth;
104
+ return Math.max(columnWidth, 1);
84
105
  }
85
106
  }, [viewMode, columnWidth]);
86
- const [tasks, setTasks] = React.useState(initialTasks);
107
+ const [tasks, setTasks] = hooks.useUncontrolled({
108
+ value: tasksProp,
109
+ defaultValue: defaultTasks,
110
+ finalValue: [],
111
+ onChange: onTasksChange
112
+ });
113
+ const [collapsedIds, setCollapsedIds] = React.useState(() => {
114
+ if (!defaultExpandedIds) {
115
+ return /* @__PURE__ */ new Set();
116
+ }
117
+ const expanded = new Set(defaultExpandedIds);
118
+ return new Set(
119
+ utils.buildTaskTree(tasksProp ?? defaultTasks ?? [], /* @__PURE__ */ new Set()).filter((row) => row.hasChildren && !expanded.has(row.task.id)).map((row) => row.task.id)
120
+ );
121
+ });
122
+ const toggleExpand = React.useCallback(
123
+ (taskId) => {
124
+ onToggleExpand?.(taskId, collapsedIds.has(taskId));
125
+ setCollapsedIds((prev) => {
126
+ const next = new Set(prev);
127
+ if (next.has(taskId)) {
128
+ next.delete(taskId);
129
+ } else {
130
+ next.add(taskId);
131
+ }
132
+ return next;
133
+ });
134
+ },
135
+ [onToggleExpand, collapsedIds]
136
+ );
137
+ const rows = React.useMemo(() => utils.buildTaskTree(tasks, collapsedIds), [tasks, collapsedIds]);
138
+ const criticalIds = React.useMemo(
139
+ () => highlightCriticalPath ? utils.getCriticalPath(tasks) : EMPTY_CRITICAL,
140
+ [highlightCriticalPath, tasks]
141
+ );
87
142
  const [announcement, setAnnouncement] = React.useState("");
88
- const [viewportWidth, setViewportWidth] = React.useState(0);
143
+ const [viewport, setViewport] = React.useState({ width: 0, height: 0 });
144
+ const [scrollTop, setScrollTop] = React.useState(0);
89
145
  const timelineBodyRef = React.useRef(null);
90
146
  const taskListBodyRef = React.useRef(null);
91
147
  const timelineHeaderRef = React.useRef(null);
92
148
  const timelineContentRef = React.useRef(null);
93
149
  const drag = useGanttDrag.useGanttDrag({
94
150
  tasks,
95
- setTasks,
151
+ commitTasks: setTasks,
96
152
  columnWidth: effectiveColumnWidth,
97
153
  bodyRef: timelineBodyRef,
98
154
  contentRef: timelineContentRef,
99
155
  onTaskUpdate,
100
156
  onLinkCreate,
157
+ onLinkDelete,
158
+ autoSchedule,
101
159
  announce: setAnnouncement
102
160
  });
103
161
  const active = drag.state;
162
+ const handleLinkDelete = React.useCallback(
163
+ (fromTaskId, toTaskId) => {
164
+ setTasks(
165
+ tasks.map(
166
+ (task) => task.id === toTaskId ? { ...task, dependencies: (task.dependencies ?? []).filter((id) => id !== fromTaskId) } : task
167
+ )
168
+ );
169
+ onLinkDelete?.(fromTaskId, toTaskId);
170
+ },
171
+ [tasks, setTasks, onLinkDelete]
172
+ );
104
173
  const calculatedBounds = React.useMemo(
105
174
  () => utils.calculateTimelineBounds(tasks, startDate, endDate),
106
175
  [tasks, startDate, endDate]
107
176
  );
108
- const stableBoundsRef = React.useRef(calculatedBounds);
109
- if (!active) {
110
- stableBoundsRef.current = calculatedBounds;
111
- }
112
- let bounds = stableBoundsRef.current;
113
- if (active && active.type !== "link") {
177
+ const bounds = React.useMemo(() => {
178
+ if (!active || active.type === "link") {
179
+ return calculatedBounds;
180
+ }
114
181
  const task = tasks.find((t) => t.id === active.taskId);
115
- if (task) {
116
- const startDay = dayjs(task.startDate).diff(bounds.start, "day");
117
- const barEndDay = startDay + task.duration + Math.round(active.deltaX / effectiveColumnWidth);
118
- const needed = bounds.start.add(barEndDay + DRAG_BUFFER_DAYS, "day");
119
- if (needed.isAfter(bounds.end)) {
120
- bounds = { start: bounds.start, end: needed };
121
- stableBoundsRef.current = bounds;
122
- }
182
+ if (!task) {
183
+ return calculatedBounds;
184
+ }
185
+ const startDay = dayjs(task.startDate).diff(calculatedBounds.start, "day");
186
+ const barEndDay = startDay + task.duration + Math.round(active.deltaX / effectiveColumnWidth);
187
+ const needed = calculatedBounds.start.add(barEndDay + DRAG_BUFFER_DAYS, "day");
188
+ if (!needed.isAfter(calculatedBounds.end)) {
189
+ return calculatedBounds;
123
190
  }
124
- }
125
- const fillDays = effectiveColumnWidth > 0 ? Math.ceil(viewportWidth / effectiveColumnWidth) : 0;
191
+ return { start: calculatedBounds.start, end: needed };
192
+ }, [calculatedBounds, active, tasks, effectiveColumnWidth]);
193
+ const fillDays = effectiveColumnWidth > 0 ? Math.ceil(viewport.width / effectiveColumnWidth) : 0;
126
194
  const totalDays = Math.max(bounds.end.diff(bounds.start, "day") + 1, fillDays);
127
195
  const displayEnd = bounds.start.add(totalDays - 1, "day");
128
196
  const timelineWidth = totalDays * effectiveColumnWidth;
197
+ const [firstRow, lastRow] = utils.visibleRowRange(scrollTop, viewport.height, rowHeight, rows.length);
198
+ const visibleRows = rows.slice(firstRow, lastRow);
199
+ const echoRef = React.useRef(null);
129
200
  const handleTimelineScroll = React.useCallback(() => {
130
- if (timelineBodyRef.current && taskListBodyRef.current) {
131
- taskListBodyRef.current.scrollTop = timelineBodyRef.current.scrollTop;
201
+ const body = timelineBodyRef.current;
202
+ if (!body) {
203
+ return;
204
+ }
205
+ const echo = echoRef.current;
206
+ if (echo?.source === "timeline" && echo.value === body.scrollTop) {
207
+ echoRef.current = null;
208
+ return;
132
209
  }
133
- if (timelineBodyRef.current && timelineHeaderRef.current) {
134
- timelineHeaderRef.current.scrollLeft = timelineBodyRef.current.scrollLeft;
210
+ if (taskListBodyRef.current) {
211
+ echoRef.current = { source: "timeline", value: body.scrollTop };
212
+ taskListBodyRef.current.scrollTop = body.scrollTop;
213
+ }
214
+ if (timelineHeaderRef.current) {
215
+ timelineHeaderRef.current.scrollLeft = body.scrollLeft;
216
+ }
217
+ setScrollTop(body.scrollTop);
218
+ }, []);
219
+ const handleTaskListScroll = React.useCallback(() => {
220
+ const list = taskListBodyRef.current;
221
+ if (!list) {
222
+ return;
223
+ }
224
+ const echo = echoRef.current;
225
+ if (echo?.source === "list" && echo.value === list.scrollTop) {
226
+ echoRef.current = null;
227
+ return;
228
+ }
229
+ if (timelineBodyRef.current) {
230
+ echoRef.current = { source: "list", value: list.scrollTop };
231
+ timelineBodyRef.current.scrollTop = list.scrollTop;
135
232
  }
136
233
  }, []);
137
234
  React.useEffect(() => {
@@ -139,21 +236,23 @@ const Gantt = core.factory((_props, ref) => {
139
236
  if (!node) {
140
237
  return void 0;
141
238
  }
142
- setViewportWidth(node.clientWidth);
239
+ setViewport({ width: node.clientWidth, height: node.clientHeight });
143
240
  if (typeof ResizeObserver === "undefined") {
144
241
  return void 0;
145
242
  }
146
243
  const observer = new ResizeObserver((entries) => {
147
- setViewportWidth(entries[0].contentRect.width);
244
+ const { width, height } = entries[0].contentRect;
245
+ setViewport({ width, height });
148
246
  });
149
247
  observer.observe(node);
150
248
  return () => observer.disconnect();
151
249
  }, []);
152
- const handleTaskListScroll = React.useCallback(() => {
153
- if (timelineBodyRef.current && taskListBodyRef.current) {
154
- timelineBodyRef.current.scrollTop = taskListBodyRef.current.scrollTop;
155
- }
156
- }, []);
250
+ const handleTaskClick = React.useCallback(
251
+ (task) => {
252
+ onTaskClick?.(task);
253
+ },
254
+ [onTaskClick]
255
+ );
157
256
  const panRef = React.useRef(null);
158
257
  const handlePanStart = React.useCallback((e) => {
159
258
  const body = timelineBodyRef.current;
@@ -201,26 +300,30 @@ const Gantt = core.factory((_props, ref) => {
201
300
  if (active?.type !== "link" || !active.linkCursor) {
202
301
  return null;
203
302
  }
204
- const srcIndex = tasks.findIndex((t) => t.id === active.taskId);
303
+ const srcIndex = rows.findIndex((r) => r.task.id === active.taskId);
205
304
  if (srcIndex === -1) {
206
305
  return null;
207
306
  }
208
- const src = tasks[srcIndex];
307
+ const src = rows[srcIndex];
209
308
  const x1 = utils.dateToPixel(src.startDate, bounds.start, effectiveColumnWidth) + utils.durationToPixels(src.duration, effectiveColumnWidth);
210
309
  const y1 = srcIndex * rowHeight + rowHeight / 2;
211
310
  return { x1, y1, x2: active.linkCursor.x, y2: active.linkCursor.y };
212
- }, [active, tasks, bounds.start, effectiveColumnWidth, rowHeight]);
311
+ }, [active, rows, bounds.start, effectiveColumnWidth, rowHeight]);
213
312
  const today = dayjs();
214
313
  const todayPosition = utils.dateToPixel(today, bounds.start, effectiveColumnWidth);
215
314
  const showTodayLine = showTodayMarker && today.isAfter(bounds.start) && today.isBefore(displayEnd);
216
315
  return /* @__PURE__ */ React.createElement(core.Box, { ref, ...getStyles("root"), ...others }, /* @__PURE__ */ React.createElement(
217
316
  TaskList.TaskList,
218
317
  {
219
- tasks,
318
+ rows: visibleRows,
220
319
  columns,
221
320
  getStyles,
222
321
  bodyRef: taskListBodyRef,
223
- onScroll: handleTaskListScroll
322
+ onScroll: handleTaskListScroll,
323
+ collapsedIds,
324
+ onToggleExpand: toggleExpand,
325
+ offsetTop: firstRow * rowHeight,
326
+ contentHeight: rows.length * rowHeight
224
327
  }
225
328
  ), /* @__PURE__ */ React.createElement("div", { ...getStyles("timeline") }, /* @__PURE__ */ React.createElement("div", { ...getStyles("timelineHeader"), ref: timelineHeaderRef }, /* @__PURE__ */ React.createElement(
226
329
  TimelineHeader.TimelineHeader,
@@ -230,7 +333,8 @@ const Gantt = core.factory((_props, ref) => {
230
333
  columnWidth: effectiveColumnWidth,
231
334
  getStyles,
232
335
  totalWidth: timelineWidth,
233
- viewMode
336
+ viewMode,
337
+ weekStart
234
338
  }
235
339
  )), /* @__PURE__ */ React.createElement(
236
340
  "div",
@@ -248,7 +352,7 @@ const Gantt = core.factory((_props, ref) => {
248
352
  {
249
353
  ...getStyles("timelineContent"),
250
354
  ref: timelineContentRef,
251
- style: { width: timelineWidth, height: tasks.length * rowHeight }
355
+ style: { width: timelineWidth, height: rows.length * rowHeight }
252
356
  },
253
357
  /* @__PURE__ */ React.createElement(
254
358
  TimelineGrid.TimelineGrid,
@@ -256,44 +360,67 @@ const Gantt = core.factory((_props, ref) => {
256
360
  startDate: bounds.start,
257
361
  endDate: displayEnd,
258
362
  columnWidth: effectiveColumnWidth,
259
- rowCount: tasks.length,
363
+ rowCount: rows.length,
260
364
  rowHeight,
261
365
  getStyles,
262
- viewMode
366
+ viewMode,
367
+ weekStart
263
368
  }
264
369
  ),
265
370
  showTodayLine && /* @__PURE__ */ React.createElement("div", { ...getStyles("todayLine", { style: { left: todayPosition } }) }),
266
- tasks.map((task, index) => /* @__PURE__ */ React.createElement(
267
- "div",
268
- {
269
- key: task.id,
270
- ...getStyles("timelineRow"),
271
- title: showTitle ? task.label : void 0,
272
- style: { top: index * rowHeight }
273
- },
274
- /* @__PURE__ */ React.createElement(
275
- TaskBar.TaskBar,
371
+ visibleRows.map((row, i) => {
372
+ const index = firstRow + i;
373
+ const task = utils.getEffectiveTask(row);
374
+ return /* @__PURE__ */ React.createElement(
375
+ "div",
276
376
  {
277
- key: `taskbar-${task.id}`,
278
- task,
279
- startDate: bounds.start,
280
- columnWidth: effectiveColumnWidth,
281
- getStyles,
282
- isDragging: active?.taskId === task.id && active.type !== "link",
283
- isLinkTarget: active?.type === "link" && active.dropTargetId === task.id,
284
- dragType: active?.taskId === task.id ? active.type : null,
285
- dragDeltaX: active?.taskId === task.id ? active.deltaX : 0,
286
- startDrag: drag.startDrag,
287
- didDrag: drag.didDrag,
288
- nudge: drag.nudge,
289
- onClick: () => onTaskClick?.(task)
290
- }
291
- )
292
- )),
377
+ key: task.id,
378
+ ...getStyles("timelineRow"),
379
+ title: showTitle ? task.label : void 0,
380
+ style: { top: index * rowHeight }
381
+ },
382
+ /* @__PURE__ */ React.createElement(
383
+ TaskBar.TaskBar,
384
+ {
385
+ key: `taskbar-${task.id}`,
386
+ task,
387
+ startDate: bounds.start,
388
+ columnWidth: effectiveColumnWidth,
389
+ getStyles,
390
+ isSummary: row.hasChildren,
391
+ isDragging: active?.taskId === task.id && active.type !== "link",
392
+ isLinkTarget: active?.type === "link" && active.dropTargetId === task.id,
393
+ dragType: active?.taskId === task.id ? active.type : null,
394
+ dragDeltaX: active?.taskId === task.id ? active.deltaX : 0,
395
+ startDrag: drag.startDrag,
396
+ didDrag: drag.didDrag,
397
+ nudge: drag.nudge,
398
+ onTaskClick: handleTaskClick,
399
+ isCritical: criticalIds.has(task.id),
400
+ showTooltip: showTitle
401
+ }
402
+ ),
403
+ showBaselines && task.baseline && /* @__PURE__ */ React.createElement(
404
+ "div",
405
+ {
406
+ ...getStyles("baselineBar"),
407
+ "data-task-id": task.id,
408
+ style: {
409
+ left: utils.dateToPixel(
410
+ task.baseline.startDate,
411
+ bounds.start,
412
+ effectiveColumnWidth
413
+ ),
414
+ width: utils.durationToPixels(task.baseline.duration, effectiveColumnWidth)
415
+ }
416
+ }
417
+ )
418
+ );
419
+ }),
293
420
  /* @__PURE__ */ React.createElement(
294
421
  DependencyLinks.DependencyLinks,
295
422
  {
296
- tasks,
423
+ rows,
297
424
  startDate: bounds.start,
298
425
  columnWidth: effectiveColumnWidth,
299
426
  rowHeight,
@@ -301,7 +428,11 @@ const Gantt = core.factory((_props, ref) => {
301
428
  activeDragId: active && active.type !== "link" ? active.taskId : null,
302
429
  activeDragType: active && active.type !== "link" ? active.type : null,
303
430
  dragDelta: active && active.type !== "link" ? active.deltaX : 0,
304
- linkPreview
431
+ linkPreview,
432
+ criticalIds,
433
+ firstRow,
434
+ lastRow,
435
+ onLinkClick: handleLinkDelete
305
436
  }
306
437
  )
307
438
  )