@moltenship/calendar-dnd 0.0.0-stage → 1.0.0-rc.2

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Timofey Maximov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,34 @@
1
- # Temporary Holding Version
1
+ # @moltenship/calendar-dnd
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The pointer layer of Moltenship Time: drag to move, resize and create events inside a view, on
4
+ native Pointer Events. Drags become change proposals for your app to save. `useCalendarPointer`
5
+ does this in a view's Renderer, and `attachCalendarPointer` outside a React component.
6
+
7
+ It also takes drags across the calendar's edge: `startCalendarDrop` drops items dragged in from
8
+ the rest of the page, such as unscheduled tasks, into the view under them, and
9
+ `monitorCalendarDrags` lets drop zones outside the calendar take events dragged out of it, and
10
+ lets an animation follow each drag. A drag library's binding drives them.
11
+
12
+ It's a layer of `@moltenship/calendar`, which re-exports all of it. Most apps install
13
+ `@moltenship/calendar` only and import `useCalendarPointer` from there.
14
+
15
+ ## Install
16
+
17
+ ```bash
18
+ pnpm add @moltenship/calendar-dnd react
19
+ ```
20
+
21
+ - React 19. React is a peer dependency.
22
+ - A runtime with `Temporal` (Node.js 26 and later include it), or a polyfill that defines
23
+ `globalThis.Temporal`, loaded first.
24
+ - TypeScript 6 or later with `"lib": ["DOM", "DOM.Iterable", "ESNext"]`. `ESNext` has the
25
+ `Temporal` types.
26
+
27
+ ## Documentation
28
+
29
+ - [Interactions](https://time.molten.sh/docs/interactions)
30
+ - [Repository](https://github.com/Moltenship/time)
31
+
32
+ ## License
33
+
34
+ MIT
@@ -0,0 +1,304 @@
1
+ import { CalendarAnnouncer, CalendarDraft, CalendarEventPlacement, CalendarEventProposal, CalendarPolicyResult, CalendarProposalDetail, CalendarTransactionResult, EventId, EventSnapshot, NormalizedCalendarEvent, ResourceId, TimeGridColumn } from "@moltenship/calendar-core";
2
+ import { WallTimeSnapOptions } from "@moltenship/calendar-temporal";
3
+ import { RefCallback } from "react";
4
+ //#region src/attach-calendar-pointer.d.ts
5
+ interface CalendarPointerOptions<TEvent, TEventId extends EventId, TResourceId extends ResourceId> {
6
+ /** Events as rendered, pending changes included; resolves `data-event-id`. */
7
+ readonly events: EventSnapshot<TEvent, TEventId, TResourceId>;
8
+ /**
9
+ * Receives one proposal per completed drag, with `{ input: "pointer" }`. Clicks and cancelled
10
+ * drags propose nothing. An item dragged in from outside the calendar (`startCalendarDrop`)
11
+ * proposes here too, a create carrying its record as `event`, with the input that drove it.
12
+ * Return the transaction's result, as `proposeEventChange` does, to announce whether it was
13
+ * saved.
14
+ */
15
+ readonly onPropose: (proposal: CalendarEventProposal<TEventId, TResourceId, TEvent>, detail: CalendarProposalDetail) => void | PromiseLike<CalendarTransactionResult<TEvent, TEventId, TResourceId>>;
16
+ /** The display time zone. */
17
+ readonly timeZone: string;
18
+ /**
19
+ * Speaks each drag's start, steps, and outcome, and labels the preview's time range; pass
20
+ * `useCalendarAnnouncer()`. Without one, ranges are formatted in the runtime's locale.
21
+ */
22
+ readonly announcer?: CalendarAnnouncer;
23
+ /**
24
+ * Asks the root's policies about the proposal a drop would make, so the preview shows whether
25
+ * it would be saved: pass `actions.checkEventChange`. Preview segments get `data-invalid` when
26
+ * a policy rejects it and `data-warning` when one flags it.
27
+ */
28
+ readonly check?: (proposal: CalendarEventProposal<TEventId, TResourceId, TEvent>) => CalendarPolicyResult<TEvent, TEventId, TResourceId> | null;
29
+ /**
30
+ * Day columns of the time axis, matched to `[data-date]` elements without `data-all-day`: a
31
+ * time grid's or a timeline's `columns`. A new array mid-gesture is picked up, as a view that
32
+ * renders more days while a drag scrolls it needs.
33
+ */
34
+ readonly columns?: readonly TimeGridColumn[];
35
+ /**
36
+ * Turns gestures off. It's also read when the layer attaches: a region disabled then doesn't
37
+ * listen for touch moves, which a drag needs on iOS, so attach again when it changes, as
38
+ * `useCalendarPointer` does.
39
+ */
40
+ readonly disabled?: boolean;
41
+ /**
42
+ * Hears the draft as the preview moves: once per change of where a drop would land, and `null`
43
+ * when the gesture ends or leaves the view. Pass `useCalendarDraftReporter()` so the root's
44
+ * draft hooks, such as `useDraftBusy`, follow pointer drags too.
45
+ */
46
+ readonly onPreview?: (draft: CalendarDraft<TEventId, TResourceId> | null) => void;
47
+ /** Extra classes for the preview segments. */
48
+ readonly ghostClassName?: string;
49
+ /**
50
+ * A ref to the element scrolled while dragging near its edges. Defaults to the nearest scrollable
51
+ * ancestor of where the drag starts.
52
+ */
53
+ readonly scrollContainer?: {
54
+ readonly current: Element | null;
55
+ };
56
+ /**
57
+ * A ref to an element inside `scrollContainer` that scrolls sideways in its place while dragging
58
+ * near its left and right edges: in a view that scrolls each axis in an element of its own, so a
59
+ * swipe moves one axis at a time. Defaults to `scrollContainer`.
60
+ */
61
+ readonly sidewaysScrollContainer?: {
62
+ readonly current: Element | null;
63
+ };
64
+ /** Wall-clock grid for time columns. Defaults to 15 minutes. */
65
+ readonly snap?: WallTimeSnapOptions;
66
+ }
67
+ /**
68
+ * Adds pointer move, resize, and create to a rendered view through delegated listeners on one
69
+ * region. The view marks its parts with data attributes:
70
+ *
71
+ * - `data-event-id` on events (spread `eventIdAttributes(id)` to keep numeric IDs distinct), with
72
+ * `data-resize="start"` or `"end"` on resize handles;
73
+ * - `data-date` (an ISO date) on time-grid day columns and on day cells (month cells, or a time
74
+ * grid's all-day row, which also carries `data-all-day`); a time column with
75
+ * `data-axis="inline"`, such as a timeline's strip for one date, runs its day from the inline
76
+ * start, the right under `dir="rtl"`;
77
+ * - in a resource view, `data-resource-id` on each cell or an element around it (spread
78
+ * `resourceIdAttributes(id)`), so a drop in another resource swaps it in the event's
79
+ * `resourceIds` and a create starts in the resource it began in;
80
+ * - `data-event-time` on the element inside an event that shows its time range, which the
81
+ * preview relabels with the proposed range;
82
+ * - `data-occludes` on parts that sit over the cells, such as sticky headers: a gesture can't start
83
+ * or land on the cells scrolled beneath them;
84
+ * - `data-calendar-pointer="none"` on controls of their own inside the region, such as annotation
85
+ * marks, so pressing them never starts a drag.
86
+ *
87
+ * The preview moves without re-rendering. `onPropose` runs once per drop; a click (less than a few
88
+ * pixels of travel) is left alone, and Escape cancels. Touch drags start after a finger rests still
89
+ * for 450 ms, so a swipe scrolls; once one starts, the page holds still under it. Returns a function
90
+ * that detaches everything.
91
+ */
92
+ export declare function attachCalendarPointer<TEvent, TEventId extends EventId, TResourceId extends ResourceId>(region: HTMLElement, getOptions: () => CalendarPointerOptions<TEvent, TEventId, TResourceId>): () => void;
93
+ //#endregion
94
+ //#region src/drags.d.ts
95
+ /** A point in viewport (client) coordinates. */
96
+ interface CalendarPoint {
97
+ readonly x: number;
98
+ readonly y: number;
99
+ }
100
+ /** A rectangle in viewport (client) coordinates. */
101
+ interface CalendarRect {
102
+ readonly height: number;
103
+ readonly width: number;
104
+ readonly x: number;
105
+ readonly y: number;
106
+ }
107
+ /** Where a view's preview shows that a moved event would land. */
108
+ interface CalendarDragPreview {
109
+ /**
110
+ * The time range the preview's copy of the event shows, as the view's announcer formats it, or
111
+ * `null` where the preview is a plain box, as in a month.
112
+ */
113
+ readonly label: string | null;
114
+ /**
115
+ * The preview's boxes at least partly in view, in order, each at its full size even where a
116
+ * scroller clips it: a day, week row or resource the event would cover. Empty when none is.
117
+ */
118
+ readonly rects: readonly CalendarRect[];
119
+ /** `invalid` when a policy would reject the drop, `warning` when one would flag it. */
120
+ readonly validity: "invalid" | "warning" | null;
121
+ }
122
+ /** An event the pointer layer is moving, offered to `monitorCalendarDrags`. */
123
+ interface CalendarEventDrag<TEvent = unknown, TEventId extends EventId = EventId, TResourceId extends ResourceId = ResourceId> {
124
+ /** The element pressed: the event's segment in the view. */
125
+ readonly element: HTMLElement;
126
+ /** The event, as the view rendered it; `event.event` is the application's record. */
127
+ readonly event: NormalizedCalendarEvent<TEvent, TEventId, TResourceId>;
128
+ /** Where the press was. */
129
+ readonly origin: CalendarPoint;
130
+ /**
131
+ * Asks `claims` again and redraws the preview, for a follower whose answer changed without the
132
+ * pointer moving, as when a library works out its drop target after a move.
133
+ */
134
+ readonly refresh: () => void;
135
+ }
136
+ /**
137
+ * Follows one drag that a monitor took on. A follower with `claims` stands for drop zones outside
138
+ * the calendar, which changes how the view treats the drag; one without only watches it, as an
139
+ * animation that decorates drags does, and the drag goes on as if no one followed.
140
+ */
141
+ interface CalendarDragFollower {
142
+ /**
143
+ * Whether a drop zone of the follower's is under the pointer. While one is, the calendar shows
144
+ * no preview of its own, and a release there proposes nothing and announces nothing: the zone's
145
+ * owner handles the drop and speaks for it.
146
+ */
147
+ readonly claims?: () => boolean;
148
+ /**
149
+ * The drag ended. `dropped` is true when it was released while `claims` said so, false after a
150
+ * drop in the calendar, a release over nowhere, Escape, or a lost pointer. `landing` is the
151
+ * preview a release in the view proposed the move at, policies still to run; `null` when it
152
+ * proposed nothing: Escape, a lost pointer, or a release where the event already was, over
153
+ * nowhere or over a zone. No `preview(null)` comes before it.
154
+ */
155
+ readonly end?: (dropped: boolean, landing: CalendarDragPreview | null) => void;
156
+ /** The pointer moved. */
157
+ readonly move?: (point: CalendarPoint) => void;
158
+ /**
159
+ * The view's preview changed, or moved as the view scrolled under it: where a drop would land
160
+ * now, or `null` when the view shows none, off it or under a claimed zone.
161
+ */
162
+ readonly preview?: (preview: CalendarDragPreview | null) => void;
163
+ }
164
+ /** Hears a move as it starts; returns a follower to take it on, or nothing to leave it. */
165
+ type CalendarDragMonitor = (drag: CalendarEventDrag) => CalendarDragFollower | void;
166
+ /**
167
+ * Hears every event a calendar view starts to move by pointer, on the page, so a drop zone
168
+ * outside the calendar can take it, such as a list of unscheduled items or a bin, or an animation
169
+ * can follow it. Resizes and creates aren't offered. While a follower with `claims` is on a drag,
170
+ * the view counts only where it's the topmost thing under the pointer, so a sheet of zones over it
171
+ * hides it; off the view, covered or claimed, a copy of the event follows the pointer. The view
172
+ * scrolls only while the pointer is over it, and after the pointer has left, only once it has
173
+ * been clear of the edges again or rested at one. Followers without `claims` change none of that.
174
+ * Returns a function that stops listening.
175
+ */
176
+ export declare function monitorCalendarDrags(monitor: CalendarDragMonitor): () => void;
177
+ //#endregion
178
+ //#region src/drop.d.ts
179
+ /** What an item dragged in from outside the calendar becomes when it's dropped on a view. */
180
+ interface CalendarDropSource<TEvent = unknown, TEventId extends EventId = EventId> {
181
+ /**
182
+ * How many days it covers when dropped on day cells: a month cell, the all-day row. 1 by
183
+ * default.
184
+ */
185
+ readonly days?: number;
186
+ /** How long it is when dropped on a time column. An hour by default. */
187
+ readonly duration?: Temporal.Duration | Temporal.DurationLike;
188
+ /**
189
+ * The application record the new event copies, such as a task mapped to your event type. The
190
+ * create change carries it as `change.event`, so `onEventChange` knows what was dropped.
191
+ */
192
+ readonly event?: TEvent;
193
+ /**
194
+ * An event the calendar shows, to move instead of creating one: its start lands where it's
195
+ * dropped. A view without it takes no drop.
196
+ */
197
+ readonly eventId?: TEventId;
198
+ /** What announcements call the item, such as a task's title. */
199
+ readonly title?: string;
200
+ }
201
+ /** How `startCalendarDrop` follows a drag. */
202
+ interface CalendarDropOptions {
203
+ /**
204
+ * The input driving the drag: `"keyboard"` for a drag library's keyboard sensor, `"pointer"`
205
+ * (the default) for a mouse, finger or pen. Proposals and announcements carry it.
206
+ */
207
+ readonly input?: "keyboard" | "pointer";
208
+ }
209
+ /** A drag from outside the calendar, as `startCalendarDrop` follows it. */
210
+ interface CalendarDropSession<TEvent = unknown, TEventId extends EventId = EventId, TResourceId extends ResourceId = ResourceId> {
211
+ /** Ends it without proposing anything, and announces a cancel if a view heard it start. */
212
+ readonly cancel: () => void;
213
+ /**
214
+ * Proposes the placement under the last point, once, through the view's `onPropose`, and ends
215
+ * the drag. Resolves with the transaction's result; `null` when it proposed nothing, over no
216
+ * view or for a move that would change nothing, and when `onPropose` returns no result.
217
+ */
218
+ readonly drop: () => Promise<CalendarTransactionResult<TEvent, TEventId, TResourceId> | null>;
219
+ /**
220
+ * Follows the drag to a viewport point: finds the view under it, previews the placement a drop
221
+ * there would make, checks it against the root's policies, announces it, and scrolls the view
222
+ * near its edges. `null` means the point is over no calendar, as when the library has found a
223
+ * drop zone of the app's there.
224
+ */
225
+ readonly move: (point: CalendarPoint | null) => void;
226
+ /** Where a drop would land now; `null` over no view, and once the drag has ended. */
227
+ readonly placement: CalendarEventPlacement<TResourceId> | null;
228
+ }
229
+ /**
230
+ * Follows a drag that started outside the calendar, such as a task dragged from a list, over the
231
+ * calendar views on the page, and drops it into one: the framework-agnostic half of external drag
232
+ * and drop, which a drag library's binding drives. Every view that uses `useCalendarPointer` takes
233
+ * part, through the options it passes: the preview shows range boxes in its slots, its `check`
234
+ * marks them `data-invalid` or `data-warning`, its announcer speaks each step with `outside` set,
235
+ * and its `onPropose` gets the drop. Read-only views take no part.
236
+ *
237
+ * Over a time column a drop creates a timed event starting at the slot under the point,
238
+ * `duration` long; over day cells, an all-day event of `days` days. In a resource view it books
239
+ * the resource it's dropped in. With `eventId`, it moves that event instead, unless it's
240
+ * read-only.
241
+ *
242
+ * The topmost element under the point decides: anything over a view, such as a sheet or a
243
+ * dialog, hides it. So the library's drag image must let the pointer through
244
+ * (`pointer-events: none`), as dnd-kit's does. From the first `move` until `drop` or `cancel`, a
245
+ * frame loop scrolls the view under the point near its edges, once the point has been clear of
246
+ * them or rested at one for a moment, so crossing an edge on the way in doesn't scroll it.
247
+ */
248
+ export declare function startCalendarDrop<TEvent = unknown, TEventId extends EventId = EventId, TResourceId extends ResourceId = ResourceId>(source: CalendarDropSource<TEvent, TEventId>, { input }?: CalendarDropOptions): CalendarDropSession<TEvent, TEventId, TResourceId>;
249
+ //#endregion
250
+ //#region src/event-id.d.ts
251
+ /**
252
+ * Attributes that identify an event element, for spreading onto it. `data-event-id` holds the ID
253
+ * as text; numeric IDs add `data-event-id-type="number"`, so `1` and `"1"` stay distinct.
254
+ */
255
+ export declare function eventIdAttributes(id: EventId): {
256
+ readonly "data-event-id": string;
257
+ readonly "data-event-id-type"?: "number";
258
+ };
259
+ /** The event ID an element carries through `eventIdAttributes`, or `null` without one. */
260
+ export declare function eventIdOf(element: Element): EventId | null;
261
+ //#endregion
262
+ //#region src/regions.d.ts
263
+ /** A calendar view's region, as a binding to a drag-and-drop library sees it. */
264
+ interface CalendarRegion {
265
+ /** Whether its gestures are off, as on a read-only root: then it takes no drops either. */
266
+ readonly disabled: boolean;
267
+ /** The element the view passed to `useCalendarPointer` or `attachCalendarPointer`. */
268
+ readonly element: HTMLElement;
269
+ }
270
+ /**
271
+ * Hears the calendar views on the page: once now, and again whenever a view's region attaches or
272
+ * detaches. A region that turns read-only attaches again, so `disabled` follows it. Returns a
273
+ * function that stops listening.
274
+ */
275
+ export declare function observeCalendarRegions(observer: (regions: readonly CalendarRegion[]) => void): () => void;
276
+ //#endregion
277
+ //#region src/resource-id.d.ts
278
+ /**
279
+ * Attributes that place an element in a resource, for spreading onto a resource's column, row, or
280
+ * cells. `data-resource-id` holds the ID as text, empty for the unassigned bucket (`null`); numeric
281
+ * IDs add `data-resource-id-type="number"`, so `1` and `"1"` stay distinct.
282
+ */
283
+ export declare function resourceIdAttributes(id: ResourceId | null): {
284
+ readonly "data-resource-id": string;
285
+ readonly "data-resource-id-type"?: "number";
286
+ };
287
+ /**
288
+ * The resource an element sits in, from the nearest `resourceIdAttributes`: `null` in the
289
+ * unassigned bucket, `undefined` outside any resource.
290
+ */
291
+ export declare function resourceIdOf(element: Element): ResourceId | null | undefined;
292
+ //#endregion
293
+ //#region src/use-calendar-pointer.d.ts
294
+ /**
295
+ * Pointer move, resize, and create for a view region; see `attachCalendarPointer` for the data
296
+ * attributes the view renders. Returns a ref for the region element. Options are read when a
297
+ * gesture starts, and `columns` again as it moves, so changing them never re-attaches listeners,
298
+ * and dragging never re-renders. `disabled` is the exception: a disabled region doesn't listen for
299
+ * touch moves, so a read-only calendar's swipes never wait for its listener, and changing it
300
+ * attaches the listeners again.
301
+ */
302
+ export declare function useCalendarPointer<TEvent, TEventId extends EventId, TResourceId extends ResourceId>(options: CalendarPointerOptions<TEvent, TEventId, TResourceId>): RefCallback<HTMLElement>;
303
+ //#endregion
304
+ export type { CalendarDragFollower, CalendarDragMonitor, CalendarDragPreview, CalendarDropOptions, CalendarDropSession, CalendarDropSource, CalendarEventDrag, CalendarPoint, CalendarPointerOptions, CalendarRect, CalendarRegion };