Feature: Drag and Drop Events
Relevant source files
The following files were used as context for generating this wiki page:
- apps/web/src/components/calendar/hooks/use-drag-to-create.tsx
- apps/web/src/components/calendar/event/draggable-event.tsx
- apps/web/src/atoms/drag-resize-state.ts
- apps/web/src/components/calendar/week-view/week-view-column.tsx
- apps/web/src/components/calendar/day-view/day-view.tsx
- apps/web/src/components/calendar/event/drag-aware-wrapper.tsx
The drag and drop feature is a core part of the Analog calendar's user experience, enabling intuitive event manipulation directly on the calendar grid. It encompasses three main interactions: creating new events by dragging on an empty time slot, moving existing events to a new time or day, and resizing existing events to change their duration.
This system is primarily built using the framer-motion library for gesture detection and animation, and Jotai for global state management during drag and resize operations. The implementation is split between dedicated React hooks for creating events (useDragToCreate) and a comprehensive component (DraggableEvent) for handling interactions with existing events.
Architecture Overview
The feature is composed of several key parts: global state atoms, specialized hooks for different drag actions, and wrapper components that apply the drag and resize behavior.
State Management
Global state for drag, drop, and resize operations is managed using Jotai atoms. This allows different components to react to and share the current drag state without prop drilling.
The diagram below shows the primary atoms and their purpose.
The key state atoms are:
| Atom | Type | Description |
|---|---|---|
isDraggingAtom | boolean | A global flag that is true when a "drag-to-create" action is in progress. |
isResizingAtom | boolean | A global flag that is true when an event is being resized. |
draggingAtom | `string | null` |
addDraggedEventIdAtom | WritableAtom | An action atom to set the draggingAtom with an event ID. |
removeDraggedEventIdAtom | WritableAtom | An action atom to clear the draggingAtom. |
Sources: apps/web/src/atoms/drag-resize-state.ts, apps/web/src/components/calendar/event/draggable-event.tsx:64-66, apps/web/src/components/calendar/hooks/use-drag-to-create.tsx:151
Drag to Create
Users can create a new event by clicking and dragging on an empty time slot in the Day or Week views. This functionality is encapsulated in the useDragToCreate hook.
Sources: apps/web/src/components/calendar/hooks/use-drag-to-create.tsx
Implementation Details
The useDragToCreate hook is used within the WeekViewDayColumn and DayViewTimeSlots components. It attaches onPanStart, onPan, and onPanEnd event handlers from framer-motion to the calendar column.
onDragStart:- Sets
isDraggingAtomtotrue. - Records the initial pointer position (Y-coordinate) and converts it to minutes.
- Sets the initial
topposition of the drag preview and makes it visible. - Sets the global cursor to
row-resize.
- Sets
onDrag:- Calculates the current pointer position relative to the column.
- Updates the
heightandtopof the drag preview element based on the drag direction (up or down from the start point). - Positions are "snapped" to 15-minute intervals.
onDragEnd:- Sets
isDraggingAtomtofalseand resets the cursor. - Calculates the final
startandendtimes based on the initial and final pointer positions. - Rounds the times to the nearest 15-minute increment.
- Calls
createDraftActionto create a new draft event with the calculated times, which triggers an optimistic UI update. - Hides the drag preview.
- Sets
The drag can be cancelled by pressing the Escape key, which is handled by a useHotkeys hook.
Sources: apps/web/src/components/calendar/hooks/use-drag-to-create.tsx:94-245, apps/web/src/components/calendar/week-view/week-view-column.tsx:75-81, apps/web/src/components/calendar/day-view/day-view.tsx:180-186
Sequence Diagram
This diagram illustrates the flow of creating a new event via dragging.
Drag to Move and Resize
Existing events are wrapped in the DraggableEvent component, which provides the logic for moving and resizing. It uses framer-motion's motion.div and its onPan handlers.
Sources: apps/web/src/components/calendar/event/draggable-event.tsx
Moving Events
An event can be moved by dragging its main body.
onDragStart:- The
addDraggedEventIdAtomaction is called to store the event's ID in the globaldraggingAtom. This is used byDragAwareWrapperto increase the element'sz-index, ensuring it appears above other events. - The initial relative pointer position within the container is recorded.
- The
onDrag:- The component's
leftandtopmotion values are updated based on the pointer's movement, creating the visual effect of dragging.
- The component's
onDragEnd:- The
removeDraggedEventIdAtomaction clears the global state. - The total
deltaXanddeltaYof the drag are calculated. - These deltas are converted into a change in days and/or minutes.
- In Month view,
deltaXanddeltaYare converted to a number of days. - In Week/Day view,
deltaXis converted to days anddeltaYis converted to minutes.
- In Month view,
- The new
startandendtimes are calculated, rounded to a 15-minute increment, and theupdateActionis called to optimistically update the event. - The component's local
topandlefttransforms are reset to 0 upon the optimistic update, as the component will be re-rendered in its new correct position in the grid.
- The
Sources: apps/web/src/components/calendar/event/draggable-event.tsx:70-178, apps/web/src/components/calendar/event/drag-aware-wrapper.tsx
Resizing Events
For non-all-day events in Week and Day views, resize handles appear at the top and bottom of the event.
onResizeTopStart/onResizeBottomStart:- The
isResizingAtomis set totrue. - The global cursor is set to
row-resize. - The initial event
heightand pointerYposition are recorded.
- The
onResizeTop/onResizeBottom:- Dragging the top handle updates the
topandheightmotion values to visually move the top edge and resize the event. - Dragging the bottom handle updates the
heightmotion value.
- Dragging the top handle updates the
onResizeTopEnd/onResizeBottomEnd:isResizingAtomis set tofalseand the cursor is reset.- The final
deltaYis calculated and converted into a number of minutes. updateStartTimeorupdateEndTimeis called, which in turn calls theupdateActionwith the new roundedstartorendtime.
This logic is handled by separate motion.div elements for the top and bottom resize handles.
Sources: apps/web/src/components/calendar/event/draggable-event.tsx:184-288
Logic Flowchart
The following diagram shows the decision logic within the DraggableEvent component for handling pan gestures.
Summary
The drag and drop functionality in Analog is a well-structured feature that leverages modern libraries for a smooth user experience. framer-motion handles the complexities of pointer events and animations, while Jotai provides a clean and decoupled way to manage the global state required for these interactions. The logic is clearly divided between creating new events and manipulating existing ones, with specific implementations tailored for each calendar view (Month, Week, and Day).