# Calendar Settings

Notion Calendar's settings dialog, with the calendar view, time format, theme and calendar accounts your app can change.

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

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

import * as React from "react";

import { CalendarProvider } from "@/components/calendar-provider";
import { CalendarSettingsDialog } from "@/components/calendar-settings";
import type { HourCycle } from "@/components/calendar-types";
import { Button } from "@/components/ui/button";
import { MonthView } from "@/components/month-view";
import { createSampleEvents } from "@/lib/sample-events";

export function CalendarSettingsDemo() {
  // The provider reads its initial events once, so create them once
  const [initialEvents] = React.useState(() => createSampleEvents(new Date()));
  const [open, setOpen] = React.useState(false);
  const [hourCycle, setHourCycle] = React.useState<HourCycle>(12);

  return (
    <CalendarProvider defaultEvents={initialEvents} hourCycle={hourCycle}>
      <div className="flex h-full flex-col">
        <div className="flex h-12 shrink-0 items-center border-b px-3">
          <Button variant="outline" size="sm" onClick={() => setOpen(true)}>
            Open settings
          </Button>
        </div>
        <div className="flex min-h-0 flex-1 flex-col">
          <MonthView />
        </div>
      </div>
      {/* Weekends and week numbers come from the provider; the time format
          shows because there's a handler for it */}
      <CalendarSettingsDialog
        open={open}
        onOpenChange={setOpen}
        onHourCycleChange={setHourCycle}
      />
    </CalendarProvider>
  );
}
```

Calendar Settings is the live demo's settings dialog, laid out as Notion Calendar's: on the left, General and each calendar account, then "Add calendar account"; on the right, the pane you picked. Open it in the [live demo](/) with `⌘,` or "Go to settings" in the command menu.

- **General**: the calendar view (weekends, declined events, week numbers, and the number of days in week view), the time format and the theme.
- **A calendar account**: its calendars, each with its color and a button to show or hide it.

Each setting shows only when something can change it, so the dialog fits what your app supports. The calendar view settings come from the surrounding [`CalendarProvider`](/docs/components/calendar-core); the time format, the theme, hiding calendars and "Add calendar account" show when you pass their handlers.

## Installation

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

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 button dialog switch`
4. Copy these files into your project, keeping the paths under your components, hooks and lib folders:
   - [`components/calendar-settings.tsx`](https://github.com/vmnog/calendarcn/blob/main/src/components/calendar-settings.tsx)

## Usage

Render it inside your `CalendarProvider`, and open it from a button, a command or `⌘,`:

```tsx
import { useTheme } from "next-themes";

import { useCalendarShortcuts } from "@/hooks/use-calendar-shortcuts";
import { CalendarSettingsDialog } from "@/components/calendar-settings";

function Settings({ hourCycle, onHourCycleChange }) {
  const [open, setOpen] = React.useState(false);
  const { theme, setTheme } = useTheme();

  // ⌘, (Ctrl+, elsewhere) opens it, even from a text field
  useCalendarShortcuts({ onOpenSettings: () => setOpen(true) });

  return (
    <CalendarSettingsDialog
      open={open}
      onOpenChange={setOpen}
      theme={theme}
      onThemeChange={setTheme}
      onHourCycleChange={onHourCycleChange}
    />
  );
}
```

The time format sets the provider's `hourCycle`, so keep it in state and pass it to `CalendarProvider`. The dialog reads the current one from the provider.

### Calendar accounts

The accounts are the provider's `calendars`, grouped by their `email` as in the [Calendar List](/docs/components/calendar-list). Pass the hidden calendars and their handler to show or hide each from its account's pane, and `onAddCalendarAccount` to list "Add calendar account":

```tsx
<CalendarSettingsDialog
  open={open}
  onOpenChange={setOpen}
  defaultSection="calendars"
  hiddenCalendarIds={hiddenCalendarIds}
  onHiddenCalendarIdsChange={setHiddenCalendarIds}
  onAddCalendarAccount={connectAccount}
/>
```

`defaultSection="calendars"` opens it on the first account, as "Manage calendar accounts" does in the live demo. [Calendar App](/docs/components/calendar-app) renders the dialog for you with `settingsDialog`.

## API Reference

### CalendarSettingsDialog

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `open` | `boolean` | – | Whether the dialog is open. |
| `onOpenChange` | `(open: boolean) => void` | – | Called when the dialog opens or closes: Escape, a click outside or the close button. |
| `defaultSection` | `"general" \| "calendars"` | `"general"` | The pane the dialog opens on, each time it opens: General, or the first calendar account. |
| `theme` | `"system" \| "light" \| "dark"` | `"system"` | The current theme, checked in the Theme setting. |
| `onThemeChange` | `(theme: "system" \| "light" \| "dark") => void` | – | Adds the Theme setting: Auto, Light mode and Dark mode. |
| `onHourCycleChange` | `(hourCycle: 12 \| 24) => void` | – | Adds the Time format setting. The current clock is the provider's `hourCycle`. |
| `hiddenCalendarIds` | `string[]` | – | Ids of the hidden calendars. |
| `onHiddenCalendarIdsChange` | `(hiddenCalendarIds: string[]) => void` | – | Adds a show or hide button to each calendar in an account's pane. |
| `onAddCalendarAccount` | `() => void` | – | Adds "Add calendar account" under the accounts. |
