Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions apps/web/src/components/calendar/event/draggable-event.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,27 @@ export function DraggableEvent({
const updateAction = usePartialUpdateAction();
const { setCursor, resetCursor } = useGlobalCursor();

const elementRef = React.useRef<HTMLDivElement>(null);
const onDragStart = (e: PointerEvent, info: PanInfo) => {
if (!containerRef.current || !elementRef.current) {
return;
}

const eventRect = elementRef.current.getBoundingClientRect();
if (
info.point.x < eventRect.left ||
info.point.x > eventRect.right ||
info.point.y < eventRect.top ||
info.point.y > eventRect.bottom
) {
return;
}

// Prevent possible text/image dragging flash on some browsers
e.preventDefault();

addDraggedEventId(item.event.id);

if (!containerRef.current) {
return;
}

const rect = containerRef.current.getBoundingClientRect();

dragStartRelative.current = {
Expand Down Expand Up @@ -198,6 +209,11 @@ export function DraggableEvent({
);

const onDragEnd = (_e: PointerEvent, info: PanInfo) => {
// If drag never started (out of bounds), don't do anything
if (dragStartRelative.current === null) {
return;
}

removeDraggedEventId(item.event.id);
// Do not reset transform immediately to avoid flashback to original
// position. We'll reset when the event data updates optimistically.
Expand All @@ -211,7 +227,7 @@ export function DraggableEvent({
// Calculate vertical movement relative to the container so that auto-scroll is taken into account.
let deltaY = info.offset.y;

if (containerRef.current && dragStartRelative.current !== null) {
if (containerRef.current) {
const rect = containerRef.current.getBoundingClientRect();
const relativeCurrentY = info.point.y - rect.top;
deltaY = relativeCurrentY - dragStartRelative.current.y;
Expand Down Expand Up @@ -397,6 +413,7 @@ export function DraggableEvent({
<motion.div
className="size-full touch-none"
style={{ transform, height, top, zIndex }}
ref={elementRef}
>
<EventContextMenu event={item.event}>
<ContextMenuTrigger>
Expand Down Expand Up @@ -431,6 +448,7 @@ export function DraggableEvent({
<motion.div
className="size-full touch-none"
style={{ transform, height, zIndex }}
ref={elementRef}
>
<EventContextMenu event={item.event}>
<ContextMenuTrigger>
Expand Down