Event calendar
A recipe, not a package namespace. An event calendar shows events on a month, on a week of day columns, or on one day, in one time zone. In the week and the day, each day is a lane of its visible hours, and an event is a block on the lane its start and end fall in. There is no EventCalendar.* export.
On this page
The example is the week of 16 November 2026, open from 8:00 to 18:00. It pins America/Los_Angeles and adds Asia/Kolkata beside it. The corner lists every zone name the visible days use, such as PST or PST / PDT, in the page's language. timeZone is an IANA id. Leave it unset and the grid uses the viewer's zone. A server-rendered app passes timeZone explicitly, so the server and the browser name the same zone and the markup matches. An event is a Temporal.Instant or a Temporal.ZonedDateTime. The grid places that instant in timeZone, so the same event sits on a different hour when the zone changes. compareZone adds the second hour column. Each row is that zone's local time at the same instant as the primary hour. When the week uses two offsets, the row shows both times in the corner's order, such as 5:00 PM / 6:00 PM. The day heading names the first visible day that uses the later offset, such as PDT begins, and says Second column when that column's zone changes. A zone whose offset is not a whole hour, such as Asia/Kolkata at +5:30 or Asia/Kathmandu at +5:45, shows that minute. When the date differs, the row also shows the day shift, such as +1 day. A day shift belongs to the time it follows. Switch to Month to see that month, to Day to see one column, and use Previous and Next to move. Thursday is a busy day: overlapping events sit side by side. Offsite is all day on Monday. Retreat runs from Friday through Sunday and crosses into the next week. Launch watch runs from 10:00 PM to 2:00 AM. Book club repeats every Sunday at 3:00 to 4:00 PM, starting 15 November, and each view shows the occurrences in that window.
import {
Button,
Calendar,
Dialog,
ErrorMessage,
Field,
Form,
Label,
Menu,
RadioGroup,
ScrollArea,
Switch,
Text,
ToggleGroup,
VisuallyHidden,
formatTime,
useAnnouncer,
useFocusHandoff,
useLaneDrag,
useLaneDragState,
type FormErrors,
type UseLaneDragResult,
} from '@tounsoo/todae';
import { memo, useId, useLayoutEffect, useRef, useState, type RefObject, type SubmitEvent } from 'react';
import {
byDayOf,
continueFrom,
excludeOccurrence,
expandSeries,
overrideOccurrence,
remainingCount,
replaceSeries,
seriesFromDraft,
seriesId,
truncateBefore,
viewWindow,
type ByDay,
type EventSeries,
type ExpandWindow,
type RecurrenceRule,
type SeriesId,
} from './recurrence';
import styles from './EventCalendar.module.css';
export type CalendarEvent = {
id: string;
title: string;
seriesId?: string;
start: Temporal.ZonedDateTime;
end: Temporal.ZonedDateTime;
allDay: boolean;
/** True when the event is one occurrence of a repeating series. */
repeats?: boolean;
/** The start the rule generated, before an override moved it. */
originalStart?: Temporal.ZonedDateTime;
};
/** An absolute instant, or a zoned value whose instant is what the grid places. */
export type CalendarPoint = Temporal.ZonedDateTime | Temporal.Instant;
/** A value the grid can be given. An all-day event keeps the zone it was written in. */
export type CalendarEventInput = Omit<CalendarEvent, 'start' | 'end'> & {
start: CalendarPoint;
end: CalendarPoint;
};
/** The instant as a zoned value in `timeZone`. A zoned value keeps that instant. */
export function inZone(value: CalendarPoint, timeZone: string): Temporal.ZonedDateTime {
return value instanceof Temporal.Instant ? value.toZonedDateTimeISO(timeZone) : value.withTimeZone(timeZone);
}
function projectEvent(event: CalendarEventInput, timeZone: string): CalendarEvent {
const keep = (value: CalendarPoint) => (event.allDay && value instanceof Temporal.ZonedDateTime ? value : inZone(value, timeZone));
return { ...event, start: keep(event.start), end: keep(event.end) };
}
export type EditScope = 'occurrence' | 'following' | 'series';
export type DaySegment = {
day: Temporal.PlainDate;
opens: Temporal.ZonedDateTime;
closes: Temporal.ZonedDateTime;
length: number;
};
export type DayAxis = { segments: readonly DaySegment[] };
export type DayPosition = { day: Temporal.PlainDate; units: number };
type Edge = 'start' | 'end';
function zoned(day: Temporal.PlainDate, time: Temporal.PlainTime, timeZone: string): Temporal.ZonedDateTime {
return day.toZonedDateTime({ timeZone, plainTime: time });
}
function minutesBetween(from: Temporal.ZonedDateTime, to: Temporal.ZonedDateTime): number {
return (to.epochMilliseconds - from.epochMilliseconds) / 60_000;
}
/**
* PlainTime has no 24:00, so a close at or before the open on the clock is that
* time the next day. When the resolved close still falls at or before the open,
* the whole range sat in a skipped hour and the lane is empty.
*/
export function daySegments({
days,
opens,
closes,
timeZone,
}: {
days: readonly Temporal.PlainDate[];
opens: Temporal.PlainTime;
closes: Temporal.PlainTime;
timeZone: string;
}): DayAxis {
const wraps = Temporal.PlainTime.compare(closes, opens) <= 0;
return {
segments: days.map((day) => {
const open = zoned(day, opens, timeZone);
let close = zoned(wraps ? day.add({ days: 1 }) : day, closes, timeZone);
if (Temporal.ZonedDateTime.compare(close, open) <= 0) close = open;
return { day, opens: open, closes: close, length: minutesBetween(open, close) };
}),
};
}
// The close of one day and the open of the next can be one instant. A start
// belongs to the later day, and an end to the earlier one.
function holds(length: number, units: number, edge: Edge): boolean {
return edge === 'start' ? units >= 0 && units < length : units > 0 && units <= length;
}
export function toUnits(axis: DayAxis, at: Temporal.ZonedDateTime, edge: Edge = 'start'): DayPosition | null {
for (const segment of axis.segments) {
const units = minutesBetween(segment.opens, at);
if (holds(segment.length, units, edge)) return { day: segment.day, units };
}
return null;
}
export function fromUnits(segment: DaySegment, units: number, edge: Edge = 'start'): Temporal.ZonedDateTime | null {
if (!holds(segment.length, units, edge)) return null;
return segment.opens.add({ minutes: units });
}
export function snapGrid(segment: DaySegment, step: number): number[] {
if (step <= 0) throw new RangeError('step must be greater than 0');
const points = new Set<number>();
for (let units = 0; units < segment.length; units += step) points.add(units);
points.add(segment.length);
return [...points].sort((a, b) => a - b);
}
/** The slot a timed event moves or resizes by. Week and day lanes snap to it. */
export const SLOT_MINUTES = 15;
export type LaneSpan = { start: number; end: number };
export type SlotEdge = 'move' | 'start' | 'end';
const GRID_DAY = Temporal.PlainDate.from('1970-01-01');
const GRID_OPEN = GRID_DAY.toZonedDateTime('UTC');
function gridFor(length: number, step: number): number[] {
return snapGrid({ day: GRID_DAY, opens: GRID_OPEN, closes: GRID_OPEN, length }, step);
}
/**
* The grid point nearest `units`. A tie goes to the earlier point, the same
* choice `useLaneDrag` makes. `length` is on the grid, so a close can be hit.
*/
export function snapToSlot(units: number, step: number, length: number): number {
const grid = gridFor(length, step);
let best = grid[0] ?? 0;
let bestDistance = Math.abs(units - best);
for (const point of grid) {
const distance = Math.abs(units - point);
if (distance < bestDistance || (distance === bestDistance && point < best)) {
best = point;
bestDistance = distance;
}
}
return best;
}
function fittingStart(span: number, proposed: number, length: number, step: number): number | null {
if (span <= 0 || span > length) return null;
let start = snapToSlot(proposed, step, length);
if (start < 0) start = 0;
if (start + span <= length) return start;
const room = length - span;
start = snapToSlot(room, step, length);
if (start + span <= length && start >= 0) return start;
const grid = gridFor(length, step);
for (let index = grid.length - 1; index >= 0; index -= 1) {
const point = grid[index] ?? 0;
if (point >= 0 && point + span <= length) return point;
}
return null;
}
/**
* Where a move or a resize lands on a lane of `length` minutes.
* A move keeps its length, snaps the start, and slides back inside the lane.
* A resize snaps the moved edge, keeps the other, and stops one `step` short
* of it. Null when the span cannot sit in the lane.
*/
export function clampSpan(
current: LaneSpan,
proposed: LaneSpan,
length: number,
step: number,
edge: SlotEdge,
): LaneSpan | null {
if (length <= 0 || step <= 0) return null;
if (edge === 'move') {
const span = current.end - current.start;
const start = fittingStart(span, proposed.start, length, step);
if (start === null) return null;
return { start, end: start + span };
}
if (edge === 'end') {
let end = snapToSlot(proposed.end, step, length);
if (end > length) end = snapToSlot(length, step, length);
const minEnd = current.start + step;
if (end < minEnd) end = minEnd;
if (end > length || end <= current.start) return null;
return { start: current.start, end };
}
let start = snapToSlot(proposed.start, step, length);
if (start < 0) start = 0;
const maxStart = current.end - step;
if (start > maxStart) start = maxStart;
if (start < 0 || start >= current.end) return null;
return { start, end: current.end };
}
/** A drop keeps `next`. Escape, or a drop with nowhere to land, keeps `original`. */
export function finishGesture<T>(original: T, next: T | null, gesture: 'drop' | 'escape'): T {
if (gesture === 'escape' || next === null) return original;
return next;
}
export type SlotChange = { kind: 'shift'; slots: number } | { kind: 'day'; days: number } | { kind: 'length'; slots: number };
function shiftEvent(event: CalendarEvent, minutes: number, days: number, timeZone: string): CalendarEvent {
// Add the day in the calendar's zone, then the minutes. One duration whose
// day and minute signs differ is rejected, and the event's own zone would
// keep its wall clock across a clock change.
const shift = (value: Temporal.ZonedDateTime) => value.withTimeZone(timeZone).add({ days }).add({ minutes });
return { ...event, start: shift(event.start), end: shift(event.end) };
}
function clippedUnits(segment: DaySegment, event: CalendarEvent): LaneSpan | null {
if (event.allDay || segment.length === 0) return null;
if (Temporal.ZonedDateTime.compare(event.start, segment.closes) >= 0) return null;
if (Temporal.ZonedDateTime.compare(event.end, segment.opens) <= 0) return null;
const clippedStart = Temporal.ZonedDateTime.compare(event.start, segment.opens) < 0 ? segment.opens : event.start;
const clippedEnd = Temporal.ZonedDateTime.compare(event.end, segment.closes) > 0 ? segment.closes : event.end;
const start = (clippedStart.epochMilliseconds - segment.opens.epochMilliseconds) / 60_000;
const end = (clippedEnd.epochMilliseconds - segment.opens.epochMilliseconds) / 60_000;
if (end <= start) return null;
return { start, end };
}
/**
* One keyboard step on `segment`. A shift or a day move that would leave the
* lane does nothing, so the menu can disable it. Lengthening and shortening
* move the end, and stop at the close and at one slot long.
*/
export function adjustEvent(event: CalendarEvent, segment: DaySegment, axis: DayAxis, change: SlotChange, step = SLOT_MINUTES): CalendarEvent | null {
const visible = clippedUnits(segment, event);
if (!visible) return null;
if (change.kind === 'shift') {
const proposed = { start: visible.start + change.slots * step, end: visible.end + change.slots * step };
if (proposed.start < 0 || proposed.end > segment.length) return null;
return shiftEvent(event, change.slots * step, 0, segment.opens.timeZoneId);
}
if (change.kind === 'day') {
const targetDay = segment.day.add({ days: change.days });
const target = axis.segments.find((each) => each.day.equals(targetDay));
if (!target || visible.end > target.length) return null;
return shiftEvent(event, 0, change.days, segment.opens.timeZoneId);
}
const proposedEnd = visible.end + change.slots * step;
if (proposedEnd > segment.length || proposedEnd - visible.start < step) return null;
const clamped = clampSpan(visible, { start: visible.start, end: proposedEnd }, segment.length, step, 'end');
if (!clamped || clamped.end !== proposedEnd) return null;
const end = fromUnits(segment, clamped.end, 'end');
if (!end || Temporal.ZonedDateTime.compare(end, event.start) <= 0) return null;
return { ...event, end };
}
export type LaneDrop = {
from: { day: Temporal.PlainDate; start: number; end: number };
to: { day: Temporal.PlainDate; start: number; end: number };
kind: 'move' | 'resize-start' | 'resize-end';
};
/**
* The event a drop leaves. A move keeps the length and shifts the whole event,
* including the part a lane clips, by the snapped delta. A resize writes the
* drawn edge, so a handle on a clipped side sets the real start or end.
*/
export function eventFromLane(event: CalendarEvent, drop: LaneDrop, axis: DayAxis): CalendarEvent | null {
const segment = axis.segments.find((each) => each.day.equals(drop.to.day));
if (!segment) return null;
const zone = segment.opens.timeZoneId;
// `to` is the place useLaneDrag already snapped. Snapping it again would hide a grid change.
if (drop.kind === 'move') {
const days = drop.from.day.until(drop.to.day, { largestUnit: 'day' }).days;
return shiftEvent(event, drop.to.start - drop.from.start, days, zone);
}
if (drop.kind === 'resize-end') {
const end = fromUnits(segment, drop.to.end, 'end');
if (!end || Temporal.ZonedDateTime.compare(end, event.start) <= 0) return null;
return { ...event, end };
}
const start = fromUnits(segment, drop.to.start, 'start');
if (!start || Temporal.ZonedDateTime.compare(event.end, start) <= 0) return null;
return { ...event, start };
}
/** A new event for a snapped place on `segment`. The title is empty until the dialog saves it. */
export function eventFromPlace(segment: DaySegment, start: number, end: number): CalendarEvent | null {
const startAt = fromUnits(segment, start, 'start');
const endAt = fromUnits(segment, end, 'end');
if (!startAt || !endAt || Temporal.ZonedDateTime.compare(endAt, startAt) <= 0) return null;
return { id: crypto.randomUUID(), title: '', start: startAt, end: endAt, allDay: false };
}
export type EventDraft = {
title: string;
date: Temporal.PlainDate | null;
endDate: Temporal.PlainDate | null;
start: Temporal.PlainTime | null;
end: Temporal.PlainTime | null;
allDay: boolean;
};
export type DraftIssue = { field: 'title' | 'date' | 'start' | 'end' | 'endDate'; message: string };
/** Keeps the draft's length when the date or the start moves, and leaves every other field as it was set. */
export function shiftDraft<K extends keyof EventDraft>(draft: EventDraft, key: K, value: EventDraft[K], timeZone: string): EventDraft {
if (key === 'date' && value instanceof Temporal.PlainDate && draft.date && draft.endDate) {
const days = draft.date.until(value, { largestUnit: 'day' }).days;
return { ...draft, date: value, endDate: draft.endDate.add({ days }) };
}
if (key === 'start' && value instanceof Temporal.PlainTime && draft.start && draft.end && draft.date && draft.endDate) {
const from = draft.date.toZonedDateTime({ timeZone, plainTime: draft.start });
const to = draft.endDate.toZonedDateTime({ timeZone, plainTime: draft.end });
const next = to.add(from.until(draft.date.toZonedDateTime({ timeZone, plainTime: value })));
return { ...draft, start: value, endDate: next.withTimeZone(timeZone).toPlainDate(), end: next.withTimeZone(timeZone).toPlainTime() };
}
return { ...draft, [key]: value };
}
/** The event a dialog's fields name, or the first field that is wrong. */
export function eventFromDraft(draft: EventDraft, timeZone: string): { ok: true; value: Pick<CalendarEvent, 'title' | 'start' | 'end' | 'allDay'> } | { ok: false; issue: DraftIssue } {
const title = draft.title.trim();
if (!title) return { ok: false, issue: { field: 'title', message: 'Add a title.' } };
if (!draft.date) return { ok: false, issue: { field: 'date', message: 'Choose a date.' } };
if (draft.allDay) {
if (!draft.endDate) return { ok: false, issue: { field: 'endDate', message: 'Choose an end.' } };
if (Temporal.PlainDate.compare(draft.endDate, draft.date) < 0) return { ok: false, issue: { field: 'endDate', message: 'End must be after the start.' } };
const start = draft.date.toZonedDateTime({ timeZone, plainTime: MIDNIGHT });
const end = draft.endDate.add({ days: 1 }).toZonedDateTime({ timeZone, plainTime: MIDNIGHT });
return { ok: true, value: { title, start, end, allDay: true } };
}
if (!draft.start) return { ok: false, issue: { field: 'start', message: 'Choose a start.' } };
if (!draft.end || !draft.endDate) return { ok: false, issue: { field: 'end', message: 'Choose an end.' } };
const start = draft.date.toZonedDateTime({ timeZone, plainTime: draft.start });
const end = draft.endDate.toZonedDateTime({ timeZone, plainTime: draft.end });
if (Temporal.ZonedDateTime.compare(end, start) <= 0) return { ok: false, issue: { field: 'end', message: 'End must be after the start.' } };
return { ok: true, value: { title, start, end, allDay: false } };
}
export function findConflicts(event: CalendarEvent, others: readonly CalendarEvent[]): { id: string }[] {
return others.flatMap((other) =>
other.id !== event.id &&
Temporal.ZonedDateTime.compare(other.start, event.end) < 0 &&
Temporal.ZonedDateTime.compare(event.start, other.end) < 0
? [{ id: other.id }]
: [],
);
}
type WeekInfoLocale = Intl.Locale & {
getWeekInfo?: () => { firstDay: number };
weekInfo?: { firstDay: number };
};
function weekStartsOn(locale: string): number {
try {
const info = new Intl.Locale(locale) as WeekInfoLocale;
const day = (info.getWeekInfo?.() ?? info.weekInfo)?.firstDay;
if (day && day >= 1 && day <= 7) return day;
} catch {
// A locale Intl cannot read keeps Sunday, as Calendar does.
}
return 7;
}
export function weekOf(day: Temporal.PlainDate, locale: string): Temporal.PlainDate[] {
const start = day.subtract({ days: (day.dayOfWeek - weekStartsOn(locale) + 7) % 7 });
return Array.from({ length: 7 }, (_, index) => start.add({ days: index }));
}
const MIDNIGHT = Temporal.PlainTime.from('00:00');
export function eventsOnDay(day: Temporal.PlainDate, events: readonly CalendarEvent[], timeZone: string, locale: string): CalendarEvent[] {
const opens = zoned(day, MIDNIGHT, timeZone);
const closes = zoned(day.add({ days: 1 }), MIDNIGHT, timeZone);
return events
.filter((event) => {
if (event.allDay) {
const covered = coveredDays(event, timeZone);
return covered !== null && Temporal.PlainDate.compare(day, covered.first) >= 0 && Temporal.PlainDate.compare(day, covered.last) <= 0;
}
const start = event.start.withTimeZone(timeZone);
const end = event.end.withTimeZone(timeZone);
return Temporal.ZonedDateTime.compare(start, closes) < 0 && Temporal.ZonedDateTime.compare(end, opens) > 0;
})
.sort((a, b) => {
if (a.allDay !== b.allDay) return a.allDay ? -1 : 1;
const key = (event: CalendarEvent) =>
event.allDay ? (coveredDays(event, timeZone)?.first ?? event.start.toPlainDate()).toZonedDateTime({ timeZone, plainTime: MIDNIGHT }) : event.start.withTimeZone(timeZone);
const byStart = Temporal.ZonedDateTime.compare(key(a), key(b));
if (byStart !== 0) return byStart;
return a.title.localeCompare(b.title, locale);
});
}
/** The calendar days an event occupies. An end at midnight stays on the earlier day. An all-day event uses the dates in its own zone. */
function coveredDays(event: CalendarEvent, timeZone: string): { first: Temporal.PlainDate; last: Temporal.PlainDate } | null {
const start = event.allDay ? event.start : event.start.withTimeZone(timeZone);
const end = event.allDay ? event.end : event.end.withTimeZone(timeZone);
if (Temporal.ZonedDateTime.compare(end, start) <= 0) return null;
const first = start.toPlainDate();
const last = end.toPlainTime().equals(MIDNIGHT) ? end.toPlainDate().subtract({ days: 1 }) : end.toPlainDate();
if (Temporal.PlainDate.compare(last, first) < 0) return null;
return { first, last };
}
function coversSeveralDays(event: CalendarEvent, timeZone: string): boolean {
const covered = coveredDays(event, timeZone);
return covered !== null && Temporal.PlainDate.compare(covered.last, covered.first) > 0;
}
export type WeekBar = {
event: CalendarEvent;
column: number;
span: number;
row: number;
continuesFrom: boolean;
continuesTo: boolean;
};
/**
* Places each all-day event, and each event that covers more than one calendar
* day, on a row of this week. Earlier starts take an earlier row. When two
* start together, the longer span in this week takes the earlier row. A bar
* that runs past either end of the week is split here and flagged.
*/
export function packWeekBars(events: readonly CalendarEvent[], days: readonly Temporal.PlainDate[], timeZone: string): WeekBar[] {
if (days.length !== 7) throw new RangeError('a week has 7 days');
const weekStart = days[0];
const weekEnd = days[6];
if (!weekStart || !weekEnd) throw new RangeError('a week has 7 days');
const segments = events.flatMap((event, index) => {
if (!event.allDay && !coversSeveralDays(event, timeZone)) return [];
const covered = coveredDays(event, timeZone);
if (!covered) return [];
if (Temporal.PlainDate.compare(covered.last, weekStart) < 0 || Temporal.PlainDate.compare(covered.first, weekEnd) > 0) return [];
const first = Temporal.PlainDate.compare(covered.first, weekStart) < 0 ? weekStart : covered.first;
const last = Temporal.PlainDate.compare(covered.last, weekEnd) > 0 ? weekEnd : covered.last;
const column = days.findIndex((day) => day.equals(first));
const endColumn = days.findIndex((day) => day.equals(last));
if (column < 0 || endColumn < column) return [];
return [
{
event,
column,
span: endColumn - column + 1,
continuesFrom: Temporal.PlainDate.compare(covered.first, weekStart) < 0,
continuesTo: Temporal.PlainDate.compare(covered.last, weekEnd) > 0,
index,
start: event.allDay ? covered.first.toZonedDateTime({ timeZone, plainTime: MIDNIGHT }) : event.start.withTimeZone(timeZone),
},
];
});
segments.sort((a, b) => Temporal.ZonedDateTime.compare(a.start, b.start) || b.span - a.span || a.index - b.index);
const rowEnds: number[] = [];
const placed = segments.map((segment) => {
const end = segment.column + segment.span;
let row = rowEnds.findIndex((used) => used <= segment.column);
if (row < 0) {
row = rowEnds.length;
rowEnds.push(end);
} else {
rowEnds[row] = end;
}
return { ...segment, row };
});
placed.sort((a, b) => a.index - b.index);
return placed.map(({ event, column, span, row, continuesFrom, continuesTo }) => ({ event, column, span, row, continuesFrom, continuesTo }));
}
export function laneScale(axis: DayAxis): number {
return axis.segments.reduce((longest, segment) => Math.max(longest, segment.length), 0);
}
export type VisibleSpan = {
event: CalendarEvent;
day: Temporal.PlainDate;
start: number;
end: number;
};
/**
* All-day events, and multi-day timed events the open hours never draw.
* A 10 PM to 2 AM event misses 8:00–18:00, so the strip carries it. A timed
* event the lanes can draw stays there, split across the days it meets.
*/
export function showsInStrip(event: CalendarEvent, timeZone: string, opens: Temporal.PlainTime, closes: Temporal.PlainTime): boolean {
if (event.allDay) return true;
const covered = coveredDays(event, timeZone);
if (!covered || Temporal.PlainDate.compare(covered.last, covered.first) <= 0) return false;
const days: Temporal.PlainDate[] = [];
for (let day = covered.first; Temporal.PlainDate.compare(day, covered.last) <= 0; day = day.add({ days: 1 })) days.push(day);
return visibleSpans(daySegments({ days, opens, closes, timeZone }), [event]).length === 0;
}
export function visibleSpans(axis: DayAxis, events: readonly CalendarEvent[]): VisibleSpan[] {
const spans: VisibleSpan[] = [];
for (const segment of axis.segments) {
if (segment.length === 0) continue;
for (const event of events) {
if (event.allDay) continue;
if (Temporal.ZonedDateTime.compare(event.start, segment.closes) >= 0) continue;
if (Temporal.ZonedDateTime.compare(event.end, segment.opens) <= 0) continue;
const clippedStart = Temporal.ZonedDateTime.compare(event.start, segment.opens) < 0 ? segment.opens : event.start;
const clippedEnd = Temporal.ZonedDateTime.compare(event.end, segment.closes) > 0 ? segment.closes : event.end;
const start = toUnits(axis, clippedStart, 'start');
const end = toUnits(axis, clippedEnd, 'end');
if (start === null || end === null || !start.day.equals(segment.day) || !end.day.equals(segment.day) || end.units <= start.units) continue;
spans.push({ event, day: segment.day, start: start.units, end: end.units });
}
}
return spans;
}
export function spanFrame(span: Pick<VisibleSpan, 'start' | 'end'>, scale: number): { top: number; height: number } {
if (scale <= 0) throw new RangeError('scale must be greater than 0');
return { top: span.start / scale, height: (span.end - span.start) / scale };
}
export type OverlapPlacement<T extends { start: number; end: number }> = {
item: T;
left: number;
width: number;
columns: number;
};
function rangesOverlap(a: { start: number; end: number }, b: { start: number; end: number }): boolean {
return a.start < b.end && b.start < a.end;
}
// A span with no length comes before a longer one that starts with it. Then the longer span takes the left column. Touching at an endpoint is not an overlap.
export function placeOverlaps<T extends { start: number; end: number }>(items: readonly T[]): OverlapPlacement<T>[] {
const order = items.map((item, index) => ({ item, index }));
order.sort(
(a, b) =>
a.item.start - b.item.start ||
(a.item.end === a.item.start ? 0 : 1) - (b.item.end === b.item.start ? 0 : 1) ||
b.item.end - a.item.end ||
a.index - b.index,
);
const placed = new Map<number, { left: number; width: number; columns: number }>();
let cluster: { index: number; column: number; item: T }[] = [];
let columnEnds: number[] = [];
const flush = () => {
const columns = columnEnds.length;
for (const entry of cluster) {
let span = 1;
for (let column = entry.column + 1; column < columns; column += 1) {
const taken = cluster.some((other) => other.column === column && rangesOverlap(entry.item, other.item));
if (taken) break;
span += 1;
}
placed.set(entry.index, { left: entry.column / columns, width: span / columns, columns });
}
cluster = [];
columnEnds = [];
};
for (const entry of order) {
const clusterEnd = columnEnds.reduce((end, value) => Math.max(end, value), Number.NEGATIVE_INFINITY);
if (cluster.length > 0 && entry.item.start >= clusterEnd) flush();
let column = columnEnds.findIndex((end) => end <= entry.item.start);
if (column < 0) {
column = columnEnds.length;
columnEnds.push(entry.item.end);
} else {
columnEnds[column] = entry.item.end;
}
cluster.push({ index: entry.index, column, item: entry.item });
}
if (cluster.length > 0) flush();
return items.map((item, index) => {
const box = placed.get(index);
if (!box) throw new Error('placeOverlaps left a span unpacked');
return { item, left: box.left, width: box.width, columns: box.columns };
});
}
export type HourMark = { units: number; time: Temporal.ZonedDateTime };
export function hourMarks(segment: DaySegment): HourMark[] {
const marks: HourMark[] = [];
for (let units = 0; units < segment.length; units += 60) {
const time = fromUnits(segment, units, 'start');
if (time) marks.push({ units, time });
}
return marks;
}
type ZoneFace = { name: string; offset: string };
function zoneFace(timeZone: string, locale: string, at: Temporal.ZonedDateTime): ZoneFace {
const read = (timeZoneName: 'short' | 'shortOffset') =>
new Intl.DateTimeFormat(locale, { hour: 'numeric', timeZone, timeZoneName })
.formatToParts(at.epochMilliseconds)
.find((part) => part.type === 'timeZoneName')
?.value?.trim() || timeZone;
return { name: read('short'), offset: read('shortOffset') };
}
/** The short zone name `locale` writes at `at`, such as PST in English or GMT-8 in Korean. */
export function zoneLabel(timeZone: string, locale: string, at: Temporal.ZonedDateTime): string {
return zoneFace(timeZone, locale, at).name;
}
function zoneFaces(timeZone: string, locale: string, times: readonly Temporal.ZonedDateTime[]): ZoneFace[] {
const faces: ZoneFace[] = [];
for (const at of times) {
const face = zoneFace(timeZone, locale, at);
if (!faces.some((item) => item.name === face.name && item.offset === face.offset)) faces.push(face);
}
return faces;
}
function zoneSamples(segments: readonly DaySegment[]): Temporal.ZonedDateTime[] {
const times = segments.flatMap((segment) => hourMarks(segment).map((mark) => mark.time));
return times.length > 0 ? times : segments.map((segment) => segment.opens);
}
function zoneNames(faces: readonly ZoneFace[]): string {
return faces.map((face) => face.name).join(' / ');
}
function ZoneLabel({ name }: { name: string }) {
const parts = name.split(' / ');
return parts.map((part, index) => (
<span key={`${part}-${index}`}>
<span className={styles.zoneName}>
{part}
{index < parts.length - 1 ? <span className={styles.markSep}> /</span> : null}
</span>
{index < parts.length - 1 ? ' ' : null}
</span>
));
}
/** "PST / PDT (GMT-8 / GMT-7)", or just the offset when the short name already is one. */
function zonePhrase(faces: readonly ZoneFace[]): string {
const names = zoneNames(faces);
if (faces.every((face) => face.name === face.offset)) return names;
return `${names} (${faces.map((face) => face.offset).join(' / ')})`;
}
function durationText(minutes: number, locale: string): string {
const hours = Math.floor(minutes / 60);
const rest = minutes % 60;
if (locale.toLowerCase().startsWith('ko')) {
if (hours > 0 && rest > 0) return `${hours}시간 ${rest}분`;
return hours > 0 ? `${hours}시간` : `${rest}분`;
}
const parts = [];
if (hours > 0) parts.push(`${hours} h`);
if (rest > 0) parts.push(`${rest} min`);
return parts.join(' ');
}
/**
* A sentence for the grid's description. The second column's lead is included only when
* every visible hour has that same lead, so a midweek change is not described as one number.
*/
export function zoneDescription(timeZone: string, compareZone: string | null, locale: string, segments: readonly DaySegment[]): string {
const samples = zoneSamples(segments);
const ko = locale.toLowerCase().startsWith('ko');
const primary = zonePhrase(zoneFaces(timeZone, locale, samples));
const times = ko ? `시각은 ${primary}입니다.` : `Times in ${primary}.`;
if (!compareZone) return times;
const gaps = samples.map((at) => Math.round((at.withTimeZone(compareZone).offsetNanoseconds - at.offsetNanoseconds) / 60_000_000_000));
const gap = gaps.length > 0 && gaps.every((value) => value === gaps[0]) ? gaps[0]! : null;
const compare = zonePhrase(zoneFaces(compareZone, locale, samples));
if (gap === null || gap === 0) return ko ? `${times} 둘째 열은 ${compare}입니다.` : `${times} Second column ${compare}.`;
const duration = durationText(Math.abs(gap), locale);
if (ko) return `${times} 둘째 열은 ${compare}이며, ${duration} ${gap > 0 ? '빠릅니다' : '느립니다'}.`;
return `${times} Second column ${compare}, ${duration} ${gap > 0 ? 'ahead' : 'behind'}.`;
}
/** A signed day count in `locale`, such as "+1 day" or "+1일". Nothing when the dates match. */
export function dayShiftLabel(days: number, locale: string): string | null {
if (days === 0) return null;
const count = new Intl.NumberFormat(locale, { signDisplay: 'always', maximumFractionDigits: 0 }).format(days);
const unit = new Intl.DisplayNames(locale, { type: 'dateTimeField', style: 'narrow' }).of('day') ?? 'day';
const space = /\p{Script=Hangul}/u.test(unit) ? '' : ' ';
return `${count}${space}${unit}`;
}
export type CompareTime = { time: Temporal.ZonedDateTime; dayShift: number };
export type CompareMark = { units: number; times: CompareTime[] };
function offsetOrder(segments: readonly DaySegment[], zone: string): Map<string, number> {
const order = new Map<string, number>();
for (const segment of segments) {
for (const mark of hourMarks(segment)) {
const key = String(mark.time.withTimeZone(zone).offsetNanoseconds);
if (!order.has(key)) order.set(key, order.size);
}
}
return order;
}
// The time stays in this column. The day column's hour is the grid zone, so a second time there would have no zone name beside it.
export function compareMarks(segments: readonly DaySegment[], units: readonly number[], compareZone: string): CompareMark[] {
const compareRank = offsetOrder(segments, compareZone);
const gridRank = offsetOrder(segments, segments[0]?.opens.timeZoneId ?? compareZone);
return units.flatMap((at) => {
const rows = segments.flatMap((segment) => {
const time = fromUnits(segment, at, 'start');
if (!time) return [];
const there = time.withTimeZone(compareZone);
return [{
time: there,
dayShift: time.toPlainDate().until(there.toPlainDate(), { largestUnit: 'day' }).days,
compareKey: String(there.offsetNanoseconds),
gridKey: String(time.offsetNanoseconds),
}];
});
if (rows.length !== segments.length || rows.length === 0) return [];
const seen = new Map<string, (typeof rows)[number]>();
for (const row of rows) {
const id = `${row.time.toPlainTime().toString()}/${row.dayShift}`;
if (!seen.has(id)) seen.set(id, row);
}
const times = [...seen.values()]
.sort((a, b) => {
const byCompare = (compareRank.get(a.compareKey) ?? 0) - (compareRank.get(b.compareKey) ?? 0);
return byCompare !== 0 ? byCompare : (gridRank.get(a.gridKey) ?? 0) - (gridRank.get(b.gridKey) ?? 0);
})
.map(({ time, dayShift }) => ({ time, dayShift }));
return [{ units: at, times }];
});
}
type ClockColumn = 'grid' | 'second';
type ClockStart = { column: ClockColumn; name: string };
function clockChanges(segments: readonly DaySegment[], zones: readonly { id: string; column: ClockColumn }[], locale: string): Map<string, ClockStart[]> {
const changes = new Map<string, ClockStart[]>();
for (const zone of zones) {
let previous: string | null = null;
for (const segment of segments) {
const names = zoneFaces(zone.id, locale, hourMarks(segment).map((mark) => mark.time)).map((face) => face.name);
const started = names.length > 1 ? names.slice(1) : previous && names[0] && names[0] !== previous ? [names[0]] : [];
if (started.length > 0) {
const key = segment.day.toString();
const prior = changes.get(key) ?? [];
const next = started
.filter((name) => !prior.some((item) => item.column === zone.column && item.name === name))
.map((name) => ({ column: zone.column, name }));
changes.set(key, [...prior, ...next]);
}
previous = names[names.length - 1] ?? previous;
}
}
return changes;
}
function changePhrase(starts: readonly ClockStart[], locale: string): string {
const ko = locale.toLowerCase().startsWith('ko');
const columns: ClockColumn[] = [];
for (const start of starts) if (!columns.includes(start.column)) columns.push(start.column);
return columns
.map((column) => {
const names = starts
.filter((start) => start.column === column)
.map((start) => start.name)
.join(', ');
if (ko) return column === 'second' ? `둘째 열 ${names} 시작` : `${names} 시작`;
return column === 'second' ? `Second column ${names} begins` : `${names} begins`;
})
.join(' · ');
}
function ComparePieces({ units, times, locale }: { units: number; times: readonly CompareTime[]; locale: string }) {
const pieces = markPieces(times, locale);
return pieces.map((piece, index) => (
<span key={`${units}-${index}`}>
<span className={styles.markPiece}>
{piece}
{index < pieces.length - 1 ? <span className={styles.markSep}> /</span> : null}
</span>
{index < pieces.length - 1 ? ' ' : null}
</span>
));
}
function markPieces(times: readonly CompareTime[], locale: string): string[] {
return times.map((part) => {
const time = formatTime(part.time.toPlainTime(), { locale });
const shift = dayShiftLabel(part.dayShift, locale);
return shift ? `${time} ${shift}` : time;
});
}
// Pinned so the page shows the same week whenever it opens.
const OPENS = Temporal.PlainTime.from('08:00');
const CLOSES = Temporal.PlainTime.from('18:00');
const ANCHOR = Temporal.PlainDate.from('2026-11-16');
const SAMPLE = [
['review', 'Design review', '2026-11-16', '09:00', '2026-11-16', '10:30', false],
['standup', 'Standup', '2026-11-16', '09:00', '2026-11-16', '09:30', false],
['handoff', 'Handoff', '2026-11-16', '17:00', '2026-11-17', '09:30', false],
['interview', 'Interview', '2026-11-17', '10:00', '2026-11-17', '11:00', false],
['early', 'Early sync', '2026-11-18', '07:00', '2026-11-18', '09:00', false],
['workshop', 'Workshop', '2026-11-18', '14:00', '2026-11-18', '16:30', false],
['planning', 'Planning', '2026-11-19', '10:00', '2026-11-19', '12:00', false],
['one', '1:1', '2026-11-19', '11:00', '2026-11-19', '11:30', false],
['crit', 'Crit', '2026-11-19', '11:00', '2026-11-19', '12:30', false],
['ship', 'Ship review', '2026-11-19', '15:00', '2026-11-19', '18:30', false],
['hiring', 'Hiring loop', '2026-11-19', '15:30', '2026-11-19', '16:30', false],
['sync', 'Staff sync', '2026-11-19', '16:00', '2026-11-19', '17:00', false],
['demo', 'Demo', '2026-11-20', '13:00', '2026-11-20', '14:00', false],
['offsite', 'Offsite', '2026-11-16', '00:00', '2026-11-17', '00:00', true],
['hours', 'Office hours', '2026-11-21', '11:00', '2026-11-21', '12:00', false],
['retreat', 'Retreat', '2026-11-20', '00:00', '2026-11-23', '00:00', true],
['launch', 'Launch watch', '2026-11-18', '22:00', '2026-11-19', '02:00', false],
] as const;
function sampleEvents(timeZone: string): CalendarEvent[] {
return SAMPLE.map(([id, title, startDay, start, endDay, end, allDay]) => ({
id,
title,
allDay,
start: Temporal.PlainDateTime.from(`${startDay}T${start}`).toZonedDateTime(timeZone),
end: Temporal.PlainDateTime.from(`${endDay}T${end}`).toZonedDateTime(timeZone),
}));
}
function bookClub(timeZone: string): EventSeries {
return {
id: seriesId('book-club'),
title: 'Book club',
timeZone,
start: Temporal.PlainDateTime.from('2026-11-15T15:00').toZonedDateTime(timeZone),
end: Temporal.PlainDateTime.from('2026-11-15T16:00').toZonedDateTime(timeZone),
allDay: false,
rule: { freq: 'WEEKLY', interval: 1, byDay: ['SU'], end: { kind: 'never' } },
exdates: [],
overrides: [],
};
}
function initialSeries(source: readonly CalendarEventInput[], timeZone: string, includeClub: boolean): EventSeries[] {
const list = source.map((event) => {
const projected = projectEvent(event, timeZone);
return seriesFromDraft(seriesId(projected.seriesId ?? projected.id), {
title: projected.title,
start: projected.start,
end: projected.end,
allDay: projected.allDay,
rule: null,
});
});
if (includeClub) list.push(bookClub(timeZone));
return list;
}
type CalendarView = 'month' | 'week' | 'day';
function visibleBounds(view: CalendarView, day: Temporal.PlainDate, locale: string, timeZone: string): ExpandWindow {
if (view === 'month') {
const first = day.with({ day: 1 });
const last = first.add({ months: 1 }).subtract({ days: 1 });
const leading = weekOf(first, locale)[0];
const trailing = weekOf(last, locale)[6];
if (!leading || !trailing) throw new RangeError('a week has 7 days');
return { start: zoned(leading, MIDNIGHT, timeZone), end: zoned(trailing.add({ days: 1 }), MIDNIGHT, timeZone) };
}
const shown = view === 'week' ? weekOf(day, locale) : [day];
const first = shown[0];
const last = shown[shown.length - 1];
if (!first || !last) throw new RangeError('a view has a day');
return { start: zoned(first, MIDNIGHT, timeZone), end: zoned(last.add({ days: 1 }), MIDNIGHT, timeZone) };
}
function occurrenceEvent(occurrence: {
id: string;
seriesId: string;
title: string;
start: Temporal.ZonedDateTime;
end: Temporal.ZonedDateTime;
allDay: boolean;
repeats: boolean;
originalStart: Temporal.ZonedDateTime;
}): CalendarEvent {
return occurrence;
}
function freshSeriesId(): SeriesId {
const values = crypto.getRandomValues(new Uint32Array(2));
return seriesId(`event-${(values[0] ?? 0).toString(36)}-${(values[1] ?? 0).toString(36)}`);
}
function firstOf(item: EventSeries): CalendarEvent | null {
const occurrence = expandSeries(item, { start: item.start.subtract({ days: 1 }), end: item.end.add({ days: 1 }) })[0];
return occurrence ? occurrenceEvent(occurrence) : null;
}
type CreatePlace = { day: Temporal.PlainDate; start: number; end: number };
type EditorSession = { intent: 'edit'; event: CalendarEvent } | { intent: 'create'; event: CalendarEvent; place: CreatePlace };
const MONTH_TITLES = 2;
function SyncTabStop({ onDay }: { onDay: (day: Temporal.PlainDate) => void }) {
const ref = useRef<HTMLSpanElement>(null);
useLayoutEffect(() => {
const iso = ref.current?.closest('table')?.querySelector('[data-todae-calendar-cell][tabindex="0"] [data-date]')?.getAttribute('data-date');
if (!iso) return;
onDay(Temporal.PlainDate.from(iso));
});
return <span ref={ref} hidden />;
}
function utcStamp(day: Temporal.PlainDate): number {
return Date.UTC(day.year, day.month - 1, day.day);
}
function dayHeading(day: Temporal.PlainDate, locale: string): string {
return new Intl.DateTimeFormat(locale, { weekday: 'short', month: 'short', day: 'numeric', timeZone: 'UTC' }).format(utcStamp(day));
}
function rangeLabel(days: readonly Temporal.PlainDate[], locale: string): string {
const first = days[0];
const last = days[days.length - 1];
if (!first || !last) return '';
if (first.equals(last)) {
return new Intl.DateTimeFormat(locale, { weekday: 'long', month: 'long', day: 'numeric', year: 'numeric', timeZone: 'UTC' }).format(utcStamp(first));
}
const start = new Intl.DateTimeFormat(locale, { month: 'short', day: 'numeric', timeZone: 'UTC' }).format(utcStamp(first));
const end = new Intl.DateTimeFormat(locale, { month: 'short', day: 'numeric', year: 'numeric', timeZone: 'UTC' }).format(utcStamp(last));
return `${start} – ${end}`;
}
function sharedOffsets(segments: readonly DaySegment[]): Set<number> {
const units = new Set<number>();
for (const segment of segments) for (const mark of hourMarks(segment)) units.add(mark.units);
const shared = new Set<number>();
for (const at of units) {
const times = segments.map((segment) => fromUnits(segment, at, 'start')?.toPlainTime() ?? null);
const first = times[0];
if (first && times.every((time) => time !== null && Temporal.PlainTime.compare(time, first) === 0)) shared.add(at);
}
return shared;
}
function clockLabel(at: Temporal.ZonedDateTime, locale: string, withDay: boolean): string {
const time = formatTime(at.toPlainTime(), { locale });
return withDay ? `${dayHeading(at.toPlainDate(), locale)}, ${time}` : time;
}
// The day number in the locale's digits, as Calendar.Grid writes the number the mark covers.
const dayDigitFormats = new Map<string, Intl.NumberFormat>();
function dayDigits(locale: string): Intl.NumberFormat {
let format = dayDigitFormats.get(locale);
if (!format) {
format = new Intl.NumberFormat(locale, { useGrouping: false });
dayDigitFormats.set(locale, format);
}
return format;
}
function percent(fraction: number): string {
return `${Math.round(fraction * 1e6) / 1e4}%`;
}
function daySpans(spans: readonly VisibleSpan[], day: Temporal.PlainDate, locale: string): VisibleSpan[] {
return spans
.filter((span) => span.day.equals(day))
.sort((a, b) => a.start - b.start || b.end - a.end || a.event.title.localeCompare(b.event.title, locale));
}
function dayTrack(columns: number): string {
return `minmax(max(5.5rem, ${columns} * 4rem), 1fr)`;
}
function blockKey(id: string, day: Temporal.PlainDate): string {
return `${id}@${day.toString()}`;
}
function stripKey(id: string): string {
return `strip:${id}`;
}
/** A strip bar's grid lines. The gutters come first, and the end never passes the last day. */
function stripColumn(column: number, span: number, days: number, gutters: number): string {
const start = column + gutters + 1;
const end = Math.min(start + span, days + gutters + 1);
return `${start} / ${end}`;
}
function eventRangeLabel(event: CalendarEvent, locale: string, timeZone: string): string {
const covered = coveredDays(event, timeZone);
if (!covered) return '';
const format = new Intl.DateTimeFormat(locale, { month: 'long', day: 'numeric', year: 'numeric', timeZone: 'UTC' });
const stamp = (day: Temporal.PlainDate) => Date.UTC(day.year, day.month - 1, day.day);
const start = format.format(stamp(covered.first));
if (covered.first.equals(covered.last)) return start;
return `${start} – ${format.format(stamp(covered.last))}`;
}
function barLabel(event: CalendarEvent, locale: string, timeZone: string): string {
const range = eventRangeLabel(event, locale, timeZone);
// The bar shows the title, then "Repeats", so the name starts with both (WCAG 2.5.3, Label in Name).
const title = event.repeats ? `${event.title}, repeats` : event.title;
return event.allDay
? `${title}, ${range}`
: `${title}, ${range}, ${formatTime(event.start.withTimeZone(timeZone).toPlainTime(), { locale })} to ${formatTime(event.end.withTimeZone(timeZone).toPlainTime(), { locale })}`;
}
type DrawnBar = WeekBar & { cutStart: boolean; cutEnd: boolean };
function barsOnBoard(events: readonly CalendarEvent[], week: readonly Temporal.PlainDate[], timeZone: string, opens: Temporal.PlainTime, closes: Temporal.PlainTime, day: Temporal.PlainDate | null): DrawnBar[] {
const packed = packWeekBars(
events.filter((event) => showsInStrip(event, timeZone, opens, closes)),
week,
timeZone,
);
const ordered = [...packed].sort((a, b) => a.row - b.row || a.column - b.column);
if (!day) return ordered.map((bar) => ({ ...bar, cutStart: bar.continuesFrom, cutEnd: bar.continuesTo }));
const index = week.findIndex((item) => item.equals(day));
return ordered.flatMap((bar) => {
if (index < bar.column || index >= bar.column + bar.span) return [];
return [{ ...bar, column: 0, span: 1, cutStart: bar.continuesFrom || index > bar.column, cutEnd: bar.continuesTo || index < bar.column + bar.span - 1 }];
});
}
function parseBlockKey(key: string): { id: string; day: string } {
const at = key.lastIndexOf('@');
return { id: key.slice(0, at), day: key.slice(at + 1) };
}
type ColumnShared = {
locale: string;
timeZone: string;
days: number;
scale: number;
axis: DayAxis;
week: boolean;
lanes: UseLaneDragResult;
landing: RefObject<HTMLButtonElement | null>;
focusKey: string | null;
onAdjust: (event: CalendarEvent, segment: DaySegment, change: SlotChange) => void;
onEdit: (event: CalendarEvent, day: Temporal.PlainDate) => void;
onDelete: (event: CalendarEvent, day: Temporal.PlainDate) => void;
pendingCreate: { day: Temporal.PlainDate; start: number; end: number } | null;
gutters: number;
};
function eventName(event: CalendarEvent, segment: DaySegment, locale: string, timeZone: string): { name: string; timeText: string; short: boolean } {
const start = event.start.withTimeZone(timeZone);
const end = event.end.withTimeZone(timeZone);
const spansDays = !start.toPlainDate().equals(end.toPlainDate());
const startLabel = clockLabel(start, locale, spansDays);
const endLabel = clockLabel(end, locale, spansDays);
const startedEarlier = Temporal.ZonedDateTime.compare(start, segment.opens) < 0;
const continues = Temporal.ZonedDateTime.compare(end, segment.closes) > 0;
const cue = [startedEarlier ? 'started earlier' : null, continues ? 'continues' : null, event.repeats ? 'repeats' : null].filter(Boolean).join(', ');
const name = [`${event.title}, ${startLabel} to ${endLabel}`, cue].filter(Boolean).join(', ');
const timeText = `${startedEarlier ? '…' : ''}${startLabel} to ${endLabel}${continues ? '…' : ''}`;
const visible = clippedUnits(segment, event);
const short = visible !== null && visible.end - visible.start < 45;
return { name, timeText, short };
}
function EventMoves({ event, segment, axis, week, onAdjust, onEdit, onDelete }: Pick<ColumnShared, 'axis' | 'week' | 'onAdjust' | 'onEdit' | 'onDelete'> & { event: CalendarEvent; segment: DaySegment }) {
if (event.allDay) {
return (
<>
<Menu.Item onSelect={() => onAdjust(event, segment, { kind: 'day', days: -1 })}>Move to previous day</Menu.Item>
<Menu.Item onSelect={() => onAdjust(event, segment, { kind: 'day', days: 1 })}>Move to next day</Menu.Item>
<Menu.Separator />
<Menu.Item onSelect={() => onEdit(event, segment.day)}>Edit…</Menu.Item>
<Menu.Item onSelect={() => onDelete(event, segment.day)}>Delete</Menu.Item>
</>
);
}
const item = (change: SlotChange) => adjustEvent(event, segment, axis, change);
const go = (change: SlotChange) => {
const next = item(change);
if (next) onAdjust(event, segment, change);
};
return (
<>
<Menu.Item disabled={item({ kind: 'shift', slots: -1 }) === null} onSelect={() => go({ kind: 'shift', slots: -1 })}>
{`${SLOT_MINUTES} minutes earlier`}
</Menu.Item>
<Menu.Item disabled={item({ kind: 'shift', slots: 1 }) === null} onSelect={() => go({ kind: 'shift', slots: 1 })}>
{`${SLOT_MINUTES} minutes later`}
</Menu.Item>
{week ? (
<>
<Menu.Item disabled={item({ kind: 'day', days: -1 }) === null} onSelect={() => go({ kind: 'day', days: -1 })}>
Move to previous day
</Menu.Item>
<Menu.Item disabled={item({ kind: 'day', days: 1 }) === null} onSelect={() => go({ kind: 'day', days: 1 })}>
Move to next day
</Menu.Item>
</>
) : null}
<Menu.Separator />
<Menu.Item disabled={item({ kind: 'length', slots: 1 }) === null} onSelect={() => go({ kind: 'length', slots: 1 })}>
{`Lengthen ${SLOT_MINUTES} minutes`}
</Menu.Item>
<Menu.Item disabled={item({ kind: 'length', slots: -1 }) === null} onSelect={() => go({ kind: 'length', slots: -1 })}>
{`Shorten ${SLOT_MINUTES} minutes`}
</Menu.Item>
<Menu.Separator />
<Menu.Item onSelect={() => onEdit(event, segment.day)}>Edit…</Menu.Item>
<Menu.Item onSelect={() => onDelete(event, segment.day)}>Delete</Menu.Item>
</>
);
}
function EventBlock({ span, segment, shared }: { span: VisibleSpan & { left: number; width: number }; segment: DaySegment; shared: ColumnShared }) {
const handoff = useFocusHandoff({ to: shared.landing });
const key = blockKey(span.event.id, segment.day);
const place = { lane: segment.day.toString(), start: span.start, end: span.end };
const { name, timeText, short } = eventName(span.event, segment, shared.locale, shared.timeZone);
const box = spanFrame(span, shared.scale);
return (
<li
ref={handoff}
{...shared.lanes.getItemProps(key, place)}
className={styles.slot}
style={{ top: percent(box.top), height: percent(box.height), insetInlineStart: percent(span.left), inlineSize: percent(span.width) }}
>
<Menu.Root>
<Menu.Trigger
{...shared.lanes.getHandleProps(key, 'move')}
ref={key === shared.focusKey ? shared.landing : undefined}
variant="ghost"
className={styles.event}
aria-label={name}
tooltip={false}
>
<span className={styles.eventLine}>
{span.event.repeats ? (
<span className={styles.repeats} aria-hidden="true" title="Repeats">
↻
</span>
) : null}
<Text.Root as="span" weight="medium" lines={1}>
{span.event.title}
</Text.Root>
{/* The lines stack as blocks. The space keeps the title and the time two words apart in the text
the button shows, which its name starts with (WCAG 2.5.3, Label in Name). */}{' '}
</span>
{short ? null : (
<Text.Root as="span" variant="label" tone="muted" lines={1}>
{timeText}
</Text.Root>
)}
{span.event.repeats ? <VisuallyHidden.Root>Repeats</VisuallyHidden.Root> : null}
</Menu.Trigger>
<Menu.Content>
<EventMoves event={span.event} segment={segment} axis={shared.axis} week={shared.week} onAdjust={shared.onAdjust} onEdit={shared.onEdit} onDelete={shared.onDelete} />
</Menu.Content>
</Menu.Root>
<span {...shared.lanes.getHandleProps(key, 'start')} aria-hidden className={styles.edge} data-edge="start" />
<span {...shared.lanes.getHandleProps(key, 'end')} aria-hidden className={styles.edge} data-edge="end" />
</li>
);
}
const DragPreview = memo(function DragPreview({
drag,
segment,
scale,
locale,
pending,
}: {
drag: UseLaneDragResult['drag'];
segment: DaySegment;
scale: number;
locale: string;
pending: { start: number; end: number } | null;
}) {
const change = useLaneDragState(drag, (state) => (state?.to?.lane === segment.day.toString() ? state : null));
const live = change?.to ?? null;
const place = live ?? pending;
if (place == null || scale <= 0) return null;
const creating = change?.kind === 'create' || (live == null && pending != null);
const box = spanFrame({ start: place.start, end: place.end }, scale);
const start = fromUnits(segment, place.start, 'start');
const end = fromUnits(segment, place.end, 'end');
const label = start && end ? `${formatTime(start.toPlainTime(), { locale })} to ${formatTime(end.toPlainTime(), { locale })}` : null;
return (
<div
aria-hidden="true"
className={styles.preview}
data-create={creating ? '' : undefined}
data-level={change?.verdict?.level}
style={{ top: percent(box.top), height: percent(box.height) }}
>
{label ? <span className={styles.previewTime}>{label}</span> : null}
</div>
);
});
function StripBar({ bar, segment, shared }: { bar: DrawnBar; segment: DaySegment; shared: ColumnShared }) {
const handoff = useFocusHandoff({ to: shared.landing });
const key = stripKey(bar.event.id);
return (
<div ref={handoff} className={styles.stripSlot} style={{ gridColumn: stripColumn(bar.column, bar.span, shared.days, shared.gutters), gridRow: bar.row + 1 }}>
<Menu.Root>
<Menu.Trigger
ref={key === shared.focusKey ? shared.landing : undefined}
variant="ghost"
className={styles.stripBar}
aria-label={barLabel(bar.event, shared.locale, shared.timeZone)}
data-bar={bar.event.id}
data-cut-start={bar.cutStart ? '' : undefined}
data-cut-end={bar.cutEnd ? '' : undefined}
tooltip={false}
>
{bar.cutStart ? (
<span aria-hidden="true" className={styles.cue}>
…
</span>
) : null}
<span className={styles.eventLine}>
<Text.Root as="span" weight="medium" lines={1}>
{bar.event.title}
</Text.Root>
{bar.event.repeats ? (
<>
{' '}
<span className={styles.repeats}>Repeats</span>
</>
) : null}
</span>
{bar.cutEnd ? (
<span aria-hidden="true" className={styles.cueEnd}>
…
</span>
) : null}
</Menu.Trigger>
<Menu.Content>
<EventMoves event={bar.event} segment={segment} axis={shared.axis} week={shared.week} onAdjust={shared.onAdjust} onEdit={shared.onEdit} onDelete={shared.onDelete} />
</Menu.Content>
</Menu.Root>
</div>
);
}
function EventColumn({
segment,
placed,
sharedMarks,
shared,
headingId,
}: {
segment: DaySegment;
placed: ReturnType<typeof placeOverlaps<VisibleSpan>>;
sharedMarks: ReadonlySet<number>;
shared: ColumnShared;
headingId: string;
}) {
const marks = shared.scale > 0 ? hourMarks(segment) : [];
return (
<div {...shared.lanes.getLaneProps(segment.day.toString())} className={styles.column}>
<div className={styles.hours} aria-hidden="true">
{marks.map((mark) => {
const top = percent(mark.units / shared.scale);
const agreed = sharedMarks.has(mark.units);
return (
<div key={mark.units}>
<div className={styles.line} style={{ top }} />
{agreed ? null : (
<Text.Root as="span" variant="label" tone="muted" className={styles.columnMark} data-first={mark.units === 0 ? '' : undefined} style={{ top }}>
{formatTime(mark.time.toPlainTime(), { locale: shared.locale })}
</Text.Root>
)}
</div>
);
})}
</div>
<DragPreview
drag={shared.lanes.drag}
segment={segment}
scale={shared.scale}
locale={shared.locale}
pending={shared.pendingCreate?.day.equals(segment.day) ? shared.pendingCreate : null}
/>
<ol className={styles.events} aria-labelledby={headingId}>
{placed.map(({ item, left, width }) => (
<EventBlock key={blockKey(item.event.id, segment.day)} span={{ ...item, left, width }} segment={segment} shared={shared} />
))}
</ol>
</div>
);
}
function MonthDay({ day, events, timeZone, locale }: { day: Temporal.PlainDate; events: readonly CalendarEvent[]; timeZone: string; locale: string }) {
const list = eventsOnDay(day, events, timeZone, locale);
if (list.length === 0) return null;
const week = weekOf(day, locale);
const bars = packWeekBars(events, week, timeZone);
const index = week.findIndex((item) => item.equals(day));
const covering = bars.filter((bar) => index >= bar.column && index < bar.column + bar.span);
const visible = covering.filter((bar) => bar.row < MONTH_TITLES).sort((a, b) => a.row - b.row);
const hidden = covering.length - visible.length;
const barRows = visible.reduce((max, bar) => Math.max(max, bar.row + 1), 0);
const placed = new Set(bars.map((bar) => bar.event.id));
const chips = list.filter((event) => !placed.has(event.id));
const shown = chips.slice(0, Math.max(0, MONTH_TITLES - visible.length));
const extra = hidden + chips.length - shown.length;
let spoken = 0;
return (
<>
<span className={styles.detail} style={{ ['--bar-rows' as string]: barRows }}>
{barRows > 0 ? (
<span className={styles.barRows}>
{visible.map((bar) => {
const first = index === bar.column;
const last = index === bar.column + bar.span - 1;
const cutStart = bar.continuesFrom && first;
const cutEnd = bar.continuesTo && last;
const comma = spoken > 0;
spoken += 1;
return (
<span
key={bar.event.id}
className={styles.monthBar}
data-bar={bar.event.id}
data-cut-start={cutStart ? '' : undefined}
data-cut-end={cutEnd ? '' : undefined}
data-join-start={first ? undefined : ''}
data-join-end={last ? undefined : ''}
style={{ gridRow: bar.row + 1 }}
>
{comma ? <VisuallyHidden.Root>, </VisuallyHidden.Root> : null}
{cutStart ? (
<span aria-hidden="true" className={styles.cue}>
…
</span>
) : null}
{index === bar.column ? <span className={styles.barLabel}>{bar.event.title}</span> : <VisuallyHidden.Root>{bar.event.title}</VisuallyHidden.Root>}
{/* "repeats" follows the title, as the bar shows Repeats after it, and the range comes after. */}
{bar.event.repeats ? <VisuallyHidden.Root>, repeats</VisuallyHidden.Root> : null}
{bar.event.repeats && index === bar.column ? (
<span aria-hidden="true" className={styles.repeats}>
{' '}
Repeats
</span>
) : null}
<VisuallyHidden.Root>{`, ${eventRangeLabel(bar.event, locale, timeZone)}`}</VisuallyHidden.Root>
{cutEnd ? (
<span aria-hidden="true" className={styles.cueEnd}>
…
</span>
) : null}
</span>
);
})}
</span>
) : null}
{shown.map((event) => {
const comma = spoken > 0;
spoken += 1;
return (
<span key={event.id} className={styles.chip}>
{comma ? <VisuallyHidden.Root>, </VisuallyHidden.Root> : null}
{event.title}
{event.repeats ? <VisuallyHidden.Root>, repeats</VisuallyHidden.Root> : null}
{event.repeats ? (
<span aria-hidden="true" className={styles.repeats}>
{' '}
Repeats
</span>
) : null}
</span>
);
})}
{extra > 0 ? (
<span className={styles.more}>
<VisuallyHidden.Root>, </VisuallyHidden.Root>
{`+${extra} more`}
</span>
) : null}
</span>
<span className={styles.count} aria-hidden="true">
<span className={styles.dot} />
{list.length}
</span>
</>
);
}
export function EventCalendarExample({
locale = 'en-US',
timeZone = Temporal.Now.timeZoneId(),
compareZone,
opens = OPENS,
closes = CLOSES,
anchor: initialAnchor = ANCHOR,
events,
}: {
locale?: string;
/** IANA time zone the grid renders in. The viewer's zone when omitted. */
timeZone?: string;
/** A second IANA zone. Each primary hour then shows that zone's time. */
compareZone?: string;
opens?: Temporal.PlainTime;
closes?: Temporal.PlainTime;
anchor?: Temporal.PlainDate;
events?: readonly CalendarEventInput[];
}) {
const [view, setView] = useState<CalendarView>('week');
const [anchor, setAnchor] = useState(initialAnchor);
const rootRef = useRef<HTMLDivElement>(null);
const [primaryPx, setPrimaryPx] = useState(0);
const [comparePx, setComparePx] = useState(0);
const [narrow, setNarrow] = useState(false);
const [seriesList, setSeriesList] = useState<EventSeries[]>(() => initialSeries(events ?? sampleEvents(timeZone), timeZone, events === undefined));
const timedBefore = useRef(new Map<string, { start: Temporal.PlainTime; end: Temporal.PlainTime; daySpan: number }>());
const [session, setSession] = useState<EditorSession | null>(null);
const [deleting, setDeleting] = useState<CalendarEvent | null>(null);
const notedDay = useRef(initialAnchor);
const notedUnits = useRef(0);
const [focusKey, setFocusKey] = useState<string | null>(null);
const heading = useId();
const descriptionId = useId();
const pendingFocus = useRef<'day' | null>(null);
const landing = useRef<HTMLButtonElement | null>(null);
const returnFocus = useRef(false);
const focusHeading = useRef<Temporal.PlainDate | null>(null);
const [pinFocus, setPinFocus] = useState(true);
const announce = useAnnouncer();
const bounds = visibleBounds(view, anchor, locale, timeZone);
const items = seriesList.flatMap((item) => expandSeries(item, viewWindow(item, bounds)).map(occurrenceEvent));
const editingSeries = session?.intent === 'edit' ? (seriesList.find((item) => item.id === (session.event.seriesId ?? session.event.id)) ?? null) : null;
const days = view === 'week' ? weekOf(anchor, locale) : [anchor];
const week = weekOf(anchor, locale);
const axis = daySegments({ days, opens, closes, timeZone });
const scale = laneScale(axis);
const spans = visibleSpans(axis, items);
const lanes = axis.segments.map((segment) => {
const placed = placeOverlaps(daySpans(spans, segment.day, locale));
const columns = placed.reduce((count, place) => Math.max(count, place.columns), 1);
return { segment, placed, columns };
});
const dayTracks = lanes.map((lane) => dayTrack(lane.columns)).join(' ');
const showCompare = compareZone !== undefined && compareZone !== timeZone && !narrow;
const gutters = showCompare ? 2 : 1;
const primaryTrack = primaryPx > 0 ? `${primaryPx}px` : '4.5rem';
const compareTrack = comparePx > 0 ? `${comparePx}px` : '4.5rem';
const tracks = `${showCompare ? `${primaryTrack} ${compareTrack}` : primaryTrack} ${dayTracks}`;
const stripBars = view === 'month' ? [] : barsOnBoard(items, week, timeZone, opens, closes, view === 'day' ? anchor : null);
const gutterMin = (primaryPx > 0 ? primaryPx : 4.5 * 16) / 16 + (showCompare && comparePx > 0 ? comparePx / 16 : 0);
const minInline = `${lanes.reduce((sum, lane) => sum + Math.max(5.5, lane.columns * 4), gutterMin)}rem`;
const sharedMarks = sharedOffsets(axis.segments);
const gutterMarks = scale > 0 ? [...sharedMarks].sort((a, b) => a - b) : [];
const compared = showCompare && compareZone ? compareMarks(axis.segments, gutterMarks, compareZone) : [];
const samples = zoneSamples(axis.segments);
const primaryName = zoneNames(zoneFaces(timeZone, locale, samples));
const compareName = showCompare && compareZone ? zoneNames(zoneFaces(compareZone, locale, samples)) : null;
const pieceKey = [...compared.flatMap((item) => markPieces(item.times, locale)), primaryName, compareName ?? ''].join('\n');
const description = zoneDescription(timeZone, compareName && compareZone ? compareZone : null, locale, axis.segments);
const changes = clockChanges(axis.segments, showCompare && compareZone ? [{ id: timeZone, column: 'grid' }, { id: compareZone, column: 'second' }] : [{ id: timeZone, column: 'grid' }], locale);
const step = view === 'week' ? 7 : 1;
const move = view === 'week' ? 'week' : 'day';
const grid = [...new Set(axis.segments.flatMap((segment) => snapGrid(segment, SLOT_MINUTES)))].sort((a, b) => a - b);
const shownDay = (event: CalendarEvent, day: Temporal.PlainDate): Temporal.PlainDate | null => {
if (event.allDay) return null;
const around = view === 'day' ? [day.add({ days: -1 }), day, day.add({ days: 1 })] : weekOf(day, locale);
const next = daySegments({ days: around, opens, closes, timeZone });
return next.segments.find((segment) => clippedUnits(segment, event))?.day ?? null;
};
const commit = (next: CalendarEvent, day: Temporal.PlainDate) => {
const current = items.find((each) => each.id === next.id) ?? next;
const settled = finishGesture(current, next, 'drop');
if (settled.allDay && !current.allDay) {
const start = current.start.withTimeZone(timeZone);
const end = current.end.withTimeZone(timeZone);
timedBefore.current.set(settled.id, { start: start.toPlainTime(), end: end.toPlainTime(), daySpan: start.toPlainDate().until(end.toPlainDate(), { largestUnit: 'day' }).days });
}
if (!settled.allDay) timedBefore.current.delete(settled.id);
const strip = showsInStrip(settled, timeZone, opens, closes);
const laneHome = strip ? null : shownDay(settled, day);
setFocusKey(strip ? stripKey(settled.id) : laneHome ? blockKey(settled.id, laneHome) : null);
focusHeading.current = strip || laneHome ? null : day;
setSeriesList((list) => {
const id = settled.seriesId ?? settled.id;
if (!list.some((item) => item.id === id)) {
return [...list, seriesFromDraft(seriesId(settled.id), { title: settled.title, start: settled.start, end: settled.end, allDay: settled.allDay, rule: null })];
}
return list.map((item) => {
if (item.id !== id) return item;
if (settled.repeats && settled.originalStart) {
return overrideOccurrence(item, settled.originalStart, { title: settled.title, start: settled.start, end: settled.end, allDay: settled.allDay });
}
return replaceSeries(item, { title: settled.title, start: settled.start, end: settled.end, allDay: settled.allDay, rule: item.rule });
});
});
const start = settled.start.withTimeZone(timeZone);
const end = settled.end.withTimeZone(timeZone);
const spansDays = !start.toPlainDate().equals(end.toPlainDate());
const detail = strip ? barLabel(settled, locale, timeZone) : `${settled.title}, ${clockLabel(start, locale, spansDays)} to ${clockLabel(end, locale, spansDays)}`;
const created = !items.some((each) => each.id === settled.id);
announce(created ? `Added ${detail}` : detail);
returnFocus.current = true;
};
// The dialog unmounts in the same commit that records the edit, so its own
// focus return has no button to land on yet. This runs after that commit.
// The heading is consumed here. A value left from a cancelled create would
// steal focus from a later cancelled edit.
useLayoutEffect(() => {
if (!returnFocus.current || session || deleting) return;
returnFocus.current = false;
const day = focusHeading.current;
focusHeading.current = null;
if (day) {
document.getElementById(`${heading}-${day.toString()}`)?.focus();
return;
}
if (landing.current?.isConnected) landing.current.focus();
}, [anchor, deleting, session, focusKey, heading, seriesList]);
const beginCreate = (segment: DaySegment, start: number, end: number) => {
const event = eventFromPlace(segment, start, end);
if (!event) return;
returnFocus.current = false;
focusHeading.current = segment.day;
setFocusKey(null);
setSession({ intent: 'create', event, place: { day: segment.day, start, end } });
};
const board = useLaneDrag({
range: { start: 0, end: Math.max(scale, SLOT_MINUTES) },
axis: 'block',
grid,
minSpan: SLOT_MINUTES,
disabled: view === 'month' || scale === 0,
check: (_id, to) => {
const segment = axis.segments.find((each) => each.day.toString() === to.lane);
if (!segment || to.end > segment.length || to.end - to.start < SLOT_MINUTES) return { level: 'blocked', reason: 'Outside this day' };
return null;
},
onDrop: (change) => {
if (change.to === null || change.from === null) return;
if (change.kind !== 'move' && change.kind !== 'resize-start' && change.kind !== 'resize-end') return;
const { id } = parseBlockKey(change.id);
const event = items.find((each) => each.id === id);
const fromDay = axis.segments.find((each) => each.day.toString() === change.from?.lane)?.day;
const toDay = axis.segments.find((each) => each.day.toString() === change.to?.lane)?.day;
if (!event || !fromDay || !toDay) return;
const next = eventFromLane(event, { from: { day: fromDay, start: change.from.start, end: change.from.end }, to: { day: toDay, start: change.to.start, end: change.to.end }, kind: change.kind }, axis);
const settled = finishGesture(event, next, 'drop');
if (settled !== event) commit(settled, toDay);
},
onCreate: (place) => {
const segment = axis.segments.find((each) => each.day.toString() === place.lane);
if (!segment) return;
beginCreate(segment, place.start, place.end);
},
});
const onAdjust = (event: CalendarEvent, segment: DaySegment, change: SlotChange) => {
if (event.allDay && change.kind === 'day') {
const day = segment.day.add({ days: change.days });
const next = { ...event, start: event.start.add({ days: change.days }), end: event.end.add({ days: change.days }) };
// The bar leaves this day. Show the day it lands on before focus moves, the same way Save does.
setAnchor(day);
commit(next, day);
return;
}
const next = adjustEvent(event, segment, axis, change);
if (!next) return;
const day = change.kind === 'day' ? segment.day.add({ days: change.days }) : segment.day;
commit(next, day);
};
const onEdit = (event: CalendarEvent, day: Temporal.PlainDate) => {
setFocusKey(showsInStrip(event, timeZone, opens, closes) ? stripKey(event.id) : blockKey(event.id, day));
setSession({ intent: 'edit', event });
};
const onDelete = (event: CalendarEvent, day: Temporal.PlainDate) => {
setFocusKey(showsInStrip(event, timeZone, opens, closes) ? stripKey(event.id) : blockKey(event.id, day));
if (event.repeats) {
setDeleting(event);
return;
}
setSeriesList((list) => list.filter((item) => item.id !== (event.seriesId ?? event.id)));
focusHeading.current = day;
setFocusKey(null);
announce(`Deleted ${event.title}, ${dayHeading(day, locale)}.`);
returnFocus.current = true;
};
const finishDelete = (scope: EditScope) => {
const event = deleting;
if (!event?.originalStart) return;
const id = event.seriesId ?? event.id;
setSeriesList((list) =>
list.flatMap((item) => {
if (item.id !== id) return [item];
if (scope === 'series') return [];
if (scope === 'occurrence') {
const kept = excludeOccurrence(item, event.originalStart!);
return kept ? [kept] : [];
}
const kept = truncateBefore(item, event.originalStart!);
return kept ? [kept] : [];
}),
);
const which = scope === 'occurrence' ? 'this event' : scope === 'following' ? 'this and following events' : 'all events';
const when = dayHeading(event.start.withTimeZone(timeZone).toPlainDate(), locale);
announce(`Deleted ${which}. ${event.title}, ${when}.`);
focusHeading.current = event.start.withTimeZone(timeZone).toPlainDate();
setFocusKey(null);
returnFocus.current = true;
setDeleting(null);
};
const openNewEvent = () => {
const noted = axis.segments.find((segment) => segment.day.equals(notedDay.current));
const segment = noted ?? axis.segments.find((each) => each.day.equals(anchor)) ?? axis.segments[0];
if (!segment || segment.length < SLOT_MINUTES) return;
const start = noted && Number.isFinite(notedUnits.current) ? Math.min(Math.max(0, notedUnits.current), segment.length - SLOT_MINUTES) : 0;
beginCreate(segment, start, start + SLOT_MINUTES);
};
const noteFocus = (target: EventTarget | null) => {
if (!(target instanceof Element)) return;
const head = target.closest('h3');
const prefix = `${heading}-`;
if (head?.id.startsWith(prefix)) {
notedDay.current = Temporal.PlainDate.from(head.id.slice(prefix.length));
notedUnits.current = 0;
return;
}
const lane = target.closest('[data-todae-lane]');
const iso = lane?.getAttribute('data-todae-lane');
if (!iso) return;
notedDay.current = Temporal.PlainDate.from(iso);
const end = target.closest('[data-todae-lane-item]')?.getAttribute('data-todae-lane-end');
notedUnits.current = end ? Number(end) : 0;
};
const columnShared: ColumnShared = {
locale,
timeZone,
days: days.length,
scale,
axis,
week: view === 'week',
lanes: board,
landing,
focusKey,
onAdjust,
onEdit,
onDelete,
pendingCreate: session?.intent === 'create' ? session.place : null,
gutters,
};
useLayoutEffect(() => {
const el = rootRef.current;
if (!el || typeof ResizeObserver === 'undefined') return;
const read = () => {
const width = el.getBoundingClientRect().width;
setNarrow(width > 0 && Math.round(width) <= 375);
};
const watcher = new ResizeObserver(read);
watcher.observe(el);
read();
return () => watcher.disconnect();
}, []);
useLayoutEffect(() => {
setPinFocus(view !== 'month');
}, [view]);
// The times are absolutely positioned, so they don't size the column.
useLayoutEffect(() => {
const root = rootRef.current;
if (!root) return;
const length = (value: string) => {
const parsed = Number.parseFloat(value);
return Number.isFinite(parsed) ? parsed : 0;
};
const zonePad = (node: HTMLElement) => {
const zone = node.closest(`.${styles.zone}`);
if (!(zone instanceof HTMLElement)) return 0;
const style = getComputedStyle(zone);
return length(style.paddingInlineStart) + length(style.paddingInlineEnd);
};
const widest = (nodes: HTMLElement[], extra: (node: HTMLElement) => number) =>
nodes.reduce((max, node) => Math.max(max, node.getBoundingClientRect().width + extra(node)), 0);
const measure = () => {
const head = root.querySelector(`.${styles.head}`);
const primaryNames = [...(head?.firstElementChild?.querySelectorAll<HTMLElement>(`.${styles.zoneName}`) ?? [])];
const primaryMarks = [...root.querySelectorAll<HTMLElement>(`.${styles.gutter}:not([data-compare]) > .${styles.mark}`)];
const compareNames = [...(head?.firstElementChild?.nextElementSibling?.querySelectorAll<HTMLElement>(`.${styles.zoneName}`) ?? [])];
const pieces = [...root.querySelectorAll<HTMLElement>(`[data-compare] .${styles.markPiece}`)];
const primary = Math.max(
widest(primaryNames, zonePad),
widest(primaryMarks, (node) => {
const style = getComputedStyle(node);
const border = node.parentElement ? length(getComputedStyle(node.parentElement).borderInlineEndWidth) : 0;
// inset-inline-start is auto, so the computed value is the used offset. Adding it makes the column equal itself.
return length(style.insetInlineEnd) + border;
}),
);
if (primary >= 1) {
const next = Math.ceil(primary);
setPrimaryPx((current) => (current === next ? current : next));
}
if (!showCompare) return;
const compareMark = root.querySelector<HTMLElement>('[data-compare] > span');
const markStyle = compareMark ? getComputedStyle(compareMark) : null;
const markPad = markStyle ? length(markStyle.insetInlineStart) + length(markStyle.insetInlineEnd) : 0;
const markBorder = compareMark?.parentElement ? length(getComputedStyle(compareMark.parentElement).borderInlineEndWidth) : 0;
const compare = Math.max(widest(compareNames, zonePad), widest(pieces, () => markPad + markBorder));
if (compare < 1) return;
const next = Math.ceil(compare);
setComparePx((current) => (current === next ? current : next));
};
measure();
if (typeof ResizeObserver === 'undefined') return;
const watcher = new ResizeObserver(measure);
watcher.observe(root);
const head = root.querySelector(`.${styles.head}`);
if (head) watcher.observe(head);
for (const gutter of root.querySelectorAll<HTMLElement>(`.${styles.gutter}`)) watcher.observe(gutter);
return () => watcher.disconnect();
}, [showCompare, pieceKey, view, anchor]);
useLayoutEffect(() => {
if (pendingFocus.current !== 'day' || view !== 'day') return;
pendingFocus.current = null;
const id = `${heading}-${anchor.toString()}`;
const list = [...document.querySelectorAll<HTMLElement>('[aria-labelledby]')].find((el) => el.getAttribute('aria-labelledby') === id);
const button = list?.querySelector<HTMLElement>('button');
if (button) {
button.focus();
return;
}
document.getElementById(id)?.focus();
}, [anchor, heading, view]);
const openDay = (day: Temporal.PlainDate) => {
pendingFocus.current = 'day';
setAnchor(day);
setView('day');
};
const keepAnchor = (day: Temporal.PlainDate) => {
setAnchor((current) => (current.equals(day) ? current : day));
};
return (
<div ref={rootRef} className={styles.calendar} onFocusCapture={(event) => noteFocus(event.target)}>
<div className={styles.toolbar}>
<ToggleGroup.Root
aria-label="Calendar view"
value={view}
onValueChange={(value) => {
if (value === 'month' || value === 'week' || value === 'day') setView(value);
}}
>
<ToggleGroup.Item value="month">Month</ToggleGroup.Item>
<ToggleGroup.Item value="week">Week</ToggleGroup.Item>
<ToggleGroup.Item value="day">Day</ToggleGroup.Item>
</ToggleGroup.Root>
{view === 'month' ? null : (
<>
<Button.Root variant="secondary" onClick={openNewEvent}>
New event
</Button.Root>
<Button.Root variant="secondary" onClick={() => setAnchor((day) => day.add({ days: -step }))}>
{`Previous ${move}`}
</Button.Root>
<Button.Root variant="secondary" onClick={() => setAnchor((day) => day.add({ days: step }))}>
{`Next ${move}`}
</Button.Root>
<Text.Root className={styles.range}>{rangeLabel(days, locale)}</Text.Root>
</>
)}
</div>
{view === 'month' ? (
<Calendar.Root
className={styles.month}
locale={locale}
// value starts the tab stop on the anchor day. Clearing it before paint leaves that day focused and not selected.
value={pinFocus ? anchor : null}
onValueChange={openDay}
>
<Calendar.Header />
<Calendar.Grid
showOutsideDays
renderDay={(day) => (
<span className={styles.chips} data-date={day.date.toString()}>
<span className={styles.dateRow}>
{day.today ? (
<span className={styles.dateMark} aria-hidden="true">
{dayDigits(locale).format(day.date.day)}
</span>
) : null}
</span>
{day.date.day === 1 && !day.outside ? <SyncTabStop onDay={keepAnchor} /> : null}
<MonthDay day={day.date} events={items} timeZone={timeZone} locale={locale} />
</span>
)}
/>
</Calendar.Root>
) : (
<ScrollArea.Root {...board.getBoardProps()} axis="both" focusable aria-label="Calendar" aria-describedby={descriptionId} className={styles.board}>
<VisuallyHidden.Root id={descriptionId}>{description}</VisuallyHidden.Root>
<div
className={styles.frame}
data-compare-grid={showCompare ? '' : undefined}
style={{
['--days' as string]: days.length,
['--minutes' as string]: scale,
['--min-inline' as string]: minInline,
['--primary' as string]: primaryPx > 0 ? `${primaryPx}px` : '4.5rem',
['--tracks' as string]: tracks,
['--day-tracks' as string]: dayTracks,
}}
>
<div className={styles.head}>
<div className={styles.corner}>
<Text.Root as="span" variant="label" tone="muted" className={styles.zone}>
<ZoneLabel name={primaryName} />
</Text.Root>
</div>
{compareName ? (
<div className={styles.corner}>
<Text.Root as="span" variant="label" tone="muted" className={styles.zone}>
<ZoneLabel name={compareName} />
</Text.Root>
</div>
) : null}
{axis.segments.map((segment) => {
const phrase = changePhrase(changes.get(segment.day.toString()) ?? [], locale);
const title = phrase ? `${dayHeading(segment.day, locale)} · ${phrase}` : dayHeading(segment.day, locale);
return (
<Text.Root as="h3" variant="label" tabIndex={-1} id={`${heading}-${segment.day.toString()}`} key={segment.day.toString()} className={styles.day}>
{title}
</Text.Root>
);
})}
</div>
{stripBars.length === 0 ? null : (
<div className={styles.strip}>
{Array.from({ length: gutters }, (_, index) => (
<div key={index} className={styles.corner} />
))}
{stripBars.map((bar) => {
const segment = axis.segments[view === 'day' ? 0 : bar.column] ?? axis.segments[0];
if (!segment) return null;
return <StripBar key={bar.event.id} bar={bar} segment={segment} shared={columnShared} />;
})}
</div>
)}
<div className={styles.body}>
<div className={styles.gutter} aria-hidden="true">
{gutterMarks.map((units) => {
const time = axis.segments.map((segment) => fromUnits(segment, units, 'start')).find((value) => value !== null);
if (!time) return null;
return (
<Text.Root
as="span"
key={units}
variant="label"
tone="muted"
className={styles.mark}
data-first={units === 0 ? '' : undefined}
style={{ top: percent(units / scale) }}
>
{formatTime(time.toPlainTime(), { locale })}
</Text.Root>
);
})}
</div>
{showCompare ? (
// The hours repeat the primary instants. The description names this zone, so the column stays quiet.
<div className={styles.gutter} aria-hidden="true" data-compare="">
{compared.map((mark) => (
<Text.Root
as="span"
key={mark.units}
variant="label"
tone="muted"
className={styles.mark}
data-first={mark.units === 0 ? '' : undefined}
data-stack={mark.times.length > 1 ? '' : undefined}
style={{ top: percent(mark.units / scale) }}
>
<ComparePieces units={mark.units} times={mark.times} locale={locale} />
</Text.Root>
))}
</div>
) : null}
{lanes.map(({ segment, placed }) => (
<EventColumn
key={segment.day.toString()}
segment={segment}
placed={placed}
sharedMarks={sharedMarks}
shared={columnShared}
headingId={`${heading}-${segment.day.toString()}`}
/>
))}
</div>
</div>
</ScrollArea.Root>
)}
{session ? (
<EventEditor
event={session.event}
intent={session.intent}
locale={locale}
timeZone={timeZone}
opens={opens}
prior={timedBefore.current.get(session.event.id) ?? null}
rule={editingSeries?.rule ?? null}
wallTime={editingSeries?.wallTime}
template={
editingSeries
? { id: editingSeries.id, seriesId: editingSeries.id, title: editingSeries.title, start: editingSeries.start, end: editingSeries.end, allDay: editingSeries.allDay, repeats: editingSeries.rule !== null, originalStart: editingSeries.start }
: null
}
chooseScope={session.intent === 'edit' && session.event.repeats === true}
restoreFocusRef={landing}
onCancel={() => {
returnFocus.current = true;
setSession(null);
}}
onSave={(next, detail) => {
const editing = session.event;
const day = next.start.withTimeZone(timeZone).toPlainDate();
const labelOf = (saved: CalendarEvent) => {
const strip = showsInStrip(saved, timeZone, opens, closes);
const start = saved.start.withTimeZone(timeZone);
const end = saved.end.withTimeZone(timeZone);
const spansDays = !start.toPlainDate().equals(end.toPlainDate());
return strip ? barLabel(saved, locale, timeZone) : `${saved.title}, ${clockLabel(start, locale, spansDays)} to ${clockLabel(end, locale, spansDays)}`;
};
const reveal = (saved: CalendarEvent, message?: string, around: Temporal.PlainDate = day) => {
const home = shownDay(saved, around);
setAnchor(home ?? around);
if (saved.allDay && !editing.allDay) {
const start = editing.start.withTimeZone(timeZone);
const end = editing.end.withTimeZone(timeZone);
timedBefore.current.set(saved.id, { start: start.toPlainTime(), end: end.toPlainTime(), daySpan: start.toPlainDate().until(end.toPlainDate(), { largestUnit: 'day' }).days });
}
if (!saved.allDay) timedBefore.current.delete(saved.id);
const strip = showsInStrip(saved, timeZone, opens, closes);
const laneHome = strip ? null : shownDay(saved, home ?? around);
setFocusKey(strip ? stripKey(saved.id) : laneHome ? blockKey(saved.id, laneHome) : null);
focusHeading.current = strip || laneHome ? null : (home ?? around);
announce(message ?? labelOf(saved));
returnFocus.current = true;
};
if (session.intent === 'create') {
const created = seriesFromDraft(seriesId(next.id), { title: next.title, start: next.start, end: next.end, allDay: next.allDay, rule: detail.rule });
setSeriesList((list) => [...list, created]);
const saved = firstOf(created) ?? { ...next, id: created.id, seriesId: created.id, repeats: detail.rule !== null, originalStart: next.start };
reveal(saved, `Added ${labelOf(saved)}`);
setSession(null);
return;
}
const current = seriesList.find((item) => item.id === (editing.seriesId ?? editing.id));
if (!current) return;
if (detail.scope === 'occurrence' && editing.originalStart) {
setSeriesList((list) => list.map((item) => (item.id === current.id ? overrideOccurrence(item, editing.originalStart!, { title: next.title, start: next.start, end: next.end, allDay: next.allDay }) : item)));
const saved = { ...editing, ...next, id: editing.id, seriesId: current.id, repeats: true, originalStart: editing.originalStart };
reveal(saved, `Changed this event. ${labelOf(saved)}.`);
} else if (detail.scope === 'following' && editing.originalStart) {
const created = continueFrom(current, editing.originalStart, { title: next.title, start: next.start, end: next.end, allDay: next.allDay, rule: detail.rule, startTime: detail.startTime }, freshSeriesId());
const previous = truncateBefore(current, editing.originalStart);
setSeriesList((list) => list.flatMap((item) => (item.id === current.id ? (previous ? [previous, created] : [created]) : [item])));
const saved = firstOf(created) ?? { ...next, id: created.id, seriesId: created.id, repeats: detail.rule !== null, originalStart: next.start };
reveal(saved, `Changed this and following events. ${labelOf(saved)}.`);
} else {
const replaced = replaceSeries(current, { title: next.title, start: next.start, end: next.end, allDay: next.allDay, rule: detail.rule, startTime: detail.startTime });
setSeriesList((list) => list.map((item) => (item.id === current.id ? replaced : item)));
const saved = firstOf(replaced) ?? { ...next, id: replaced.id, seriesId: replaced.id, repeats: detail.rule !== null, originalStart: next.start };
if (editing.repeats) {
const days = view === 'week' ? weekOf(anchor, locale) : [anchor];
const onScreen = expandSeries(replaced, visibleBounds(view, anchor, locale, timeZone)).map(occurrenceEvent).find((event) => {
if (showsInStrip(event, timeZone, opens, closes)) {
const covered = coveredDays(event, timeZone);
return covered !== null && days.some((item) => Temporal.PlainDate.compare(item, covered.first) >= 0 && Temporal.PlainDate.compare(item, covered.last) <= 0);
}
return shownDay(event, anchor) !== null;
});
// The hours and the focus target belong to the occurrence still in this week. The first occurrence is the one to show when this week no longer has it.
const chosen = onScreen ?? saved;
reveal(chosen, `Changed all events. ${labelOf(chosen)}.`, onScreen ? anchor : day);
} else {
reveal(saved);
}
}
setSession(null);
}}
/>
) : null}
{deleting ? (
<ScopeChoice
intent="delete"
restoreFocusRef={landing}
onCancel={() => {
returnFocus.current = true;
setDeleting(null);
}}
onChoose={finishDelete}
/>
) : null}
</div>
);
}
const WEEK_FROM_SUNDAY: readonly ByDay[] = ['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'];
function orderedDays(locale: string): ByDay[] {
const sunday = Temporal.PlainDate.from('2026-11-15');
const start = weekOf(sunday, locale)[0] ?? sunday;
return Array.from({ length: 7 }, (_, index) => byDayOf(start.add({ days: index })));
}
function weekdayName(day: ByDay, locale: string): string {
const date = Temporal.PlainDate.from('2026-11-15').add({ days: WEEK_FROM_SUNDAY.indexOf(day) });
return new Intl.DateTimeFormat(locale, { weekday: 'short', timeZone: 'UTC' }).format(Date.UTC(date.year, date.month - 1, date.day));
}
type RepeatFields = {
freq: 'none' | 'DAILY' | 'WEEKLY' | 'MONTHLY' | 'YEARLY';
byDay: ByDay[];
end: 'never' | 'count' | 'until';
count: number | null;
until: Temporal.PlainDate | null;
};
function draftFromEvent(event: CalendarEvent, timeZone: string): EventDraft {
const start = event.start.withTimeZone(timeZone);
const end = event.end.withTimeZone(timeZone);
const covered = event.allDay ? coveredDays(event, timeZone) : null;
return {
title: event.title,
date: covered?.first ?? start.toPlainDate(),
endDate: covered?.last ?? end.toPlainDate(),
start: start.toPlainTime(),
end: end.toPlainTime(),
allDay: event.allDay,
};
}
function repeatFromRule(rule: RecurrenceRule | null, day: Temporal.PlainDate): RepeatFields {
return {
freq: rule?.freq ?? 'none',
byDay: rule?.byDay ? [...rule.byDay] : [byDayOf(day)],
end: rule?.end.kind ?? 'never',
count: rule?.end.kind === 'count' ? rule.end.count : 10,
until: rule?.end.kind === 'until' ? rule.end.until.toPlainDate() : null,
};
}
function endOfChosenDay(date: Temporal.PlainDate, timeZone: string): Temporal.ZonedDateTime {
return zoned(date.add({ days: 1 }), MIDNIGHT, timeZone).subtract({ nanoseconds: 1 });
}
function oneWeekdayFollows(fields: RepeatFields, date: Temporal.PlainDate | null): RepeatFields {
if (fields.freq !== 'WEEKLY' || fields.byDay.length !== 1 || !date) return fields;
const day = byDayOf(date);
return fields.byDay[0] === day ? fields : { ...fields, byDay: [day] };
}
function ruleFromRepeat(fields: RepeatFields, start: Temporal.PlainDate | null, timeZone: string): RecurrenceRule | null | { field: 'date' | 'repeat'; message: string } {
if (fields.freq === 'none') return null;
if (fields.freq === 'WEEKLY' && fields.byDay.length === 0) return { field: 'repeat', message: 'Choose at least one day.' };
if (fields.freq === 'WEEKLY' && start && !fields.byDay.includes(byDayOf(start))) return { field: 'date', message: 'The start day has to be one of the weekly days.' };
if (fields.end === 'count' && (fields.count === null || !Number.isInteger(fields.count) || fields.count < 1)) return { field: 'repeat', message: 'Choose how many times it repeats.' };
if (fields.end === 'until' && !fields.until) return { field: 'repeat', message: 'Choose the last date.' };
const byDay = fields.freq === 'WEEKLY' ? fields.byDay : undefined;
if (fields.end === 'count') return { freq: fields.freq, interval: 1, byDay, end: { kind: 'count', count: fields.count ?? 1 } };
if (fields.end === 'until' && fields.until) return { freq: fields.freq, interval: 1, byDay, end: { kind: 'until', until: endOfChosenDay(fields.until, timeZone) } };
return { freq: fields.freq, interval: 1, byDay, end: { kind: 'never' } };
}
function ScopeChoice({
intent,
onChoose,
onCancel,
restoreFocusRef,
}: {
intent: 'edit' | 'delete';
onChoose: (scope: EditScope) => void;
onCancel: () => void;
restoreFocusRef?: RefObject<HTMLElement | null>;
}) {
const first = useRef<HTMLButtonElement>(null);
const titleId = useId();
const descriptionId = useId();
const verb = intent === 'delete' ? 'Delete' : 'Edit';
return (
<Dialog.Root open onOpenChange={(open) => !open && onCancel()} initialFocusRef={first} restoreFocusRef={restoreFocusRef} aria-describedby={descriptionId}>
<div className={styles.fields}>
<Dialog.Header>
<Dialog.Title id={titleId}>{`${verb} repeating event`}</Dialog.Title>
<Dialog.Close />
</Dialog.Header>
<Dialog.Body>
<p id={descriptionId}>{`This event repeats. Choose which events to ${intent}.`}</p>
<div className={styles.scopeChoices} role="group" aria-labelledby={titleId}>
<Button.Root ref={first} variant="secondary" onClick={() => onChoose('occurrence')}>
This event
</Button.Root>
<Button.Root variant="secondary" onClick={() => onChoose('following')}>
This and following
</Button.Root>
<Button.Root variant="secondary" onClick={() => onChoose('series')}>
All events
</Button.Root>
</div>
</Dialog.Body>
</div>
</Dialog.Root>
);
}
/**
* Edits one event, or fills in a new one, including how a new event or a whole series repeats.
* Drag-to-create opens it with the event a drag sketched.
*/
export function EventEditor({
event,
intent = 'edit',
locale,
timeZone,
opens = OPENS,
prior = null,
mode = 'edit',
rule = null,
template = null,
wallTime,
chooseScope = false,
onSave,
onCancel,
restoreFocusRef,
}: {
event: CalendarEvent;
intent?: 'edit' | 'create';
locale: string;
timeZone: string;
opens?: Temporal.PlainTime;
prior?: { start: Temporal.PlainTime; end: Temporal.PlainTime; daySpan: number } | null;
mode?: 'create' | 'edit';
rule?: RecurrenceRule | null;
template?: CalendarEvent | null;
/** The series clock when `template.start` shows a different local time. */
wallTime?: Temporal.PlainTime;
/** A repeating event asks which occurrences the edit applies to before the fields. */
chooseScope?: boolean;
onSave: (event: CalendarEvent, detail: { rule: RecurrenceRule | null; scope: EditScope; startTime?: Temporal.PlainTime }) => void;
onCancel: () => void;
restoreFocusRef?: RefObject<HTMLElement | null>;
}) {
const ids = { title: useId(), date: useId(), endDate: useId(), start: useId(), end: useId(), dialog: useId(), ask: useId(), repeat: useId(), count: useId(), until: useId(), days: useId(), ends: useId() };
const titleRef = useRef<HTMLInputElement>(null);
const choiceRef = useRef<HTMLButtonElement>(null);
const remembered = useRef<{ start: Temporal.PlainTime | null; end: Temporal.PlainTime | null; endDate?: Temporal.PlainDate | null; daySpan?: number } | null>(prior);
const [scope, setScope] = useState<EditScope | null>(chooseScope ? null : 'series');
const [fields, setFields] = useState<EventDraft>(() => draftFromEvent(event, timeZone));
const [repeat, setRepeat] = useState<RepeatFields>(() => repeatFromRule(rule, event.start.withTimeZone(timeZone).toPlainDate()));
const [errors, setErrors] = useState<FormErrors>({});
const asking = chooseScope && scope === null;
useLayoutEffect(() => {
if (asking || !chooseScope) return;
titleRef.current?.focus();
}, [asking, chooseScope]);
const edit =
<K extends keyof EventDraft>(key: K) =>
(value: EventDraft[K]) => {
setErrors({});
setFields((current) => shiftDraft(current, key, value, timeZone));
if (key === 'date') setRepeat((current) => oneWeekdayFollows(current, value instanceof Temporal.PlainDate ? value : null));
};
const setAllDay = (on: boolean) => {
setErrors({});
setFields((current) => {
if (on) {
const blank = current.start?.equals(MIDNIGHT) && current.end?.equals(MIDNIGHT);
if (!blank) remembered.current = { start: current.start, end: current.end, endDate: current.endDate };
let endDate = current.endDate;
if (current.end?.equals(MIDNIGHT) && endDate && current.date && Temporal.PlainDate.compare(endDate, current.date) > 0) endDate = endDate.subtract({ days: 1 });
return { ...current, allDay: true, endDate: endDate ?? current.date };
}
const kept = remembered.current;
const usable = kept?.start && kept.end && !(kept.start.equals(MIDNIGHT) && kept.end.equals(MIDNIGHT));
if (usable && kept) {
const endDate = current.date && typeof kept.daySpan === 'number' ? current.date.add({ days: kept.daySpan }) : (kept.endDate ?? current.date);
return { ...current, allDay: false, start: kept.start, end: kept.end, endDate };
}
return { ...current, allDay: false, start: opens, end: opens.add({ minutes: SLOT_MINUTES }), endDate: current.date };
});
};
const pickScope = (next: EditScope) => {
setScope(next);
if (next === 'series' && template) {
const draft = draftFromEvent(template, timeZone);
setFields(draft);
setRepeat(oneWeekdayFollows(repeatFromRule(rule, template.start.withTimeZone(timeZone).toPlainDate()), draft.date));
}
if (next === 'following' && rule && template && event.originalStart) {
const probe = {
...seriesFromDraft(seriesId('probe'), {
title: template.title,
start: template.start,
end: template.end,
allDay: template.allDay,
rule,
}),
wallTime,
};
const left = remainingCount(probe, event.originalStart);
if (left !== null) setRepeat((current) => ({ ...current, end: 'count', count: left }));
}
};
const submit = (submitEvent: SubmitEvent<HTMLFormElement>) => {
submitEvent.preventDefault();
const result = eventFromDraft(fields, timeZone);
if (!result.ok) {
setErrors({ [result.issue.field]: result.issue.message });
return;
}
const showRepeat = scope !== 'occurrence';
const parsed = showRepeat ? ruleFromRepeat(repeat, fields.date, timeZone) : null;
if (parsed && 'message' in parsed) {
setErrors({ [parsed.field]: parsed.message });
return;
}
onSave({ ...event, ...result.value }, { rule: parsed, scope: scope ?? 'series', startTime: result.value.allDay ? undefined : (fields.start ?? undefined) });
};
const creating = intent === 'create' || mode === 'create';
const dialogTitle = creating ? 'New event' : asking ? 'Edit repeating event' : `Edit ${fields.title || event.title}`;
const days = orderedDays(locale);
return (
<Dialog.Root open onOpenChange={(open) => !open && onCancel()} initialFocusRef={asking ? choiceRef : titleRef} restoreFocusRef={restoreFocusRef} aria-describedby={asking ? ids.ask : undefined}>
{asking ? (
<div className={styles.fields}>
<Dialog.Header>
<Dialog.Title id={ids.dialog}>{dialogTitle}</Dialog.Title>
<Dialog.Close />
</Dialog.Header>
<Dialog.Body>
<p id={ids.ask}>This event repeats. Choose which events to edit.</p>
<div className={styles.scopeChoices} role="group" aria-labelledby={ids.dialog}>
<Button.Root ref={choiceRef} variant="secondary" onClick={() => pickScope('occurrence')}>
This event
</Button.Root>
<Button.Root variant="secondary" onClick={() => pickScope('following')}>
This and following
</Button.Root>
<Button.Root variant="secondary" onClick={() => pickScope('series')}>
All events
</Button.Root>
</div>
</Dialog.Body>
</div>
) : (
<Form.Root errors={errors} onSubmit={submit} aria-labelledby={ids.dialog} className={styles.fields}>
<Dialog.Header>
<Dialog.Title id={ids.dialog}>{dialogTitle}</Dialog.Title>
<Dialog.Close />
</Dialog.Header>
<Dialog.Body>
<Field.Group>
<Label.Root htmlFor={ids.title}>Title</Label.Root>
<Field.Root id={ids.title} name="title">
<Field.TextInput ref={titleRef} required value={fields.title} onValueChange={edit('title')} />
</Field.Root>
<ErrorMessage.Root />
</Field.Group>
<Field.Group>
<Label.Root htmlFor={ids.date}>Date</Label.Root>
<Field.Root id={ids.date} name="date">
<Field.DateInput required value={fields.date} onValueChange={edit('date')} locale={locale} />
</Field.Root>
<ErrorMessage.Root />
</Field.Group>
<Switch.Root className={styles.allDay} checked={fields.allDay} onCheckedChange={setAllDay}>
All day
</Switch.Root>
{fields.allDay ? null : (
<Field.Group>
<Label.Root htmlFor={ids.start}>Start</Label.Root>
<Field.Root id={ids.start} name="start">
<Field.TimeInput required value={fields.start} onValueChange={edit('start')} locale={locale} />
</Field.Root>
<ErrorMessage.Root />
</Field.Group>
)}
<Field.Group>
<Label.Root htmlFor={ids.endDate}>End date</Label.Root>
<Field.Root id={ids.endDate} name="endDate">
<Field.DateInput required value={fields.endDate} onValueChange={edit('endDate')} locale={locale} />
</Field.Root>
<ErrorMessage.Root />
</Field.Group>
{fields.allDay ? null : (
<Field.Group>
<Label.Root htmlFor={ids.end}>End</Label.Root>
<Field.Root id={ids.end} name="end">
<Field.TimeInput required value={fields.end} onValueChange={edit('end')} locale={locale} />
</Field.Root>
<ErrorMessage.Root />
</Field.Group>
)}
{scope === 'occurrence' ? null : (
<Field.Group>
<Label.Root htmlFor={ids.repeat}>Repeat</Label.Root>
<Field.Root id={ids.repeat} name="repeat">
<Field.Select
value={repeat.freq}
onValueChange={(value) => {
setErrors({});
const freq = value === 'DAILY' || value === 'WEEKLY' || value === 'MONTHLY' || value === 'YEARLY' ? value : 'none';
setRepeat((current) => oneWeekdayFollows({
...current,
freq,
byDay: freq === 'WEEKLY' && current.byDay.length === 0 && fields.date ? [byDayOf(fields.date)] : current.byDay,
}, fields.date));
}}
>
<option value="none">None</option>
<option value="DAILY">Daily</option>
<option value="WEEKLY">Weekly</option>
<option value="MONTHLY">Monthly</option>
<option value="YEARLY">Yearly</option>
</Field.Select>
</Field.Root>
<ErrorMessage.Root />
</Field.Group>
)}
{scope === 'occurrence' || repeat.freq !== 'WEEKLY' ? null : (
<div className={styles.repeatBlock}>
<Text.Root id={ids.days} as="span" variant="label" weight="medium">
Days
</Text.Root>
<ToggleGroup.Root
aria-labelledby={ids.days}
selectionMode="multiple"
value={repeat.byDay}
onValueChange={(value) => {
setErrors({});
setRepeat((current) => ({ ...current, byDay: value.filter((day): day is ByDay => WEEK_FROM_SUNDAY.includes(day as ByDay)) }));
}}
className={styles.repeatDays}
>
{days.map((day) => (
<ToggleGroup.Item key={day} value={day}>
{weekdayName(day, locale)}
</ToggleGroup.Item>
))}
</ToggleGroup.Root>
</div>
)}
{scope === 'occurrence' || repeat.freq === 'none' ? null : (
<>
<div className={styles.repeatBlock}>
<Text.Root id={ids.ends} as="span" variant="label" weight="medium">
Ends
</Text.Root>
<RadioGroup.Root
aria-labelledby={ids.ends}
value={repeat.end}
onValueChange={(value) => {
if (value === 'never' || value === 'count' || value === 'until') setRepeat((current) => ({ ...current, end: value }));
}}
>
<RadioGroup.Item value="never">Never</RadioGroup.Item>
<RadioGroup.Item value="count">After</RadioGroup.Item>
<RadioGroup.Item value="until">On</RadioGroup.Item>
</RadioGroup.Root>
</div>
{repeat.end === 'count' ? (
<Field.Group>
<Label.Root htmlFor={ids.count}>Times</Label.Root>
<Field.Root id={ids.count} name="count">
<Field.NumberInput min={1} step={1} value={repeat.count} onValueChange={(count) => setRepeat((current) => ({ ...current, count }))} locale={locale} />
</Field.Root>
<ErrorMessage.Root />
</Field.Group>
) : null}
{repeat.end === 'until' ? (
<Field.Group>
<Label.Root htmlFor={ids.until}>Last date</Label.Root>
<Field.Root id={ids.until} name="until">
<Field.DateInput value={repeat.until} onValueChange={(until) => setRepeat((current) => ({ ...current, until }))} locale={locale} />
</Field.Root>
<ErrorMessage.Root />
</Field.Group>
) : null}
</>
)}
</Dialog.Body>
<Dialog.Footer>
<Dialog.Close>Cancel</Dialog.Close>
<Button.Root type="submit">Save</Button.Root>
</Dialog.Footer>
</Form.Root>
)}
</Dialog.Root>
);
}
export default function EventCalendarZones({ locale = 'en-US' }: { locale?: string }) {
return <EventCalendarExample locale={locale} timeZone="America/Los_Angeles" compareZone="Asia/Kolkata" />;
}
.calendar {
display: flex;
flex-direction: column;
gap: var(--todae-space-3);
inline-size: 100%;
min-inline-size: 0;
container: calendar / inline-size;
}
.toolbar {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: var(--todae-space-2);
}
.range {
margin: 0;
}
.board {
border: var(--todae-border-width) solid var(--todae-color-border-subtle);
border-radius: var(--todae-radius-md);
background: var(--todae-color-bg-canvas);
}
.frame {
--hour: 2.5rem;
inline-size: max(100%, var(--min-inline, calc(4.5rem + var(--days) * 5.5rem)));
}
.head,
.body {
display: grid;
grid-template-columns: var(--tracks, 4.5rem repeat(var(--days), minmax(5.5rem, 1fr)));
}
.head {
position: sticky;
inset-block-start: 0;
z-index: 2;
background: var(--todae-color-bg-canvas);
border-block-end: var(--todae-border-width) solid var(--todae-color-border-subtle);
}
.corner {
position: sticky;
inset-inline-start: 0;
z-index: 1;
background: var(--todae-color-bg-canvas);
}
.corner + .corner {
inset-inline-start: var(--primary, 4.5rem);
}
.zone {
display: block;
margin: 0;
padding-block: var(--todae-space-2);
padding-inline: var(--todae-space-1);
text-align: end;
white-space: normal;
}
.zoneName {
white-space: nowrap;
}
.day {
margin: 0;
min-inline-size: 0;
padding: var(--todae-space-2);
text-align: center;
border-inline-start: var(--todae-border-width) solid var(--todae-color-border-subtle);
}
.strip {
display: grid;
grid-template-columns: var(--tracks, 4.5rem repeat(var(--days), minmax(5.5rem, 1fr)));
background: var(--todae-color-bg-canvas);
border-block-end: var(--todae-border-width) solid var(--todae-color-border-subtle);
}
.stripSlot {
display: flex;
box-sizing: border-box;
min-inline-size: 0;
padding: var(--todae-space-1);
}
.stripSlot > * {
display: flex;
flex: 1;
min-inline-size: 0;
}
.stripBar {
flex: 1;
align-items: center;
justify-content: flex-start;
gap: var(--todae-space-1);
box-sizing: border-box;
min-inline-size: 0;
max-inline-size: 100%;
overflow: hidden;
padding-block: 0;
padding-inline: var(--todae-space-1);
border: var(--todae-border-width) solid var(--todae-color-border-default);
border-inline-start-width: calc(4 * var(--todae-border-width));
border-inline-start-color: var(--todae-color-accent-default);
border-radius: var(--todae-radius-sm);
background: var(--todae-color-bg-surface);
color: var(--todae-color-fg-default);
text-align: start;
}
.stripBar[data-cut-start] {
border-start-start-radius: 0;
border-end-start-radius: 0;
}
.stripBar[data-cut-end] {
border-start-end-radius: 0;
border-end-end-radius: 0;
}
.body {
position: relative;
z-index: 0;
block-size: calc(var(--minutes) / 60 * var(--hour));
}
.gutter {
position: sticky;
inset-inline-start: 0;
z-index: 2;
background: var(--todae-color-bg-canvas);
border-inline-end: var(--todae-border-width) solid var(--todae-color-border-subtle);
}
.gutter[data-compare] {
inset-inline-start: var(--primary, 4.5rem);
}
.gutter[data-compare] .mark {
inset-inline-start: var(--todae-space-1);
white-space: normal;
text-align: end;
}
.gutter[data-compare] .mark[data-stack] {
transform: none;
}
/* The script removes this column after it measures a narrow calendar.
Until then the column is in the first paint, so the query hides it at that width. */
@container calendar (max-inline-size: 375px) {
.frame[data-compare-grid] .head,
.frame[data-compare-grid] .body,
.frame[data-compare-grid] .strip {
grid-template-columns: var(--primary, 4.5rem) 0 var(--day-tracks);
}
.corner + .corner,
.gutter[data-compare] {
visibility: hidden;
min-inline-size: 0;
overflow: hidden;
border-inline-end-width: 0;
}
}
.markPiece {
white-space: nowrap;
}
.markSep {
display: contents;
}
.mark {
position: absolute;
inset-inline-end: var(--todae-space-1);
margin: 0;
line-height: var(--todae-text-leading-tight);
white-space: nowrap;
transform: translateY(-50%);
}
.mark[data-first] {
transform: none;
}
.column {
position: relative;
border-inline-start: var(--todae-border-width) solid var(--todae-color-border-subtle);
/* pan-x keeps a sideways swipe on the week. none would take that swipe. auto lets the browser claim a vertical touch for scrolling, so a finger on empty space never creates. */
touch-action: pan-x;
}
.hours {
position: absolute;
inset: 0;
pointer-events: none;
}
.line {
position: absolute;
inset-inline: 0;
block-size: var(--todae-border-width);
background: var(--todae-color-border-subtle);
}
.columnMark {
position: absolute;
inset-inline-start: var(--todae-space-1);
z-index: 1;
margin: 0;
line-height: var(--todae-text-leading-tight);
white-space: nowrap;
transform: translateY(-50%);
}
.columnMark[data-first] {
transform: none;
}
.events {
position: absolute;
inset: 0;
margin: 0;
padding: 0;
list-style: none;
}
.slot {
position: absolute;
margin: 0;
}
.slot:focus-within {
z-index: 1;
}
.slot[data-dragging] {
opacity: 0.5;
}
.event {
position: absolute;
inset-block: 0;
inset-inline: var(--todae-space-1);
box-sizing: border-box;
align-items: flex-start;
justify-content: flex-start;
flex-direction: column;
gap: 0;
overflow: hidden;
padding-block: 0;
padding-inline: var(--todae-space-1);
border: var(--todae-border-width) solid var(--todae-color-border-default);
border-inline-start-width: calc(4 * var(--todae-border-width));
border-inline-start-color: var(--todae-color-accent-default);
border-radius: var(--todae-radius-sm);
background: var(--todae-color-bg-surface);
color: var(--todae-color-fg-default);
z-index: 1;
text-align: start;
white-space: normal;
cursor: grab;
touch-action: none;
}
.event:focus-visible {
z-index: 1;
}
.edge {
position: absolute;
inset-inline: 0;
z-index: 2;
block-size: var(--todae-space-2);
cursor: ns-resize;
touch-action: none;
}
.edge[data-edge='start'] {
inset-block-start: 0;
}
.edge[data-edge='end'] {
inset-block-end: 0;
}
.preview {
position: absolute;
z-index: 3;
inset-inline: var(--todae-space-1);
box-sizing: border-box;
overflow: hidden;
padding-inline: var(--todae-space-1);
border: var(--todae-border-width) dashed var(--todae-color-accent-default);
border-radius: var(--todae-radius-sm);
background: var(--todae-color-accent-soft);
pointer-events: none;
}
.preview[data-level='blocked'] {
border-style: solid;
border-color: var(--todae-color-status-critical-border);
background: var(--todae-color-status-critical-bg);
}
.previewTime {
font-size: var(--todae-text-label-font-size);
font-weight: var(--todae-text-weight-medium);
line-height: var(--todae-text-leading-tight);
}
.fields {
display: flex;
flex-direction: column;
gap: var(--todae-space-3);
}
.scopeChoices {
display: flex;
flex-direction: column;
align-items: stretch;
gap: var(--todae-space-2);
}
.repeatBlock {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: var(--todae-space-1);
}
.repeatDays {
display: flex;
flex-wrap: wrap;
gap: var(--todae-space-1);
}
.eventLine {
display: flex;
align-items: baseline;
min-inline-size: 0;
max-inline-size: 100%;
gap: var(--todae-space-1);
}
.eventLine > * {
min-inline-size: 0;
}
.repeats {
flex: 0 0 auto;
}
.allDay {
margin-block-start: var(--todae-space-2);
}
.month {
inline-size: 100%;
min-inline-size: 0;
container: month / inline-size;
}
.month :global(table) {
inline-size: 100%;
table-layout: fixed;
border-collapse: collapse;
}
.month :global(th[scope='col']) {
border: var(--todae-border-width) solid var(--todae-color-border-default);
background: var(--todae-color-bg-canvas);
padding-inline: var(--todae-space-1);
text-align: start;
}
.month :global(td) {
max-inline-size: 0;
block-size: 100%;
overflow: visible;
vertical-align: top;
border: var(--todae-border-width) solid var(--todae-color-border-default);
background: var(--todae-color-bg-canvas);
}
.month :global(td:has(button[data-outside])) {
background: var(--todae-color-bg-subtle);
}
.month :global(td button[type='button']) {
--_date-mark: calc(var(--todae-font-size-2) * 2);
position: relative;
display: flex;
flex-direction: column;
align-items: stretch;
justify-content: flex-start;
box-sizing: border-box;
inline-size: 100%;
block-size: 100%;
max-inline-size: 100%;
min-inline-size: 0;
min-block-size: calc(var(--todae-calendar-day-size) * 3);
padding-block: var(--todae-space-1);
padding-inline: var(--todae-space-1);
font-size: var(--todae-calendar-font-size);
line-height: 1;
text-align: start;
}
.month :global(td button[type='button'] > span:last-child) {
align-items: stretch;
align-self: stretch;
inline-size: 100%;
max-inline-size: 100%;
}
.month :global(td button[type='button'][aria-current='date']) {
border-color: transparent;
color: transparent;
}
.month :global(td button[type='button'][aria-current='date'] > span:last-child) {
color: var(--todae-color-fg-default);
}
.dateRow {
display: flex;
flex: none;
align-items: center;
justify-content: center;
inline-size: var(--_date-mark);
block-size: var(--_date-mark);
/* The day number is the button's first line, so this row pulls up over it and the bars follow the row. */
margin-block-start: calc(-1 * 1lh);
}
.dateMark {
display: grid;
place-items: center;
inline-size: 100%;
block-size: 100%;
border-radius: var(--todae-radius-full);
background: var(--todae-color-accent-default);
color: var(--todae-color-on-accent);
font-size: var(--todae-calendar-font-size);
line-height: 1;
}
.month :global(td button[type='button']:focus-visible) {
outline: var(--todae-focus-ring-width) solid var(--todae-calendar-day-ring-focus);
outline-offset: calc(-1 * var(--todae-focus-ring-width));
}
.chips,
.detail {
display: flex;
flex-direction: column;
align-items: stretch;
align-self: stretch;
gap: var(--todae-space-1);
inline-size: 100%;
min-inline-size: 0;
}
.chip {
box-sizing: border-box;
align-self: stretch;
inline-size: 100%;
min-inline-size: 0;
max-inline-size: 100%;
overflow: hidden;
border: var(--todae-border-width) solid var(--todae-color-border-default);
border-inline-start-width: calc(4 * var(--todae-border-width));
border-inline-start-color: var(--todae-color-accent-default);
border-radius: var(--todae-radius-sm);
background: var(--todae-color-bg-surface);
color: var(--todae-color-fg-default);
padding-inline: var(--todae-space-1);
font-size: var(--todae-text-label-font-size);
line-height: var(--todae-text-leading-tight);
text-align: start;
white-space: nowrap;
text-overflow: ellipsis;
}
.more {
min-inline-size: 0;
max-inline-size: 100%;
font-size: var(--todae-text-label-font-size);
line-height: var(--todae-text-leading-tight);
text-align: start;
white-space: nowrap;
}
.barRows {
--_row: calc(var(--todae-text-label-font-size) * var(--todae-text-leading-tight));
display: grid;
grid-template-rows: repeat(var(--bar-rows), var(--_row));
align-self: stretch;
inline-size: calc(100% + 2 * var(--todae-space-1));
margin-inline: calc(-1 * var(--todae-space-1));
}
.monthBar {
position: relative;
z-index: 1;
display: flex;
align-items: center;
gap: var(--todae-space-1);
box-sizing: border-box;
min-inline-size: 0;
block-size: 100%;
overflow: hidden;
padding-inline: var(--todae-space-1);
border-block: var(--todae-border-width) solid var(--todae-color-accent-default);
background: var(--todae-color-accent-default);
color: var(--todae-color-on-accent);
font-size: var(--todae-text-label-font-size);
line-height: var(--todae-text-leading-tight);
text-align: start;
white-space: nowrap;
}
.barLabel {
min-inline-size: 0;
overflow: hidden;
text-overflow: ellipsis;
}
.monthBar:not([data-cut-start]):not([data-join-start]) {
border-inline-start: calc(4 * var(--todae-border-width)) solid var(--todae-color-accent-default);
border-start-start-radius: var(--todae-radius-sm);
border-end-start-radius: var(--todae-radius-sm);
}
.monthBar:not([data-cut-end]):not([data-join-end]) {
border-inline-end: var(--todae-border-width) solid var(--todae-color-accent-default);
border-start-end-radius: var(--todae-radius-sm);
border-end-end-radius: var(--todae-radius-sm);
}
.monthBar[data-cut-start] {
margin-inline-start: calc(-1 * var(--todae-calendar-day-border-width));
}
.monthBar[data-cut-end] {
margin-inline-end: calc(-1 * var(--todae-calendar-day-border-width));
}
.monthBar[data-join-start] {
margin-inline-start: calc(-1 * (var(--todae-calendar-day-border-width) + 2 * var(--todae-border-width)));
}
.monthBar[data-join-end] {
margin-inline-end: calc(-1 * (var(--todae-calendar-day-border-width) + 2 * var(--todae-border-width)));
}
.cue {
flex: none;
}
.cueEnd {
flex: none;
margin-inline-start: auto;
}
.dot {
inline-size: var(--todae-space-2);
block-size: var(--todae-space-2);
border-radius: var(--todae-radius-full);
background: var(--todae-color-accent-default);
}
.count {
display: none;
}
@container calendar (max-inline-size: 30rem) {
.stripBar {
min-block-size: var(--todae-space-2);
padding-block: 0;
}
.monthBar {
min-block-size: var(--todae-space-2);
padding-block: 0;
}
}
@container month (max-inline-size: 30rem) {
.month :global(td button[type='button']) {
--_date-mark: var(--todae-space-4);
font-size: var(--todae-font-size-1);
}
.dateMark {
font-size: var(--todae-font-size-1);
}
.barRows {
--_row: var(--todae-space-1);
}
.monthBar {
min-block-size: 0;
}
.barLabel,
.cue,
.cueEnd,
.monthBar .repeats,
.chip,
.more {
position: absolute;
inline-size: 1px;
block-size: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}
.count {
display: flex;
align-items: center;
justify-content: center;
gap: var(--todae-space-1);
font-size: var(--todae-text-label-font-size);
line-height: var(--todae-text-leading-tight);
}
}
Sun, Nov 15
Mon, Nov 16
Tue, Nov 17
Wed, Nov 18
Thu, Nov 19
Fri, Nov 20
Sat, Nov 21
The time lanes
daySegments() builds one lane per visible day, open from opens to closes in timeZone. Units are elapsed minutes from that day's opening, so a day the clock changes is as long as it really is, and the night stays off the lane. toUnits() and fromUnits() turn a Temporal.ZonedDateTime into a point on that lane and back. A start at a shared midnight belongs to the later day, and an end belongs to the earlier day.
weekOf() returns the seven days that contain a day, starting on the locale's first day of the week, the same day Calendar starts on. English and Korean start on Sunday. The week view passes those seven days to daySegments(), and the day view passes the one day on screen. Every column is as tall as the longest lane, so a minute is the same height in each column. hourMarks() lists each hour from that lane's opening through the last hour that starts before the close. Each column draws those lines itself. The gutter names an hour only when every column has that same wall time at that offset. A column that differs names the hour inside the column. The corner lists every zone name the visible days use. compareZone adds the second hour column. Its row is the second zone's time at that hour. When the week shows two of those times, the row lists both, in the corner's order. Both hour columns stay out of the accessibility tree, because those hours repeat. The grid's description is a sentence that names the zone, and the second column's lead when that column is showing and the lead is the same all week. When the lead changes during the week, the sentence leaves out the time difference. When the calendar is 375px wide or narrower, the second column is left out and the first zone's name stays.
The event block
visibleSpans() keeps the part of each event that falls inside a lane and drops the rest. An event that starts before the opening is drawn from the opening, and one that runs past the close stops at the close. An event that meets two days becomes one block on each day when it meets the open hours. An all-day event is left out, and so is an event that only touches a lane at an endpoint. A timed event that covers more than one calendar day and never meets the open hours is left out too. Launch watch, from 10:00 PM to 2:00 AM, misses 8:00 to 18:00, so the lanes cannot draw it. showsInStrip() is that choice. packWeekBars() then places every all-day event, and every event the lanes leave out, on a row in the strip above the time grid. Earlier starts take an earlier row, and a longer span takes the earlier row when two start together. The strip uses the same packing in the week and in the day. spanFrame() turns a span into a top and a height as fractions of the shared length, so nothing is measured.
placeOverlaps() packs one lane's spans into columns. Spans that overlap in time, including a chain where each one overlaps the next, share that cluster's column count. Each span takes the first column that is free when it starts. When two start together, the longer one takes the column on the left. A span may widen into columns on its right that nothing else uses while it runs. The left edge and the width are fractions of the lane, so nothing is measured. Spans that only touch at an endpoint each keep the full width, one after the other. A span with no length overlaps a span only when its instant is strictly inside that span, and two spans with no length at the same instant do not overlap. The grid does not draw a span with no length, because visibleSpans() drops it. Each day column is at least 5.5rem wide, and another 4rem for every column in that day's busiest cluster, so a busy week scrolls sideways.
The block is a Menu button. Its name is the title, then the event's own start and end, written with formatTime. When the start or the end falls on another day, the name includes that day in the same form as the column heading. A block the visible hours clip says "started earlier" or "continues", and the text shows "…" on that side. A block shorter than 45 minutes shows the title only. Drag the block to move the event. Drag the top or bottom edge to change that end. The edges are not keyboard controls. The menu is. A bar in the strip is a Menu button too. Its name is the title and the full date range, in the page's language. A timed bar also reads its start and end times. … marks an end the week cuts. An all-day bar's menu moves that event to the previous or next day. Edit… changes the dates. Dragging a bar is left for later.
findConflicts() lists other occurrences that overlap an event in time. Touching at an endpoint is not an overlap. placeOverlaps() uses that same test on the lane's minutes.
The month
Month sits beside Week and Day. The switch lists Month, then Week, then Day. It shows one month in timeZone. eventsOnDay() lists the events that overlap a calendar day, from that midnight up to the next midnight. An event that ends on the next midnight stays on the earlier day. An all-day event is included here, and its dates come from its own time zone. The time lanes still leave it out. All-day events come first, then earlier starts, then titles.
Calendar draws the day buttons through Calendar.Grid, and Calendar.Header names the focused month and moves to the previous or next month. renderDay draws up to two titles in each day, in the same colors as an event block, and writes +N more as text when the day has more. The button is named by the full date, then those titles and that count, so its name holds every word the day shows. Nothing inside the day is a control. The day number sits at the start of the top of every cell, and today circles that number. Each title spans the cell, directly under that day's bars. A multi-day bar uses the accent fill and runs across the grid lines between its days. A border bounds every day, including a day outside the month, which is dimmer. Each week row is at least three day sizes tall, and it grows when a day needs more. When the month is narrower than 30rem, each bar stays as a thin segment under the day number, with no title and no end cue, and the day still shows a dot and the number of events. The titles stay in the name.
Press a day, or press Enter or Space while it is focused, and the view switches to that day. Focus moves to the day's first event. When the day has no event on the time lane, focus moves to the day heading. Arrow keys move a week. Page Up and Page Down move a month, and with Shift they move a year. The header's label and the grid's name follow the focused month. Switching to Week or Day keeps that day. The week is the one that contains it. Previous and Next keep focus on the button that was pressed.
packWeekBars() places each all-day event, and each event that covers more than one calendar day, on one row of that week. The bar is one strip across those days, split where the week ends. … appears only on the first or last day of that week, and only when the event continues past that edge. The days in between join with no cue, and each row keeps the same height so a bar stays on one line. It sits above that day's timed titles. The day still shows two of those bars and titles, and +N more counts the bars and the titles that do not fit. A day the bar covers still reads that event, with its title and its full date range.
showOutsideDays draws the days that lead and trail the month, so each week is a full row. The days that lead and trail the month stay pressable and show their events. A press opens that day.
Dragging
In the week and the day, useLaneDrag runs on axis: "block". Each day column is a lane. Every lane covers one shared range, the longest day's open minutes, which is the height of the columns. A drag snaps to a 15 minute slot. A move keeps the event's length, including any part the lane clips, and can land on another day in the week. A resize stays on the day it started and writes the edge that was dragged. Escape cancels the drag and leaves the event where it was.
A press on empty space in a lane creates an event. The press floors to the 15 minute cell under it. Dragging draws a preview of the snapped span. Dragged downward, the span starts at that cell. Dragged upward, the cell that was pressed stays inside the span, and the other end snaps to the nearest slot. The preview lines up with an event of that length. It starts at the same place in the lane, and it is the same height. It is not a control, and assistive technology does not read it. Releasing the pointer opens the editor with that day, that start, and that end. The title is ready to type. A click that stays inside the drag threshold opens the editor for one slot from that cell. Escape during the drag opens nothing and adds no event.
useLaneDrag reads that one shared range. When a clock change falls inside the visible hours, a day is shorter than the range. A place past that day's close is blocked, so the drop does not land. A resize cannot cross into the next day. The library keeps a resize on the lane where it started.
The menu on each event makes every move a drag can make. It moves the event one slot earlier or later, and, in the week, to the previous or next day. It lengthens or shortens the event by one slot, from the end. An item that would leave the day's open hours is unavailable. The day view has no previous or next day for a timed event, because that board has one lane. An all-day bar can still move to the previous or next day. After each item, focus returns to the event. A live region reads the new time, and the button's name reads it too.
Editing
Edit… opens a Dialog. The fields are the title, the date, the start, the end date, the end, and an all-day switch, using Field date and time inputs. Changing the date or the start keeps the length and moves the end with it. Save writes the event and returns focus to it, or to that day's heading when the event is off the time lanes and the strip. Cancel and Escape leave it as it was and return focus to it. The end has to be after the start. When it is not, the end field shows "End must be after the start." An all-day event keeps its end date. Save runs from the start date at midnight through the day after the end date, and those dates stay in the event's own zone. Turning an event all-day, or moving its end onto another day, puts it in the strip or on a month bar. Turning all-day off restores the times from before the switch, or starts at the opening for one slot when the event was already all day, and puts it on the time lanes, or back among that day's titles. The month still lists it.
New event, in the week and the day, opens the same dialog for a new event. It is one button. Empty slots are not tab stops. The day is the one that last had focus in the grid. When that focus was an event, the new event starts when that event ends, and it lasts one slot. When that end is too close to the day's close, the slot stops at the close. When that focus was a day heading, or nothing in the grid had focus, the new event starts at the first slot of the day on screen. A drag or a click that creates an event opens this dialog with the snapped day, start, and end. The preview stays in that day's column while the dialog is open. Save adds the event on that span. The live region says it was added, which a move does not say, and focus moves to the new event. Cancel and Escape add nothing, remove the preview, and return focus to that day's heading. Cancelling an edit returns focus to that event.
EventEditor is that dialog. It edits an event, and it fills in a new one. eventFromPlace() builds the unsaved event for the snapped place, with an empty title. The dialog calls onSave with the event the fields name.
Repeating
The calendar stores a series and draws occurrences. A series is the title, the start, the end, the time zone, and a rule. An occurrence is one instance of that series: a stable occurrence id, the series id, and the start the rule generated. The month, the week, and the day read occurrences whose start falls in the visible window. They do not read the rule, so a block is drawn the same way whether or not the event repeats. A repeating block says Repeats where it has a line for it. A timed block shows a repeat mark beside the title, so a one-hour block keeps its time. The accessible name says "repeats" too: a block's name ends with it, and a bar's name has it right after the title, where the bar shows Repeats.
Book club is one series. It starts Sunday 15 November 2026 at 3:00 PM and repeats every Sunday. The week of 16 November shows that Sunday. Next week shows the next one. A Sunday before 15 November has none.
expandSeries() returns the occurrences inside a half-open window, [start, end). The rule is a small subset of a recurrence rule, evaluated with Temporal and no extra library. It supports a daily, weekly, monthly, or yearly frequency, an interval, a count, an until instant, and, for a weekly rule, the weekdays. The week starts on Monday, as that rule does, independent of the page locale. A count counts from the series start, and a removed date still uses one of those counts. An until instant includes an occurrence that starts on it. The editor's On end is the last moment of the chosen day, so a meeting that day is included.
A monthly series that starts on the 31st skips a month that has no 31st. It does not move to the last day of that month. A yearly series that starts on 29 February skips a year that is not a leap year. An occurrence keeps its wall-clock time in the series' time zone when the clock changes, so a meeting at 9:00 stays at 9:00 after the spring-forward and after the fall-back. The length is that wall length too, so 9:00 to 10:00 stays 9:00 to 10:00. A wall time that the clock skips follows Temporal's compatible resolution.
An EXDATE removes one occurrence. An override replaces one, keyed by the start the rule generated, so a moved or edited occurrence stays that same instance. The view asks for a window that begins earlier by the series' length, so an occurrence that started before the visible days and still overlaps them is included.
New event opens this dialog with Repeat set to none. Repeat can be none, daily, weekly, monthly, or yearly. Weekly lists the days in the locale's week order, and at least one stays chosen. While one weekday is selected, that weekday follows the date, so the series starts on a day it repeats. Saving is refused when the chosen weekdays leave the start out. Ends is never, after a number of times, or on a date. Save stores a series. There is no interval field. The interval stays 1 unless a caller sets it on the series. The recurrence module sits next to the example. This page describes it and does not paste the file.
Edit… or Delete on a repeating occurrence asks which events to change: This event, This and following, or All events. The dialog is named for that choice, the explanation is its description, and the first button takes focus. Tab moves through the three buttons. Enter activates the focused one. Escape closes the dialog and returns focus to the occurrence. This event writes an override, or an EXDATE when the choice is a delete. This and following ends the series just before that occurrence and starts a new series from the form. Its count is the number still left, and later exceptions move onto that new series. A later edit moves with the new start, so an edit at 1:00 PM is at 3:00 PM when this and the following events start two hours later. All events changes the series. A one-weekday series moves onto the new start's weekday. Exceptions move by wall-clock time, so a deleted or edited occurrence keeps its local time when the start crosses a daylight-saving change. Deleting the last occurrence removes the series. After a delete, focus moves to the day heading, because the button is gone, and the live region names the date. Dragging one occurrence moves that occurrence only, and a menu move of one block does the same. Neither one asks. The live region says what changed: added, changed this event, changed this and following events, changed all events, or those same words for a delete.
Expansion walks only the window. It returns at most 400 occurrences, the earliest ones in that window. A daily series across a year stays under that cap. A longer window returns those 400 and stops.
Accessible composition
Todae handles: ToggleGroup switches Month, Week and Day and keeps one selected. Button moves to the previous or next week or day, and New event opens the editor for a new event without a pointer. Calendar draws the month, names the focused month from Calendar.Header, keeps one tab stop, names each day with the full date, then that day's titles. ScrollArea scrolls the week and the day and keeps a focused block's ring in view. Text clamps the title and the time. useLaneDrag moves, resizes, and creates a block with the pointer, snaps it to the slot grid, and cancels on Escape. Menu is the keyboard path for moving and resizing. Dialog and Field edit the exact title and time, and they name a new event. The event button takes its focus ring from Button. A live region reads the time after a change, and it says when an event was added.
You supply: A title for each event. A time zone, an optional second zone, and the visible hours. Day headings that name each column and the list of events in it. An accessible name on each event that starts with the title and includes the event's real start and end, not only the hours on screen. A bar's name starts with the title and reads the full date range. A timed bar also reads its start and end times. The 15 minute slot, and a check that the end is after the start.
Watch for: An event named only by its color. A repeating event with no visible or accessible cue that it repeats. Times written in the browser's zone when the calendar's zone is the one that matters. A second hour column that makes the page scroll sideways on a narrow screen. A week of columns with nowhere to scroll on a narrow screen. An all-day event drawn on the time grid. A multi-day event drawn as a separate chip on every day. A bar named without the days it covers. A day column fixed so narrow that side-by-side blocks are squeezed to a letter. A control inside a month day. The day button is the only interaction there. A drag with no keyboard path. A create gesture with no keyboard path, or a tab stop on every empty slot. A resize that tries to cross a day. An end that is not after the start, with nothing saying so.
When to use
When: People look at events across a month, a week, or a day in one time zone, such as a team's calendar.
When not: Items on parallel resource lanes, with conflicts across lanes → the Scheduler recipe. Picking one date or time → the Date and time picker recipe.