# Week View

A week, or any number of days, on the time grid.

Live example, interactive at https://calendarcn.xyz/docs/components/week-view. Its source:

```tsx title="src/examples/week-view-demo.tsx"
"use client";

import * as React from "react";

import { CalendarProvider } from "@/components/calendar-provider";
import { WeekView } from "@/components/week-view";
import { createSampleEvents } from "@/lib/sample-events";

export function WeekViewDemo() {
  // The provider reads its initial events once, so create them once
  const [initialEvents] = React.useState(() => createSampleEvents(new Date()));

  return (
    <CalendarProvider defaultEvents={initialEvents}>
      <WeekView scrollToHour={8} />
    </CalendarProvider>
  );
}
```

Week View shows several days side by side on the [Time Grid](/docs/components/time-grid): an all-day row on top, an hour grid below. It reads the date, events and selection from the surrounding [`CalendarProvider`](/docs/components/calendar-core), so it has almost no props of its own.

## Installation

```bash
npx shadcn add @calendarcn/week-view
```

Or install it by hand:

1. Install the items it builds on: `@calendarcn/time-grid`
2. Copy these files into your project, keeping the paths under your components, hooks and lib folders:
   - [`components/week-view.tsx`](https://github.com/vmnog/calendarcn/blob/main/src/components/week-view.tsx)

## Usage

```tsx
import { CalendarProvider } from "@/components/calendar-provider";
import { WeekView } from "@/components/week-view";

export function Schedule() {
  return (
    <CalendarProvider defaultEvents={events}>
      {/* Week View fills its parent's height */}
      <div className="h-[600px]">
        <WeekView scrollToHour={8} />
      </div>
    </CalendarProvider>
  );
}
```

Show fewer days with the provider's `numberOfDays` (or `defaultNumberOfDays`). Below 7, the columns start on the current day and previous/next move by that many days.

```tsx
<CalendarProvider defaultEvents={events} defaultNumberOfDays={3}>
  <WeekView />
</CalendarProvider>
```

## Features

- Drag an event to move it, including to another day. The grid scrolls when you reach its edge.
- Drag the top or bottom edge of an event to resize it.
- Double-click an empty slot to create an event, or press and drag to draw one, then type its title. Do the same in the all-day row for an all-day event. See [Creating events](/docs/components/calendar-core#creating-events).
- Click an event to open its details in a popover. Right-click it to change its color or calendar.
- Overlapping events sit side by side. All-day and multi-day events sit in the all-day row.
- Scroll sideways to move through days. Scrolling snaps to the nearest day.
- Touch: long-press an event to drag it, or empty space to create one.
- Move and resize from the keyboard. See [Keyboard & accessibility](/docs/components/calendar#keyboard--accessibility).

## API Reference

### WeekView

Everything else (date, events, number of days, hours, clock, locale) is set on [`CalendarProvider`](/docs/components/calendar-core#calendarprovider).

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `numberOfDays` | `number` | – | Number of day columns. Defaults to the provider's `numberOfDays`. When set, it fixes the column count, and prev/next step by it below 7 while the provider's `numberOfDays` is uncontrolled. |
| `showEventPopover` | `boolean` | `true` | Whether the selected event opens a details popover beside it. Turn it off when the details are shown elsewhere, such as a side panel. |
| `onEventPopoverExpand` | `() => void` | – | Called by the popover's expand button, e.g. to move the details into a side panel. The button is hidden when this is not set. |
| `scrollToHour` | `number` | – | Hour the grid scrolls to when it mounts, e.g. `8` for 8 AM or `8.5` for 8:30. Applied once and clamped to the provider's `dayStartHour`..`dayEndHour`. Unset starts at the top. |
| `className` | `string` | – | Classes for the root element. |
