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