@moltenship/calendar 1.0.0-rc.2 → 2.0.0-beta.1

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 (70) hide show
  1. package/README.md +24 -4
  2. package/dist/index.d.mts +2 -2
  3. package/docs/accessibility.md +304 -0
  4. package/docs/agents/changes.md +449 -0
  5. package/docs/agents/coding-agents.md +117 -0
  6. package/docs/agents/connect.md +435 -0
  7. package/docs/agents/deleting.md +296 -0
  8. package/docs/agents/reference.md +512 -0
  9. package/docs/agents/server.md +317 -0
  10. package/docs/agents/webmcp.md +153 -0
  11. package/docs/agents.md +353 -0
  12. package/docs/annotations.md +196 -0
  13. package/docs/api/changes.md +248 -0
  14. package/docs/api/dnd-kit.md +267 -0
  15. package/docs/api/events.md +152 -0
  16. package/docs/api/ics.md +524 -0
  17. package/docs/api/primitives.md +168 -0
  18. package/docs/api/react.md +236 -0
  19. package/docs/api/recurrence.md +586 -0
  20. package/docs/api/solid.md +460 -0
  21. package/docs/api/store.md +92 -0
  22. package/docs/api/temporal.md +172 -0
  23. package/docs/api.md +57 -0
  24. package/docs/availability.md +573 -0
  25. package/docs/comparison.md +279 -0
  26. package/docs/concepts/anatomy.md +73 -0
  27. package/docs/concepts/events.md +164 -0
  28. package/docs/concepts/how-it-works.md +95 -0
  29. package/docs/concepts/recurrence.md +127 -0
  30. package/docs/concepts/state.md +244 -0
  31. package/docs/concepts/time.md +170 -0
  32. package/docs/guides/dnd-kit.md +333 -0
  33. package/docs/guides/google-calendar-sync.md +1246 -0
  34. package/docs/guides/google-calendar.md +367 -0
  35. package/docs/guides/ics.md +486 -0
  36. package/docs/guides/motion.md +414 -0
  37. package/docs/guides/nextjs-app-router.md +630 -0
  38. package/docs/guides/nextjs.md +149 -0
  39. package/docs/guides/performance.md +103 -0
  40. package/docs/guides/recurring-events.md +583 -0
  41. package/docs/guides/tanstack-query.md +485 -0
  42. package/docs/index.md +118 -0
  43. package/docs/installation.md +194 -0
  44. package/docs/interactions.md +408 -0
  45. package/docs/llms.txt +86 -0
  46. package/docs/migrating-to-2.md +322 -0
  47. package/docs/quick-start.md +159 -0
  48. package/docs/stylex.md +265 -0
  49. package/docs/testing.md +581 -0
  50. package/docs/theme-builder.md +81 -0
  51. package/docs/theming.md +169 -0
  52. package/docs/undo-redo.md +339 -0
  53. package/docs/views/agenda.md +221 -0
  54. package/docs/views/custom-views.md +192 -0
  55. package/docs/views/month.md +109 -0
  56. package/docs/views/resource-columns.md +261 -0
  57. package/docs/views/resource-timeline.md +225 -0
  58. package/docs/views/scrolling.md +192 -0
  59. package/docs/views/week-and-day.md +257 -0
  60. package/docs/views/year.md +138 -0
  61. package/docs/views.md +69 -0
  62. package/package.json +41 -14
  63. package/skills/moltenship-time/SKILL.md +75 -0
  64. package/skills/moltenship-time/references/agent-ready.md +272 -0
  65. package/skills/moltenship-time/references/changes.md +188 -0
  66. package/skills/moltenship-time/references/compose.md +168 -0
  67. package/skills/moltenship-time/references/mistakes.md +98 -0
  68. package/skills/moltenship-time/references/solid.md +202 -0
  69. package/skills/moltenship-time/references/state.md +129 -0
  70. package/skills/moltenship-time/references/testing.md +176 -0
@@ -0,0 +1,248 @@
1
+ # Event changes
2
+
3
+ Proposals, changes, results, policies, placement helpers, and the pointer layer.
4
+
5
+ Source: https://time.molten.sh/docs/api/changes
6
+
7
+ Everything on this page is exported from `@moltenship/calendar`. [Interactions](../interactions.md)
8
+ explains how the pieces fit together.
9
+
10
+ ## Proposals
11
+
12
+ `actions.proposeEventChange(proposal)` takes one of these:
13
+
14
+ ```ts
15
+ type CalendarEventProposal =
16
+ | { cause: "create"; proposed: Placement; event?: TEvent }
17
+ | {
18
+ cause: "move" | "resize-start" | "resize-end" | "update";
19
+ eventId: EventId;
20
+ proposed: Placement;
21
+ }
22
+ | { cause: "delete"; eventId: EventId };
23
+ ```
24
+
25
+ A placement is a [range](./events.md#ranges) (`kind`, `start`, `end`) plus optional
26
+ `resourceIds`. Leaving out `resourceIds` keeps the event's current resources. A create may carry
27
+ one of your records as `event`, such as a deleted event an
28
+ [undo](../undo-redo.md#creates-and-deletes) restores; the change hands it on.
29
+
30
+ ## Changes
31
+
32
+ `onEventChange` and policies receive a `CalendarEventChange`:
33
+
34
+ | Field | `create` | `move`, `resize-*`, `update` | `delete` |
35
+ | ---------- | ---------------------------------------- | ---------------------------- | -------------------- |
36
+ | `cause` | `"create"` | the cause | `"delete"` |
37
+ | `id` | a change ID | a change ID | a change ID |
38
+ | `eventId` | `null` | the event's ID | the event's ID |
39
+ | `event` | `null`, or the record the create carries | your record | your record |
40
+ | `original` | `null` | the placement before | the placement before |
41
+ | `proposed` | the new placement | the new placement | `null` |
42
+ | `warnings` | reasons `warn` policies flagged, if any | the same | the same |
43
+
44
+ Placements in a change always include `resourceIds`. `original` is what the user saw, including any
45
+ change still pending for the same event. `warnings` is absent when no policy flagged the change.
46
+
47
+ ## Results
48
+
49
+ `proposeEventChange` resolves with `{ status: "committed", change }` or
50
+ `{ status: "rejected", reason, change?, error? }`. It never rejects. The
51
+ [reasons](../interactions.md#changing-events-from-your-own-ui) are listed on the Interactions page.
52
+ `Root`'s `onEventChangeResult` gets the same result for every proposal, with the detail it was
53
+ proposed with (`CalendarProposalDetail`): `{ input: "pointer" | "keyboard" | "api" | "agent" }`,
54
+ `history: "undo" | "redo"` on an event history's inverse, and `fields` when the proposer asked
55
+ for more than a placement. A detail that names no input reads `{ input: "api" }`. `"agent"` is an
56
+ AI agent's change, made through the [agent bridge](../agents.md). `onEventChange` gets the same
57
+ detail as its second argument, and a policy as `context.detail`, so both can
58
+ [refuse an agent's change](../agents/changes.md).
59
+
60
+ `fields` is what a change says beside where the event goes. The engine models placement only, so
61
+ a title, a colour or anything else of your record travels there, as it came and unchecked. An
62
+ agent's `createEvent` and `updateEvent` fill it, with the title as `fields.title`, and your
63
+ `onEventChange` reads it to [save what the agent sent](../agents/connect.md#save-what-the-agent-sent).
64
+ Your own code can send fields the same way, as `proposeEventChange(proposal, { input: "api", fields })`.
65
+
66
+ While a proposal is on its way, `useCalendarPendingChanges` lists it as a `CalendarPendingChange`,
67
+ `{ change, phase }`, with `phase` one of `"validating"`, `"saving"` and `"awaiting-events"`. See
68
+ [pending changes](../interactions.md#across-the-calendar).
69
+
70
+ ## Event history
71
+
72
+ `createEventHistory(options?)` records committed changes and undoes and redoes them through the
73
+ pipeline. Its members are plain functions: `record(result, detail)` for `onEventChangeResult`,
74
+ `undo(context)` and `redo(context)`, `clear()`, `getSnapshot()` and `subscribe(listener)`. The
75
+ context is `{ getEvents, propose, input?, whenSettled? }`. See [Undo and redo](../undo-redo.md)
76
+ for the options, the steps (`EventHistoryStep`), the outcomes (`EventHistoryOutcome`) and the
77
+ snapshot (`EventHistorySnapshot`), and `useCalendarHistory` binds a history to a `Root`.
78
+
79
+ ## Policies
80
+
81
+ ```ts
82
+ type CalendarPolicy = (
83
+ change: CalendarEventChange,
84
+ context: {
85
+ events: EventSnapshot;
86
+ timeZone: string;
87
+ availability?: AvailabilitySnapshot;
88
+ detail?: CalendarProposalDetail;
89
+ },
90
+ ) => Decision | Promise<Decision>;
91
+
92
+ type CalendarProposalDetail = {
93
+ input: "pointer" | "keyboard" | "api" | "agent";
94
+ history?: "undo" | "redo";
95
+ fields?: Readonly<Record<string, unknown>>;
96
+ };
97
+
98
+ type Decision =
99
+ | { type: "accept" }
100
+ | { type: "adjust"; proposed: Placement }
101
+ | { type: "reject"; reason: string }
102
+ | { type: "warn"; reason: string };
103
+ ```
104
+
105
+ `warn` accepts the change and adds `reason` to its `warnings`. `context.availability` holds the
106
+ root's working hours, for `availabilityPolicy`. `context.events` includes pending changes, so a
107
+ policy sees the calendar as the user does. `context.detail` says who proposed the change, so a
108
+ policy can [refuse an agent's change](../agents/changes.md#refuse-an-agents-change) and allow a
109
+ person's; it's absent when your own code calls a policy outside a proposal. A policy
110
+ that throws rejects the change with the reason `policy-error`.
111
+
112
+ | Policy | Behaviour |
113
+ | ------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
114
+ | `snapPolicy(increment)` | Rounds timed and floating changes to a wall-clock grid in the calendar's time zone, such as `{ minutes: 15 }`. Moves keep their length; resizes snap only the dragged edge. All-day changes pass through. The increment must divide an hour or a day evenly. |
115
+ | `minimumDurationPolicy(minimum)` | Lengthens timed and floating changes shorter than `minimum`. Resizing the start moves the start earlier; anything else moves the end later. |
116
+ | `boundsPolicy({ start, end })` | Rejects changes that reach outside `[start, end)`, with the reason `out-of-bounds`. Both bounds are optional instants. |
117
+ | `readOnlyPolicy()` | Rejects changes to events whose adapter `isReadOnly` returns `true`, with the reason `read-only`. `Root` runs it before your policies. |
118
+ | `availabilityPolicy({ mode })` | Rejects a change outside working hours, with the reason `outside-availability`, or with `mode: "warn"` saves it with that warning. See [Availability](../availability.md#keep-changes-inside). |
119
+ | `conflictPolicy({ mode, getResourceMode })` | Rejects a change that makes a busy event overlap another busy event on one of its resources, with the reason `resource-conflict`; with `mode: "warn"` saves it with that warning, and with `"allow"` as it is. `getResourceMode` gives a resource its own mode; the strictest decides. Deletes always pass. See [Availability](../availability.md#double-booking). |
120
+ | `readOnlyResourcesPolicy(isReadOnly)` | Rejects creating, deleting, or changing the time of an event on a resource `isReadOnly(resourceId)` marks, and adding or removing that resource, with the reason `read-only-resource`. |
121
+
122
+ ## Placement helpers
123
+
124
+ These build placements for proposals, with the same wall-clock rules the built-in gestures use.
125
+
126
+ | Function | Returns |
127
+ | ----------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
128
+ | `eventPlacement(normalizedEvent)` | The placement of an event from the snapshot |
129
+ | `moveEventPlacement(placement, delta, { timeZone })` | The placement moved by a wall-clock delta, such as `{ days: 1, minutes: -30 }`. A 09:00 event moved by a week stays at 09:00 across a DST change. |
130
+ | `resizeEventPlacement(placement, edge, delta, { timeZone, minimumDuration })` | One edge moved, keeping at least `minimumDuration` (15 minutes by default; all-day events keep a day) |
131
+ | `createEventPlacement(anchor, focus, resourceIds)` | A placement covering two ranges of the same kind, such as the first and last slot of a drag |
132
+ | `toAllDayPlacement(placement, { timeZone })` | The dates a placement touches, as an all-day placement |
133
+ | `toTimedPlacement(placement, start, duration)` | A timed placement starting at a `Temporal.ZonedDateTime` |
134
+
135
+ For example, this moves an event to the same time on the next day:
136
+
137
+ ```tsx title="move-to-tomorrow.ts"
138
+ export function useMoveToTomorrow() {
139
+ const actions = Calendar.useCalendarActions();
140
+ const { eventSnapshot } = Calendar.useCalendarMeta();
141
+ const { timeZone } = Calendar.useCalendarState();
142
+
143
+ return (eventId: string) => {
144
+ const event = eventSnapshot.get(eventId);
145
+ if (event === undefined) return;
146
+ return actions.proposeEventChange({
147
+ cause: "move",
148
+ eventId,
149
+ proposed: moveEventPlacement(eventPlacement(event), { days: 1 }, { timeZone }),
150
+ });
151
+ };
152
+ }
153
+ ```
154
+
155
+ `createEventTransactions` and `applyEventOverlay` are the engine `Root` uses to run proposals and
156
+ draw pending changes. You only need them to handle changes outside React. The engine's
157
+ `getPending()` returns the pending placements `applyEventOverlay` draws, and `getPendingChanges()`
158
+ the changes `useCalendarPendingChanges` reads.
159
+
160
+ ## Pointer layer
161
+
162
+ `useCalendarPointer(options)` returns a ref for the element that contains a view's days, and turns
163
+ drags inside it into proposals. `attachCalendarPointer(element, getOptions)` does the same without
164
+ React and returns a cleanup function. Both come from `@moltenship/calendar-dnd`, which
165
+ `@moltenship/calendar` re-exports.
166
+
167
+ | Name | Type | Default | Description |
168
+ | ------------------------- | ------------------------------------------------------------------------------------ | ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
169
+ | `events` | `EventSnapshot` | | Required. The events as rendered, pending changes included: the Renderer's `events` prop. |
170
+ | `onPropose` | `(proposal, { input: "pointer" }) => void \| PromiseLike<CalendarTransactionResult>` | | Required. Receives one proposal per finished drag, and `{ input: "pointer" }`, and one per item dropped in from outside the calendar, with that drag's input. Usually `actions.proposeEventChange`, so `onEventChangeResult` hears the drag as the pointer's. Return the result, as it does, so the announcer says whether the change was saved. |
171
+ | `check` | `(proposal) => CalendarPolicyResult \| null` | | Asks the policies about the drop the preview shows, so the preview can mark it. Pass `actions.checkEventChange`. |
172
+ | `onPreview` | `(draft: { eventId, proposed } \| null) => void` | | Hears where a drop would land, once per change, and `null` when there's nowhere to drop or the gesture ends. Pass `useCalendarDraftReporter()` so `useCalendarDraft` and `useDraftBusy` follow pointer drags. |
173
+ | `timeZone` | `string` | | Required. The calendar's time zone. |
174
+ | `columns` | `readonly TimeGridColumn[]` | | The day columns of a time axis, a time grid's or an hour timeline's: `projection.days.map((day) => day.column)` in a time grid, `projection.columns` in resource columns or an hour timeline. Leave it out in a month grid or a day timeline. |
175
+ | `snap` | `{ increment, smallestUnit, mode? }` | `15 minutes` | The wall-clock grid for times. |
176
+ | `scrollContainer` | `{ current: Element \| null }` | `the nearest scrollable ancestor` | A ref to the element to scroll while dragging near its edges. It's read when a drag starts. |
177
+ | `sidewaysScrollContainer` | `{ current: Element \| null }` | `scrollContainer` | A ref to an element inside `scrollContainer` to scroll in its place at the left and right edges, for a view that scrolls its two axes in two elements. It's read when a drag starts. |
178
+ | `announcer` | `CalendarAnnouncer` | `silent; ranges in the runtime's locale` | Speaks each drag through the root's `Announcer` and labels the drag preview's range the same way. Pass `useCalendarAnnouncer()`. See [pointer gestures](../accessibility.md#pointer-gestures). |
179
+ | `ghostClassName` | `string` | | Extra classes for the drag preview. |
180
+ | `disabled` | `boolean` | `false` | Turns gestures off. Pass the Renderer's `isReadOnly`, so a calendar without `onEventChange` starts none. `attachCalendarPointer` also reads it when it attaches, to listen for touch moves only when enabled, so attach again when it changes; `useCalendarPointer` does. |
181
+
182
+ While a gesture runs, the region has `data-interaction="move"`, `"resize"`, or `"create"`, and the
183
+ event being dragged has `data-dragging`. Preview elements have `data-calendar-ghost="event"` or
184
+ `"range"`, and `data-invalid` or `data-warning` when `check` says a policy would reject or flag the
185
+ drop. Range previews read `--calendar-ghost-background` and `--calendar-ghost-radius`, event
186
+ previews `--calendar-ghost-shadow` and `--calendar-ghost-opacity`, and both
187
+ `--calendar-ghost-z-index`.
188
+
189
+ ### Drags from and to the rest of the page
190
+
191
+ These connect the views to drags that start or end outside the calendar, such as tasks dragged in
192
+ from a list or events dragged out to a bin. A drag-and-drop library's binding calls them with the
193
+ points it tracks; [dnd-kit's](./dnd-kit.md) is `@moltenship/calendar-dnd-kit`. Every view that uses
194
+ `useCalendarPointer` takes part, through the options it passes, and read-only views take none.
195
+
196
+ `startCalendarDrop(source, { input? })` follows one item dragged in:
197
+
198
+ | Source field | Meaning |
199
+ | ------------ | -------------------------------------------------------------------------------------------------------------------------- |
200
+ | `event` | Your record the new event copies, such as a task mapped to your event type. The create change carries it as `change.event` |
201
+ | `duration` | How long it is over a time column. An hour by default |
202
+ | `days` | How many days it covers over day cells, such as a month cell or the all-day row. 1 by default |
203
+ | `eventId` | An event the calendar shows, to move instead of creating one; its start lands where it's dropped. Not a read-only one |
204
+ | `title` | What announcements call it |
205
+
206
+ `input` is `"pointer"` by default, or `"keyboard"` for a library's keyboard sensor. The result has
207
+ three methods and a property:
208
+
209
+ - `move(point)` takes a viewport point `{ x, y }`. It finds the view under it, previews the
210
+ placement a drop there would make as range boxes, checks it with the view's `check`, reports it
211
+ to `onPreview`, and announces it. Over a time column the item starts at the slot under the
212
+ point; in a resource view it books that resource. `move(null)` means the point is over a drop
213
+ zone of your own, which clears the preview.
214
+ - `drop()` proposes once through the view's `onPropose`, with the `input`, and resolves with the
215
+ result. It resolves `null` when it proposed nothing, over no view or for a move that changes
216
+ nothing, and when `onPropose` returns no result.
217
+ - `cancel()` ends it without proposing.
218
+ - `placement` is where a drop would land now, or `null`, as it is once the drag has ended.
219
+
220
+ The topmost element under the point decides which view it's over, so anything over a view, such
221
+ as a sheet or a dialog, hides it. Give a drag image of your own `pointer-events: none`, as
222
+ dnd-kit's has. From the first `move()` until `drop()` or `cancel()`, a frame loop scrolls the view
223
+ under the point near its edges, once the point has been clear of them or has rested at one for a
224
+ quarter of a second, so a drag that crosses an edge on its way in doesn't scroll the view.
225
+
226
+ `monitorCalendarDrags(monitor)` offers each event a view starts to move to your drop zones, or to
227
+ an animation that follows it, and returns a function that stops. The monitor gets
228
+ `{ event, element, origin, refresh }` and returns a follower, or nothing to leave the drag alone:
229
+
230
+ | Follower method | Called |
231
+ | ----------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
232
+ | `move(point)` | On every pointer move |
233
+ | `preview(preview)` | Whenever the view's preview changes, or moves as the view scrolls: `{ rects, label, validity }`, its boxes in viewport coordinates, the time range it shows (`null` for a plain box), and `"invalid"` or `"warning"` when a policy would reject or flag the drop. `null` when the view shows no preview |
234
+ | `claims()` | Whenever the preview updates. Return `true` while one of your zones is under the pointer: the calendar then shows no preview, and a release there proposes and announces nothing |
235
+ | `end(dropped, landing)` | When the drag ends: `dropped` is `true` if it was released while you claimed it, so handle the drop; `false` after a drop in the calendar, Escape, or a release over nowhere. `landing` is the preview a release in the view proposed the move at, policies still to run, or `null` when it proposed nothing. No `preview(null)` comes first |
236
+
237
+ Call `refresh()` when your answer to `claims()` changes without the pointer moving. While a
238
+ follower with `claims` is on a drag, the view counts only where it's the topmost thing under the
239
+ pointer, as for drags in: over a sheet of your zones that covers it, it shows no preview and a
240
+ release proposes nothing. Off the view, covered or claimed, a copy of the event follows the
241
+ pointer, with `data-outside`. The view scrolls only while the pointer is over it, and once the
242
+ pointer has left, only after it has been clear of the edges again or rested at one. A follower
243
+ without `claims` only watches, as the [Motion guide](../guides/motion.md)'s card does, and changes
244
+ none of that: as without a follower, nothing shows off the view. Resizes and creates aren't
245
+ offered.
246
+
247
+ `observeCalendarRegions(observer)` hears the views on the page, `{ element, disabled }` each, now
248
+ and whenever one attaches or detaches, so a binding can register them with its library.
@@ -0,0 +1,267 @@
1
+ # dnd-kit adapter
2
+
3
+ Drag your own dnd-kit items into the calendar, and calendar events out to your droppables, with @moltenship/calendar-dnd-kit.
4
+
5
+ Source: https://time.molten.sh/docs/api/dnd-kit
6
+
7
+ `@moltenship/calendar-dnd-kit` connects the calendar to [dnd-kit](https://dndkit.com), for apps
8
+ that already use it:
9
+
10
+ - **Drag your items in.** A task, or anything else you make draggable, becomes an event when it's
11
+ dropped on a time slot, a day, the all-day row or a resource.
12
+ - **Drag events out.** A calendar event dragged onto one of your droppables, such as an
13
+ "unscheduled" list or a bin, goes to it.
14
+
15
+ Drops go through the calendar like any drag: its hit-testing, snapping, policies, `onEventChange`
16
+ and `onEventChangeResult`. The calendar's own drags stay on its native pointer layer.
17
+ [Drag tasks in with dnd-kit](../guides/dnd-kit.md) builds a backlog beside a week, live.
18
+
19
+ It's a separate package. `@moltenship/calendar` doesn't re-export it, so a calendar without it
20
+ installs and bundles no dnd-kit. It works with dnd-kit's current packages, `@dnd-kit/react` and
21
+ `@dnd-kit/dom` 0.5, not the older `@dnd-kit/core`. Without dnd-kit, the same calls it makes are in
22
+ the pointer layer: see [drags from and to the rest of the page](./changes.md#drags-from-and-to-the-rest-of-the-page).
23
+
24
+ ## Install
25
+
26
+ Like the calendar, `@moltenship/calendar-dnd-kit` is on npm as a release candidate for 1.0.0.
27
+
28
+ **pnpm**
29
+
30
+ ```bash
31
+ pnpm add @moltenship/calendar-dnd-kit @dnd-kit/react @dnd-kit/dom
32
+ ```
33
+
34
+ **npm**
35
+
36
+ ```bash
37
+ npm install @moltenship/calendar-dnd-kit @dnd-kit/react @dnd-kit/dom
38
+ ```
39
+
40
+ **yarn**
41
+
42
+ ```bash
43
+ yarn add @moltenship/calendar-dnd-kit @dnd-kit/react @dnd-kit/dom
44
+ ```
45
+
46
+ **bun**
47
+
48
+ ```bash
49
+ bun add @moltenship/calendar-dnd-kit @dnd-kit/react @dnd-kit/dom
50
+ ```
51
+
52
+ dnd-kit 0.5 is still pre-1.0, and its minor versions change its API, so the adapter takes 0.5
53
+ only. It needs React 19, like the calendar.
54
+
55
+ ## Connect it
56
+
57
+ Render `<CalendarDndKit />` anywhere inside your `DragDropProvider`. It renders nothing, and
58
+ connects every calendar view on the page to that provider:
59
+
60
+ ```tsx
61
+ import { DragDropProvider } from "@dnd-kit/react";
62
+ import { CalendarDndKit } from "@moltenship/calendar-dnd-kit";
63
+
64
+ <DragDropProvider>
65
+ <CalendarDndKit />
66
+ <TaskList />
67
+ <Calendar.Root events={meetings} onEventChange={save} timeZone={timeZone}>
68
+ …
69
+ </Calendar.Root>
70
+ </DragDropProvider>;
71
+ ```
72
+
73
+ Each view becomes one droppable that takes only calendar items and calendar events. Its ID is
74
+ opaque, so tell it by `isCalendarTarget(droppable)`. It wins where the view is the topmost thing
75
+ under the pointer, over a droppable of yours around or under the calendar, and a droppable of
76
+ yours over the view, such as a floating bin, wins there. A read-only view's droppable is disabled.
77
+
78
+ `connectCalendarDragDrop(manager)` does the same for a `DragDropManager` of `@dnd-kit/dom` without
79
+ React, and returns a function that disconnects. Connecting a manager again, or rendering a second
80
+ `CalendarDndKit` in the same provider, shares the first connection.
81
+
82
+ ## Drag your items in
83
+
84
+ Spread `calendarDropData(source)` into a draggable's `data`, next to your own:
85
+
86
+ ```tsx
87
+ import { useDraggable } from "@dnd-kit/react";
88
+ import { calendarDropData } from "@moltenship/calendar-dnd-kit";
89
+
90
+ function TaskCard({ task }: { task: Task }) {
91
+ const { ref } = useDraggable({
92
+ id: task.id,
93
+ data: {
94
+ task,
95
+ ...calendarDropData({
96
+ event: meetingFromTask(task),
97
+ duration: { minutes: task.minutes },
98
+ title: task.title,
99
+ }),
100
+ },
101
+ });
102
+ return <li ref={ref}>{task.title}</li>;
103
+ }
104
+ ```
105
+
106
+ | Field | Meaning |
107
+ | ---------- | --------------------------------------------------------------------------------------------------------------------- |
108
+ | `event` | Your record the new event copies. Your `onEventChange` gets it as `change.event`, so it knows which task was dropped |
109
+ | `duration` | How long the event is when it's dropped on a time slot. An hour by default |
110
+ | `days` | How many days it covers when it's dropped on a day, such as a month cell or the all-day row. 1 by default |
111
+ | `eventId` | An event the calendar shows, to move instead of creating one: its start lands where it's dropped. Not a read-only one |
112
+ | `title` | What the calendar's announcements call it |
113
+
114
+ While the item is over a view, the view previews the slots it would take, from the slot under the
115
+ pointer, and marks them `data-invalid` or `data-warning` when your policies would reject or flag
116
+ them. Over a resource view it books the resource under the pointer. Dropped, it proposes a create
117
+ through the view, as dragging out a range does:
118
+
119
+ ```ts
120
+ function save(change: CalendarEventChange<Meeting, string, string>) {
121
+ if (change.cause === "create" && change.event !== null) {
122
+ // A task dropped on the calendar: its record, where it was dropped.
123
+ addMeeting({ ...change.event, id: newId(), ...placementFields(change.proposed) });
124
+ removeTask(change.event.taskId);
125
+ return;
126
+ }
127
+ // …moves, resizes, deletes and ranges dragged out in a view.
128
+ }
129
+ ```
130
+
131
+ Your own `onDragEnd` still hears the drop. `isCalendarTarget(operation.target)` says it landed on
132
+ a view, which handled it already.
133
+
134
+ ## Drag events out
135
+
136
+ A calendar event a view moves becomes a dnd-kit drag once the pointer leaves the view, of type
137
+ `calendarEventType`, until the move ends. Moves that stay in the view never reach dnd-kit. Your
138
+ droppables see it like any drag: list the type in `accept`, and `isDropTarget` lights up. Released
139
+ over one of them, the drag ends as a drop and the calendar proposes nothing. Any other end, a drop
140
+ back in the calendar or Escape, ends it as `canceled`.
141
+
142
+ ```tsx
143
+ import { useDragDropMonitor, useDroppable } from "@dnd-kit/react";
144
+ import { calendarEventOf, calendarEventType } from "@moltenship/calendar-dnd-kit";
145
+
146
+ function Unscheduled() {
147
+ const actions = Calendar.useCalendarActions();
148
+ const { isDropTarget, ref } = useDroppable({ id: "unscheduled", accept: calendarEventType });
149
+ useDragDropMonitor({
150
+ onDragEnd({ canceled, operation }) {
151
+ const dragged = calendarEventOf<Meeting, string, string>(operation.source);
152
+ if (canceled || dragged === null || operation.target?.id !== "unscheduled") return;
153
+ // Through the pipeline, so policies run and the event hides while it saves.
154
+ void actions.proposeEventChange(
155
+ { cause: "delete", eventId: dragged.eventId },
156
+ { input: "pointer" },
157
+ );
158
+ },
159
+ });
160
+ return (
161
+ <ul ref={ref} data-over={isDropTarget || undefined}>
162
+ …
163
+ </ul>
164
+ );
165
+ }
166
+ ```
167
+
168
+ `calendarEventOf(source)` gives `{ eventId, event, placement }`: the ID, your record and where it
169
+ was, or `null` for any other draggable. Off the view, a copy of the event follows the pointer, with
170
+ `data-outside`. Only moves drag out: resizes and creating a range stay in the view.
171
+
172
+ Once it has left the view, the drag is one of your provider's, so mind what else hears it:
173
+
174
+ - **Droppables without `accept`**, such as `useSortable` items, take calendar events too, and a
175
+ release over one proposes nothing in the calendar. Give your droppables an `accept` that leaves
176
+ calendar events out, or handles them.
177
+ - **Your `onDragStart` and `onDragEnd`** hear it. Leave alone what
178
+ `calendarEventOf(operation.source)` names when it isn't for you; a drag that came back and
179
+ dropped in the calendar ends `canceled`. A very short trip out can end before it starts, with a
180
+ `dragend` and no `dragstart`.
181
+ - **A `DragOverlay`** renders for it. Turn it off for calendar events, which the calendar draws:
182
+ `<DragOverlay disabled={(source) => calendarEventOf(source) !== null}>`.
183
+ - **The Feedback plugin**, one of dnd-kit's defaults, measures the drag's shape, which collision
184
+ detection needs. Without it, drags out reach no droppable. The adapter sets the stand-in's own
185
+ Feedback options and leaves yours alone. The Cursor plugin shows its grabbing cursor too.
186
+
187
+ ## How it behaves
188
+
189
+ - **Auto-scroll.** A view scrolls itself near its edges, once the pointer has been inside it clear
190
+ of them or has rested near one for a quarter of a second, so a drag that crosses an edge on its
191
+ way in doesn't scroll it, and one held there does. dnd-kit's `AutoScroller` is off while the
192
+ pointer is over a view that takes drops, and works as before elsewhere.
193
+ - **Cancel.** Escape, or a lost pointer, cancels: the preview goes and nothing is proposed.
194
+ - **Read-only.** A view of a `Root` without `onEventChange` previews nothing and takes no drop, and
195
+ starts no drags out.
196
+ - **Layers.** The topmost element under the pointer decides. A sheet, dialog or droppable of yours
197
+ over a view hides it, and one around or under the calendar doesn't.
198
+ - **Touch.** Your items use dnd-kit's sensors and their activation, and calendar events keep the
199
+ calendar's long-press. A gesture belongs to one of them: calendar events have no dnd-kit sensor,
200
+ and the calendar never sees a press that began outside it. Don't make dnd-kit draggables of your
201
+ own inside a view.
202
+ - **Keyboard.** dnd-kit's keyboard sensor drags your items in: Space picks one up, the arrow keys
203
+ move it, by 10 pixels or 50 with Shift, with the calendar previewing and announcing where it
204
+ would land, and Space, Enter or Tab drops it. Its steps are pixels, not slots, so it's slow
205
+ across a long way. Calendar events can't be dragged out by keyboard. Give both an explicit
206
+ action too, such as a button that schedules a task and one that unschedules an event.
207
+ - **Server rendering.** The package is a client module. It connects in an effect and reads no DOM
208
+ until then.
209
+ - **Announcements.** The calendar's `Announcer` speaks where an item would land and what came of
210
+ the drop, with its `title`, and proposes with `input: "keyboard"` when the keyboard sensor drove
211
+ it, `"pointer"` otherwise. dnd-kit's own announcements speak too, and their default text names
212
+ draggables and droppables by ID. Give its `Accessibility` plugin announcements that name your
213
+ items, and stay quiet where the calendar speaks: over a view, and for calendar events, which the
214
+ calendar announces until they reach one of your droppables. For example:
215
+
216
+ ```ts
217
+ import { Accessibility } from "@dnd-kit/dom";
218
+ import { calendarEventOf, isCalendarTarget } from "@moltenship/calendar-dnd-kit";
219
+
220
+ type Entity = { readonly data?: unknown; readonly id: string | number } | null;
221
+ type Announced = { readonly operation: { readonly source: Entity; readonly target: Entity } };
222
+
223
+ const taskOf = (source: Entity) => (source?.data as { task?: Task } | undefined)?.task;
224
+ const eventTitle = (source: Entity) => calendarEventOf<Meeting>(source)?.event.title;
225
+
226
+ const announcements = {
227
+ dragstart: ({ operation }: Announced) => {
228
+ const task = taskOf(operation.source);
229
+ return task && `Picked up ${task.title}.`;
230
+ },
231
+ dragover: ({ operation }: Announced) =>
232
+ operation.target === null || isCalendarTarget(operation.target)
233
+ ? undefined
234
+ : eventTitle(operation.source) && "Over the unscheduled tasks.",
235
+ dragend: ({ canceled, operation }: Announced & { readonly canceled: boolean }) => {
236
+ const task = taskOf(operation.source);
237
+ if (task) {
238
+ if (canceled) return `Cancelled. ${task.title} is still unscheduled.`;
239
+ return operation.target === null ? `${task.title} is still unscheduled.` : undefined;
240
+ }
241
+ const title = eventTitle(operation.source);
242
+ return title === undefined || canceled ? undefined : `${title} is unscheduled.`;
243
+ },
244
+ };
245
+
246
+ <DragDropProvider
247
+ plugins={(defaults) => [
248
+ ...defaults.filter((plugin) => plugin !== Accessibility),
249
+ Accessibility.configure({ announcements }),
250
+ ]}
251
+ >
252
+ …
253
+ </DragDropProvider>;
254
+ ```
255
+
256
+ ## API
257
+
258
+ | Export | What it is |
259
+ | ---------------------------------- | --------------------------------------------------------------------------------- |
260
+ | `CalendarDndKit` | A component that connects the views on the page to the `DragDropProvider` it's in |
261
+ | `connectCalendarDragDrop(manager)` | The same for a `DragDropManager`; returns a function that disconnects |
262
+ | `calendarDropData(source)` | `data` that makes a draggable an item the calendar takes |
263
+ | `calendarDropOf(source)` | The calendar drop a draggable carries, or `null` |
264
+ | `calendarEventType` | The `type` of a calendar event dragged out, for `accept` |
265
+ | `calendarEventOf(source)` | `{ eventId, event, placement }` of a calendar event dragged out, or `null` |
266
+ | `isCalendarTarget(target)` | Whether a droppable is a calendar view's |
267
+ | `CalendarEventDragData` | The type `calendarEventOf` returns |