mantine-gantt 0.0.4 → 0.2.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 (41) hide show
  1. package/dist/cjs/Gantt/DependencyLinks.cjs +17 -6
  2. package/dist/cjs/Gantt/DependencyLinks.cjs.map +1 -1
  3. package/dist/cjs/Gantt/Gantt.cjs +136 -143
  4. package/dist/cjs/Gantt/Gantt.cjs.map +1 -1
  5. package/dist/cjs/Gantt/TaskBar.cjs +37 -116
  6. package/dist/cjs/Gantt/TaskBar.cjs.map +1 -1
  7. package/dist/cjs/Gantt/TaskList.cjs +30 -2
  8. package/dist/cjs/Gantt/TaskList.cjs.map +1 -1
  9. package/dist/cjs/Gantt/TimelineGrid.cjs +7 -18
  10. package/dist/cjs/Gantt/TimelineGrid.cjs.map +1 -1
  11. package/dist/cjs/Gantt/TimelineHeader.cjs +13 -19
  12. package/dist/cjs/Gantt/TimelineHeader.cjs.map +1 -1
  13. package/dist/cjs/Gantt/use-gantt-drag.cjs +243 -0
  14. package/dist/cjs/Gantt/use-gantt-drag.cjs.map +1 -0
  15. package/dist/esm/Gantt/DependencyLinks.mjs +17 -6
  16. package/dist/esm/Gantt/DependencyLinks.mjs.map +1 -1
  17. package/dist/esm/Gantt/Gantt.mjs +139 -146
  18. package/dist/esm/Gantt/Gantt.mjs.map +1 -1
  19. package/dist/esm/Gantt/TaskBar.mjs +39 -118
  20. package/dist/esm/Gantt/TaskBar.mjs.map +1 -1
  21. package/dist/esm/Gantt/TaskList.mjs +30 -2
  22. package/dist/esm/Gantt/TaskList.mjs.map +1 -1
  23. package/dist/esm/Gantt/TimelineGrid.mjs +7 -18
  24. package/dist/esm/Gantt/TimelineGrid.mjs.map +1 -1
  25. package/dist/esm/Gantt/TimelineHeader.mjs +13 -19
  26. package/dist/esm/Gantt/TimelineHeader.mjs.map +1 -1
  27. package/dist/esm/Gantt/use-gantt-drag.mjs +241 -0
  28. package/dist/esm/Gantt/use-gantt-drag.mjs.map +1 -0
  29. package/dist/styles.css +1 -1
  30. package/dist/styles.layer.css +1 -1
  31. package/dist/types/Gantt/DependencyLinks.d.ts +13 -3
  32. package/dist/types/Gantt/TaskBar.d.ts +11 -4
  33. package/dist/types/Gantt/TaskList.d.ts +3 -2
  34. package/dist/types/Gantt/TimelineGrid.d.ts +1 -1
  35. package/dist/types/Gantt/TimelineHeader.d.ts +1 -1
  36. package/dist/types/Gantt/index.d.ts +1 -1
  37. package/dist/types/Gantt/types.d.ts +15 -7
  38. package/dist/types/Gantt/use-gantt-drag.d.ts +46 -0
  39. package/dist/types/index.d.mts +1 -1
  40. package/dist/types/index.d.ts +1 -1
  41. package/package.json +1 -3
@@ -1,17 +1,17 @@
1
1
  'use client';
2
2
  import dayjs from 'dayjs';
3
- import React, { useMemo, useState, useRef, useCallback } from 'react';
4
- import { useSensors, useSensor, PointerSensor, DndContext, DragOverlay } from '@dnd-kit/core';
5
- import { flushSync } from 'react-dom';
6
- import { createVarsResolver, factory, useProps, useStyles, Box } from '@mantine/core';
3
+ import React, { useMemo, useState, useRef, useCallback, useEffect, useLayoutEffect } from 'react';
4
+ import { createVarsResolver, factory, useProps, useStyles, Box, VisuallyHidden } from '@mantine/core';
7
5
  import { DependencyLinks } from './DependencyLinks.mjs';
8
6
  import { TaskBar } from './TaskBar.mjs';
9
7
  import { TaskList } from './TaskList.mjs';
10
8
  import { TimelineGrid } from './TimelineGrid.mjs';
11
9
  import { TimelineHeader } from './TimelineHeader.mjs';
12
- import { calculateTimelineBounds, snapToGrid, dateToPixel } from './utils.mjs';
10
+ import { useGanttDrag } from './use-gantt-drag.mjs';
11
+ import { calculateTimelineBounds, dateToPixel, durationToPixels } from './utils.mjs';
13
12
  import classes from './Gantt.module.css.mjs';
14
13
 
14
+ const DRAG_BUFFER_DAYS = 30;
15
15
  const defaultProps = {
16
16
  columnWidth: 40,
17
17
  rowHeight: 44,
@@ -39,7 +39,9 @@ const Gantt = factory((_props, ref) => {
39
39
  styles,
40
40
  unstyled,
41
41
  vars,
42
+ attributes,
42
43
  tasks: initialTasks,
44
+ columns,
43
45
  onTaskUpdate,
44
46
  onTaskClick,
45
47
  onLinkCreate,
@@ -63,6 +65,7 @@ const Gantt = factory((_props, ref) => {
63
65
  styles,
64
66
  unstyled,
65
67
  vars,
68
+ attributes,
66
69
  varsResolver
67
70
  });
68
71
  const effectiveColumnWidth = useMemo(() => {
@@ -79,23 +82,48 @@ const Gantt = factory((_props, ref) => {
79
82
  }
80
83
  }, [viewMode, columnWidth]);
81
84
  const [tasks, setTasks] = useState(initialTasks);
82
- const [activeDragId, setActiveDragId] = useState(null);
83
- const [activeDragType, setActiveDragType] = useState(null);
84
- const [dragDelta, setDragDelta] = useState(0);
85
+ const [announcement, setAnnouncement] = useState("");
86
+ const [viewportWidth, setViewportWidth] = useState(0);
87
+ const timelineBodyRef = useRef(null);
88
+ const taskListBodyRef = useRef(null);
89
+ const timelineHeaderRef = useRef(null);
90
+ const timelineContentRef = useRef(null);
91
+ const drag = useGanttDrag({
92
+ tasks,
93
+ setTasks,
94
+ columnWidth: effectiveColumnWidth,
95
+ bodyRef: timelineBodyRef,
96
+ contentRef: timelineContentRef,
97
+ onTaskUpdate,
98
+ onLinkCreate,
99
+ announce: setAnnouncement
100
+ });
101
+ const active = drag.state;
85
102
  const calculatedBounds = useMemo(
86
103
  () => calculateTimelineBounds(tasks, startDate, endDate),
87
104
  [tasks, startDate, endDate]
88
105
  );
89
106
  const stableBoundsRef = useRef(calculatedBounds);
90
- if (!activeDragId) {
107
+ if (!active) {
91
108
  stableBoundsRef.current = calculatedBounds;
92
109
  }
93
- const bounds = stableBoundsRef.current;
94
- const totalDays = bounds.end.diff(bounds.start, "day") + 1;
110
+ let bounds = stableBoundsRef.current;
111
+ if (active && active.type !== "link") {
112
+ const task = tasks.find((t) => t.id === active.taskId);
113
+ if (task) {
114
+ const startDay = dayjs(task.startDate).diff(bounds.start, "day");
115
+ const barEndDay = startDay + task.duration + Math.round(active.deltaX / effectiveColumnWidth);
116
+ const needed = bounds.start.add(barEndDay + DRAG_BUFFER_DAYS, "day");
117
+ if (needed.isAfter(bounds.end)) {
118
+ bounds = { start: bounds.start, end: needed };
119
+ stableBoundsRef.current = bounds;
120
+ }
121
+ }
122
+ }
123
+ const fillDays = effectiveColumnWidth > 0 ? Math.ceil(viewportWidth / effectiveColumnWidth) : 0;
124
+ const totalDays = Math.max(bounds.end.diff(bounds.start, "day") + 1, fillDays);
125
+ const displayEnd = bounds.start.add(totalDays - 1, "day");
95
126
  const timelineWidth = totalDays * effectiveColumnWidth;
96
- const timelineBodyRef = useRef(null);
97
- const taskListBodyRef = useRef(null);
98
- const timelineHeaderRef = useRef(null);
99
127
  const handleTimelineScroll = useCallback(() => {
100
128
  if (timelineBodyRef.current && taskListBodyRef.current) {
101
129
  taskListBodyRef.current.scrollTop = timelineBodyRef.current.scrollTop;
@@ -104,122 +132,90 @@ const Gantt = factory((_props, ref) => {
104
132
  timelineHeaderRef.current.scrollLeft = timelineBodyRef.current.scrollLeft;
105
133
  }
106
134
  }, []);
135
+ useEffect(() => {
136
+ const node = timelineBodyRef.current;
137
+ if (!node) {
138
+ return void 0;
139
+ }
140
+ setViewportWidth(node.clientWidth);
141
+ if (typeof ResizeObserver === "undefined") {
142
+ return void 0;
143
+ }
144
+ const observer = new ResizeObserver((entries) => {
145
+ setViewportWidth(entries[0].contentRect.width);
146
+ });
147
+ observer.observe(node);
148
+ return () => observer.disconnect();
149
+ }, []);
107
150
  const handleTaskListScroll = useCallback(() => {
108
151
  if (timelineBodyRef.current && taskListBodyRef.current) {
109
152
  timelineBodyRef.current.scrollTop = taskListBodyRef.current.scrollTop;
110
153
  }
111
154
  }, []);
112
- const sensors = useSensors(
113
- useSensor(PointerSensor, {
114
- activationConstraint: {
115
- distance: 5
116
- }
117
- })
118
- );
119
- const handleDragStart = useCallback((event) => {
120
- const { active } = event;
121
- const data = active.data.current;
122
- setActiveDragId(data.taskId);
123
- setActiveDragType(data.type);
124
- setDragDelta(0);
155
+ const panRef = useRef(null);
156
+ const handlePanStart = useCallback((e) => {
157
+ const body = timelineBodyRef.current;
158
+ if (e.pointerType !== "mouse" || !body) {
159
+ return;
160
+ }
161
+ e.preventDefault();
162
+ panRef.current = { x: e.clientX, y: e.clientY, left: body.scrollLeft, top: body.scrollTop };
163
+ body.setPointerCapture?.(e.pointerId);
164
+ body.style.cursor = "grabbing";
165
+ document.body.style.userSelect = "none";
125
166
  }, []);
126
- const handleDragMove = useCallback(
127
- (event) => {
128
- const snappedDelta = snapToGrid(event.delta.x, effectiveColumnWidth);
129
- setDragDelta(snappedDelta);
130
- },
131
- [effectiveColumnWidth]
132
- );
133
- const handleDragEnd = useCallback(
134
- (event) => {
135
- const { active, over, delta } = event;
136
- const data = active.data.current;
137
- if (data.type === "link") {
138
- if (over) {
139
- const overData = over.data.current;
140
- if (overData && overData.taskId !== data.taskId) {
141
- const fromTaskId = data.taskId;
142
- const toTaskId = overData.taskId;
143
- setTasks(
144
- (currentTasks) => currentTasks.map((task) => {
145
- if (task.id !== toTaskId) {
146
- return task;
147
- }
148
- const deps = task.dependencies || [];
149
- if (deps.includes(fromTaskId)) {
150
- return task;
151
- }
152
- return {
153
- ...task,
154
- dependencies: [...deps, fromTaskId]
155
- };
156
- })
157
- );
158
- if (onLinkCreate) {
159
- onLinkCreate(fromTaskId, toTaskId);
160
- }
161
- }
162
- }
163
- setActiveDragId(null);
164
- setActiveDragType(null);
165
- setDragDelta(0);
166
- return;
167
+ const handlePanMove = useCallback((e) => {
168
+ const pan = panRef.current;
169
+ const body = timelineBodyRef.current;
170
+ if (!pan || !body) {
171
+ return;
172
+ }
173
+ body.scrollLeft = pan.left - (e.clientX - pan.x);
174
+ body.scrollTop = pan.top - (e.clientY - pan.y);
175
+ }, []);
176
+ const handlePanEnd = useCallback(() => {
177
+ panRef.current = null;
178
+ document.body.style.userSelect = "";
179
+ if (timelineBodyRef.current) {
180
+ timelineBodyRef.current.style.cursor = "";
181
+ }
182
+ }, []);
183
+ const prevStartRef = useRef(bounds.start);
184
+ useLayoutEffect(() => {
185
+ const prev = prevStartRef.current;
186
+ if (!prev.isSame(bounds.start, "day")) {
187
+ const deltaDays = bounds.start.diff(prev, "day");
188
+ const px = -deltaDays * effectiveColumnWidth;
189
+ if (timelineBodyRef.current) {
190
+ timelineBodyRef.current.scrollLeft += px;
167
191
  }
168
- const snappedDelta = snapToGrid(delta.x, effectiveColumnWidth);
169
- const daysDelta = Math.round(snappedDelta / effectiveColumnWidth);
170
- if (daysDelta !== 0) {
171
- flushSync(() => {
172
- setTasks((currentTasks) => {
173
- const updatedTasks = currentTasks.map((task) => {
174
- if (task.id !== data.taskId) {
175
- return task;
176
- }
177
- if (data.type === "move") {
178
- const newStartDate2 = dayjs(task.startDate).add(daysDelta, "day");
179
- return {
180
- ...task,
181
- startDate: newStartDate2.format("YYYY-MM-DD")
182
- };
183
- } else if (data.type === "resize-end") {
184
- const newDuration2 = Math.max(1, task.duration + daysDelta);
185
- return {
186
- ...task,
187
- duration: newDuration2
188
- };
189
- }
190
- const newDuration = Math.max(1, task.duration - daysDelta);
191
- const newStartDate = dayjs(task.startDate).add(daysDelta, "day");
192
- return {
193
- ...task,
194
- startDate: newStartDate.format("YYYY-MM-DD"),
195
- duration: newDuration
196
- };
197
- });
198
- const updatedTask = updatedTasks.find((t) => t.id === data.taskId);
199
- if (updatedTask && onTaskUpdate) {
200
- onTaskUpdate(updatedTask);
201
- }
202
- return updatedTasks;
203
- });
204
- setActiveDragId(null);
205
- setActiveDragType(null);
206
- setDragDelta(0);
207
- });
208
- } else {
209
- setActiveDragId(null);
210
- setActiveDragType(null);
211
- setDragDelta(0);
192
+ if (timelineHeaderRef.current) {
193
+ timelineHeaderRef.current.scrollLeft += px;
212
194
  }
213
- },
214
- [effectiveColumnWidth, onTaskUpdate, onLinkCreate]
215
- );
195
+ prevStartRef.current = bounds.start;
196
+ }
197
+ });
198
+ const linkPreview = useMemo(() => {
199
+ if (active?.type !== "link" || !active.linkCursor) {
200
+ return null;
201
+ }
202
+ const srcIndex = tasks.findIndex((t) => t.id === active.taskId);
203
+ if (srcIndex === -1) {
204
+ return null;
205
+ }
206
+ const src = tasks[srcIndex];
207
+ const x1 = dateToPixel(src.startDate, bounds.start, effectiveColumnWidth) + durationToPixels(src.duration, effectiveColumnWidth);
208
+ const y1 = srcIndex * rowHeight + rowHeight / 2;
209
+ return { x1, y1, x2: active.linkCursor.x, y2: active.linkCursor.y };
210
+ }, [active, tasks, bounds.start, effectiveColumnWidth, rowHeight]);
216
211
  const today = dayjs();
217
212
  const todayPosition = dateToPixel(today, bounds.start, effectiveColumnWidth);
218
- const showTodayLine = showTodayMarker && today.isAfter(bounds.start) && today.isBefore(bounds.end);
213
+ const showTodayLine = showTodayMarker && today.isAfter(bounds.start) && today.isBefore(displayEnd);
219
214
  return /* @__PURE__ */ React.createElement(Box, { ref, ...getStyles("root"), ...others }, /* @__PURE__ */ React.createElement(
220
215
  TaskList,
221
216
  {
222
217
  tasks,
218
+ columns,
223
219
  getStyles,
224
220
  bodyRef: taskListBodyRef,
225
221
  onScroll: handleTaskListScroll
@@ -228,31 +224,35 @@ const Gantt = factory((_props, ref) => {
228
224
  TimelineHeader,
229
225
  {
230
226
  startDate: bounds.start,
231
- endDate: bounds.end,
227
+ endDate: displayEnd,
232
228
  columnWidth: effectiveColumnWidth,
233
229
  getStyles,
234
230
  totalWidth: timelineWidth,
235
231
  viewMode
236
232
  }
237
233
  )), /* @__PURE__ */ React.createElement(
238
- DndContext,
234
+ "div",
239
235
  {
240
- sensors,
241
- onDragStart: handleDragStart,
242
- onDragMove: handleDragMove,
243
- onDragEnd: handleDragEnd
236
+ ...getStyles("timelineBody"),
237
+ ref: timelineBodyRef,
238
+ onScroll: handleTimelineScroll,
239
+ onPointerDown: handlePanStart,
240
+ onPointerMove: handlePanMove,
241
+ onPointerUp: handlePanEnd,
242
+ onPointerCancel: handlePanEnd
244
243
  },
245
- /* @__PURE__ */ React.createElement("div", { ...getStyles("timelineBody"), ref: timelineBodyRef, onScroll: handleTimelineScroll }, /* @__PURE__ */ React.createElement(
244
+ /* @__PURE__ */ React.createElement(
246
245
  "div",
247
246
  {
248
247
  ...getStyles("timelineContent"),
248
+ ref: timelineContentRef,
249
249
  style: { width: timelineWidth, height: tasks.length * rowHeight }
250
250
  },
251
251
  /* @__PURE__ */ React.createElement(
252
252
  TimelineGrid,
253
253
  {
254
254
  startDate: bounds.start,
255
- endDate: bounds.end,
255
+ endDate: displayEnd,
256
256
  columnWidth: effectiveColumnWidth,
257
257
  rowCount: tasks.length,
258
258
  rowHeight,
@@ -260,7 +260,7 @@ const Gantt = factory((_props, ref) => {
260
260
  viewMode
261
261
  }
262
262
  ),
263
- showTodayLine && /* @__PURE__ */ React.createElement("div", { className: classes.todayLine, style: { left: todayPosition } }),
263
+ showTodayLine && /* @__PURE__ */ React.createElement("div", { ...getStyles("todayLine", { style: { left: todayPosition } }) }),
264
264
  tasks.map((task, index) => /* @__PURE__ */ React.createElement(
265
265
  "div",
266
266
  {
@@ -277,9 +277,13 @@ const Gantt = factory((_props, ref) => {
277
277
  startDate: bounds.start,
278
278
  columnWidth: effectiveColumnWidth,
279
279
  getStyles,
280
- isDragging: activeDragId === task.id,
281
- isLinkDragging: activeDragType === "link",
282
- linkSourceId: activeDragType === "link" ? activeDragId : null,
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,
283
287
  onClick: () => onTaskClick?.(task)
284
288
  }
285
289
  )
@@ -291,26 +295,15 @@ const Gantt = factory((_props, ref) => {
291
295
  startDate: bounds.start,
292
296
  columnWidth: effectiveColumnWidth,
293
297
  rowHeight,
294
- activeDragId,
295
- activeDragType,
296
- dragDelta
298
+ getStyles,
299
+ activeDragId: active && active.type !== "link" ? active.taskId : null,
300
+ activeDragType: active && active.type !== "link" ? active.type : null,
301
+ dragDelta: active && active.type !== "link" ? active.deltaX : 0,
302
+ linkPreview
297
303
  }
298
304
  )
299
- )),
300
- /* @__PURE__ */ React.createElement(DragOverlay, null, activeDragType === "link" && /* @__PURE__ */ React.createElement(
301
- "div",
302
- {
303
- style: {
304
- width: 16,
305
- height: 16,
306
- borderRadius: "50%",
307
- backgroundColor: "var(--mantine-primary-color-filled)",
308
- border: "2px solid white",
309
- boxShadow: "0 2px 8px rgba(0,0,0,0.3)"
310
- }
311
- }
312
- ))
313
- )));
305
+ )
306
+ )), /* @__PURE__ */ React.createElement(VisuallyHidden, { "aria-live": "polite" }, announcement));
314
307
  });
315
308
  Gantt.displayName = "Gantt";
316
309
  Gantt.classes = classes;