# Calendar List

The calendars of the sidebar, grouped by account, each with a show and hide button.

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

```tsx title="src/examples/calendar-list-demo.tsx"
"use client";

import * as React from "react";

import { cn } from "@/lib/utils";
import { CalendarList } from "@/components/calendar-list";
import type { CalendarInfo } from "@/components/calendar-types";
import { CALENDAR_SIDEBAR_COLORS_CLASS } from "@/components/sidebar-overlay";
import { SidebarProvider } from "@/components/ui/sidebar";

/** Two accounts' calendars; a subscribed one gets a feed icon */
const CALENDARS: CalendarInfo[] = [
  {
    id: "you@example.com",
    name: "you@example.com",
    color: "red",
    email: "you@example.com",
  },
  {
    id: "personal",
    name: "Personal",
    color: "purple",
    email: "you@example.com",
  },
  {
    id: "holidays",
    name: "Holidays in Brazil",
    color: "green",
    email: "you@example.com",
    isSubscribed: true,
  },
  {
    id: "work",
    name: "Work",
    color: "blue",
    email: "you@work.example.com",
  },
];

export function CalendarListDemo() {
  const [hiddenCalendarIds, setHiddenCalendarIds] = React.useState<string[]>(
    [],
  );
  const hiddenNames = CALENDARS.filter((calendar) =>
    hiddenCalendarIds.includes(calendar.id),
  ).map((calendar) => calendar.name);

  return (
    // The list is built from shadcn/ui sidebar parts, which need a SidebarProvider
    <SidebarProvider className="min-h-0 h-full flex-col items-center justify-center gap-3">
      <div
        className={cn(
          CALENDAR_SIDEBAR_COLORS_CLASS,
          "w-60 rounded-lg bg-sidebar py-2 text-sidebar-foreground shadow-xs ring-1 ring-black/5 dark:ring-white/10",
        )}
      >
        <CalendarList
          calendars={CALENDARS}
          hiddenCalendarIds={hiddenCalendarIds}
          onHiddenCalendarIdsChange={setHiddenCalendarIds}
        />
      </div>
      <p className="text-sm text-muted-foreground">
        {hiddenNames.length > 0
          ? `Hidden: ${hiddenNames.join(", ")}`
          : "Hover a calendar and click its eye to hide it"}
      </p>
    </SidebarProvider>
  );
}
```

Calendar List is the list of calendars in the [Calendar Sidebar](/docs/components/calendar-sidebar). Calendars are grouped under their account's email, each account collapsible, each calendar with its color. Hover a calendar to show its eye button and hide it; a subscribed calendar, like a holidays feed, gets a feed icon.

## Installation

```bash
npx shadcn add @calendarcn/calendar-list
```

Or install it by hand:

1. Install the items it builds on: `@calendarcn/calendar-core`
2. Install the dependencies: `npm install lucide-react`
3. Add the shadcn/ui components it uses: `npx shadcn add collapsible sidebar`
4. Copy these files into your project, keeping the paths under your components, hooks and lib folders:
   - [`components/calendar-list.tsx`](https://github.com/vmnog/calendarcn/blob/main/src/components/calendar-list.tsx)

## Usage

The list lists the `calendars` of the surrounding [`CalendarProvider`](/docs/components/calendar-core), the same ones the event details' calendar picker offers. It's built from shadcn/ui's sidebar parts, so it needs a `SidebarProvider` around it.

```tsx
import { CalendarList } from "@/components/calendar-list";
import type { CalendarInfo } from "@/components/calendar-types";

const calendars: CalendarInfo[] = [
  {
    id: "personal",
    name: "Personal",
    color: "purple",
    email: "you@example.com",
  },
  {
    id: "holidays",
    name: "Holidays",
    color: "green",
    email: "you@example.com",
    isSubscribed: true,
  },
  { id: "work", name: "Work", color: "blue", email: "you@work.example.com" },
];

<CalendarProvider defaultEvents={events} calendars={calendars}>
  <SidebarProvider>
    <CalendarList />
  </SidebarProvider>
</CalendarProvider>;
```

Outside a provider, pass `calendars` yourself. Hiding a calendar only dims its row: read the hidden ids with `onHiddenCalendarIdsChange` to filter your events, or set them with `hiddenCalendarIds`.

```tsx
const [hidden, setHidden] = React.useState<string[]>([]);

<CalendarList
  calendars={calendars}
  hiddenCalendarIds={hidden}
  onHiddenCalendarIdsChange={setHidden}
/>;
```

## API Reference

### CalendarList

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `calendars` | `CalendarInfo[]` | – | Calendars to list, grouped under their account `email`. Defaults to the `calendars` of the nearest `CalendarProvider`. |
| `hiddenCalendarIds` | `string[]` | – | Ids of the hidden calendars (controlled). |
| `defaultHiddenCalendarIds` | `string[]` | `[]` | Ids of the calendars hidden at first (uncontrolled). |
| `onHiddenCalendarIdsChange` | `(hiddenCalendarIds: string[]) => void` | – | Called with the new hidden ids when a calendar is shown or hidden. |

### CalendarInfo

The list reads these fields of each calendar (from `@/components/calendar-types`):

| Field | Type | Description |
| --- | --- | --- |
| `id` (required) | `string` | What `hiddenCalendarIds` holds. |
| `name` (required) | `string` | Shown in the row. |
| `color` (required) | `EventColor` | The row's swatch. |
| `email` | `string` | Account the calendar is listed under. Calendars without one are listed under "Calendars". |
| `isSubscribed` | `boolean` | A calendar followed rather than owned, like a holidays feed. Its swatch gets a feed icon. |
