# Week calendar

> A scheduling week you hold in the hand: drag a slot into an event while its times roll, and anything it lands on eases aside.

Most web calendars drag a copy of the event around, swap its times in jumps and only sort out overlaps once you let go. Here you drag the event itself. Press on an empty slot and drag: a block grows from where you pressed, gliding from step to step while "9:15 – 10:30" rolls, and you name it in the block when you let go. Drag an event and it keeps the point you grabbed it by, crosses days, and whatever it lands on eases into side-by-side columns while you are still holding, with the clash spelled out in amber. Edges resize. Near the top or bottom the hours scroll themselves. ‹ and › bring the next days in from the side you went toward while the dates roll and the range morphs, and a hairline marks now. It fits seven days wide, three in a narrow card and one on a phone, where you hold for a moment to pick up so a swipe still scrolls. Saves are optimistic: one that fails puts the event back with a shake and says so.

## Install

```bash
npx shadcn@latest add @hairline/week-calendar
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: `lucide-react`
- Hairline UI components it needs (the CLI installs them too): `number-roll`, `text-morph`
- Docs and live preview: https://hairlineui.com/ui/week-calendar
- Registry item (source): https://hairlineui.com/r/week-calendar.json

## Usage

```tsx
import { WeekCalendar, type CalendarEvent } from "@/components/week-calendar";

const [events, setEvents] = useState<CalendarEvent[]>(initial);

<WeekCalendar
  events={events}
  onEventsChange={setEvents}
  onCreate={(event) => api.events.create(event)}
  onUpdate={(event) => api.events.update(event.id, event)}
/>
```

## Book time on a shared calendar

The calendar is optimistic: onEventsChange gets every change at once, and the callbacks run your saves. Throw from one to put the event back. Resolve onCreate with the stored event to swap in its real id; mark another calendar's time busy so it shows but can't be moved.

```tsx
"use client";
import { useState } from "react";
import { WeekCalendar, type CalendarEvent } from "@/components/week-calendar";

export function TeamCalendar({ initial, busy }: { initial: CalendarEvent[]; busy: CalendarEvent[] }) {
  const [events, setEvents] = useState<CalendarEvent[]>([...initial, ...busy.map((b) => ({ ...b, busy: true }))]);

  const send = async (url: string, method: string, body?: unknown) => {
    const res = await fetch(url, { method, headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
    if (!res.ok) throw new Error(await res.text());
    return res.status === 204 ? undefined : res.json();
  };

  return (
    <WeekCalendar
      events={events}
      onEventsChange={setEvents}
      startHour={8}
      endHour={19}
      onCreate={async (event) => {
        const saved = await send("/api/events", "POST", event);
        return { ...saved, start: new Date(saved.start), end: new Date(saved.end) };
      }}
      onUpdate={(event) => send(`/api/events/${event.id}`, "PATCH", { start: event.start, end: event.end })}
      onDelete={(event) => send(`/api/events/${event.id}`, "DELETE")}
    />
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `events` | `CalendarEvent[]` | – | Controlled events: { id, title, start, end, tone?, busy? }. Events on days in view are shown; tone picks one of the five chart colours. |
| `defaultEvents` | `CalendarEvent[]` | – | Starting events when uncontrolled. |
| `onEventsChange` | `(events: CalendarEvent[]) => void` | – | Every change at once, before any save settles. |
| `onCreate` | `(event) => Promise<CalendarEvent \| void>` | – | A new event once it's named. Resolve with the stored event to take its id; throw to take it back. |
| `onUpdate` | `(event, previous) => Promise<void>` | – | An event moved or resized. Throw to put it back. |
| `onDelete` | `(event) => Promise<void>` | – | Delete or Backspace on an event. Throw to bring it back. |
| `date` | `Date` | – | Controlled: any day in the range to show. |
| `defaultDate` | `Date` | `today` | Starting day when uncontrolled. |
| `onDateChange` | `(firstDay: Date) => void` | – | After ‹ › or Today. |
| `days` | `1 \| 3 \| 5 \| 7 \| "auto"` | `"auto"` | "auto" shows 7 from 560px wide, 3 from 300px, otherwise 1. 5 is Monday to Friday. |
| `weekStartsOn` | `0 \| 1` | `1` | Sunday or Monday. |
| `startHour / endHour` | `number` | `7 / 21` | The hours shown. |
| `step` | `number` | `15` | Snap in minutes. |
| `defaultDuration` | `number` | `30` | Length of an event made by a click or a hold. |
| `hourHeight` | `number` | `44` | Pixels per hour. |
| `height` | `number` | `420` | Height of the scrolling hours. |
| `locale` | `string` | – | For weekday, date and time formats (12 or 24 hour follows it). |

## States

- **create**: Pressing an empty slot and dragging grows a block from the slot you pressed, snapped to the step (15 minutes); its top, height and times ease 200ms between steps. A click makes a 30-minute event. Letting go opens a name field in the block; Enter keeps it, Escape folds it away.
- **move**: After 4px (or a 360ms hold on touch) the event lifts: a stronger ring, 1.015 scale, above the rest. It keeps the offset you grabbed it by, snaps in time, jumps day to day, and its times roll in the direction they changed.
- **overlap**: Every step re-solves the columns for each day with the held event in them, so events it lands on narrow and slide aside (260ms) before you let go; a tall event says "Overlaps …" in amber while it's held there.
- **resize**: The top and bottom 6px of an event resize it, never shorter than one step and never past the visible hours.
- **scroll**: Within 40px of the top or bottom of the hours, they scroll themselves at up to 14px a frame, faster the closer you are, and the held event follows the grid.
- **range**: ‹ › and Today slide the events in 32px from the side you went toward through a blur (460ms); day numbers roll that way and the range morphs.
- **saving**: A new event shows a sweep of light while onCreate runs. A move, resize or delete that throws is put back, shakes, and a line under the header says what happened for 3.2s.

## What it handles

Most web calendars drag a copy of the event around, swap its times in jumps and only sort out overlaps once you let go. Here you drag the event itself, and the calendar reacts while you are still holding it.

- **Drag to create**: Press an empty slot and drag. A block grows with its times rolling, and you name it when you let go. A click makes a 30-minute event.
- **Move across days**: An event keeps the point you grabbed it by, snaps to 15 minutes and jumps from day to day.
- **Overlaps resolve while you hold**: Events it lands on ease aside into columns, and the clash is spelled out in amber.
- **Resize and auto-scroll**: Drag an edge to resize. Near the top or bottom, the hours scroll themselves.
- **Fits any width**: Seven days wide, three in a narrow card, one on a phone, where you hold to pick up so a swipe still scrolls.
- **Saves can fail**: Changes show at once. If onUpdate or onCreate throws, the event goes back with a shake and a line says what happened.

## Replaces

a calendar library, drag to create, drag and resize, overlap layout, time snapping, optimistic saves with rollback

## Where it fits

- **Team scheduling**: Plan a week of meetings and see clashes as they happen.
- **Appointment booking**: Clinics, salons and studios that book time in slots.
- **Planners and time-boxing**: Drag blocks of focus time into a week.
- **Room and resource booking**: Reserve a space, with other bookings shown as busy.

## Notes

- The hours are a focusable group: arrow keys move a slot outline, Enter adds an event there, Page Up and Page Down change the range. Tab reaches the events.
- On an event, arrow keys move it by a step or a day, Shift with up or down changes its length, Enter renames it and Delete removes it. Each change is announced with the new day and times, and any clash.
- Each event is labelled with its title, day and times; busy time is a note, so it's read but never focused.
- Gestures report a change of step, not every pointer move, so a drag re-renders a handful of times a second; events move with eased top, left, width and height on their own layer while held.
- On touch the hours scroll as usual; holding 360ms picks up (with a short vibration where supported), and only then does the page stop scrolling under the finger.
- Reduced motion moves events in place, without glides, slides or shakes; rings, colours and announcements still say what happened.

License: MIT (https://hairlineui.com/license#components)
