mantine-gantt 0.6.0 → 0.7.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 (44) hide show
  1. package/README.md +43 -166
  2. package/dist/cjs/Gantt/DependencyLinks.cjs +10 -5
  3. package/dist/cjs/Gantt/DependencyLinks.cjs.map +1 -1
  4. package/dist/cjs/Gantt/Gantt.cjs +121 -60
  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 -19
  8. package/dist/cjs/Gantt/TaskBar.cjs.map +1 -1
  9. package/dist/cjs/Gantt/TaskList.cjs +30 -10
  10. package/dist/cjs/Gantt/TaskList.cjs.map +1 -1
  11. package/dist/cjs/Gantt/TimelineGrid.cjs +2 -1
  12. package/dist/cjs/Gantt/TimelineGrid.cjs.map +1 -1
  13. package/dist/cjs/Gantt/TimelineHeader.cjs +4 -4
  14. package/dist/cjs/Gantt/TimelineHeader.cjs.map +1 -1
  15. package/dist/cjs/Gantt/use-gantt-drag.cjs +76 -23
  16. package/dist/cjs/Gantt/use-gantt-drag.cjs.map +1 -1
  17. package/dist/cjs/Gantt/utils.cjs +71 -28
  18. package/dist/cjs/Gantt/utils.cjs.map +1 -1
  19. package/dist/esm/Gantt/DependencyLinks.mjs +11 -6
  20. package/dist/esm/Gantt/DependencyLinks.mjs.map +1 -1
  21. package/dist/esm/Gantt/Gantt.mjs +123 -62
  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 +14 -20
  25. package/dist/esm/Gantt/TaskBar.mjs.map +1 -1
  26. package/dist/esm/Gantt/TaskList.mjs +30 -10
  27. package/dist/esm/Gantt/TaskList.mjs.map +1 -1
  28. package/dist/esm/Gantt/TimelineGrid.mjs +2 -1
  29. package/dist/esm/Gantt/TimelineGrid.mjs.map +1 -1
  30. package/dist/esm/Gantt/TimelineHeader.mjs +4 -4
  31. package/dist/esm/Gantt/TimelineHeader.mjs.map +1 -1
  32. package/dist/esm/Gantt/use-gantt-drag.mjs +76 -23
  33. package/dist/esm/Gantt/use-gantt-drag.mjs.map +1 -1
  34. package/dist/esm/Gantt/utils.mjs +71 -29
  35. package/dist/esm/Gantt/utils.mjs.map +1 -1
  36. package/dist/styles.css +2 -2
  37. package/dist/styles.layer.css +2 -2
  38. package/dist/types/Gantt/DependencyLinks.d.ts +2 -4
  39. package/dist/types/Gantt/TaskList.d.ts +4 -4
  40. package/dist/types/Gantt/TimelineGrid.d.ts +2 -4
  41. package/dist/types/Gantt/TimelineHeader.d.ts +5 -5
  42. package/dist/types/Gantt/types.d.ts +18 -3
  43. package/dist/types/Gantt/utils.d.ts +13 -5
  44. package/package.json +3 -4
@@ -15,6 +15,9 @@ 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 PAN_CAPTURE_DISTANCE = 3;
19
+ const MIN_TIMELINE_WIDTH = 240;
20
+ const UNMEASURED_VIEWPORT_HEIGHT = 1200;
18
21
  const EMPTY_CRITICAL = /* @__PURE__ */ new Set();
19
22
  const FLEX_COLUMN_WIDTH = 200;
20
23
  const FLEX_COLUMN_MIN_WIDTH = 100;
@@ -49,7 +52,7 @@ const varsResolver = core.createVarsResolver(
49
52
  }
50
53
  })
51
54
  );
52
- const Gantt = core.factory((_props, ref) => {
55
+ const Gantt = core.factory((_props) => {
53
56
  const props = core.useProps("Gantt", defaultProps, _props);
54
57
  const theme = core.useMantineTheme();
55
58
  const {
@@ -71,6 +74,9 @@ const Gantt = core.factory((_props, ref) => {
71
74
  columnWidth = 40,
72
75
  rowHeight = 44,
73
76
  taskListWidth,
77
+ taskListCollapsed,
78
+ defaultTaskListCollapsed,
79
+ onTaskListCollapsedChange,
74
80
  showTitle,
75
81
  showTodayMarker,
76
82
  autoSchedule,
@@ -92,6 +98,7 @@ const Gantt = core.factory((_props, ref) => {
92
98
  workingDays,
93
99
  onColumnWidthChange,
94
100
  showDragLabel,
101
+ ref,
95
102
  ...others
96
103
  } = props;
97
104
  const [zoomWidth, setZoomWidth] = React.useState(null);
@@ -110,6 +117,12 @@ const Gantt = core.factory((_props, ref) => {
110
117
  attributes,
111
118
  varsResolver
112
119
  });
120
+ const getStylesRef = React.useRef(getStyles);
121
+ getStylesRef.current = getStyles;
122
+ const stableGetStyles = React.useCallback(
123
+ (...args) => getStylesRef.current(...args),
124
+ [classNames, styles, unstyled, vars, attributes, theme]
125
+ );
113
126
  const effectiveColumnWidth = React.useMemo(() => {
114
127
  switch (viewMode) {
115
128
  case "month":
@@ -124,12 +137,13 @@ const Gantt = core.factory((_props, ref) => {
124
137
  }
125
138
  }, [viewMode, baseColumnWidth]);
126
139
  const calendar = workingDays ? isNonWorkingDay ?? utils.isWeekend : void 0;
127
- const [tasks, setTasks] = hooks.useUncontrolled({
140
+ const [tasks, setTasksUnstable] = hooks.useUncontrolled({
128
141
  value: tasksProp,
129
142
  defaultValue: defaultTasks,
130
143
  finalValue: [],
131
144
  onChange: onTasksChange
132
145
  });
146
+ const setTasks = hooks.useCallbackRef(setTasksUnstable);
133
147
  const [collapsedIds, setCollapsedIds] = React.useState(() => {
134
148
  if (!defaultExpandedIds) {
135
149
  return /* @__PURE__ */ new Set();
@@ -165,10 +179,25 @@ const Gantt = core.factory((_props, ref) => {
165
179
  const [announcement, setAnnouncement] = React.useState("");
166
180
  const [viewport, setViewport] = React.useState({ width: 0, height: 0 });
167
181
  const [scrollTop, setScrollTop] = React.useState(0);
182
+ const [narrow, setNarrow] = React.useState(false);
183
+ const [collapsedChoice, setCollapsedChoiceUnstable] = hooks.useUncontrolled({
184
+ value: taskListCollapsed,
185
+ defaultValue: defaultTaskListCollapsed,
186
+ onChange: onTaskListCollapsedChange
187
+ });
188
+ const setCollapsedChoice = hooks.useCallbackRef(setCollapsedChoiceUnstable);
189
+ const listCollapsed = collapsedChoice ?? narrow;
190
+ const toggleListCollapsed = React.useCallback(
191
+ () => setCollapsedChoice(!listCollapsed),
192
+ [listCollapsed, setCollapsedChoice]
193
+ );
194
+ const listWidth = resolveTaskListWidth(taskListWidth, columns);
168
195
  const timelineBodyRef = React.useRef(null);
169
196
  const taskListBodyRef = React.useRef(null);
170
197
  const timelineHeaderRef = React.useRef(null);
171
198
  const timelineContentRef = React.useRef(null);
199
+ const rootRef = React.useRef(null);
200
+ const mergedRootRef = hooks.useMergedRef(ref, rootRef);
172
201
  const drag = useGanttDrag.useGanttDrag({
173
202
  tasks,
174
203
  commitTasks: setTasks,
@@ -221,10 +250,18 @@ const Gantt = core.factory((_props, ref) => {
221
250
  }, [calculatedBounds, active, tasks, effectiveColumnWidth, calendar]);
222
251
  const fillDays = effectiveColumnWidth > 0 ? Math.ceil(viewport.width / effectiveColumnWidth) : 0;
223
252
  const totalDays = Math.max(bounds.end.diff(bounds.start, "day") + 1, fillDays);
224
- const displayEnd = bounds.start.add(totalDays - 1, "day");
253
+ const displayEnd = React.useMemo(
254
+ () => bounds.start.add(totalDays - 1, "day"),
255
+ [bounds.start, totalDays]
256
+ );
225
257
  const timelineWidth = totalDays * effectiveColumnWidth;
226
- const [firstRow, lastRow] = utils.visibleRowRange(scrollTop, viewport.height, rowHeight, rows.length);
227
- const visibleRows = rows.slice(firstRow, lastRow);
258
+ const [firstRow, lastRow] = utils.visibleRowRange(
259
+ scrollTop,
260
+ viewport.height || UNMEASURED_VIEWPORT_HEIGHT,
261
+ rowHeight,
262
+ rows.length
263
+ );
264
+ const visibleRows = React.useMemo(() => rows.slice(firstRow, lastRow), [rows, firstRow, lastRow]);
228
265
  const handleTimelineScroll = React.useCallback(() => {
229
266
  const body = timelineBodyRef.current;
230
267
  if (!body) {
@@ -236,29 +273,39 @@ const Gantt = core.factory((_props, ref) => {
236
273
  if (timelineHeaderRef.current) {
237
274
  timelineHeaderRef.current.scrollLeft = body.scrollLeft;
238
275
  }
239
- setScrollTop(body.scrollTop);
240
- }, []);
276
+ setScrollTop(Math.floor(body.scrollTop / rowHeight) * rowHeight);
277
+ }, [rowHeight]);
241
278
  const handleTaskListScroll = React.useCallback(() => {
242
279
  if (taskListBodyRef.current && timelineBodyRef.current) {
243
280
  timelineBodyRef.current.scrollTop = taskListBodyRef.current.scrollTop;
244
281
  }
245
282
  }, []);
246
- React.useEffect(() => {
283
+ React.useLayoutEffect(() => {
247
284
  const node = timelineBodyRef.current;
248
- if (!node) {
285
+ const root = rootRef.current;
286
+ if (!node || !root) {
249
287
  return void 0;
250
288
  }
251
289
  setViewport({ width: node.clientWidth, height: node.clientHeight });
290
+ const measureRoot = (width) => setNarrow(width > 0 && width < listWidth + MIN_TIMELINE_WIDTH);
291
+ measureRoot(root.clientWidth);
252
292
  if (typeof ResizeObserver === "undefined") {
253
293
  return void 0;
254
294
  }
255
295
  const observer = new ResizeObserver((entries) => {
256
- const { width, height } = entries[0].contentRect;
257
- setViewport({ width, height });
296
+ for (const entry of entries) {
297
+ const { width, height } = entry.contentRect;
298
+ if (entry.target === root) {
299
+ measureRoot(width);
300
+ } else {
301
+ setViewport({ width, height });
302
+ }
303
+ }
258
304
  });
259
305
  observer.observe(node);
306
+ observer.observe(root);
260
307
  return () => observer.disconnect();
261
- }, []);
308
+ }, [listWidth]);
262
309
  const zoomAnchorRef = React.useRef(0);
263
310
  const zoomRef = React.useRef({ width: baseColumnWidth, onChange: onColumnWidthChange });
264
311
  zoomRef.current = { width: baseColumnWidth, onChange: onColumnWidthChange };
@@ -337,6 +384,18 @@ const Gantt = core.factory((_props, ref) => {
337
384
  body.scrollTop += speed(pan.top - pan.y);
338
385
  pan.raf = requestAnimationFrame(autoScrollTick);
339
386
  }, []);
387
+ const handlePanEnd = React.useCallback(function end() {
388
+ document.removeEventListener("pointerup", end);
389
+ document.removeEventListener("pointercancel", end);
390
+ if (panRef.current?.raf !== null && panRef.current?.raf !== void 0) {
391
+ cancelAnimationFrame(panRef.current.raf);
392
+ }
393
+ panRef.current = null;
394
+ document.body.style.userSelect = "";
395
+ if (timelineBodyRef.current) {
396
+ timelineBodyRef.current.style.cursor = "";
397
+ }
398
+ }, []);
340
399
  const handlePanStart = React.useCallback(
341
400
  (e) => {
342
401
  const body = timelineBodyRef.current;
@@ -345,22 +404,28 @@ const Gantt = core.factory((_props, ref) => {
345
404
  }
346
405
  e.preventDefault();
347
406
  const auto = e.button === 1;
348
- panRef.current = auto ? { x: e.clientX, y: e.clientY, left: e.clientX, top: e.clientY, auto, raf: null } : {
407
+ panRef.current = {
349
408
  x: e.clientX,
350
409
  y: e.clientY,
351
- left: body.scrollLeft,
352
- top: body.scrollTop,
410
+ left: auto ? e.clientX : body.scrollLeft,
411
+ top: auto ? e.clientY : body.scrollTop,
353
412
  auto,
354
- raf: null
413
+ raf: null,
414
+ pointerId: e.pointerId,
415
+ captured: auto
355
416
  };
356
- body.setPointerCapture?.(e.pointerId);
417
+ if (auto) {
418
+ body.setPointerCapture?.(e.pointerId);
419
+ }
357
420
  body.style.cursor = auto ? "all-scroll" : "grabbing";
358
421
  document.body.style.userSelect = "none";
422
+ document.addEventListener("pointerup", handlePanEnd);
423
+ document.addEventListener("pointercancel", handlePanEnd);
359
424
  if (auto) {
360
425
  panRef.current.raf = requestAnimationFrame(autoScrollTick);
361
426
  }
362
427
  },
363
- [autoScrollTick]
428
+ [autoScrollTick, handlePanEnd]
364
429
  );
365
430
  const handlePanMove = React.useCallback((e) => {
366
431
  const pan = panRef.current;
@@ -373,19 +438,13 @@ const Gantt = core.factory((_props, ref) => {
373
438
  pan.top = e.clientY;
374
439
  return;
375
440
  }
441
+ if (!pan.captured && Math.hypot(e.clientX - pan.x, e.clientY - pan.y) >= PAN_CAPTURE_DISTANCE) {
442
+ pan.captured = true;
443
+ body.setPointerCapture?.(pan.pointerId);
444
+ }
376
445
  body.scrollLeft = pan.left - (e.clientX - pan.x);
377
446
  body.scrollTop = pan.top - (e.clientY - pan.y);
378
447
  }, []);
379
- const handlePanEnd = React.useCallback(() => {
380
- if (panRef.current?.raf !== null && panRef.current?.raf !== void 0) {
381
- cancelAnimationFrame(panRef.current.raf);
382
- }
383
- panRef.current = null;
384
- document.body.style.userSelect = "";
385
- if (timelineBodyRef.current) {
386
- timelineBodyRef.current.style.cursor = "";
387
- }
388
- }, []);
389
448
  React.useEffect(() => handlePanEnd, [handlePanEnd]);
390
449
  const prevStartRef = React.useRef(bounds.start);
391
450
  React.useLayoutEffect(() => {
@@ -417,21 +476,23 @@ const Gantt = core.factory((_props, ref) => {
417
476
  }, [active, rows, bounds.start, effectiveColumnWidth, rowHeight]);
418
477
  const today = dayjs();
419
478
  const todayPosition = utils.dateToPixel(today, bounds.start, effectiveColumnWidth);
420
- const showTodayLine = showTodayMarker && today.isAfter(bounds.start) && today.isBefore(displayEnd);
421
- return /* @__PURE__ */ React.createElement(core.Box, { ref, ...getStyles("root"), ...others }, /* @__PURE__ */ React.createElement(
479
+ const showTodayLine = showTodayMarker && !today.isBefore(bounds.start, "day") && !today.isAfter(displayEnd, "day");
480
+ return /* @__PURE__ */ React.createElement(core.Box, { ref: mergedRootRef, ...getStyles("root"), ...others }, /* @__PURE__ */ React.createElement(
422
481
  TaskList.TaskList,
423
482
  {
424
483
  rows: visibleRows,
425
484
  columns,
426
485
  locale,
427
- getStyles,
486
+ getStyles: stableGetStyles,
428
487
  bodyRef: taskListBodyRef,
429
488
  onScroll: handleTaskListScroll,
430
489
  collapsedIds,
431
490
  onToggleExpand: toggleExpand,
432
491
  offsetTop: firstRow * rowHeight,
433
492
  contentHeight: rows.length * rowHeight,
434
- selectedTaskId
493
+ selectedTaskId,
494
+ collapsed: listCollapsed,
495
+ onToggleCollapsed: toggleListCollapsed
435
496
  }
436
497
  ), /* @__PURE__ */ React.createElement("div", { ...getStyles("timeline") }, /* @__PURE__ */ React.createElement("div", { ...getStyles("timelineHeader"), ref: timelineHeaderRef }, /* @__PURE__ */ React.createElement(
437
498
  TimelineHeader.TimelineHeader,
@@ -439,10 +500,11 @@ const Gantt = core.factory((_props, ref) => {
439
500
  startDate: bounds.start,
440
501
  endDate: displayEnd,
441
502
  columnWidth: effectiveColumnWidth,
442
- getStyles,
503
+ getStyles: stableGetStyles,
443
504
  totalWidth: timelineWidth,
444
505
  viewMode,
445
506
  weekStart,
507
+ today: today.format("YYYY-MM-DD"),
446
508
  locale,
447
509
  isNonWorkingDay
448
510
  }
@@ -453,16 +515,15 @@ const Gantt = core.factory((_props, ref) => {
453
515
  ref: timelineBodyRef,
454
516
  onScroll: handleTimelineScroll,
455
517
  onPointerDown: handlePanStart,
456
- onPointerMove: handlePanMove,
457
- onPointerUp: handlePanEnd,
458
- onPointerCancel: handlePanEnd
518
+ onPointerMove: handlePanMove
459
519
  },
460
520
  /* @__PURE__ */ React.createElement(
461
521
  "div",
462
522
  {
463
- ...getStyles("timelineContent"),
464
- ref: timelineContentRef,
465
- style: { width: timelineWidth, height: rows.length * rowHeight }
523
+ ...getStyles("timelineContent", {
524
+ style: { width: timelineWidth, height: rows.length * rowHeight }
525
+ }),
526
+ ref: timelineContentRef
466
527
  },
467
528
  /* @__PURE__ */ React.createElement(
468
529
  TimelineGrid.TimelineGrid,
@@ -472,14 +533,14 @@ const Gantt = core.factory((_props, ref) => {
472
533
  columnWidth: effectiveColumnWidth,
473
534
  rowCount: rows.length,
474
535
  rowHeight,
475
- getStyles,
536
+ getStyles: stableGetStyles,
476
537
  viewMode,
477
538
  weekStart,
478
539
  isNonWorkingDay
479
540
  }
480
541
  ),
481
542
  showTodayLine && /* @__PURE__ */ React.createElement("div", { ...getStyles("todayLine", { style: { left: todayPosition } }) }),
482
- markers?.map((marker) => {
543
+ markers?.map((marker, index) => {
483
544
  const date = dayjs(marker.date);
484
545
  if (date.isBefore(bounds.start) || date.isAfter(displayEnd)) {
485
546
  return null;
@@ -487,7 +548,7 @@ const Gantt = core.factory((_props, ref) => {
487
548
  return /* @__PURE__ */ React.createElement(
488
549
  "div",
489
550
  {
490
- key: `${marker.date}-${marker.color ?? ""}`,
551
+ key: index,
491
552
  ...getStyles("marker", {
492
553
  style: {
493
554
  left: utils.dateToPixel(date, bounds.start, effectiveColumnWidth),
@@ -505,10 +566,9 @@ const Gantt = core.factory((_props, ref) => {
505
566
  "div",
506
567
  {
507
568
  key: task.id,
508
- ...getStyles("timelineRow"),
569
+ ...getStyles("timelineRow", { style: { top: index * rowHeight } }),
509
570
  "data-first": index === Math.floor(scrollTop / rowHeight) || void 0,
510
- title: showTitle ? task.label : void 0,
511
- style: { top: index * rowHeight }
571
+ title: showTitle ? task.label : void 0
512
572
  },
513
573
  /* @__PURE__ */ React.createElement(
514
574
  TaskBar.TaskBar,
@@ -516,7 +576,7 @@ const Gantt = core.factory((_props, ref) => {
516
576
  task,
517
577
  startDate: bounds.start,
518
578
  columnWidth: effectiveColumnWidth,
519
- getStyles,
579
+ getStyles: stableGetStyles,
520
580
  span: row.span,
521
581
  isNonWorkingDay: calendar,
522
582
  isSummary: row.hasChildren,
@@ -539,19 +599,20 @@ const Gantt = core.factory((_props, ref) => {
539
599
  showBaselines && task.baseline && /* @__PURE__ */ React.createElement(
540
600
  "div",
541
601
  {
542
- ...getStyles("baselineBar"),
543
- "data-task-id": task.id,
544
- style: {
545
- left: utils.dateToPixel(
546
- task.baseline.startDate,
547
- bounds.start,
548
- effectiveColumnWidth
549
- ),
550
- width: utils.durationToPixels(
551
- utils.getTaskSpan(task.baseline.startDate, task.baseline.duration, calendar),
552
- effectiveColumnWidth
553
- )
554
- }
602
+ ...getStyles("baselineBar", {
603
+ style: {
604
+ left: utils.dateToPixel(
605
+ task.baseline.startDate,
606
+ bounds.start,
607
+ effectiveColumnWidth
608
+ ),
609
+ width: utils.durationToPixels(
610
+ utils.getTaskSpan(task.baseline.startDate, task.baseline.duration, calendar),
611
+ effectiveColumnWidth
612
+ )
613
+ }
614
+ }),
615
+ "data-task-id": task.id
555
616
  }
556
617
  )
557
618
  );
@@ -563,7 +624,7 @@ const Gantt = core.factory((_props, ref) => {
563
624
  startDate: bounds.start,
564
625
  columnWidth: effectiveColumnWidth,
565
626
  rowHeight,
566
- getStyles,
627
+ getStyles: stableGetStyles,
567
628
  activeDragId: active && active.type !== "link" ? active.taskId : null,
568
629
  activeDragType: active && active.type !== "link" ? active.type : null,
569
630
  dragDelta: active && active.type !== "link" ? active.deltaX : 0,