Event calendar
패키지 네임스페이스가 아니라 레시피입니다. 이벤트 캘린더는 하나의 시간대에서 한 달, 한 주의 날짜 열, 또는 하루를 보여 줍니다. 주와 일에서 각 날은 보이는 시간의 레인이고, 이벤트는 시작과 끝이 들어가는 레인의 블록입니다. EventCalendar.* export는 없습니다.
이 페이지의 내용
예제는 2026년 11월 16일 주이고, 8:00부터 18:00까지 열려 있습니다. America/Los_Angeles를 고정하고 그 옆에 Asia/Kolkata를 더합니다. 모서리는 보이는 날이 쓰는 시간대 이름을 모두 페이지의 언어로 적습니다. PST나 PST / PDT가 그런 예입니다. timeZone은 IANA 시간대 id입니다. 비워 두면 그리드는 보는 사람의 시간대를 씁니다. 서버에서 그리는 앱은 timeZone을 직접 넘깁니다. 서버와 브라우저가 같은 시간대를 이름 붙여야 마크업이 맞습니다. 이벤트는 Temporal.Instant 또는 Temporal.ZonedDateTime입니다. 그리드는 그 순간을 timeZone에 놓으므로, 시간대가 바뀌면 같은 이벤트가 다른 시각에 앉습니다. compareZone은 둘째 시간 열을 더합니다. 각 줄은 첫째 시각과 같은 순간에서의 그 시간대 현지 시각입니다. 한 주가 오프셋을 둘 쓰면 줄은 모서리와 같은 순서로 두 시각을 적습니다. 오후 5:00 / 오후 6:00이 그런 예입니다. 제목은 나중 오프셋을 쓰는 첫 보이는 날을 적습니다. PDT 시작이 그런 예입니다. 바뀌는 쪽이 둘째 열이면 둘째 열이라고 적습니다. 시간 단위가 아닌 오프셋, 예를 들어 +5:30인 Asia/Kolkata나 +5:45인 Asia/Kathmandu는 그 분을 보여 줍니다. 날짜가 다르면 줄은 날짜 차이도 보여 줍니다. +1일이 그런 예입니다. 날짜 차이는 바로 앞의 시각에 속합니다. Month로 바꾸면 그 달이 보이고, Day로 바꾸면 열이 하나이며, Previous와 Next로 옮깁니다. 목요일은 바쁜 날입니다. 겹치는 이벤트는 나란히 놓입니다. Offsite는 월요일의 종일 이벤트입니다. Retreat는 금요일부터 일요일까지 이어지며 다음 주로 넘어갑니다. Launch watch는 오후 10시부터 오전 2시까지입니다. Book club은 11월 15일부터 일요일마다 오후 3시부터 4시까지 반복하고, 각 보기는 그 창 안의 발생을 보여 줍니다.
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);
}
}
11월 15일 (일)
11월 16일 (월)
11월 17일 (화)
11월 18일 (수)
11월 19일 (목)
11월 20일 (금)
11월 21일 (토)
시간 레인
daySegments()는 보이는 날마다 레인을 하나 만들고, timeZone에서 opens부터 closes까지 엽니다. 단위는 그날 여는 시각부터 흐른 분이므로, 시계가 바뀌는 날은 실제 길이 그대로이고 밤은 레인에 남지 않습니다. toUnits()와 fromUnits()는 Temporal.ZonedDateTime을 그 레인의 점으로 바꾸고 다시 되돌립니다. 자정을 공유하는 시작은 나중 날에 속하고, 끝은 앞날에 속합니다.
weekOf()는 어떤 날이 들어 있는 이레를 돌려주며, 그 주의 첫날은 로케일의 한 주 시작 요일입니다. Calendar가 쓰는 것과 같은 날입니다. 영어와 한국어는 일요일에 시작합니다. 주 보기는 그 이레를 daySegments()에 넘기고, 일 보기는 화면에 있는 하루를 넘깁니다. 모든 열은 가장 긴 레인만큼 높으므로, 1분의 높이가 열마다 같습니다. hourMarks()는 그 레인의 여는 시각부터, 닫기 전에 시작하는 마지막 시각까지 한 시간마다 나열합니다. 각 열은 그 선을 스스로 그립니다. 홈은 모든 열이 그 오프셋에서 같은 벽시계 시각을 가질 때만 그 시각을 적습니다. 다른 열은 그 시각을 열 안에 적습니다. 모서리는 보이는 날이 쓰는 시간대 이름을 모두 적습니다. compareZone은 둘째 시간 열을 더합니다. 그 줄은 둘째 시간대의 시각입니다. 한 주가 그 시각을 둘 보이면 모서리의 순서로 둘 다 적습니다. 두 시간 열은 접근성 트리 밖에 남습니다. 그 시각은 반복되기 때문입니다. 그리드의 설명은 시간대를 이름 붙이는 문장이고, 시차가 한 주 내내 같을 때만 둘째 열의 시차도 적습니다. 주 중에 시차가 바뀌면 설명은 시차를 적지 않습니다. 캘린더가 375px 이하이면 둘째 열은 빠지고 첫째 시간대의 이름은 남습니다.
이벤트 블록
visibleSpans()는 각 이벤트 가운데 레인 안에 들어오는 부분만 남기고 나머지는 버립니다. 여는 시각보다 먼저 시작한 이벤트는 여는 시각부터 그려지고, 닫는 시각을 넘는 이벤트는 닫는 시각에서 멈춥니다. 열린 시간에 닿는 이틀 이상의 이벤트는 날마다 블록이 하나가 됩니다. 종일 이벤트는 빠지고, 끝점에서만 레인에 닿는 이벤트도 빠집니다. 달력의 이틀 이상을 덮으면서 열린 시간에 한 번도 닿지 않는 시간 이벤트도 빠집니다. Launch watch는 오후 10시부터 오전 2시까지라 8:00부터 18:00을 비키므로, 레인이 그릴 수 없습니다. showsInStrip()가 그 선택입니다. packWeekBars()는 그다음 모든 종일 이벤트와, 레인이 빼 둔 모든 이벤트를 시간 그리드 위 띠의 한 줄에 놓습니다. 더 일찍 시작한 이벤트가 더 앞줄을 차지하고, 둘이 같이 시작하면 더 긴 구간이 앞줄을 차지합니다. 띠는 주와 일에서 같은 배치를 씁니다. spanFrame()은 구간을 공유 길이에 대한 위쪽 위치와 높이의 비율로 바꾸므로, 아무것도 측정하지 않습니다.
placeOverlaps()는 한 레인의 구간을 열로 나눕니다. 시간이 겹치는 구간은, 각 구간이 다음 구간과 겹치는 사슬까지 포함해서, 그 묶음의 열 개수를 같이 씁니다. 각 구간은 시작할 때 비어 있는 첫 열을 차지합니다. 둘이 같이 시작하면 더 긴 구간이 왼쪽 열을 차지합니다. 구간은 자기가 있는 동안 아무도 쓰지 않는 오른쪽 열까지 넓어질 수 있습니다. 왼쪽 가장자리와 너비는 레인의 비율이므로, 아무것도 측정하지 않습니다. 끝점에서만 닿는 구간은 차례로 전체 너비를 유지합니다. 길이가 없는 구간은 그 순간이 다른 구간의 시작과 끝 사이에 있을 때만 그 구간과 겹치고, 같은 순간의 길이가 없는 두 구간은 겹치지 않습니다. 그리드는 길이가 없는 구간을 그리지 않습니다. visibleSpans()가 그 구간을 버리기 때문입니다. 각 날짜 열은 적어도 5.5rem이고, 그날 가장 바쁜 묶음의 열마다 4rem이 더해지므로, 바쁜 주는 옆으로 스크롤합니다.
블록은 Menu 버튼입니다. 이름은 제목, 그다음 이벤트 자신의 시작과 끝이며, formatTime으로 씁니다. 시작이나 끝이 다른 날이면 이름은 열 제목과 같은 형식으로 그 날을 포함합니다. 보이는 시간에 잘린 블록은 "started earlier" 또는 "continues"라고 하고, 글은 그쪽에 "…"를 보여 줍니다. 45분보다 짧은 블록은 제목만 보여 줍니다. 블록을 끌면 이벤트가 옮겨 갑니다. 위쪽이나 아래쪽 가장자리를 끌면 그 끝이 바뀝니다. 가장자리는 키보드 컨트롤이 아닙니다. 메뉴가 그 역할을 합니다. 띠의 막대도 Menu 버튼입니다. 이름은 제목과 전체 날짜 범위이며, 페이지의 언어로 읽습니다. 시간 막대는 시작과 끝 시각도 읽습니다. 주가 자른 끝에는 …가 붙습니다. 종일 막대의 메뉴는 그 이벤트를 이전 날이나 다음 날로 옮깁니다. Edit…가 날짜를 바꿉니다. 막대를 끄는 것은 나중으로 둡니다.
findConflicts()는 한 이벤트와 시간이 겹치는 다른 발생을 나열합니다. 끝점에서 닿기만 하는 것은 겹침이 아닙니다. placeOverlaps()는 레인의 분에 같은 기준을 씁니다.
월
월은 Week와 Day 옆의 보기입니다. 전환은 Month, Week, Day 순입니다. timeZone의 한 달을 보여 줍니다. eventsOnDay()는 달력의 하루와 겹치는 이벤트를 나열합니다. 하루는 그 자정부터 다음 자정 직전까지입니다. 다음 자정에 끝나는 이벤트는 앞날에 남습니다. 종일 이벤트는 여기에 포함되고, 그 날짜는 이벤트 자신의 시간대에서 옵니다. 시간 레인은 종일 이벤트를 그대로 빼 둡니다. 종일 이벤트가 먼저이고, 그다음 더 이른 시작, 그다음 제목입니다.
Calendar는 Calendar.Grid로 날짜 버튼을 그리고, Calendar.Header가 포커스된 달을 이름 붙이며 이전 달과 다음 달로 옮깁니다. renderDay는 각 날짜 안에 제목을 최대 두 개 그리며, 이벤트 블록과 같은 색을 씁니다. 그 날짜에 이벤트가 더 있으면 +N more를 글자로 씁니다. 버튼의 이름은 전체 날짜 다음에 그 제목과 개수가 이어지므로, 날짜에 보이는 모든 낱말이 들어 있습니다. 날짜 안에는 컨트롤이 없습니다. 날짜 숫자는 칸의 위, 시작 쪽에 있고, 오늘은 그 숫자를 원으로 표시합니다. 제목은 칸의 너비를 채우며 그 날의 막대 바로 아래에 있습니다. 여러 날에 걸친 막대는 강조 색으로 채워지고, 날짜 사이의 격자선을 가로질러 이어집니다. 테두리가 모든 날짜를 나누고, 달 밖의 날짜도 테두리를 유지한 채 더 흐리게 보입니다. 각 주 줄은 날짜 크기의 세 배 이상이고, 날짜에 더 필요한 만큼 늘어납니다. 달이 30rem보다 좁으면 각 막대는 날짜 숫자 아래에 제목과 끝 표시 없는 얇은 조각으로 남고, 날짜는 점과 이벤트 개수를 그대로 보여 줍니다. 제목은 이름에 남습니다.
날짜를 누르거나, 포커스가 있는 날짜에서 Enter 또는 Space를 누르면 그 날의 일 보기로 바뀝니다. 포커스는 그날의 첫 이벤트로 옮겨 갑니다. 시간 레인에 이벤트가 없으면 포커스는 날짜 제목으로 옮겨 갑니다. 화살표 키는 한 주를 옮기고, Page Up과 Page Down은 한 달을 옮기며, Shift와 함께면 한 해를 옮깁니다. 헤더의 이름과 그리드의 이름은 포커스된 달을 따릅니다. Week나 Day로 바꾸면 그 날이 유지됩니다. 주는 그 날이 들어 있는 주입니다. Previous와 Next는 포커스를 누른 버튼에 둡니다.
packWeekBars()는 각 종일 이벤트와, 달력의 이틀 이상을 덮는 각 이벤트를 그 주의 한 줄에 놓습니다. 막대는 그 날들을 가로지르는 하나의 띠이고, 주가 끝나는 곳에서 나뉩니다. …는 그 주의 첫날이나 마지막 날에만, 이벤트가 그 가장자리를 넘어 이어질 때만 붙습니다. 그 사이의 날들은 표식 없이 이어지고, 각 줄의 높이가 같아서 막대는 한 줄로 남습니다. 그 날의 시간 제목보다 위에 있습니다. 날짜는 그 막대와 제목을 여전히 두 개까지 보여 주고, +N more는 들어가지 못한 막대와 제목을 셉니다. 막대가 덮는 날은 그 이벤트를 그대로 읽으며, 제목과 전체 날짜 범위를 함께 읽습니다.
showOutsideDays는 달의 앞과 뒤를 채우는 날을 그리므로, 한 주가 한 줄로 남습니다. 달을 앞서거나 뒤따르는 날은 누를 수 있고 그 이벤트를 보여 줍니다. 누르면 그 날이 열립니다.
끌기
주와 일에서 useLaneDrag는 axis: "block"으로 동작합니다. 각 날짜 열은 레인입니다. 모든 레인은 하나의 공유 range를 덮으며, 그 range는 가장 긴 날의 열린 분이고 열의 높이입니다. 끌기는 15분 슬롯에 맞춥니다. 이동은 레인이 잘라 낸 부분을 포함해 이벤트의 길이를 유지하고, 주 안의 다른 날로 놓을 수 있습니다. 크기 변경은 시작한 날에 남고, 끈 가장자리를 기록합니다. Escape는 끌기를 취소하고 이벤트를 원래 자리에 둡니다.
레인의 빈 칸을 누르면 이벤트를 만듭니다. 누른 지점은 그 아래의 15분 칸으로 내려갑니다. 끌면 맞춰진 구간의 미리보기가 그려집니다. 아래로 끌면 그 칸에서 시작합니다. 위로 끌면 누른 칸이 구간에 남고, 다른 끝은 가장 가까운 슬롯에 맞춥니다. 미리보기는 그 길이의 이벤트와 같은 자리에서 시작하고 높이가 같습니다. 컨트롤이 아니며, 보조 기술은 읽지 않습니다. 포인터를 놓으면 그 날, 그 시작, 그 끝으로 편집기가 열립니다. 제목을 바로 입력할 수 있습니다. 끌기 문턱 안에서 머무는 클릭은 그 칸에서 한 슬롯으로 편집기를 엽니다. 끄는 동안 Escape를 누르면 아무것도 열리지 않고 이벤트도 더해지지 않습니다.
useLaneDrag는 그 하나의 공유 range를 읽습니다. 시계 변경이 보이는 시간 안에 있으면 어떤 날은 그 range보다 짧습니다. 그날의 닫는 시각을 넘는 자리는 막히므로, 그 자리에 내려놓지 않습니다. 크기 변경은 다음 날로 넘어가지 않습니다. 라이브러리는 크기 변경을 시작한 레인에 둡니다.
각 이벤트의 메뉴는 끌기가 할 수 있는 이동을 모두 합니다. 이벤트를 한 슬롯 앞이나 뒤로 옮기고, 주에서는 이전 날이나 다음 날로 옮깁니다. 끝에서 한 슬롯만큼 늘리거나 줄입니다. 그날의 열린 시간을 벗어나는 항목은 쓸 수 없습니다. 일 보기에서 시간 이벤트에는 이전 날과 다음 날이 없습니다. 그 보드는 레인이 하나이기 때문입니다. 종일 막대는 이전 날이나 다음 날로 옮길 수 있습니다. 각 항목 뒤에는 포커스가 이벤트로 돌아옵니다. 라이브 영역이 새 시각을 읽고, 버튼의 이름도 그 시각을 읽습니다.
고치기
Edit…는 Dialog를 엽니다. 필드는 제목, 날짜, 시작, 끝나는 날짜, 끝, 그리고 종일 스위치이며, Field의 날짜와 시각 입력을 씁니다. 날짜나 시작을 바꾸면 길이를 유지한 채로 끝이 함께 옮겨 갑니다. Save는 이벤트를 기록하고, 포커스를 그 이벤트로 되돌립니다. 이벤트가 시간 레인과 띠 밖에 있으면 그 날의 제목으로 되돌립니다. Cancel과 Escape는 원래대로 두고 포커스를 그 이벤트로 되돌립니다. 끝은 시작보다 뒤여야 합니다. 그렇지 않으면 끝 필드에 "End must be after the start."가 보입니다. 종일 이벤트는 끝나는 날짜를 유지합니다. Save는 시작 날짜의 자정부터 끝나는 날짜의 다음 날까지 기록하고, 그 날짜는 이벤트 자신의 시간대에 남습니다. 이벤트를 종일로 바꾸거나 끝을 다른 날로 옮기면 띠나 월의 막대로 갑니다. 종일을 끄면 켜기 전의 시각을 되돌리고, 이미 종일이었으면 여는 시각에서 한 슬롯으로 시작하며, 시간 레인으로 또는 그 날의 제목 사이로 돌아옵니다. 월은 그 이벤트를 그대로 나열합니다.
New event는 주와 일에서 새 이벤트를 위해 같은 대화상자를 엽니다. 버튼은 하나입니다. 빈 슬롯은 Tab 정지가 아닙니다. 날은 그리드에서 마지막으로 포커스를 가진 날입니다. 그 포커스가 이벤트였으면 새 이벤트는 그 이벤트가 끝날 때 시작하고 한 슬롯 동안 이어집니다. 그 끝이 그날의 닫는 시각에 너무 가까우면 슬롯은 닫는 시각에서 멈춥니다. 그 포커스가 날짜 제목이었거나 그리드에 포커스가 없었으면 새 이벤트는 화면에 있는 날의 첫 슬롯에서 시작합니다. 이벤트를 만드는 끌기나 클릭은 맞춰진 날, 시작, 끝으로 이 대화상자를 엽니다. 대화상자가 열려 있는 동안 미리보기는 그 날의 열에 남습니다. Save는 그 구간에 이벤트를 더합니다. 라이브 영역은 이벤트가 더해졌다고 말하며, 옮긴 경우에는 그렇게 말하지 않습니다. 포커스는 새 이벤트로 이동합니다. Cancel과 Escape는 아무것도 더하지 않고, 미리보기를 없애며, 포커스를 그 날의 제목으로 되돌립니다. 고치기를 취소하면 포커스는 그 이벤트로 돌아갑니다.
EventEditor가 그 대화상자입니다. 이벤트를 고치고, 새 이벤트를 채웁니다. eventFromPlace()는 맞춰진 자리의 저장 전 이벤트를 만들며, 제목은 비어 있습니다. 대화상자는 필드가 가리키는 이벤트를 onSave로 돌려줍니다.
반복
캘린더는 시리즈를 저장하고 발생을 그립니다. 시리즈는 제목, 시작, 끝, 시간대, 그리고 규칙입니다. 발생은 그 시리즈의 한 번입니다. 안정된 발생 id, 시리즈 id, 그리고 규칙이 만든 시작을 가집니다. 월, 주, 일은 시작이 보이는 창 안에 있는 발생을 읽습니다. 규칙은 읽지 않으므로, 이벤트가 반복하든 아니든 블록은 같은 방식으로 그려집니다. 반복하는 블록은 줄이 있으면 Repeats라고 씁니다. 시간이 있는 블록은 제목 옆에 반복 표시를 두어, 한 시간짜리 블록도 시간을 보여 줍니다. 접근 가능한 이름도 "repeats"를 말합니다. 블록의 이름은 그 말로 끝나고, 막대의 이름은 막대가 Repeats를 보여 주는 대로 제목 바로 뒤에 그 말을 둡니다.
Book club은 시리즈 하나입니다. 2026년 11월 15일 일요일 오후 3시에 시작하고 일요일마다 반복합니다. 11월 16일 주는 그 일요일을 보여 줍니다. 다음 주는 그다음 일요일을 보여 줍니다. 11월 15일보다 앞선 일요일에는 없습니다.
expandSeries()는 반열린 창 [start, end) 안의 발생을 돌려줍니다. 규칙은 반복 규칙의 작은 부분집합이고, Temporal로 계산하며 다른 라이브러리는 쓰지 않습니다. 매일, 매주, 매월, 매년의 빈도와, 간격, 횟수, until 시각, 그리고 매주 규칙의 요일을 지원합니다. 그 주의 시작은 월요일입니다. 반복 규칙이 그렇게 정하고, 페이지 로케일과는 별개입니다. 횟수는 시리즈의 시작부터 세고, 빼 둔 날짜도 그 횟수 하나를 씁니다. until 시각은 그 시각에 시작하는 발생을 포함합니다. 편집기의 On 끝은 고른 날의 마지막 순간이므로, 그날의 모임은 포함됩니다.
31일에 시작하는 매월 시리즈는 31일이 없는 달을 건너뜁니다. 그 달의 마지막 날로 옮기지 않습니다. 2월 29일에 시작하는 매년 시리즈는 윤년이 아닌 해를 건너뜁니다. 발생은 시계가 바뀔 때도 시리즈 시간대의 벽시계 시각을 유지하므로, 9:00 모임은 봄에 시계가 앞으로 간 뒤에도, 가을에 뒤로 간 뒤에도 9:00입니다. 길이도 그 벽시계 길이이므로, 9:00부터 10:00은 9:00부터 10:00으로 남습니다. 시계가 건너뛰는 벽시계 시각은 Temporal의 compatible 해석을 따릅니다.
EXDATE는 발생 하나를 뺍니다. 재정의는 발생 하나를 바꾸며, 규칙이 만든 시작으로 찾으므로, 옮기거나 고친 발생은 같은 인스턴스로 남습니다. 보기는 시리즈의 길이만큼 앞에서 시작하는 창을 요청하므로, 보이는 날보다 먼저 시작해서 그 날과 겹치는 발생도 포함됩니다.
New event는 Repeat가 none인 이 대화상자를 엽니다. Repeat는 none, daily, weekly, monthly, yearly 중 하나입니다. weekly는 로케일의 한 주 순서로 요일을 나열하고, 적어도 하루는 선택된 채로 둡니다. 요일을 하나만 고른 동안에는 그 요일이 날짜를 따라가므로, 시리즈는 반복하는 날에 시작합니다. 고른 요일에 시작 요일이 없으면 저장은 거절됩니다. Ends는 never, 횟수만큼 뒤, 또는 날짜입니다. Save는 시리즈를 저장합니다. 간격 필드는 없습니다. 호출하는 쪽이 시리즈에 간격을 넣지 않으면 간격은 1입니다. 반복 모듈은 예제 옆에 있습니다. 이 페이지는 그 파일을 붙이지 않고 설명합니다.
반복하는 발생의 Edit…나 Delete는 어떤 이벤트를 바꿀지 묻습니다. This event, This and following, All events입니다. 대화상자는 그 선택의 이름을 가지고, 설명이 그 설명이며, 첫 버튼이 포커스를 받습니다. Tab은 세 버튼을 지나갑니다. Enter는 포커스된 버튼을 실행합니다. Escape는 대화상자를 닫고 포커스를 그 발생으로 되돌립니다. This event는 재정의를 기록하고, 삭제이면 EXDATE를 기록합니다. This and following은 그 발생 직전에 시리즈를 끝내고, 양식에서 새 시리즈를 시작합니다. 횟수는 남은 수이고, 그다음 예외는 새 시리즈로 옮겨 갑니다. 고친 발생도 새 시작이 옮겨 간 만큼 같이 옮겨 가므로, 오후 1시로 고친 발생은 새 시리즈가 두 시간 늦으면 오후 3시가 됩니다. All events는 시리즈를 바꿉니다. 요일이 하나인 시리즈는 새 시작의 요일로 옮겨 갑니다. 예외는 벽시계 시각으로 옮겨 가므로, 지운 발생과 고친 발생은 시작이 일광 절약 시간을 지나도 같은 지역 시각에 남습니다. 마지막 발생을 지우면 시리즈도 사라집니다. 삭제 뒤에는 버튼이 사라지므로 포커스가 날짜 제목으로 옮겨 가고, 라이브 영역은 날짜를 말합니다. 발생 하나를 끌면 그 발생만 옮겨 가고, 블록 하나의 메뉴 이동도 같습니다. 둘 다 묻지 않습니다. 라이브 영역은 무엇이 바뀌었는지 말합니다. 더해졌는지, 이 이벤트를 바꿨는지, 이 이벤트와 그다음을 바꿨는지, 모든 이벤트를 바꿨는지, 삭제도 같은 말로 말합니다.
확장은 창만 걷습니다. 그 창에서 가장 이른 발생을 최대 400개 돌려줍니다. 1년의 매일 시리즈는 그 한도 아래입니다. 더 긴 창은 그 400개를 돌려주고 멈춥니다.
접근성을 지키는 조합
Todae가 맡는 것: ToggleGroup이 Month, Week, Day를 바꾸고 하나를 선택한 채로 둡니다. Button이 이전 또는 다음 주나 날로 옮기고, New event는 포인터 없이 새 이벤트의 편집기를 엽니다. Calendar가 월을 그리고, Calendar.Header로 포커스된 달을 이름 붙이며, Tab 정지를 하나로 유지하고, 각 날짜를 전체 날짜와 그 뒤에 이어지는 그 날의 제목으로 이름 붙입니다. ScrollArea가 주와 일을 스크롤하고 포커스된 블록의 링을 보이게 유지합니다. Text가 제목과 시각을 자릅니다. useLaneDrag가 포인터로 블록을 옮기고 크기를 바꾸고 만들며, 슬롯 격자에 맞추고, Escape로 취소합니다. Menu는 이동과 크기 변경의 키보드 경로입니다. Dialog와 Field가 제목과 시각을 정확히 고치고, 새 이벤트의 이름을 받습니다. 이벤트 버튼은 Button의 포커스 링을 씁니다. 라이브 영역은 변경 뒤의 시각을 읽고, 이벤트가 더해졌을 때 그렇게 말합니다.
직접 제공할 것: 각 이벤트의 제목. 시간대, 선택적인 둘째 시간대, 그리고 보이는 시간. 각 열과 그 안의 이벤트 목록에 이름을 붙이는 날짜 제목. 제목으로 시작하고, 화면에 있는 시간만이 아니라 이벤트의 실제 시작과 끝을 담은, 각 이벤트의 접근 가능한 이름. 막대의 이름은 제목으로 시작하고 전체 날짜 범위를 읽습니다. 시간 막대는 시작과 끝 시각도 읽습니다. 이 레시피의 15분 슬롯과, 끝이 시작보다 뒤인지 보는 검사.
주의할 점: 색으로만 이름 붙인 이벤트. 반복한다는 보이는 단서나 접근 가능한 단서가 없는 반복 이벤트. 달력의 시간대가 중요한데 브라우저 시간대로 적은 시각. 좁은 화면에서 페이지를 옆으로 스크롤하게 만드는 둘째 시간 열. 좁은 화면에서 스크롤할 곳이 없는 한 주의 열. 시간 그리드에 그린 종일 이벤트. 날마다 따로 칩으로 그린 여러 날 이벤트. 덮는 날 없이 이름 붙인 막대. 날짜 열 너비가 고정되어 나란히 놓인 블록이 글자 하나로 줄어드는 것. 월의 날짜 안에 둔 컨트롤. 거기서 누를 수 있는 것은 날짜 버튼뿐입니다. 키보드 경로가 없는 끌기. 키보드 경로가 없는 만들기 동작, 또는 빈 슬롯마다 둔 Tab 정지. 날을 넘기려는 크기 변경. 시작보다 뒤가 아닌 끝을, 아무 말도 없이 두는 것.
언제 쓰나요
쓸 때: 사람들이 한 시간대에서 한 달, 한 주, 또는 하루의 이벤트를 볼 때. 팀 캘린더가 그런 예입니다.
쓰지 않을 때: 레인을 넘는 충돌이 있는, 나란한 자원 레인의 항목 → Scheduler 레시피. 날짜나 시각을 하나 고르기 → Date and time picker 레시피.