mantine-gantt 0.0.3 → 0.1.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 (36) hide show
  1. package/README.md +152 -26
  2. package/dist/cjs/Gantt/DependencyLinks.cjs +3 -3
  3. package/dist/cjs/Gantt/DependencyLinks.cjs.map +1 -1
  4. package/dist/cjs/Gantt/Gantt.cjs +198 -48
  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 +37 -19
  8. package/dist/cjs/Gantt/TaskBar.cjs.map +1 -1
  9. package/dist/cjs/Gantt/TimelineGrid.cjs +57 -17
  10. package/dist/cjs/Gantt/TimelineGrid.cjs.map +1 -1
  11. package/dist/cjs/Gantt/TimelineHeader.cjs +54 -8
  12. package/dist/cjs/Gantt/TimelineHeader.cjs.map +1 -1
  13. package/dist/cjs/Gantt/utils.cjs +6 -9
  14. package/dist/cjs/Gantt/utils.cjs.map +1 -1
  15. package/dist/esm/Gantt/DependencyLinks.mjs +3 -3
  16. package/dist/esm/Gantt/DependencyLinks.mjs.map +1 -1
  17. package/dist/esm/Gantt/Gantt.mjs +200 -50
  18. package/dist/esm/Gantt/Gantt.mjs.map +1 -1
  19. package/dist/esm/Gantt/Gantt.module.css.mjs +1 -1
  20. package/dist/esm/Gantt/TaskBar.mjs +39 -21
  21. package/dist/esm/Gantt/TaskBar.mjs.map +1 -1
  22. package/dist/esm/Gantt/TimelineGrid.mjs +57 -17
  23. package/dist/esm/Gantt/TimelineGrid.mjs.map +1 -1
  24. package/dist/esm/Gantt/TimelineHeader.mjs +54 -8
  25. package/dist/esm/Gantt/TimelineHeader.mjs.map +1 -1
  26. package/dist/esm/Gantt/utils.mjs +6 -9
  27. package/dist/esm/Gantt/utils.mjs.map +1 -1
  28. package/dist/styles.css +1 -1
  29. package/dist/styles.layer.css +1 -1
  30. package/dist/types/Gantt/DependencyLinks.d.ts +5 -3
  31. package/dist/types/Gantt/TaskBar.d.ts +4 -2
  32. package/dist/types/Gantt/TimelineGrid.d.ts +2 -1
  33. package/dist/types/Gantt/TimelineHeader.d.ts +2 -1
  34. package/dist/types/Gantt/types.d.ts +6 -2
  35. package/dist/types/Gantt/utils.d.ts +1 -0
  36. package/package.json +2 -2
@@ -14,10 +14,13 @@ var TimelineHeader = require('./TimelineHeader.cjs');
14
14
  var utils = require('./utils.cjs');
15
15
  var Gantt_module = require('./Gantt.module.css.cjs');
16
16
 
17
+ const DRAG_BUFFER_DAYS = 30;
17
18
  const defaultProps = {
18
19
  columnWidth: 40,
19
20
  rowHeight: 44,
20
21
  taskListWidth: 320,
22
+ showTitle: false,
23
+ showTodayMarker: true,
21
24
  viewMode: "day"
22
25
  };
23
26
  const varsResolver = core.createVarsResolver(
@@ -43,9 +46,11 @@ const Gantt = core.factory((_props, ref) => {
43
46
  onTaskUpdate,
44
47
  onTaskClick,
45
48
  onLinkCreate,
46
- columnWidth = 40,
47
- rowHeight = 44,
48
- showTitle = false,
49
+ columnWidth,
50
+ rowHeight,
51
+ taskListWidth,
52
+ showTitle,
53
+ showTodayMarker,
49
54
  startDate,
50
55
  endDate,
51
56
  viewMode,
@@ -63,10 +68,24 @@ const Gantt = core.factory((_props, ref) => {
63
68
  vars,
64
69
  varsResolver
65
70
  });
71
+ const effectiveColumnWidth = React.useMemo(() => {
72
+ switch (viewMode) {
73
+ case "month":
74
+ return Math.max(columnWidth / 6, 7);
75
+ // Each day is 1/6th, min 7px
76
+ case "week":
77
+ return Math.max(columnWidth / 2, 14);
78
+ // Each day is 1/2nd, min 14px
79
+ case "day":
80
+ default:
81
+ return columnWidth;
82
+ }
83
+ }, [viewMode, columnWidth]);
66
84
  const [tasks, setTasks] = React.useState(initialTasks);
67
85
  const [activeDragId, setActiveDragId] = React.useState(null);
68
86
  const [activeDragType, setActiveDragType] = React.useState(null);
69
87
  const [dragDelta, setDragDelta] = React.useState(0);
88
+ const [viewportWidth, setViewportWidth] = React.useState(0);
70
89
  const calculatedBounds = React.useMemo(
71
90
  () => utils.calculateTimelineBounds(tasks, startDate, endDate),
72
91
  [tasks, startDate, endDate]
@@ -76,11 +95,37 @@ const Gantt = core.factory((_props, ref) => {
76
95
  stableBoundsRef.current = calculatedBounds;
77
96
  }
78
97
  const bounds = stableBoundsRef.current;
79
- const totalDays = bounds.end.diff(bounds.start, "day") + 1;
80
- const timelineWidth = totalDays * columnWidth;
98
+ const fillDays = effectiveColumnWidth > 0 ? Math.ceil(viewportWidth / effectiveColumnWidth) : 0;
99
+ const totalDays = Math.max(bounds.end.diff(bounds.start, "day") + 1, fillDays);
100
+ const displayEnd = bounds.start.add(totalDays - 1, "day");
101
+ const timelineWidth = totalDays * effectiveColumnWidth;
81
102
  const timelineBodyRef = React.useRef(null);
82
103
  const taskListBodyRef = React.useRef(null);
83
104
  const timelineHeaderRef = React.useRef(null);
105
+ const timelineContentRef = React.useRef(null);
106
+ const [moveLeft, setMoveLeft] = React.useState(null);
107
+ const moveInfoRef = React.useRef(
108
+ null
109
+ );
110
+ const lastClientXRef = React.useRef(0);
111
+ const moveDayIndexRef = React.useRef(0);
112
+ const recomputeMove = React.useCallback(() => {
113
+ const info = moveInfoRef.current;
114
+ const content = timelineContentRef.current;
115
+ if (!info || !content) {
116
+ return;
117
+ }
118
+ const rect = content.getBoundingClientRect();
119
+ const pointerContentX = lastClientXRef.current - rect.left;
120
+ const dayIndex = Math.round((pointerContentX - info.grabOffsetX) / effectiveColumnWidth);
121
+ const start = stableBoundsRef.current.start;
122
+ const neededEndDay = dayIndex + info.duration + DRAG_BUFFER_DAYS;
123
+ if (neededEndDay > stableBoundsRef.current.end.diff(start, "day")) {
124
+ stableBoundsRef.current = { start, end: start.add(neededEndDay, "day") };
125
+ }
126
+ moveDayIndexRef.current = dayIndex;
127
+ setMoveLeft(dayIndex * effectiveColumnWidth);
128
+ }, [effectiveColumnWidth]);
84
129
  const handleTimelineScroll = React.useCallback(() => {
85
130
  if (timelineBodyRef.current && taskListBodyRef.current) {
86
131
  taskListBodyRef.current.scrollTop = timelineBodyRef.current.scrollTop;
@@ -88,12 +133,56 @@ const Gantt = core.factory((_props, ref) => {
88
133
  if (timelineBodyRef.current && timelineHeaderRef.current) {
89
134
  timelineHeaderRef.current.scrollLeft = timelineBodyRef.current.scrollLeft;
90
135
  }
136
+ if (moveInfoRef.current) {
137
+ recomputeMove();
138
+ }
139
+ }, [recomputeMove]);
140
+ React.useEffect(() => {
141
+ if (activeDragType !== "move" || !activeDragId) {
142
+ return void 0;
143
+ }
144
+ const onPointerMove = (e) => {
145
+ lastClientXRef.current = e.clientX;
146
+ recomputeMove();
147
+ };
148
+ window.addEventListener("pointermove", onPointerMove);
149
+ return () => window.removeEventListener("pointermove", onPointerMove);
150
+ }, [activeDragId, activeDragType, recomputeMove]);
151
+ React.useEffect(() => {
152
+ const node = timelineBodyRef.current;
153
+ if (!node) {
154
+ return void 0;
155
+ }
156
+ setViewportWidth(node.clientWidth);
157
+ if (typeof ResizeObserver === "undefined") {
158
+ return void 0;
159
+ }
160
+ const observer = new ResizeObserver((entries) => {
161
+ setViewportWidth(entries[0].contentRect.width);
162
+ });
163
+ observer.observe(node);
164
+ return () => observer.disconnect();
91
165
  }, []);
92
166
  const handleTaskListScroll = React.useCallback(() => {
93
167
  if (timelineBodyRef.current && taskListBodyRef.current) {
94
168
  timelineBodyRef.current.scrollTop = taskListBodyRef.current.scrollTop;
95
169
  }
96
170
  }, []);
171
+ const prevStartRef = React.useRef(bounds.start);
172
+ React.useLayoutEffect(() => {
173
+ const prev = prevStartRef.current;
174
+ if (!prev.isSame(bounds.start, "day")) {
175
+ const deltaDays = bounds.start.diff(prev, "day");
176
+ const px = -deltaDays * effectiveColumnWidth;
177
+ if (timelineBodyRef.current) {
178
+ timelineBodyRef.current.scrollLeft += px;
179
+ }
180
+ if (timelineHeaderRef.current) {
181
+ timelineHeaderRef.current.scrollLeft += px;
182
+ }
183
+ prevStartRef.current = bounds.start;
184
+ }
185
+ });
97
186
  const sensors = core$1.useSensors(
98
187
  core$1.useSensor(core$1.PointerSensor, {
99
188
  activationConstraint: {
@@ -101,19 +190,48 @@ const Gantt = core.factory((_props, ref) => {
101
190
  }
102
191
  })
103
192
  );
104
- const handleDragStart = React.useCallback((event) => {
105
- const { active } = event;
106
- const data = active.data.current;
107
- setActiveDragId(data.taskId);
108
- setActiveDragType(data.type);
109
- setDragDelta(0);
110
- }, []);
193
+ const handleDragStart = React.useCallback(
194
+ (event) => {
195
+ const { active } = event;
196
+ const data = active.data.current;
197
+ if (data.type === "move") {
198
+ const task = tasks.find((t) => t.id === data.taskId);
199
+ const content = timelineContentRef.current;
200
+ const pe = event.activatorEvent;
201
+ if (task && content && typeof pe?.clientX === "number") {
202
+ const baseLeft = utils.dateToPixel(
203
+ task.startDate,
204
+ stableBoundsRef.current.start,
205
+ effectiveColumnWidth
206
+ );
207
+ const rect = content.getBoundingClientRect();
208
+ lastClientXRef.current = pe.clientX;
209
+ moveInfoRef.current = {
210
+ taskId: data.taskId,
211
+ grabOffsetX: pe.clientX - rect.left - baseLeft,
212
+ duration: task.duration
213
+ };
214
+ }
215
+ setMoveLeft(null);
216
+ }
217
+ if (data.type !== "link") {
218
+ stableBoundsRef.current = {
219
+ start: calculatedBounds.start,
220
+ end: calculatedBounds.end.add(DRAG_BUFFER_DAYS, "day")
221
+ };
222
+ }
223
+ setActiveDragId(data.taskId);
224
+ setActiveDragType(data.type);
225
+ setDragDelta(0);
226
+ },
227
+ [calculatedBounds, tasks, effectiveColumnWidth]
228
+ );
111
229
  const handleDragMove = React.useCallback(
112
230
  (event) => {
113
- const snappedDelta = utils.snapToGrid(event.delta.x, columnWidth);
231
+ const snappedDelta = utils.snapToGrid(event.delta.x, effectiveColumnWidth);
114
232
  setDragDelta(snappedDelta);
115
233
  },
116
- [columnWidth]
234
+ [effectiveColumnWidth]
117
235
  );
118
236
  const handleDragEnd = React.useCallback(
119
237
  (event) => {
@@ -150,8 +268,33 @@ const Gantt = core.factory((_props, ref) => {
150
268
  setDragDelta(0);
151
269
  return;
152
270
  }
153
- const snappedDelta = utils.snapToGrid(delta.x, columnWidth);
154
- const daysDelta = Math.round(snappedDelta / columnWidth);
271
+ if (data.type === "move") {
272
+ const newStart = stableBoundsRef.current.start.add(moveDayIndexRef.current, "day");
273
+ const current = tasks.find((t) => t.id === data.taskId);
274
+ const changed = !current || !dayjs(current.startDate).isSame(newStart, "day");
275
+ reactDom.flushSync(() => {
276
+ if (changed) {
277
+ setTasks((currentTasks) => {
278
+ const updatedTasks = currentTasks.map(
279
+ (task) => task.id === data.taskId ? { ...task, startDate: newStart.format("YYYY-MM-DD") } : task
280
+ );
281
+ const updatedTask = updatedTasks.find((t) => t.id === data.taskId);
282
+ if (updatedTask && onTaskUpdate) {
283
+ onTaskUpdate(updatedTask);
284
+ }
285
+ return updatedTasks;
286
+ });
287
+ }
288
+ moveInfoRef.current = null;
289
+ setMoveLeft(null);
290
+ setActiveDragId(null);
291
+ setActiveDragType(null);
292
+ setDragDelta(0);
293
+ });
294
+ return;
295
+ }
296
+ const snappedDelta = utils.snapToGrid(delta.x, effectiveColumnWidth);
297
+ const daysDelta = Math.round(snappedDelta / effectiveColumnWidth);
155
298
  if (daysDelta !== 0) {
156
299
  reactDom.flushSync(() => {
157
300
  setTasks((currentTasks) => {
@@ -159,13 +302,7 @@ const Gantt = core.factory((_props, ref) => {
159
302
  if (task.id !== data.taskId) {
160
303
  return task;
161
304
  }
162
- if (data.type === "move") {
163
- const newStartDate2 = dayjs(task.startDate).add(daysDelta, "day");
164
- return {
165
- ...task,
166
- startDate: newStartDate2.format("YYYY-MM-DD")
167
- };
168
- } else if (data.type === "resize-end") {
305
+ if (data.type === "resize-end") {
169
306
  const newDuration2 = Math.max(1, task.duration + daysDelta);
170
307
  return {
171
308
  ...task,
@@ -196,11 +333,11 @@ const Gantt = core.factory((_props, ref) => {
196
333
  setDragDelta(0);
197
334
  }
198
335
  },
199
- [columnWidth, onTaskUpdate, onLinkCreate]
336
+ [effectiveColumnWidth, onTaskUpdate, onLinkCreate, tasks]
200
337
  );
201
338
  const today = dayjs();
202
- const todayPosition = utils.dateToPixel(today, bounds.start, columnWidth);
203
- const showTodayLine = today.isAfter(bounds.start) && today.isBefore(bounds.end);
339
+ const todayPosition = utils.dateToPixel(today, bounds.start, effectiveColumnWidth);
340
+ const showTodayLine = showTodayMarker && today.isAfter(bounds.start) && today.isBefore(displayEnd);
204
341
  return /* @__PURE__ */ React.createElement(core.Box, { ref, ...getStyles("root"), ...others }, /* @__PURE__ */ React.createElement(
205
342
  TaskList.TaskList,
206
343
  {
@@ -213,10 +350,11 @@ const Gantt = core.factory((_props, ref) => {
213
350
  TimelineHeader.TimelineHeader,
214
351
  {
215
352
  startDate: bounds.start,
216
- endDate: bounds.end,
217
- columnWidth,
353
+ endDate: displayEnd,
354
+ columnWidth: effectiveColumnWidth,
218
355
  getStyles,
219
- totalWidth: timelineWidth
356
+ totalWidth: timelineWidth,
357
+ viewMode
220
358
  }
221
359
  )), /* @__PURE__ */ React.createElement(
222
360
  core$1.DndContext,
@@ -230,49 +368,61 @@ const Gantt = core.factory((_props, ref) => {
230
368
  "div",
231
369
  {
232
370
  ...getStyles("timelineContent"),
371
+ ref: timelineContentRef,
233
372
  style: { width: timelineWidth, height: tasks.length * rowHeight }
234
373
  },
235
374
  /* @__PURE__ */ React.createElement(
236
375
  TimelineGrid.TimelineGrid,
237
376
  {
238
377
  startDate: bounds.start,
239
- endDate: bounds.end,
240
- columnWidth,
378
+ endDate: displayEnd,
379
+ columnWidth: effectiveColumnWidth,
241
380
  rowCount: tasks.length,
242
381
  rowHeight,
243
- getStyles
382
+ getStyles,
383
+ viewMode
244
384
  }
245
385
  ),
246
386
  showTodayLine && /* @__PURE__ */ React.createElement("div", { className: Gantt_module.todayLine, style: { left: todayPosition } }),
247
- tasks.map((task, index) => /* @__PURE__ */ React.createElement("div", { key: task.id, ...getStyles("timelineRow"), style: { top: index * rowHeight } }, /* @__PURE__ */ React.createElement(
248
- TaskBar.TaskBar,
387
+ tasks.map((task, index) => /* @__PURE__ */ React.createElement(
388
+ "div",
249
389
  {
250
- key: `taskbar-${task.id}`,
251
- task,
252
- startDate: bounds.start,
253
- columnWidth,
254
- getStyles,
255
- isDragging: activeDragId === task.id,
256
- isLinkDragging: activeDragType === "link",
257
- linkSourceId: activeDragType === "link" ? activeDragId : null,
258
- showTitle,
259
- onClick: () => onTaskClick?.(task)
260
- }
261
- ))),
390
+ key: task.id,
391
+ ...getStyles("timelineRow"),
392
+ title: showTitle ? task.label : void 0,
393
+ style: { top: index * rowHeight }
394
+ },
395
+ /* @__PURE__ */ React.createElement(
396
+ TaskBar.TaskBar,
397
+ {
398
+ key: `taskbar-${task.id}`,
399
+ task,
400
+ startDate: bounds.start,
401
+ columnWidth: effectiveColumnWidth,
402
+ getStyles,
403
+ isDragging: activeDragId === task.id,
404
+ isLinkDragging: activeDragType === "link",
405
+ linkSourceId: activeDragType === "link" ? activeDragId : null,
406
+ overrideLeft: activeDragType === "move" && activeDragId === task.id ? moveLeft : null,
407
+ onClick: () => onTaskClick?.(task)
408
+ }
409
+ )
410
+ )),
262
411
  /* @__PURE__ */ React.createElement(
263
412
  DependencyLinks.DependencyLinks,
264
413
  {
265
414
  tasks,
266
415
  startDate: bounds.start,
267
- columnWidth,
416
+ columnWidth: effectiveColumnWidth,
268
417
  rowHeight,
418
+ getStyles,
269
419
  activeDragId,
270
420
  activeDragType,
271
421
  dragDelta
272
422
  }
273
423
  )
274
424
  )),
275
- /* @__PURE__ */ React.createElement(core$1.DragOverlay, null, activeDragType === "link" && /* @__PURE__ */ React.createElement(
425
+ /* @__PURE__ */ React.createElement(core$1.DragOverlay, { dropAnimation: null }, activeDragType === "link" && /* @__PURE__ */ React.createElement(
276
426
  "div",
277
427
  {
278
428
  style: {