@moltenship/calendar 1.0.0-rc.2 → 2.0.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 (78) hide show
  1. package/README.md +27 -6
  2. package/dist/index.d.mts +3 -3
  3. package/dist/index.mjs +2 -2
  4. package/docs/accessibility.md +305 -0
  5. package/docs/agents/changes.md +449 -0
  6. package/docs/agents/coding-agents.md +116 -0
  7. package/docs/agents/connect.md +444 -0
  8. package/docs/agents/deleting.md +296 -0
  9. package/docs/agents/reference.md +511 -0
  10. package/docs/agents/server.md +317 -0
  11. package/docs/agents/webmcp.md +153 -0
  12. package/docs/agents.md +355 -0
  13. package/docs/annotations.md +198 -0
  14. package/docs/api/changes.md +264 -0
  15. package/docs/api/components.md +245 -0
  16. package/docs/api/dnd-kit.md +265 -0
  17. package/docs/api/events.md +153 -0
  18. package/docs/api/primitives.md +218 -0
  19. package/docs/api/store.md +92 -0
  20. package/docs/api.md +57 -0
  21. package/docs/availability.md +576 -0
  22. package/docs/comparison.md +278 -0
  23. package/docs/concepts/anatomy.md +73 -0
  24. package/docs/concepts/events.md +163 -0
  25. package/docs/concepts/how-it-works.md +112 -0
  26. package/docs/concepts/state.md +244 -0
  27. package/docs/concepts/time.md +168 -0
  28. package/docs/customization.md +271 -0
  29. package/docs/frameworks.md +116 -0
  30. package/docs/guides/dnd-kit.md +333 -0
  31. package/docs/guides/google-calendar-sync.md +1246 -0
  32. package/docs/guides/google-calendar.md +367 -0
  33. package/docs/guides/motion.md +414 -0
  34. package/docs/guides/nextjs-app-router.md +630 -0
  35. package/docs/guides/nextjs.md +149 -0
  36. package/docs/guides/performance.md +103 -0
  37. package/docs/guides/tanstack-query.md +485 -0
  38. package/docs/ics/feed.md +484 -0
  39. package/docs/ics/import.md +183 -0
  40. package/docs/ics/reference.md +482 -0
  41. package/docs/ics.md +142 -0
  42. package/docs/index.md +149 -0
  43. package/docs/installation.md +209 -0
  44. package/docs/interactions.md +441 -0
  45. package/docs/llms.txt +99 -0
  46. package/docs/migrating-to-2.md +497 -0
  47. package/docs/quick-start.md +163 -0
  48. package/docs/recurrence/calendar.md +598 -0
  49. package/docs/recurrence/model.md +128 -0
  50. package/docs/recurrence/reference.md +547 -0
  51. package/docs/recurrence.md +122 -0
  52. package/docs/stylex.md +290 -0
  53. package/docs/temporal/daylight-saving-time.md +172 -0
  54. package/docs/temporal/month-grid.md +119 -0
  55. package/docs/temporal/reference.md +167 -0
  56. package/docs/temporal.md +105 -0
  57. package/docs/testing.md +600 -0
  58. package/docs/theme-builder.md +82 -0
  59. package/docs/theming.md +275 -0
  60. package/docs/undo-redo.md +347 -0
  61. package/docs/views/agenda.md +222 -0
  62. package/docs/views/custom-views.md +195 -0
  63. package/docs/views/month.md +110 -0
  64. package/docs/views/resource-columns.md +265 -0
  65. package/docs/views/resource-timeline.md +226 -0
  66. package/docs/views/scrolling.md +193 -0
  67. package/docs/views/week-and-day.md +258 -0
  68. package/docs/views/year.md +138 -0
  69. package/docs/views.md +69 -0
  70. package/package.json +42 -14
  71. package/skills/moltenship-time/SKILL.md +77 -0
  72. package/skills/moltenship-time/references/agent-ready.md +272 -0
  73. package/skills/moltenship-time/references/changes.md +188 -0
  74. package/skills/moltenship-time/references/compose.md +175 -0
  75. package/skills/moltenship-time/references/mistakes.md +100 -0
  76. package/skills/moltenship-time/references/solid.md +213 -0
  77. package/skills/moltenship-time/references/state.md +129 -0
  78. package/skills/moltenship-time/references/testing.md +176 -0
@@ -0,0 +1,333 @@
1
+ # Drag tasks in with dnd-kit
2
+
3
+ Drag a backlog of your own into the calendar, and meetings back out, with dnd-kit and @moltenship/calendar-dnd-kit.
4
+
5
+ Source: https://time.molten.sh/docs/react/guides/dnd-kit
6
+
7
+ > Live example: see it at https://time.molten.sh/docs/react/guides/dnd-kit
8
+
9
+ Drag a task from the backlog onto the week: the week shows the slots it would take, from the slot
10
+ under the pointer, and it becomes a meeting there when you let go. Drag a meeting onto the backlog
11
+ and it becomes a task again. The all-day row turns red under a task, because a policy only takes
12
+ timed meetings.
13
+
14
+ Your app already uses [dnd-kit](https://dndkit.com) for its own lists, so the backlog's cards are
15
+ ordinary dnd-kit draggables. `@moltenship/calendar-dnd-kit` connects them to the calendar: a drop
16
+ goes through the calendar's slots, policies and `onEventChange` like any drag. The calendar's own
17
+ drags, moving and resizing meetings inside the week, stay on its own pointer layer. The
18
+ [API reference](../api/dnd-kit.md) lists every export and option.
19
+
20
+ ## Install
21
+
22
+ **pnpm**
23
+
24
+ ```bash
25
+ pnpm add @moltenship/calendar-dnd-kit @dnd-kit/react @dnd-kit/dom
26
+ ```
27
+
28
+ **npm**
29
+
30
+ ```bash
31
+ npm install @moltenship/calendar-dnd-kit @dnd-kit/react @dnd-kit/dom
32
+ ```
33
+
34
+ **yarn**
35
+
36
+ ```bash
37
+ yarn add @moltenship/calendar-dnd-kit @dnd-kit/react @dnd-kit/dom
38
+ ```
39
+
40
+ **bun**
41
+
42
+ ```bash
43
+ bun add @moltenship/calendar-dnd-kit @dnd-kit/react @dnd-kit/dom
44
+ ```
45
+
46
+ The adapter takes dnd-kit 0.5, its current packages. Like the calendar, it's on npm as a release
47
+ candidate for 1.0.0.
48
+
49
+ ## The calendar
50
+
51
+ The example's meetings are timed. A policy rejects any change that would make one all-day, so the
52
+ week's all-day row takes no task, and its preview shows that:
53
+
54
+ ```tsx title="backlog-calendar.tsx"
55
+ import { createCalendar, defineEventAdapter, type CalendarEventChange } from "@moltenship/calendar";
56
+ import {
57
+ CalendarDndKit,
58
+ calendarDropData,
59
+ calendarEventOf,
60
+ calendarEventType,
61
+ isCalendarTarget,
62
+ } from "@moltenship/calendar-dnd-kit";
63
+ import { Accessibility } from "@dnd-kit/dom";
64
+ import { DragDropProvider, useDragDropMonitor, useDraggable, useDroppable } from "@dnd-kit/react";
65
+ import { useCallback, useMemo, useState } from "react";
66
+
67
+ import { calendarAnnouncements } from "@/components/calendar/announcements";
68
+ import { createWeekView } from "@/components/calendar/time-grid-view/time-grid-view";
69
+
70
+ export interface Meeting {
71
+ readonly end: Temporal.Instant;
72
+ readonly id: string;
73
+ readonly start: Temporal.Instant;
74
+ readonly title: string;
75
+ }
76
+
77
+ export interface Task {
78
+ readonly id: string;
79
+ readonly minutes: number;
80
+ readonly title: string;
81
+ }
82
+
83
+ export const Calendar = createCalendar<Meeting>()({
84
+ eventAdapter: defineEventAdapter<Meeting>()({
85
+ getId: (meeting) => meeting.id,
86
+ getInterval: ({ end, start }) => ({ end, kind: "timed", start }),
87
+ getResourceIds: () => [],
88
+ getTitle: (meeting) => meeting.title,
89
+ }),
90
+ views: [createWeekView<Meeting>()],
91
+ });
92
+
93
+ export type MeetingChange = CalendarEventChange<Meeting, string, string>;
94
+
95
+ /** Meetings have times: the all-day row takes no drop, and its preview says so. */
96
+ function timedOnly({ proposed }: Pick<MeetingChange, "proposed">) {
97
+ return proposed !== null && proposed.kind !== "timed"
98
+ ? ({ reason: "timed-only", type: "reject" } as const)
99
+ : ({ type: "accept" } as const);
100
+ }
101
+ const policies = [timedOnly];
102
+ ```
103
+
104
+ ## A task becomes a meeting
105
+
106
+ Each card spreads `calendarDropData` into its `data`: the record the task becomes, how long it
107
+ takes, and a title for screen readers. The record's times are placeholders; the drop decides when
108
+ the meeting is.
109
+
110
+ ```tsx title="backlog-calendar.tsx"
111
+ /** The meeting a task becomes, under the task's ID. The drop decides when it is. */
112
+ export function meetingFromTask(task: Task): Meeting {
113
+ const start = Temporal.Instant.fromEpochMilliseconds(0);
114
+ return { end: start.add({ minutes: task.minutes }), id: task.id, start, title: task.title };
115
+ }
116
+
117
+ function TaskCard({ task }: { readonly task: Task }) {
118
+ const data = useMemo(
119
+ () => ({
120
+ task,
121
+ ...calendarDropData({
122
+ duration: { minutes: task.minutes },
123
+ event: meetingFromTask(task),
124
+ title: task.title,
125
+ }),
126
+ }),
127
+ [task],
128
+ );
129
+ const { ref } = useDraggable({ data, id: task.id });
130
+ return (
131
+ <li>
132
+ <button
133
+ ref={ref}
134
+ type="button"
135
+ className="w-full cursor-grab rounded-md border-s-3 border-calendar-event-border bg-calendar-event px-2 py-1.5 text-start text-sm text-calendar-event-foreground"
136
+ >
137
+ <span className="block font-medium">{task.title}</span>
138
+ <span className="sr-only">, </span>
139
+ <span className="block text-xs">{task.minutes} min</span>
140
+ </button>
141
+ </li>
142
+ );
143
+ }
144
+ ```
145
+
146
+ Put one `DragDropProvider` around both the backlog and the calendar, and `CalendarDndKit` inside
147
+ it. `onEventChange` gets a dropped task as a create whose `event` is the record. A range dragged
148
+ out in the week is a create without one, which this example leaves alone:
149
+
150
+ ```tsx title="backlog-calendar.tsx"
151
+ const weekRules = { firstDay: 1, minimalDays: 4, weekend: [6, 7] } as const;
152
+
153
+ export interface BacklogCalendarProps {
154
+ readonly anchorDate: Temporal.PlainDate;
155
+ readonly initialMeetings: readonly Meeting[];
156
+ readonly initialTasks: readonly Task[];
157
+ readonly timeZone: string;
158
+ }
159
+
160
+ export function BacklogCalendar({
161
+ anchorDate,
162
+ initialMeetings,
163
+ initialTasks,
164
+ timeZone,
165
+ }: BacklogCalendarProps) {
166
+ const [meetings, setMeetings] = useState(initialMeetings);
167
+ const [tasks, setTasks] = useState(initialTasks);
168
+
169
+ const save = useCallback((change: MeetingChange) => {
170
+ if (change.cause === "delete") {
171
+ setMeetings((current) => current.filter(({ id }) => id !== change.eventId));
172
+ return;
173
+ }
174
+ const { proposed } = change;
175
+ if (proposed.kind !== "timed") return;
176
+ const { end, start } = proposed;
177
+ if (change.cause === "create") {
178
+ // A range dragged out in the week has no record; only dropped tasks carry one.
179
+ const dropped = change.event;
180
+ if (dropped === null) return;
181
+ setMeetings((current) => [...current, { ...dropped, end, start }]);
182
+ setTasks((current) => current.filter(({ id }) => id !== dropped.id));
183
+ return;
184
+ }
185
+ setMeetings((current) =>
186
+ current.map((meeting) =>
187
+ meeting.id === change.eventId ? { ...meeting, end, start } : meeting,
188
+ ),
189
+ );
190
+ }, []);
191
+ const unscheduled = useCallback((task: Task) => setTasks((current) => [task, ...current]), []);
192
+
193
+ return (
194
+ <Calendar.Root
195
+ defaultAnchorDate={anchorDate}
196
+ defaultView="week"
197
+ events={meetings}
198
+ locale="en-US"
199
+ onEventChange={save}
200
+ policies={policies}
201
+ timeZone={timeZone}
202
+ weekRules={weekRules}
203
+ >
204
+ {/* One provider around the backlog and the calendar; CalendarDndKit connects them. */}
205
+ <DragDropProvider plugins={plugins}>
206
+ <CalendarDndKit />
207
+ <Calendar.Frame className="flex h-full min-h-0">
208
+ <aside className="flex w-44 shrink-0 flex-col gap-2 overflow-y-auto border-e p-2">
209
+ <h2 className="px-1 text-sm font-medium">Backlog</h2>
210
+ <Backlog onUnscheduled={unscheduled} tasks={tasks} />
211
+ </aside>
212
+ <Calendar.Viewport className="min-h-0 min-w-0 flex-1">
213
+ <Calendar.ActiveView />
214
+ </Calendar.Viewport>
215
+ <Calendar.Announcer messages={calendarAnnouncements} />
216
+ </Calendar.Frame>
217
+ </DragDropProvider>
218
+ </Calendar.Root>
219
+ );
220
+ }
221
+ ```
222
+
223
+ The create runs through the root's policies first, so the drop on the all-day row is rejected and
224
+ the task stays in the backlog. The calendar's `Announcer` names the task as it moves over the week
225
+ and says where it was added.
226
+
227
+ ## A meeting goes back to the backlog
228
+
229
+ While a meeting is dragged out of the week, it's a dnd-kit drag of type `calendarEventType`, so the
230
+ backlog takes it with `accept`, and lights up with `isDropTarget`. Dropped there, the drag ends on
231
+ the backlog and the calendar proposes nothing. The backlog proposes the delete itself, so the
232
+ policies run and the meeting hides while it saves, and turns it into a task once it's saved:
233
+
234
+ ```tsx title="backlog-calendar.tsx"
235
+ /** The tasks, and a drop zone that takes meetings back off the calendar. */
236
+ function Backlog({
237
+ onUnscheduled,
238
+ tasks,
239
+ }: {
240
+ readonly onUnscheduled: (task: Task) => void;
241
+ readonly tasks: readonly Task[];
242
+ }) {
243
+ const actions = Calendar.useCalendarActions();
244
+ const { isDropTarget, ref } = useDroppable({ accept: calendarEventType, id: "backlog" });
245
+ useDragDropMonitor(
246
+ useMemo(
247
+ () => ({
248
+ onDragEnd: ({ canceled, operation }) => {
249
+ const dragged = calendarEventOf<Meeting, string>(operation.source);
250
+ if (canceled || dragged === null || operation.target?.id !== "backlog") return;
251
+ const { end, id, start, title } = dragged.event;
252
+ // Through the calendar, so policies run and the meeting hides while it saves.
253
+ void actions
254
+ .proposeEventChange({ cause: "delete", eventId: id }, { input: "pointer" })
255
+ .then((result) => {
256
+ if (result.status !== "committed") return;
257
+ onUnscheduled({ id, minutes: start.until(end).total({ unit: "minutes" }), title });
258
+ });
259
+ },
260
+ }),
261
+ [actions, onUnscheduled],
262
+ ),
263
+ );
264
+ return (
265
+ <ul
266
+ ref={ref}
267
+ aria-label="Backlog"
268
+ className="flex min-h-24 flex-col gap-1.5 rounded-lg p-1.5 data-over:bg-accent data-over:ring-2 data-over:ring-ring"
269
+ data-over={isDropTarget || undefined}
270
+ >
271
+ {tasks.map((task) => (
272
+ <TaskCard key={task.id} task={task} />
273
+ ))}
274
+ </ul>
275
+ );
276
+ }
277
+ ```
278
+
279
+ Any other end of that drag, a drop in the week or Escape, ends it as `canceled`.
280
+
281
+ ## Keyboard and touch
282
+
283
+ - dnd-kit's keyboard sensor drags tasks in: focus a card, press Space, move it with the arrow
284
+ keys, Shift for bigger steps, and press Space again to drop it. The week previews and announces
285
+ each step. Meetings can't be dragged out by keyboard, so give tasks and meetings a button of their
286
+ own too, as the [demo](https://moltenship-time-demo.vercel.app) does: "Schedule" on a task opens
287
+ the event editor, and "Unschedule" in a meeting's popover moves it back.
288
+ - On a touch screen, cards use dnd-kit's touch activation and meetings the calendar's long-press.
289
+ - dnd-kit's own announcements name draggables by ID. The example gives its `Accessibility` plugin
290
+ announcements that name tasks and meetings, and stay quiet where the calendar's `Announcer`
291
+ speaks for itself:
292
+
293
+ ```tsx title="backlog-calendar.tsx"
294
+ type Entity = { readonly data?: unknown; readonly id: string | number } | null;
295
+ type Announced = { readonly operation: { readonly source: Entity; readonly target: Entity } };
296
+
297
+ const taskOf = (source: Entity) => (source?.data as { task?: Task } | undefined)?.task;
298
+ const meetingOf = (source: Entity) => calendarEventOf<Meeting>(source)?.event;
299
+
300
+ /**
301
+ * What dnd-kit says, by name rather than its default IDs. Over the week, and for meetings until
302
+ * they reach the backlog, the calendar's own Announcer speaks instead.
303
+ */
304
+ const announcements = {
305
+ dragstart: ({ operation }: Announced) => {
306
+ const task = taskOf(operation.source);
307
+ return task && `Picked up ${task.title}.`;
308
+ },
309
+ dragover: ({ operation }: Announced) =>
310
+ operation.target === null || isCalendarTarget(operation.target)
311
+ ? undefined
312
+ : meetingOf(operation.source) && "Over the backlog.",
313
+ dragend: ({ canceled, operation }: Announced & { readonly canceled: boolean }) => {
314
+ const task = taskOf(operation.source);
315
+ if (task)
316
+ return canceled || operation.target === null
317
+ ? `${task.title} stays in the backlog.`
318
+ : undefined;
319
+ const meeting = meetingOf(operation.source);
320
+ return meeting === undefined || canceled
321
+ ? undefined
322
+ : `${meeting.title} is back in the backlog.`;
323
+ },
324
+ };
325
+
326
+ /** dnd-kit's default plugins, with these announcements. */
327
+ function plugins<TPlugin>(defaults: readonly TPlugin[]) {
328
+ return [
329
+ ...defaults.filter((plugin) => plugin !== (Accessibility as unknown)),
330
+ Accessibility.configure({ announcements }),
331
+ ];
332
+ }
333
+ ```