mantine-gantt 0.4.0 → 0.5.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 (48) hide show
  1. package/dist/cjs/Gantt/DependencyLinks.cjs +7 -2
  2. package/dist/cjs/Gantt/DependencyLinks.cjs.map +1 -1
  3. package/dist/cjs/Gantt/Gantt.cjs +128 -20
  4. package/dist/cjs/Gantt/Gantt.cjs.map +1 -1
  5. package/dist/cjs/Gantt/Gantt.module.css.cjs +1 -1
  6. package/dist/cjs/Gantt/TaskBar.cjs +21 -1
  7. package/dist/cjs/Gantt/TaskBar.cjs.map +1 -1
  8. package/dist/cjs/Gantt/TaskList.cjs +46 -36
  9. package/dist/cjs/Gantt/TaskList.cjs.map +1 -1
  10. package/dist/cjs/Gantt/TimelineGrid.cjs +7 -27
  11. package/dist/cjs/Gantt/TimelineGrid.cjs.map +1 -1
  12. package/dist/cjs/Gantt/TimelineHeader.cjs +16 -34
  13. package/dist/cjs/Gantt/TimelineHeader.cjs.map +1 -1
  14. package/dist/cjs/Gantt/use-gantt-drag.cjs +55 -30
  15. package/dist/cjs/Gantt/use-gantt-drag.cjs.map +1 -1
  16. package/dist/cjs/Gantt/utils.cjs +22 -50
  17. package/dist/cjs/Gantt/utils.cjs.map +1 -1
  18. package/dist/cjs/index.cjs +0 -4
  19. package/dist/cjs/index.cjs.map +1 -1
  20. package/dist/esm/Gantt/DependencyLinks.mjs +8 -3
  21. package/dist/esm/Gantt/DependencyLinks.mjs.map +1 -1
  22. package/dist/esm/Gantt/Gantt.mjs +130 -22
  23. package/dist/esm/Gantt/Gantt.mjs.map +1 -1
  24. package/dist/esm/Gantt/Gantt.module.css.mjs +1 -1
  25. package/dist/esm/Gantt/TaskBar.mjs +22 -2
  26. package/dist/esm/Gantt/TaskBar.mjs.map +1 -1
  27. package/dist/esm/Gantt/TaskList.mjs +46 -36
  28. package/dist/esm/Gantt/TaskList.mjs.map +1 -1
  29. package/dist/esm/Gantt/TimelineGrid.mjs +7 -27
  30. package/dist/esm/Gantt/TimelineGrid.mjs.map +1 -1
  31. package/dist/esm/Gantt/TimelineHeader.mjs +16 -34
  32. package/dist/esm/Gantt/TimelineHeader.mjs.map +1 -1
  33. package/dist/esm/Gantt/use-gantt-drag.mjs +55 -30
  34. package/dist/esm/Gantt/use-gantt-drag.mjs.map +1 -1
  35. package/dist/esm/Gantt/utils.mjs +21 -51
  36. package/dist/esm/Gantt/utils.mjs.map +1 -1
  37. package/dist/esm/index.mjs +0 -4
  38. package/dist/esm/index.mjs.map +1 -1
  39. package/dist/styles.css +2 -2
  40. package/dist/styles.layer.css +2 -2
  41. package/dist/types/Gantt/TaskBar.d.ts +5 -2
  42. package/dist/types/Gantt/TaskList.d.ts +2 -1
  43. package/dist/types/Gantt/TimelineGrid.d.ts +2 -1
  44. package/dist/types/Gantt/TimelineHeader.d.ts +2 -1
  45. package/dist/types/Gantt/index.d.ts +1 -5
  46. package/dist/types/Gantt/types.d.ts +58 -10
  47. package/dist/types/Gantt/utils.d.ts +12 -1
  48. package/package.json +1 -1
@@ -1,6 +1,6 @@
1
1
  'use client';
2
2
  import dayjs from 'dayjs';
3
- import React, { useMemo, useState, useCallback, useRef, useEffect, useLayoutEffect } from 'react';
3
+ import React, { useState, useEffect, useMemo, useCallback, useRef, useLayoutEffect } from 'react';
4
4
  import { createVarsResolver, getThemeColor, factory, useProps, useStyles, Box, VisuallyHidden } from '@mantine/core';
5
5
  import { useUncontrolled } from '@mantine/hooks';
6
6
  import { DependencyLinks } from './DependencyLinks.mjs';
@@ -9,7 +9,7 @@ import { defaultColumns, TaskList } from './TaskList.mjs';
9
9
  import { TimelineGrid } from './TimelineGrid.mjs';
10
10
  import { TimelineHeader } from './TimelineHeader.mjs';
11
11
  import { useGanttDrag } from './use-gantt-drag.mjs';
12
- import { buildTaskTree, getCriticalPath, calculateTimelineBounds, visibleRowRange, dateToPixel, durationToPixels, getEffectiveTask } from './utils.mjs';
12
+ import { buildTaskTree, getCriticalPath, calculateTimelineBounds, visibleRowRange, dateToPixel, getEffectiveTask, barAnchors, durationToPixels } from './utils.mjs';
13
13
  import classes from './Gantt.module.css.mjs';
14
14
 
15
15
  const DRAG_BUFFER_DAYS = 30;
@@ -28,7 +28,8 @@ const defaultProps = {
28
28
  highlightCriticalPath: false,
29
29
  criticalPathColor: "red",
30
30
  showBaselines: true,
31
- autoSchedule: false
31
+ autoSchedule: false,
32
+ showDragLabel: true
32
33
  };
33
34
  const varsResolver = createVarsResolver(
34
35
  (theme, { columnWidth, rowHeight, taskListWidth, columns, criticalPathColor }) => ({
@@ -74,12 +75,20 @@ const Gantt = factory((_props, ref) => {
74
75
  showBaselines,
75
76
  defaultExpandedIds,
76
77
  onToggleExpand,
78
+ scrollTo,
79
+ isNonWorkingDay,
80
+ selectedTaskId,
81
+ onColumnWidthChange,
82
+ showDragLabel,
77
83
  ...others
78
84
  } = props;
85
+ const [zoomWidth, setZoomWidth] = useState(null);
86
+ useEffect(() => setZoomWidth(null), [columnWidth]);
87
+ const baseColumnWidth = zoomWidth ?? columnWidth;
79
88
  const getStyles = useStyles({
80
89
  name: "Gantt",
81
90
  classes,
82
- props,
91
+ props: { ...props, columnWidth: baseColumnWidth },
83
92
  className,
84
93
  style,
85
94
  classNames,
@@ -92,16 +101,16 @@ const Gantt = factory((_props, ref) => {
92
101
  const effectiveColumnWidth = useMemo(() => {
93
102
  switch (viewMode) {
94
103
  case "month":
95
- return Math.max(columnWidth / 6, 7);
104
+ return Math.max(baseColumnWidth / 6, 7);
96
105
  // Each day is 1/6th, min 7px
97
106
  case "week":
98
- return Math.max(columnWidth / 2, 14);
107
+ return Math.max(baseColumnWidth / 2, 14);
99
108
  // Each day is 1/2nd, min 14px
100
109
  case "day":
101
110
  default:
102
- return Math.max(columnWidth, 1);
111
+ return Math.max(baseColumnWidth, 1);
103
112
  }
104
- }, [viewMode, columnWidth]);
113
+ }, [viewMode, baseColumnWidth]);
105
114
  const [tasks, setTasks] = useUncontrolled({
106
115
  value: tasksProp,
107
116
  defaultValue: defaultTasks,
@@ -245,6 +254,66 @@ const Gantt = factory((_props, ref) => {
245
254
  observer.observe(node);
246
255
  return () => observer.disconnect();
247
256
  }, []);
257
+ const zoomAnchorRef = useRef(0);
258
+ const zoomRef = useRef({ width: baseColumnWidth, onChange: onColumnWidthChange });
259
+ zoomRef.current = { width: baseColumnWidth, onChange: onColumnWidthChange };
260
+ useEffect(() => {
261
+ const body = timelineBodyRef.current;
262
+ if (!body) {
263
+ return void 0;
264
+ }
265
+ const onWheel = (e) => {
266
+ if (!e.ctrlKey) {
267
+ return;
268
+ }
269
+ e.preventDefault();
270
+ zoomAnchorRef.current = e.clientX - body.getBoundingClientRect().left;
271
+ const factor = e.deltaY < 0 ? 1.1 : 1 / 1.1;
272
+ const next = Math.round(Math.min(200, Math.max(8, zoomRef.current.width * factor)));
273
+ setZoomWidth(next);
274
+ zoomRef.current.onChange?.(next);
275
+ };
276
+ body.addEventListener("wheel", onWheel, { passive: false });
277
+ return () => body.removeEventListener("wheel", onWheel);
278
+ }, []);
279
+ const prevColumnWidthRef = useRef(effectiveColumnWidth);
280
+ useLayoutEffect(() => {
281
+ const prev = prevColumnWidthRef.current;
282
+ if (prev === effectiveColumnWidth) {
283
+ return;
284
+ }
285
+ prevColumnWidthRef.current = effectiveColumnWidth;
286
+ const body = timelineBodyRef.current;
287
+ if (body) {
288
+ const anchor = zoomAnchorRef.current;
289
+ body.scrollLeft = (body.scrollLeft + anchor) * (effectiveColumnWidth / prev) - anchor;
290
+ }
291
+ zoomAnchorRef.current = 0;
292
+ }, [effectiveColumnWidth]);
293
+ const scrollToKey = scrollTo instanceof Date ? scrollTo.getTime() : typeof scrollTo === "object" && scrollTo ? `task:${scrollTo.taskId}` : scrollTo;
294
+ useEffect(() => {
295
+ const body = timelineBodyRef.current;
296
+ if (!scrollTo || !body) {
297
+ return;
298
+ }
299
+ let date;
300
+ let rowIndex = -1;
301
+ if (scrollTo === "today") {
302
+ date = dayjs();
303
+ } else if (scrollTo instanceof Date) {
304
+ date = dayjs(scrollTo);
305
+ } else {
306
+ rowIndex = rows.findIndex((r) => r.task.id === scrollTo.taskId);
307
+ if (rowIndex === -1) {
308
+ return;
309
+ }
310
+ date = dayjs(rows[rowIndex].startDate);
311
+ }
312
+ body.scrollLeft = dateToPixel(date, bounds.start, effectiveColumnWidth) - effectiveColumnWidth;
313
+ if (rowIndex >= 0) {
314
+ body.scrollTop = Math.max(0, rowIndex * rowHeight - (body.clientHeight - rowHeight) / 2);
315
+ }
316
+ }, [scrollToKey]);
248
317
  const handleTaskClick = useCallback(
249
318
  (task) => {
250
319
  onTaskClick?.(task);
@@ -252,33 +321,67 @@ const Gantt = factory((_props, ref) => {
252
321
  [onTaskClick]
253
322
  );
254
323
  const panRef = useRef(null);
255
- const handlePanStart = useCallback((e) => {
324
+ const autoScrollTick = useCallback(() => {
325
+ const pan = panRef.current;
256
326
  const body = timelineBodyRef.current;
257
- if (e.pointerType !== "mouse" || !body) {
327
+ if (!pan?.auto || !body) {
258
328
  return;
259
329
  }
260
- e.preventDefault();
261
- panRef.current = { x: e.clientX, y: e.clientY, left: body.scrollLeft, top: body.scrollTop };
262
- body.setPointerCapture?.(e.pointerId);
263
- body.style.cursor = "grabbing";
264
- document.body.style.userSelect = "none";
330
+ const speed = (d) => Math.sign(d) * Math.min(30, Math.max(0, Math.abs(d) - 8) / 4);
331
+ body.scrollLeft += speed(pan.left - pan.x);
332
+ body.scrollTop += speed(pan.top - pan.y);
333
+ pan.raf = requestAnimationFrame(autoScrollTick);
265
334
  }, []);
335
+ const handlePanStart = useCallback(
336
+ (e) => {
337
+ const body = timelineBodyRef.current;
338
+ if (e.pointerType !== "mouse" || !body || e.button && e.button !== 1) {
339
+ return;
340
+ }
341
+ e.preventDefault();
342
+ const auto = e.button === 1;
343
+ panRef.current = auto ? { x: e.clientX, y: e.clientY, left: e.clientX, top: e.clientY, auto, raf: null } : {
344
+ x: e.clientX,
345
+ y: e.clientY,
346
+ left: body.scrollLeft,
347
+ top: body.scrollTop,
348
+ auto,
349
+ raf: null
350
+ };
351
+ body.setPointerCapture?.(e.pointerId);
352
+ body.style.cursor = auto ? "all-scroll" : "grabbing";
353
+ document.body.style.userSelect = "none";
354
+ if (auto) {
355
+ panRef.current.raf = requestAnimationFrame(autoScrollTick);
356
+ }
357
+ },
358
+ [autoScrollTick]
359
+ );
266
360
  const handlePanMove = useCallback((e) => {
267
361
  const pan = panRef.current;
268
362
  const body = timelineBodyRef.current;
269
363
  if (!pan || !body) {
270
364
  return;
271
365
  }
366
+ if (pan.auto) {
367
+ pan.left = e.clientX;
368
+ pan.top = e.clientY;
369
+ return;
370
+ }
272
371
  body.scrollLeft = pan.left - (e.clientX - pan.x);
273
372
  body.scrollTop = pan.top - (e.clientY - pan.y);
274
373
  }, []);
275
374
  const handlePanEnd = useCallback(() => {
375
+ if (panRef.current?.raf !== null && panRef.current?.raf !== void 0) {
376
+ cancelAnimationFrame(panRef.current.raf);
377
+ }
276
378
  panRef.current = null;
277
379
  document.body.style.userSelect = "";
278
380
  if (timelineBodyRef.current) {
279
381
  timelineBodyRef.current.style.cursor = "";
280
382
  }
281
383
  }, []);
384
+ useEffect(() => handlePanEnd, [handlePanEnd]);
282
385
  const prevStartRef = useRef(bounds.start);
283
386
  useLayoutEffect(() => {
284
387
  const prev = prevStartRef.current;
@@ -302,8 +405,8 @@ const Gantt = factory((_props, ref) => {
302
405
  if (srcIndex === -1) {
303
406
  return null;
304
407
  }
305
- const src = rows[srcIndex];
306
- const x1 = dateToPixel(src.startDate, bounds.start, effectiveColumnWidth) + durationToPixels(src.duration, effectiveColumnWidth);
408
+ const src = getEffectiveTask(rows[srcIndex]);
409
+ const x1 = barAnchors(src, bounds.start, effectiveColumnWidth, rowHeight).right;
307
410
  const y1 = srcIndex * rowHeight + rowHeight / 2;
308
411
  return { x1, y1, x2: active.linkCursor.x, y2: active.linkCursor.y };
309
412
  }, [active, rows, bounds.start, effectiveColumnWidth, rowHeight]);
@@ -321,7 +424,8 @@ const Gantt = factory((_props, ref) => {
321
424
  collapsedIds,
322
425
  onToggleExpand: toggleExpand,
323
426
  offsetTop: firstRow * rowHeight,
324
- contentHeight: rows.length * rowHeight
427
+ contentHeight: rows.length * rowHeight,
428
+ selectedTaskId
325
429
  }
326
430
  ), /* @__PURE__ */ React.createElement("div", { ...getStyles("timeline") }, /* @__PURE__ */ React.createElement("div", { ...getStyles("timelineHeader"), ref: timelineHeaderRef }, /* @__PURE__ */ React.createElement(
327
431
  TimelineHeader,
@@ -332,7 +436,8 @@ const Gantt = factory((_props, ref) => {
332
436
  getStyles,
333
437
  totalWidth: timelineWidth,
334
438
  viewMode,
335
- weekStart
439
+ weekStart,
440
+ isNonWorkingDay
336
441
  }
337
442
  )), /* @__PURE__ */ React.createElement(
338
443
  "div",
@@ -362,7 +467,8 @@ const Gantt = factory((_props, ref) => {
362
467
  rowHeight,
363
468
  getStyles,
364
469
  viewMode,
365
- weekStart
470
+ weekStart,
471
+ isNonWorkingDay
366
472
  }
367
473
  ),
368
474
  showTodayLine && /* @__PURE__ */ React.createElement("div", { ...getStyles("todayLine", { style: { left: todayPosition } }) }),
@@ -374,13 +480,13 @@ const Gantt = factory((_props, ref) => {
374
480
  {
375
481
  key: task.id,
376
482
  ...getStyles("timelineRow"),
483
+ "data-first": index === Math.floor(scrollTop / rowHeight) || void 0,
377
484
  title: showTitle ? task.label : void 0,
378
485
  style: { top: index * rowHeight }
379
486
  },
380
487
  /* @__PURE__ */ React.createElement(
381
488
  TaskBar,
382
489
  {
383
- key: `taskbar-${task.id}`,
384
490
  task,
385
491
  startDate: bounds.start,
386
492
  columnWidth: effectiveColumnWidth,
@@ -395,7 +501,9 @@ const Gantt = factory((_props, ref) => {
395
501
  nudge: drag.nudge,
396
502
  onTaskClick: handleTaskClick,
397
503
  isCritical: criticalIds.has(task.id),
398
- showTooltip: showTitle
504
+ isSelected: task.id === selectedTaskId,
505
+ showTooltip: showTitle,
506
+ showDragLabel
399
507
  }
400
508
  ),
401
509
  showBaselines && task.baseline && /* @__PURE__ */ React.createElement(