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