Drag, resize and selectCalendar
Move and resize events by pointer, touch and keyboard, select time to create events, choose the edit scope for recurring events, revert changes and validate them.
Users edit the calendar directly: drag an event to move it, drag its edge to change its length, or drag across empty time to select a span for a new event. Every change is announced through an event whose payload you can inspect, adjust or revert.
Pointer and touch#
| Gesture | Mouse and pen | Touch |
|---|---|---|
| Move | Press on an event and drag. The drag starts after 4 pixels of movement, so a click still opens the event. | Press and hold for about 350 ms, then drag. Moving the finger earlier scrolls the page instead. |
| Resize | Drag the start or end edge of an event in the time grid, or the end of an all-day bar. | Same, after the long press. |
| Select | Drag across empty slots or days. | Long press on an empty slot, then drag. |
| Cancel | Press Escape while dragging. | The drag is cancelled when the browser cancels the touch, for example to scroll. |
Dragging works in the month, week, day and N-day views. In the time grid, moving between the all-day row and the slots changes an event between all-day and timed; a timed event dropped on the all-day row becomes a one-day event, and an all-day event dropped on a slot gets defaultTimedDuration (60 minutes unless you change it). Moves and resizes in the time grid snap to snapMinutes (15 by default).
Agenda and year views are for reading: events there can be clicked and removed from the keyboard, not dragged.
The change payload#
eventDrop and eventResize fire after the user lets go and before the change is applied. The payload describes the proposed change:
| Field | Description |
|---|---|
event |
The event as it is now (for a recurring event, the series master). |
occurrence |
The EventOccurrence that was dragged. |
start, end, allDay |
The new times, as Date objects. |
oldStart, oldEnd, oldAllDay |
The times before the change. |
isRecurring |
Whether the event belongs to a series. |
scope |
'this' for recurring events, 'all' otherwise. Set it to choose how a series is edited. |
source |
'pointer' or 'keyboard'. |
revert() |
Cancels the change, or undoes it if it has already been applied. |
delta |
eventDrop only: how far the event moved, in milliseconds. |
edge |
eventResize only: 'start' or 'end'. |
The change is applied as soon as your listener returns, unless you called revert() in it. That gives three patterns.
Save, and roll back on failure#
revert() keeps working after the change has been applied, which makes optimistic updates simple:
calendar.on('eventDrop', (change) => {
saveEvent(change.event.id, { start: change.start, end: change.end, allDay: change.allDay }).catch(() => {
change.revert();
toast.show(`${change.event.title} could not be moved. The visit is already confirmed with the customer.`);
});
});
calendar.on('eventResize', (change) => {
saveEvent(change.event.id, { start: change.start, end: change.end }).catch(() => change.revert());
});To persist exactly what the calendar stored, including the format of your date strings, read the event back after the listener has returned:
calendar.on('eventDrop', (change) => {
queueMicrotask(() => {
const isDetached = change.isRecurring && change.scope === 'this';
const stored = calendar.getEvent(isDetached ? change.occurrence.occurrenceId : change.occurrence.id);
if (stored) void api.put(`/events/${encodeURIComponent(String(stored.id))}`, stored);
});
});For a recurring event moved with scope 'this', the stored result is a new detached event whose id is the occurrence's occurrenceId, and the master gains an exdates entry. See Editing occurrences.
Choose the scope for recurring events#
calendar.on('eventDrop', (change) => {
if (change.isRecurring) change.scope = window.confirm('Move all future and past occurrences?') ? 'all' : 'this';
});scope is read when your listener returns, so set it synchronously. For an asynchronous dialog, revert() first and apply the answer later with updateOccurrence(occurrenceId, { start, end }, scope).
Refuse a change#
calendar.on('eventDrop', (change) => {
if (change.start.getTime() < Date.now()) change.revert();
});Refusing after the drop works, but the user only learns about it when the event jumps back. Validators, below, show the refusal during the drag instead.
Validation#
validateDrop(info) and validateResize(info) run on every snapped position while the user drags, and before a keyboard change. Return false to refuse: the drag preview turns into an invalid state (class lq-cal-event--invalid), and releasing there leaves the event unchanged with no event emitted. Any other value accepts the change. A validator that throws refuses the change and logs the error.
info has event, occurrence, start, end and allDay; validateResize also gets edge.
const scheduleCalendar = createCalendar(container, {
view: 'week',
timeZone: 'America/Chicago',
businessHours: { daysOfWeek: [1, 2, 3, 4, 5], startTime: '08:00', endTime: '18:00' },
events: visits,
validateDrop: ({ event, start, end, allDay }) =>
!allDay && isWithinBusinessHours(start, end) && !overlapsOtherVisit(event.id, start, end),
validateResize: ({ start, end }) => end.getTime() - start.getTime() <= 4 * 60 * 60 * 1000,
});
function overlapsOtherVisit(id: string | number, start: Date, end: Date): boolean {
return scheduleCalendar
.getOccurrences(start, end)
.some((other) => other.id !== String(id) && other.start < end && other.end > start);
}Keep validators fast and free of side effects; they run many times per drag. Business hours only shade the grid, so check them in the validator when they are a hard rule.
Select to create#
Dragging across empty slots, or across days in the month view and the all-day row, emits select with the span. The calendar does not create anything itself; add the event when you have the details:
calendar.on('select', async ({ start, end, allDay, view }) => {
const details = await newEventDialog.open({ start, end, allDay });
if (!details) return;
calendar.addEvent({
id: crypto.randomUUID(),
title: details.title,
start,
end,
allDay,
location: details.location,
});
});| Payload field | Description |
|---|---|
start, end |
The selected span as Dates. In the time grid it snaps to snapMinutes; in day cells it covers whole days, with end exclusive. |
allDay |
true when the selection was made on days rather than time slots. |
view |
The view the selection was made in. |
Set selectable: false to turn selection off.
A plain click on an empty slot or day, without dragging, emits dateClick instead, with the clicked date (floored to snapMinutes in the time grid), allDay, view and the native event. Use it for one-click creation:
calendar.on('dateClick', ({ date, allDay }) => {
const start = date.getTime();
calendar.addEvent({
id: crypto.randomUUID(),
title: 'New visit',
start,
end: allDay ? undefined : start + 60 * 60 * 1000,
allDay,
});
});Removing events#
There is no delete gesture for the mouse. Keyboard users remove the focused event with Delete or Backspace, which emits eventRemove before the event is removed. The payload has event, occurrence, isRecurring, scope, source and revert(), and works like the change payload:
calendar.on('eventRemove', (removal) => {
if (removal.isRecurring) removal.scope = window.confirm('Delete the whole series?') ? 'all' : 'this';
deleteOnServer(removal.event.id, removal.scope).catch(() => removal.revert());
});For a delete button in your own event dialog, call removeEvent(id) or removeOccurrence(occurrenceId, scope). Those API calls do not emit eventRemove.
Clicks#
| Event | Payload | Fired when |
|---|---|---|
eventClick |
{ event, occurrence, element, nativeEvent } |
An event is clicked, or activated with Enter or Space. |
eventDoubleClick |
{ event, occurrence, element, nativeEvent } |
An event is double-clicked. |
dateClick |
{ date, allDay, view, nativeEvent } |
Empty space is clicked, or a day cell is activated from the keyboard. |
moreClick |
{ date, occurrences, hiddenCount, preventDefault } |
A "+N more" button is activated. |
element is the event's DOM element, convenient for anchoring a popover:
calendar.on('eventClick', ({ occurrence, element }) => {
eventPopover.show({ anchor: element, title: occurrence.title, start: occurrence.start, end: occurrence.end });
});Read-only and locked events#
| Setting | Effect |
|---|---|
readOnly: true |
No moving, resizing, selecting or removing. Clicks, navigation and keyboard focus still work. |
editable: false on an event |
That event cannot be moved, resized or removed. |
selectable: false |
No selecting; moving and resizing still work. |
calendar.update({ readOnly: !currentUser.canEditSchedule });
calendar.addEvent({ id: 'holiday-ny', title: "New Year's Day", start: '2027-01-01', allDay: true, editable: false });Next steps#
- Keyboard and accessibility for keyboard editing.
- Recurrence for series and detached occurrences.
- API and events for every payload type.