mantine-gantt 0.5.0 → 0.6.1

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 (47) hide show
  1. package/README.md +60 -17
  2. package/dist/cjs/Gantt/DependencyLinks.cjs +23 -25
  3. package/dist/cjs/Gantt/DependencyLinks.cjs.map +1 -1
  4. package/dist/cjs/Gantt/Gantt.cjs +97 -40
  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 +24 -20
  8. package/dist/cjs/Gantt/TaskBar.cjs.map +1 -1
  9. package/dist/cjs/Gantt/TaskList.cjs +32 -9
  10. package/dist/cjs/Gantt/TaskList.cjs.map +1 -1
  11. package/dist/cjs/Gantt/TimelineHeader.cjs +5 -4
  12. package/dist/cjs/Gantt/TimelineHeader.cjs.map +1 -1
  13. package/dist/cjs/Gantt/use-gantt-drag.cjs +64 -35
  14. package/dist/cjs/Gantt/use-gantt-drag.cjs.map +1 -1
  15. package/dist/cjs/Gantt/utils.cjs +144 -34
  16. package/dist/cjs/Gantt/utils.cjs.map +1 -1
  17. package/dist/cjs/index.cjs +3 -0
  18. package/dist/cjs/index.cjs.map +1 -1
  19. package/dist/esm/Gantt/DependencyLinks.mjs +24 -26
  20. package/dist/esm/Gantt/DependencyLinks.mjs.map +1 -1
  21. package/dist/esm/Gantt/Gantt.mjs +101 -44
  22. package/dist/esm/Gantt/Gantt.mjs.map +1 -1
  23. package/dist/esm/Gantt/Gantt.module.css.mjs +1 -1
  24. package/dist/esm/Gantt/TaskBar.mjs +25 -21
  25. package/dist/esm/Gantt/TaskBar.mjs.map +1 -1
  26. package/dist/esm/Gantt/TaskList.mjs +33 -10
  27. package/dist/esm/Gantt/TaskList.mjs.map +1 -1
  28. package/dist/esm/Gantt/TimelineHeader.mjs +6 -5
  29. package/dist/esm/Gantt/TimelineHeader.mjs.map +1 -1
  30. package/dist/esm/Gantt/use-gantt-drag.mjs +65 -36
  31. package/dist/esm/Gantt/use-gantt-drag.mjs.map +1 -1
  32. package/dist/esm/Gantt/utils.mjs +140 -35
  33. package/dist/esm/Gantt/utils.mjs.map +1 -1
  34. package/dist/esm/index.mjs +1 -1
  35. package/dist/styles.css +2 -2
  36. package/dist/styles.layer.css +2 -2
  37. package/dist/types/Gantt/DependencyLinks.d.ts +3 -3
  38. package/dist/types/Gantt/TaskBar.d.ts +10 -2
  39. package/dist/types/Gantt/TaskList.d.ts +4 -1
  40. package/dist/types/Gantt/TimelineHeader.d.ts +2 -1
  41. package/dist/types/Gantt/index.d.ts +2 -2
  42. package/dist/types/Gantt/types.d.ts +83 -10
  43. package/dist/types/Gantt/use-gantt-drag.d.ts +4 -2
  44. package/dist/types/Gantt/utils.d.ts +39 -11
  45. package/dist/types/index.d.mts +2 -2
  46. package/dist/types/index.d.ts +2 -2
  47. package/package.json +1 -1
@@ -15,10 +15,15 @@ var utils = require('./utils.cjs');
15
15
  var Gantt_module = require('./Gantt.module.css.cjs');
16
16
 
17
17
  const DRAG_BUFFER_DAYS = 30;
18
+ const MIN_TIMELINE_WIDTH = 240;
18
19
  const EMPTY_CRITICAL = /* @__PURE__ */ new Set();
19
20
  const FLEX_COLUMN_WIDTH = 200;
20
- function autoTaskListWidth(columns = TaskList.defaultColumns) {
21
- return columns.reduce((sum, col) => sum + (col.width ?? FLEX_COLUMN_WIDTH), 0);
21
+ const FLEX_COLUMN_MIN_WIDTH = 100;
22
+ function columnsWidth(columns = TaskList.defaultColumns, flexWidth) {
23
+ return columns.reduce((sum, col) => sum + (col.width ?? flexWidth), 0);
24
+ }
25
+ function resolveTaskListWidth(taskListWidth, columns) {
26
+ return taskListWidth === void 0 ? columnsWidth(columns, FLEX_COLUMN_WIDTH) : Math.max(taskListWidth, columnsWidth(columns, FLEX_COLUMN_MIN_WIDTH));
22
27
  }
23
28
  const defaultProps = {
24
29
  columnWidth: 40,
@@ -27,6 +32,7 @@ const defaultProps = {
27
32
  showTodayMarker: true,
28
33
  viewMode: "day",
29
34
  weekStart: 1,
35
+ locale: "en",
30
36
  highlightCriticalPath: false,
31
37
  criticalPathColor: "red",
32
38
  showBaselines: true,
@@ -39,13 +45,14 @@ const varsResolver = core.createVarsResolver(
39
45
  "--gantt-column-width": `${columnWidth}px`,
40
46
  "--gantt-row-height": `${rowHeight}px`,
41
47
  "--gantt-header-height": "50px",
42
- "--gantt-task-list-width": `${taskListWidth ?? autoTaskListWidth(columns)}px`,
48
+ "--gantt-task-list-width": `${resolveTaskListWidth(taskListWidth, columns)}px`,
43
49
  "--gantt-critical-color": core.getThemeColor(criticalPathColor, theme)
44
50
  }
45
51
  })
46
52
  );
47
53
  const Gantt = core.factory((_props, ref) => {
48
54
  const props = core.useProps("Gantt", defaultProps, _props);
55
+ const theme = core.useMantineTheme();
49
56
  const {
50
57
  classNames,
51
58
  className,
@@ -65,6 +72,9 @@ const Gantt = core.factory((_props, ref) => {
65
72
  columnWidth = 40,
66
73
  rowHeight = 44,
67
74
  taskListWidth,
75
+ taskListCollapsed,
76
+ defaultTaskListCollapsed,
77
+ onTaskListCollapsedChange,
68
78
  showTitle,
69
79
  showTodayMarker,
70
80
  autoSchedule,
@@ -72,6 +82,7 @@ const Gantt = core.factory((_props, ref) => {
72
82
  endDate,
73
83
  viewMode = "day",
74
84
  weekStart = 1,
85
+ locale = "en",
75
86
  highlightCriticalPath,
76
87
  criticalPathColor,
77
88
  showBaselines,
@@ -80,6 +91,9 @@ const Gantt = core.factory((_props, ref) => {
80
91
  scrollTo,
81
92
  isNonWorkingDay,
82
93
  selectedTaskId,
94
+ markers,
95
+ readOnly,
96
+ workingDays,
83
97
  onColumnWidthChange,
84
98
  showDragLabel,
85
99
  ...others
@@ -113,6 +127,7 @@ const Gantt = core.factory((_props, ref) => {
113
127
  return Math.max(baseColumnWidth, 1);
114
128
  }
115
129
  }, [viewMode, baseColumnWidth]);
130
+ const calendar = workingDays ? isNonWorkingDay ?? utils.isWeekend : void 0;
116
131
  const [tasks, setTasks] = hooks.useUncontrolled({
117
132
  value: tasksProp,
118
133
  defaultValue: defaultTasks,
@@ -143,7 +158,10 @@ const Gantt = core.factory((_props, ref) => {
143
158
  },
144
159
  [onToggleExpand, collapsedIds]
145
160
  );
146
- const rows = React.useMemo(() => utils.buildTaskTree(tasks, collapsedIds), [tasks, collapsedIds]);
161
+ const rows = React.useMemo(
162
+ () => utils.buildTaskTree(tasks, collapsedIds, calendar),
163
+ [tasks, collapsedIds, calendar]
164
+ );
147
165
  const criticalIds = React.useMemo(
148
166
  () => highlightCriticalPath ? utils.getCriticalPath(tasks) : EMPTY_CRITICAL,
149
167
  [highlightCriticalPath, tasks]
@@ -151,10 +169,20 @@ const Gantt = core.factory((_props, ref) => {
151
169
  const [announcement, setAnnouncement] = React.useState("");
152
170
  const [viewport, setViewport] = React.useState({ width: 0, height: 0 });
153
171
  const [scrollTop, setScrollTop] = React.useState(0);
172
+ const [narrow, setNarrow] = React.useState(false);
173
+ const [collapsedChoice, setCollapsedChoice] = hooks.useUncontrolled({
174
+ value: taskListCollapsed,
175
+ defaultValue: defaultTaskListCollapsed,
176
+ onChange: onTaskListCollapsedChange
177
+ });
178
+ const listCollapsed = collapsedChoice ?? narrow;
179
+ const listWidth = resolveTaskListWidth(taskListWidth, columns);
154
180
  const timelineBodyRef = React.useRef(null);
155
181
  const taskListBodyRef = React.useRef(null);
156
182
  const timelineHeaderRef = React.useRef(null);
157
183
  const timelineContentRef = React.useRef(null);
184
+ const rootRef = React.useRef(null);
185
+ const mergedRootRef = hooks.useMergedRef(ref, rootRef);
158
186
  const drag = useGanttDrag.useGanttDrag({
159
187
  tasks,
160
188
  commitTasks: setTasks,
@@ -163,25 +191,31 @@ const Gantt = core.factory((_props, ref) => {
163
191
  contentRef: timelineContentRef,
164
192
  onTaskUpdate,
165
193
  onLinkCreate,
166
- onLinkDelete,
167
194
  autoSchedule,
195
+ isNonWorkingDay: calendar,
168
196
  announce: setAnnouncement
169
197
  });
170
198
  const active = drag.state;
171
199
  const handleLinkDelete = React.useCallback(
172
- (fromTaskId, toTaskId) => {
200
+ (fromTaskId, toTaskId, type) => {
173
201
  setTasks(
174
202
  tasks.map(
175
- (task) => task.id === toTaskId ? { ...task, dependencies: (task.dependencies ?? []).filter((id) => id !== fromTaskId) } : task
203
+ (task) => task.id === toTaskId ? {
204
+ ...task,
205
+ dependencies: (task.dependencies ?? []).filter((dep) => {
206
+ const normalized = utils.normalizeDependency(dep);
207
+ return normalized.taskId !== fromTaskId || normalized.type !== type;
208
+ })
209
+ } : task
176
210
  )
177
211
  );
178
- onLinkDelete?.(fromTaskId, toTaskId);
212
+ onLinkDelete?.(fromTaskId, toTaskId, type);
179
213
  },
180
214
  [tasks, setTasks, onLinkDelete]
181
215
  );
182
216
  const calculatedBounds = React.useMemo(
183
- () => utils.calculateTimelineBounds(tasks, startDate, endDate),
184
- [tasks, startDate, endDate]
217
+ () => utils.calculateTimelineBounds(tasks, startDate, endDate, void 0, calendar),
218
+ [tasks, startDate, endDate, calendar]
185
219
  );
186
220
  const bounds = React.useMemo(() => {
187
221
  if (!active || active.type === "link") {
@@ -192,32 +226,25 @@ const Gantt = core.factory((_props, ref) => {
192
226
  return calculatedBounds;
193
227
  }
194
228
  const startDay = dayjs(task.startDate).diff(calculatedBounds.start, "day");
195
- const barEndDay = startDay + task.duration + Math.round(active.deltaX / effectiveColumnWidth);
229
+ const barEndDay = startDay + utils.getTaskSpan(task.startDate, task.duration, calendar) + Math.round(active.deltaX / effectiveColumnWidth);
196
230
  const needed = calculatedBounds.start.add(barEndDay + DRAG_BUFFER_DAYS, "day");
197
231
  if (!needed.isAfter(calculatedBounds.end)) {
198
232
  return calculatedBounds;
199
233
  }
200
234
  return { start: calculatedBounds.start, end: needed };
201
- }, [calculatedBounds, active, tasks, effectiveColumnWidth]);
235
+ }, [calculatedBounds, active, tasks, effectiveColumnWidth, calendar]);
202
236
  const fillDays = effectiveColumnWidth > 0 ? Math.ceil(viewport.width / effectiveColumnWidth) : 0;
203
237
  const totalDays = Math.max(bounds.end.diff(bounds.start, "day") + 1, fillDays);
204
238
  const displayEnd = bounds.start.add(totalDays - 1, "day");
205
239
  const timelineWidth = totalDays * effectiveColumnWidth;
206
240
  const [firstRow, lastRow] = utils.visibleRowRange(scrollTop, viewport.height, rowHeight, rows.length);
207
241
  const visibleRows = rows.slice(firstRow, lastRow);
208
- const echoRef = React.useRef(null);
209
242
  const handleTimelineScroll = React.useCallback(() => {
210
243
  const body = timelineBodyRef.current;
211
244
  if (!body) {
212
245
  return;
213
246
  }
214
- const echo = echoRef.current;
215
- if (echo?.source === "timeline" && echo.value === body.scrollTop) {
216
- echoRef.current = null;
217
- return;
218
- }
219
247
  if (taskListBodyRef.current) {
220
- echoRef.current = { source: "timeline", value: body.scrollTop };
221
248
  taskListBodyRef.current.scrollTop = body.scrollTop;
222
249
  }
223
250
  if (timelineHeaderRef.current) {
@@ -226,36 +253,36 @@ const Gantt = core.factory((_props, ref) => {
226
253
  setScrollTop(body.scrollTop);
227
254
  }, []);
228
255
  const handleTaskListScroll = React.useCallback(() => {
229
- const list = taskListBodyRef.current;
230
- if (!list) {
231
- return;
232
- }
233
- const echo = echoRef.current;
234
- if (echo?.source === "list" && echo.value === list.scrollTop) {
235
- echoRef.current = null;
236
- return;
237
- }
238
- if (timelineBodyRef.current) {
239
- echoRef.current = { source: "list", value: list.scrollTop };
240
- timelineBodyRef.current.scrollTop = list.scrollTop;
256
+ if (taskListBodyRef.current && timelineBodyRef.current) {
257
+ timelineBodyRef.current.scrollTop = taskListBodyRef.current.scrollTop;
241
258
  }
242
259
  }, []);
243
260
  React.useEffect(() => {
244
261
  const node = timelineBodyRef.current;
245
- if (!node) {
262
+ const root = rootRef.current;
263
+ if (!node || !root) {
246
264
  return void 0;
247
265
  }
248
266
  setViewport({ width: node.clientWidth, height: node.clientHeight });
267
+ const measureRoot = (width) => setNarrow(width > 0 && width < listWidth + MIN_TIMELINE_WIDTH);
268
+ measureRoot(root.clientWidth);
249
269
  if (typeof ResizeObserver === "undefined") {
250
270
  return void 0;
251
271
  }
252
272
  const observer = new ResizeObserver((entries) => {
253
- const { width, height } = entries[0].contentRect;
254
- setViewport({ width, height });
273
+ for (const entry of entries) {
274
+ const { width, height } = entry.contentRect;
275
+ if (entry.target === root) {
276
+ measureRoot(width);
277
+ } else {
278
+ setViewport({ width, height });
279
+ }
280
+ }
255
281
  });
256
282
  observer.observe(node);
283
+ observer.observe(root);
257
284
  return () => observer.disconnect();
258
- }, []);
285
+ }, [listWidth]);
259
286
  const zoomAnchorRef = React.useRef(0);
260
287
  const zoomRef = React.useRef({ width: baseColumnWidth, onChange: onColumnWidthChange });
261
288
  zoomRef.current = { width: baseColumnWidth, onChange: onColumnWidthChange };
@@ -407,7 +434,7 @@ const Gantt = core.factory((_props, ref) => {
407
434
  if (srcIndex === -1) {
408
435
  return null;
409
436
  }
410
- const src = utils.getEffectiveTask(rows[srcIndex]);
437
+ const src = { ...utils.getEffectiveTask(rows[srcIndex]), duration: rows[srcIndex].span };
411
438
  const x1 = utils.barAnchors(src, bounds.start, effectiveColumnWidth, rowHeight).right;
412
439
  const y1 = srcIndex * rowHeight + rowHeight / 2;
413
440
  return { x1, y1, x2: active.linkCursor.x, y2: active.linkCursor.y };
@@ -415,11 +442,12 @@ const Gantt = core.factory((_props, ref) => {
415
442
  const today = dayjs();
416
443
  const todayPosition = utils.dateToPixel(today, bounds.start, effectiveColumnWidth);
417
444
  const showTodayLine = showTodayMarker && today.isAfter(bounds.start) && today.isBefore(displayEnd);
418
- return /* @__PURE__ */ React.createElement(core.Box, { ref, ...getStyles("root"), ...others }, /* @__PURE__ */ React.createElement(
445
+ return /* @__PURE__ */ React.createElement(core.Box, { ref: mergedRootRef, ...getStyles("root"), ...others }, /* @__PURE__ */ React.createElement(
419
446
  TaskList.TaskList,
420
447
  {
421
448
  rows: visibleRows,
422
449
  columns,
450
+ locale,
423
451
  getStyles,
424
452
  bodyRef: taskListBodyRef,
425
453
  onScroll: handleTaskListScroll,
@@ -427,7 +455,9 @@ const Gantt = core.factory((_props, ref) => {
427
455
  onToggleExpand: toggleExpand,
428
456
  offsetTop: firstRow * rowHeight,
429
457
  contentHeight: rows.length * rowHeight,
430
- selectedTaskId
458
+ selectedTaskId,
459
+ collapsed: listCollapsed,
460
+ onToggleCollapsed: () => setCollapsedChoice(!listCollapsed)
431
461
  }
432
462
  ), /* @__PURE__ */ React.createElement("div", { ...getStyles("timeline") }, /* @__PURE__ */ React.createElement("div", { ...getStyles("timelineHeader"), ref: timelineHeaderRef }, /* @__PURE__ */ React.createElement(
433
463
  TimelineHeader.TimelineHeader,
@@ -439,6 +469,7 @@ const Gantt = core.factory((_props, ref) => {
439
469
  totalWidth: timelineWidth,
440
470
  viewMode,
441
471
  weekStart,
472
+ locale,
442
473
  isNonWorkingDay
443
474
  }
444
475
  )), /* @__PURE__ */ React.createElement(
@@ -474,6 +505,25 @@ const Gantt = core.factory((_props, ref) => {
474
505
  }
475
506
  ),
476
507
  showTodayLine && /* @__PURE__ */ React.createElement("div", { ...getStyles("todayLine", { style: { left: todayPosition } }) }),
508
+ markers?.map((marker) => {
509
+ const date = dayjs(marker.date);
510
+ if (date.isBefore(bounds.start) || date.isAfter(displayEnd)) {
511
+ return null;
512
+ }
513
+ return /* @__PURE__ */ React.createElement(
514
+ "div",
515
+ {
516
+ key: `${marker.date}-${marker.color ?? ""}`,
517
+ ...getStyles("marker", {
518
+ style: {
519
+ left: utils.dateToPixel(date, bounds.start, effectiveColumnWidth),
520
+ ["--marker-color"]: core.getThemeColor(marker.color ?? "orange", theme)
521
+ }
522
+ })
523
+ },
524
+ marker.label != null && /* @__PURE__ */ React.createElement("span", { ...getStyles("markerLabel") }, marker.label)
525
+ );
526
+ }),
477
527
  visibleRows.map((row, i) => {
478
528
  const index = firstRow + i;
479
529
  const task = utils.getEffectiveTask(row);
@@ -493,7 +543,10 @@ const Gantt = core.factory((_props, ref) => {
493
543
  startDate: bounds.start,
494
544
  columnWidth: effectiveColumnWidth,
495
545
  getStyles,
546
+ span: row.span,
547
+ isNonWorkingDay: calendar,
496
548
  isSummary: row.hasChildren,
549
+ isLocked: readOnly || task.locked,
497
550
  isDragging: active?.taskId === task.id && active.type !== "link",
498
551
  isLinkTarget: active?.type === "link" && active.dropTargetId === task.id,
499
552
  dragType: active?.taskId === task.id ? active.type : null,
@@ -501,6 +554,7 @@ const Gantt = core.factory((_props, ref) => {
501
554
  startDrag: drag.startDrag,
502
555
  didDrag: drag.didDrag,
503
556
  nudge: drag.nudge,
557
+ locale,
504
558
  onTaskClick: handleTaskClick,
505
559
  isCritical: criticalIds.has(task.id),
506
560
  isSelected: task.id === selectedTaskId,
@@ -519,7 +573,10 @@ const Gantt = core.factory((_props, ref) => {
519
573
  bounds.start,
520
574
  effectiveColumnWidth
521
575
  ),
522
- width: utils.durationToPixels(task.baseline.duration, effectiveColumnWidth)
576
+ width: utils.durationToPixels(
577
+ utils.getTaskSpan(task.baseline.startDate, task.baseline.duration, calendar),
578
+ effectiveColumnWidth
579
+ )
523
580
  }
524
581
  }
525
582
  )
@@ -540,7 +597,7 @@ const Gantt = core.factory((_props, ref) => {
540
597
  criticalIds,
541
598
  firstRow,
542
599
  lastRow,
543
- onLinkClick: handleLinkDelete
600
+ onLinkClick: readOnly ? void 0 : handleLinkDelete
544
601
  }
545
602
  )
546
603
  )