mantine-gantt 0.2.0 → 0.3.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 +48 -18
  3. package/dist/cjs/Gantt/DependencyLinks.cjs.map +1 -1
  4. package/dist/cjs/Gantt/Gantt.cjs +139 -52
  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 +13 -8
  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 +54 -53
  16. package/dist/cjs/Gantt/use-gantt-drag.cjs.map +1 -1
  17. package/dist/cjs/Gantt/utils.cjs +224 -11
  18. package/dist/cjs/Gantt/utils.cjs.map +1 -1
  19. package/dist/cjs/index.cjs +1 -0
  20. package/dist/cjs/index.cjs.map +1 -1
  21. package/dist/esm/Gantt/DependencyLinks.mjs +49 -19
  22. package/dist/esm/Gantt/DependencyLinks.mjs.map +1 -1
  23. package/dist/esm/Gantt/Gantt.mjs +143 -56
  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 +13 -8
  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 +55 -54
  35. package/dist/esm/Gantt/use-gantt-drag.mjs.map +1 -1
  36. package/dist/esm/Gantt/utils.mjs +219 -12
  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 +9 -3
  42. package/dist/types/Gantt/TaskBar.d.ts +5 -1
  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 +66 -8
  48. package/dist/types/Gantt/use-gantt-drag.d.ts +2 -1
  49. package/dist/types/Gantt/utils.d.ts +41 -2
  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,30 @@ 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
24
33
  };
25
34
  const varsResolver = core.createVarsResolver(
26
- (_, { columnWidth, rowHeight, taskListWidth }) => ({
35
+ (theme, { columnWidth, rowHeight, taskListWidth, columns, criticalPathColor }) => ({
27
36
  root: {
28
37
  "--gantt-column-width": `${columnWidth}px`,
29
38
  "--gantt-row-height": `${rowHeight}px`,
30
39
  "--gantt-header-height": "50px",
31
- "--gantt-task-list-width": `${taskListWidth}px`
40
+ "--gantt-task-list-width": `${taskListWidth ?? autoTaskListWidth(columns)}px`,
41
+ "--gantt-critical-color": core.getThemeColor(criticalPathColor, theme)
32
42
  }
33
43
  })
34
44
  );
@@ -42,19 +52,27 @@ const Gantt = core.factory((_props, ref) => {
42
52
  unstyled,
43
53
  vars,
44
54
  attributes,
45
- tasks: initialTasks,
55
+ tasks: tasksProp,
56
+ defaultTasks,
57
+ onTasksChange,
46
58
  columns,
47
59
  onTaskUpdate,
48
60
  onTaskClick,
49
61
  onLinkCreate,
50
- columnWidth,
51
- rowHeight,
62
+ columnWidth = 40,
63
+ rowHeight = 44,
52
64
  taskListWidth,
53
65
  showTitle,
54
66
  showTodayMarker,
55
67
  startDate,
56
68
  endDate,
57
- viewMode,
69
+ viewMode = "day",
70
+ weekStart = 1,
71
+ highlightCriticalPath,
72
+ criticalPathColor,
73
+ showBaselines,
74
+ defaultExpandedIds,
75
+ onToggleExpand,
58
76
  ...others
59
77
  } = props;
60
78
  const getStyles = core.useStyles({
@@ -80,19 +98,54 @@ const Gantt = core.factory((_props, ref) => {
80
98
  // Each day is 1/2nd, min 14px
81
99
  case "day":
82
100
  default:
83
- return columnWidth;
101
+ return Math.max(columnWidth, 1);
84
102
  }
85
103
  }, [viewMode, columnWidth]);
86
- const [tasks, setTasks] = React.useState(initialTasks);
104
+ const [tasks, setTasks] = hooks.useUncontrolled({
105
+ value: tasksProp,
106
+ defaultValue: defaultTasks,
107
+ finalValue: [],
108
+ onChange: onTasksChange
109
+ });
110
+ const [collapsedIds, setCollapsedIds] = React.useState(() => {
111
+ if (!defaultExpandedIds) {
112
+ return /* @__PURE__ */ new Set();
113
+ }
114
+ const expanded = new Set(defaultExpandedIds);
115
+ return new Set(
116
+ utils.buildTaskTree(tasksProp ?? defaultTasks ?? [], /* @__PURE__ */ new Set()).filter((row) => row.hasChildren && !expanded.has(row.task.id)).map((row) => row.task.id)
117
+ );
118
+ });
119
+ const toggleExpand = React.useCallback(
120
+ (taskId) => {
121
+ onToggleExpand?.(taskId, collapsedIds.has(taskId));
122
+ setCollapsedIds((prev) => {
123
+ const next = new Set(prev);
124
+ if (next.has(taskId)) {
125
+ next.delete(taskId);
126
+ } else {
127
+ next.add(taskId);
128
+ }
129
+ return next;
130
+ });
131
+ },
132
+ [onToggleExpand, collapsedIds]
133
+ );
134
+ const rows = React.useMemo(() => utils.buildTaskTree(tasks, collapsedIds), [tasks, collapsedIds]);
135
+ const criticalIds = React.useMemo(
136
+ () => highlightCriticalPath ? utils.getCriticalPath(tasks) : EMPTY_CRITICAL,
137
+ [highlightCriticalPath, tasks]
138
+ );
87
139
  const [announcement, setAnnouncement] = React.useState("");
88
- const [viewportWidth, setViewportWidth] = React.useState(0);
140
+ const [viewport, setViewport] = React.useState({ width: 0, height: 0 });
141
+ const [scrollTop, setScrollTop] = React.useState(0);
89
142
  const timelineBodyRef = React.useRef(null);
90
143
  const taskListBodyRef = React.useRef(null);
91
144
  const timelineHeaderRef = React.useRef(null);
92
145
  const timelineContentRef = React.useRef(null);
93
146
  const drag = useGanttDrag.useGanttDrag({
94
147
  tasks,
95
- setTasks,
148
+ commitTasks: setTasks,
96
149
  columnWidth: effectiveColumnWidth,
97
150
  bodyRef: timelineBodyRef,
98
151
  contentRef: timelineContentRef,
@@ -122,10 +175,12 @@ const Gantt = core.factory((_props, ref) => {
122
175
  }
123
176
  }
124
177
  }
125
- const fillDays = effectiveColumnWidth > 0 ? Math.ceil(viewportWidth / effectiveColumnWidth) : 0;
178
+ const fillDays = effectiveColumnWidth > 0 ? Math.ceil(viewport.width / effectiveColumnWidth) : 0;
126
179
  const totalDays = Math.max(bounds.end.diff(bounds.start, "day") + 1, fillDays);
127
180
  const displayEnd = bounds.start.add(totalDays - 1, "day");
128
181
  const timelineWidth = totalDays * effectiveColumnWidth;
182
+ const [firstRow, lastRow] = utils.visibleRowRange(scrollTop, viewport.height, rowHeight, rows.length);
183
+ const visibleRows = rows.slice(firstRow, lastRow);
129
184
  const handleTimelineScroll = React.useCallback(() => {
130
185
  if (timelineBodyRef.current && taskListBodyRef.current) {
131
186
  taskListBodyRef.current.scrollTop = timelineBodyRef.current.scrollTop;
@@ -133,18 +188,20 @@ const Gantt = core.factory((_props, ref) => {
133
188
  if (timelineBodyRef.current && timelineHeaderRef.current) {
134
189
  timelineHeaderRef.current.scrollLeft = timelineBodyRef.current.scrollLeft;
135
190
  }
191
+ setScrollTop(timelineBodyRef.current?.scrollTop ?? 0);
136
192
  }, []);
137
193
  React.useEffect(() => {
138
194
  const node = timelineBodyRef.current;
139
195
  if (!node) {
140
196
  return void 0;
141
197
  }
142
- setViewportWidth(node.clientWidth);
198
+ setViewport({ width: node.clientWidth, height: node.clientHeight });
143
199
  if (typeof ResizeObserver === "undefined") {
144
200
  return void 0;
145
201
  }
146
202
  const observer = new ResizeObserver((entries) => {
147
- setViewportWidth(entries[0].contentRect.width);
203
+ const { width, height } = entries[0].contentRect;
204
+ setViewport({ width, height });
148
205
  });
149
206
  observer.observe(node);
150
207
  return () => observer.disconnect();
@@ -201,26 +258,30 @@ const Gantt = core.factory((_props, ref) => {
201
258
  if (active?.type !== "link" || !active.linkCursor) {
202
259
  return null;
203
260
  }
204
- const srcIndex = tasks.findIndex((t) => t.id === active.taskId);
261
+ const srcIndex = rows.findIndex((r) => r.task.id === active.taskId);
205
262
  if (srcIndex === -1) {
206
263
  return null;
207
264
  }
208
- const src = tasks[srcIndex];
265
+ const src = rows[srcIndex];
209
266
  const x1 = utils.dateToPixel(src.startDate, bounds.start, effectiveColumnWidth) + utils.durationToPixels(src.duration, effectiveColumnWidth);
210
267
  const y1 = srcIndex * rowHeight + rowHeight / 2;
211
268
  return { x1, y1, x2: active.linkCursor.x, y2: active.linkCursor.y };
212
- }, [active, tasks, bounds.start, effectiveColumnWidth, rowHeight]);
269
+ }, [active, rows, bounds.start, effectiveColumnWidth, rowHeight]);
213
270
  const today = dayjs();
214
271
  const todayPosition = utils.dateToPixel(today, bounds.start, effectiveColumnWidth);
215
272
  const showTodayLine = showTodayMarker && today.isAfter(bounds.start) && today.isBefore(displayEnd);
216
273
  return /* @__PURE__ */ React.createElement(core.Box, { ref, ...getStyles("root"), ...others }, /* @__PURE__ */ React.createElement(
217
274
  TaskList.TaskList,
218
275
  {
219
- tasks,
276
+ rows: visibleRows,
220
277
  columns,
221
278
  getStyles,
222
279
  bodyRef: taskListBodyRef,
223
- onScroll: handleTaskListScroll
280
+ onScroll: handleTaskListScroll,
281
+ collapsedIds,
282
+ onToggleExpand: toggleExpand,
283
+ offsetTop: firstRow * rowHeight,
284
+ contentHeight: rows.length * rowHeight
224
285
  }
225
286
  ), /* @__PURE__ */ React.createElement("div", { ...getStyles("timeline") }, /* @__PURE__ */ React.createElement("div", { ...getStyles("timelineHeader"), ref: timelineHeaderRef }, /* @__PURE__ */ React.createElement(
226
287
  TimelineHeader.TimelineHeader,
@@ -230,7 +291,8 @@ const Gantt = core.factory((_props, ref) => {
230
291
  columnWidth: effectiveColumnWidth,
231
292
  getStyles,
232
293
  totalWidth: timelineWidth,
233
- viewMode
294
+ viewMode,
295
+ weekStart
234
296
  }
235
297
  )), /* @__PURE__ */ React.createElement(
236
298
  "div",
@@ -248,7 +310,7 @@ const Gantt = core.factory((_props, ref) => {
248
310
  {
249
311
  ...getStyles("timelineContent"),
250
312
  ref: timelineContentRef,
251
- style: { width: timelineWidth, height: tasks.length * rowHeight }
313
+ style: { width: timelineWidth, height: rows.length * rowHeight }
252
314
  },
253
315
  /* @__PURE__ */ React.createElement(
254
316
  TimelineGrid.TimelineGrid,
@@ -256,44 +318,66 @@ const Gantt = core.factory((_props, ref) => {
256
318
  startDate: bounds.start,
257
319
  endDate: displayEnd,
258
320
  columnWidth: effectiveColumnWidth,
259
- rowCount: tasks.length,
321
+ rowCount: rows.length,
260
322
  rowHeight,
261
323
  getStyles,
262
- viewMode
324
+ viewMode,
325
+ weekStart
263
326
  }
264
327
  ),
265
328
  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,
329
+ visibleRows.map((row, i) => {
330
+ const index = firstRow + i;
331
+ const task = utils.getEffectiveTask(row);
332
+ return /* @__PURE__ */ React.createElement(
333
+ "div",
276
334
  {
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
- )),
335
+ key: task.id,
336
+ ...getStyles("timelineRow"),
337
+ title: showTitle ? task.label : void 0,
338
+ style: { top: index * rowHeight }
339
+ },
340
+ /* @__PURE__ */ React.createElement(
341
+ TaskBar.TaskBar,
342
+ {
343
+ key: `taskbar-${task.id}`,
344
+ task,
345
+ startDate: bounds.start,
346
+ columnWidth: effectiveColumnWidth,
347
+ getStyles,
348
+ isSummary: row.hasChildren,
349
+ isDragging: active?.taskId === task.id && active.type !== "link",
350
+ isLinkTarget: active?.type === "link" && active.dropTargetId === task.id,
351
+ dragType: active?.taskId === task.id ? active.type : null,
352
+ dragDeltaX: active?.taskId === task.id ? active.deltaX : 0,
353
+ startDrag: drag.startDrag,
354
+ didDrag: drag.didDrag,
355
+ nudge: drag.nudge,
356
+ onClick: () => onTaskClick?.(task),
357
+ isCritical: criticalIds.has(task.id)
358
+ }
359
+ ),
360
+ showBaselines && task.baseline && /* @__PURE__ */ React.createElement(
361
+ "div",
362
+ {
363
+ ...getStyles("baselineBar"),
364
+ "data-task-id": task.id,
365
+ style: {
366
+ left: utils.dateToPixel(
367
+ task.baseline.startDate,
368
+ bounds.start,
369
+ effectiveColumnWidth
370
+ ),
371
+ width: utils.durationToPixels(task.baseline.duration, effectiveColumnWidth)
372
+ }
373
+ }
374
+ )
375
+ );
376
+ }),
293
377
  /* @__PURE__ */ React.createElement(
294
378
  DependencyLinks.DependencyLinks,
295
379
  {
296
- tasks,
380
+ rows,
297
381
  startDate: bounds.start,
298
382
  columnWidth: effectiveColumnWidth,
299
383
  rowHeight,
@@ -301,7 +385,10 @@ const Gantt = core.factory((_props, ref) => {
301
385
  activeDragId: active && active.type !== "link" ? active.taskId : null,
302
386
  activeDragType: active && active.type !== "link" ? active.type : null,
303
387
  dragDelta: active && active.type !== "link" ? active.deltaX : 0,
304
- linkPreview
388
+ linkPreview,
389
+ criticalIds,
390
+ firstRow,
391
+ lastRow
305
392
  }
306
393
  )
307
394
  )