{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "calendar-settings",
  "title": "Calendar Settings",
  "author": "vmnog <https://vmnog.com>",
  "description": "Notion Calendar's settings dialog: the calendar view, time format and theme, and each calendar account's calendars, with what your app can change.",
  "dependencies": [
    "lucide-react"
  ],
  "registryDependencies": [
    "@calendarcn/calendar-core",
    "button",
    "dialog",
    "switch"
  ],
  "files": [
    {
      "path": "src/components/calendar-settings.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { CalendarDays, Eye, EyeOff, Plus, Rss, Settings } from \"lucide-react\";\n\nimport { cn } from \"@/lib/utils\";\nimport { useCalendar, useCalendarConfig } from \"@/components/calendar-provider\";\nimport { Button } from \"@/components/ui/button\";\nimport {\n  Dialog,\n  DialogContent,\n  DialogDescription,\n  DialogTitle,\n} from \"@/components/ui/dialog\";\nimport { Switch } from \"@/components/ui/switch\";\nimport { groupCalendarsByEmail } from \"@/lib/event-utils\";\nimport type {\n  CalendarGroup,\n  CalendarTheme,\n  EventColor,\n  HourCycle,\n} from \"@/components/calendar-types\";\nimport type {\n  CalendarSettingsDialogProps,\n  CalendarSettingsSection,\n} from \"@/components/week-view-types\";\n\n/** Week view day counts the \"Number of days\" setting offers, as the view menu does */\nconst NUMBER_OF_DAYS_OPTIONS = [2, 3, 4, 5, 6, 7, 8, 9] as const;\n\n/** Theme choices, with Notion Calendar's labels */\nconst THEME_OPTIONS: { value: CalendarTheme; label: string }[] = [\n  { value: \"system\", label: \"Auto\" },\n  { value: \"light\", label: \"Light mode\" },\n  { value: \"dark\", label: \"Dark mode\" },\n];\n\n/** Clock choices, with Notion Calendar's labels and an example time */\nconst HOUR_CYCLE_OPTIONS: { value: HourCycle; label: string }[] = [\n  { value: 12, label: \"12-hour time (1:00 PM)\" },\n  { value: 24, label: \"24-hour time (13:00)\" },\n];\n\n/** Label of the calendars that have no account `email`, as in the calendar list */\nconst NO_ACCOUNT_LABEL = \"Calendars\";\n\n/** Swatch color of each calendar color, as in the calendar list */\nconst CALENDAR_SWATCH_CLASSES: Record<EventColor, string> = {\n  red: \"bg-event-red\",\n  orange: \"bg-event-orange\",\n  yellow: \"bg-event-yellow\",\n  green: \"bg-event-green\",\n  blue: \"bg-event-blue\",\n  purple: \"bg-event-purple\",\n  gray: \"bg-event-gray\",\n};\n\n/** A nav row: the selected one is filled */\nconst NAV_ITEM_CLASS =\n  \"flex w-auto shrink-0 items-center gap-2 rounded-sm px-2 py-1.5 text-left text-sm whitespace-nowrap text-foreground/80 hover:bg-accent hover:text-accent-foreground sm:w-full\";\n\n/** Small heading above a group of nav rows (hidden on phones, where the nav is one row) */\nconst NAV_HEADING_CLASS =\n  \"text-muted-foreground hidden px-2 pt-3 pb-1 text-xs first:pt-0 sm:block\";\n\n/** A native select, styled like the app's inputs */\nconst SELECT_CLASS =\n  \"border-input bg-background text-foreground h-8 rounded-md border px-2 text-sm outline-none focus-visible:ring-2 focus-visible:ring-ring/50\";\n\n/** Key of a pane: \"general\", or a calendar account's email (\"\" without one) */\ntype PaneKey = \"general\" | `account:${string}`;\n\n/** Pane key of a calendar group */\nfunction accountPaneKey(group: CalendarGroup): PaneKey {\n  return `account:${group.email ?? \"\"}`;\n}\n\n/** The pane a section opens on: General, or the first calendar account */\nfunction sectionPane(\n  section: CalendarSettingsSection,\n  groups: CalendarGroup[],\n): PaneKey {\n  if (section === \"calendars\" && groups.length > 0) {\n    return accountPaneKey(groups[0]);\n  }\n  return \"general\";\n}\n\n/**\n * Notion Calendar's settings, as a dialog: on the left, General and each\n * calendar account, then \"Add calendar account\"; on the right, the chosen\n * pane. General holds the calendar view settings (weekends, declined\n * events, week numbers, number of days) from the nearest `CalendarProvider`,\n * and the time format and theme when you pass their handlers. An account's\n * pane lists its calendars, with a button to show or hide each when you\n * pass `onHiddenCalendarIdsChange`. Each setting shows only when something\n * can change it, so the dialog fits what your app supports.\n */\nexport function CalendarSettingsDialog({\n  open,\n  onOpenChange,\n  defaultSection = \"general\",\n  theme,\n  onThemeChange,\n  onHourCycleChange,\n  hiddenCalendarIds = [],\n  onHiddenCalendarIdsChange,\n  onAddCalendarAccount,\n}: CalendarSettingsDialogProps) {\n  const { calendars } = useCalendarConfig();\n  const groups = React.useMemo(\n    () => groupCalendarsByEmail(calendars),\n    [calendars],\n  );\n  const [pane, setPane] = React.useState<PaneKey>(() =>\n    sectionPane(defaultSection, groups),\n  );\n\n  // Each opening starts on `defaultSection`\n  const [prevOpen, setPrevOpen] = React.useState(open);\n  if (open !== prevOpen) {\n    setPrevOpen(open);\n    if (open) setPane(sectionPane(defaultSection, groups));\n  }\n\n  const account = groups.find((group) => accountPaneKey(group) === pane);\n\n  return (\n    <Dialog open={open} onOpenChange={onOpenChange}>\n      <DialogContent\n        className=\"gap-0 overflow-hidden p-0 sm:max-w-[680px]\"\n        // The calendar's single-key shortcuts stay out while it's open;\n        // Escape and Tab still close and move through the dialog\n        onKeyDown={(e) => {\n          if (e.key !== \"Escape\" && e.key !== \"Tab\") e.stopPropagation();\n        }}\n      >\n        {/* Phones: a title row for the close button, above the nav's row */}\n        <DialogTitle className=\"px-4 pt-4 pb-3 text-base font-semibold sm:sr-only\">\n          Settings\n        </DialogTitle>\n        <DialogDescription className=\"sr-only\">\n          The calendar&apos;s view, time format and theme, and its calendar\n          accounts.\n        </DialogDescription>\n        {/* min-w-0: the nav's row scrolls instead of widening the dialog */}\n        <div className=\"flex h-[min(480px,85vh)] min-w-0 flex-col sm:flex-row\">\n          <nav\n            aria-label=\"Settings\"\n            className=\"bg-muted/40 flex shrink-0 gap-1 overflow-x-auto border-b p-2 sm:w-56 sm:flex-col sm:overflow-x-visible sm:overflow-y-auto sm:border-r sm:border-b-0\"\n          >\n            <p className={NAV_HEADING_CLASS}>Account</p>\n            <button\n              type=\"button\"\n              aria-current={pane === \"general\" ? \"page\" : undefined}\n              className={cn(\n                NAV_ITEM_CLASS,\n                pane === \"general\" && \"bg-accent text-accent-foreground\",\n              )}\n              onClick={() => setPane(\"general\")}\n            >\n              <Settings className=\"size-4 shrink-0\" />\n              General\n            </button>\n            <p className={NAV_HEADING_CLASS}>Calendar accounts</p>\n            {groups.map((group) => {\n              const key = accountPaneKey(group);\n              return (\n                <button\n                  key={key}\n                  type=\"button\"\n                  aria-current={pane === key ? \"page\" : undefined}\n                  className={cn(\n                    NAV_ITEM_CLASS,\n                    pane === key && \"bg-accent text-accent-foreground\",\n                  )}\n                  onClick={() => setPane(key)}\n                >\n                  <CalendarDays className=\"size-4 shrink-0\" />\n                  <span className=\"min-w-0 truncate\">\n                    {group.email ?? NO_ACCOUNT_LABEL}\n                  </span>\n                </button>\n              );\n            })}\n            {onAddCalendarAccount && (\n              <button\n                type=\"button\"\n                className={NAV_ITEM_CLASS}\n                onClick={onAddCalendarAccount}\n              >\n                <Plus className=\"size-4 shrink-0\" />\n                Add calendar account\n              </button>\n            )}\n          </nav>\n          <div className=\"min-h-0 flex-1 overflow-y-auto p-5\">\n            {account ? (\n              <AccountPane\n                group={account}\n                hiddenCalendarIds={hiddenCalendarIds}\n                onHiddenCalendarIdsChange={onHiddenCalendarIdsChange}\n              />\n            ) : (\n              <GeneralPane\n                theme={theme}\n                onThemeChange={onThemeChange}\n                onHourCycleChange={onHourCycleChange}\n              />\n            )}\n          </div>\n        </div>\n      </DialogContent>\n    </Dialog>\n  );\n}\n\n/** A titled block of the General pane, ruled off from the next */\nfunction SettingsSection({\n  title,\n  children,\n}: {\n  title: string;\n  children: React.ReactNode;\n}) {\n  return (\n    <section\n      aria-label={title}\n      className=\"flex flex-col gap-3 border-b pb-5 last:border-b-0 last:pb-0\"\n    >\n      <h3 className=\"text-foreground text-base font-semibold\">{title}</h3>\n      {children}\n    </section>\n  );\n}\n\n/** A switch with its label, the label first as in Notion Calendar's settings */\nfunction SettingsSwitch({\n  label,\n  checked,\n  onCheckedChange,\n}: {\n  label: string;\n  checked: boolean;\n  onCheckedChange: () => void;\n}) {\n  const id = React.useId();\n  return (\n    <div className=\"flex items-center gap-2 text-sm\">\n      <Switch\n        id={id}\n        aria-label={label}\n        checked={checked}\n        onCheckedChange={() => onCheckedChange()}\n      />\n      <label htmlFor={id}>{label}</label>\n    </div>\n  );\n}\n\n/**\n * General: the calendar view settings from the provider, and the time\n * format and theme when the app passes their handlers\n */\nfunction GeneralPane({\n  theme,\n  onThemeChange,\n  onHourCycleChange,\n}: Pick<\n  CalendarSettingsDialogProps,\n  \"theme\" | \"onThemeChange\" | \"onHourCycleChange\"\n>) {\n  const {\n    viewSettings,\n    numberOfDays,\n    toggleWeekends,\n    toggleDeclinedEvents,\n    toggleWeekNumbers,\n    setNumberOfDays,\n  } = useCalendar();\n  const { hourCycle } = useCalendarConfig();\n  const numberOfDaysId = React.useId();\n  const hourCycleId = React.useId();\n\n  return (\n    <div className=\"flex flex-col gap-5\">\n      <SettingsSection title=\"Calendar view\">\n        <SettingsSwitch\n          label=\"Weekends\"\n          checked={viewSettings.showWeekends}\n          onCheckedChange={toggleWeekends}\n        />\n        <SettingsSwitch\n          label=\"Declined events\"\n          checked={viewSettings.showDeclinedEvents}\n          onCheckedChange={toggleDeclinedEvents}\n        />\n        <SettingsSwitch\n          label=\"Week numbers\"\n          checked={viewSettings.showWeekNumbers}\n          onCheckedChange={toggleWeekNumbers}\n        />\n        <div className=\"flex flex-col gap-1.5 text-sm\">\n          <label htmlFor={numberOfDaysId}>Number of days in week view:</label>\n          <select\n            id={numberOfDaysId}\n            value={numberOfDays}\n            onChange={(e) => setNumberOfDays(Number(e.target.value))}\n            className={cn(SELECT_CLASS, \"w-40\")}\n          >\n            {NUMBER_OF_DAYS_OPTIONS.map((days) => (\n              <option key={days} value={days}>\n                {days} days\n              </option>\n            ))}\n          </select>\n        </div>\n        {/* \"Start week on:\" goes here once the provider's week start can change */}\n      </SettingsSection>\n      {onHourCycleChange && (\n        <SettingsSection title=\"Time format\">\n          <select\n            id={hourCycleId}\n            aria-label=\"Time format\"\n            value={hourCycle}\n            onChange={(e) =>\n              onHourCycleChange(Number(e.target.value) as HourCycle)\n            }\n            className={cn(SELECT_CLASS, \"w-56\")}\n          >\n            {HOUR_CYCLE_OPTIONS.map((option) => (\n              <option key={option.value} value={option.value}>\n                {option.label}\n              </option>\n            ))}\n          </select>\n        </SettingsSection>\n      )}\n      {/* Time zones go here once the calendar can show more than one */}\n      {onThemeChange && (\n        <SettingsSection title=\"Theme\">\n          <div\n            role=\"radiogroup\"\n            aria-label=\"Theme\"\n            className=\"flex flex-col gap-2\"\n          >\n            {THEME_OPTIONS.map((option) => (\n              <label\n                key={option.value}\n                className=\"flex items-center gap-2 text-sm\"\n              >\n                <input\n                  type=\"radio\"\n                  name=\"calendar-settings-theme\"\n                  value={option.value}\n                  checked={(theme ?? \"system\") === option.value}\n                  onChange={() => onThemeChange(option.value)}\n                  className=\"accent-primary size-4\"\n                />\n                {option.label}\n              </label>\n            ))}\n          </div>\n        </SettingsSection>\n      )}\n    </div>\n  );\n}\n\n/** A calendar account: its calendars, each with its color and a show or hide button */\nfunction AccountPane({\n  group,\n  hiddenCalendarIds,\n  onHiddenCalendarIdsChange,\n}: {\n  group: CalendarGroup;\n  hiddenCalendarIds: string[];\n  onHiddenCalendarIdsChange?: (hiddenCalendarIds: string[]) => void;\n}) {\n  const toggleVisibility = (calendarId: string) => {\n    onHiddenCalendarIdsChange?.(\n      hiddenCalendarIds.includes(calendarId)\n        ? hiddenCalendarIds.filter((id) => id !== calendarId)\n        : [...hiddenCalendarIds, calendarId],\n    );\n  };\n\n  return (\n    <div className=\"flex flex-col gap-5\">\n      <div className=\"flex items-center gap-3 border-b pb-4\">\n        <div className=\"bg-muted flex size-10 items-center justify-center rounded-md\">\n          <CalendarDays className=\"size-5\" />\n        </div>\n        <div className=\"min-w-0\">\n          <h3 className=\"text-foreground truncate text-base font-semibold\">\n            {group.email ?? NO_ACCOUNT_LABEL}\n          </h3>\n          <p className=\"text-muted-foreground text-sm\">\n            {group.calendars.length === 1\n              ? \"1 calendar\"\n              : `${group.calendars.length} calendars`}\n          </p>\n        </div>\n      </div>\n      <section aria-label=\"Calendars\" className=\"rounded-md border\">\n        <h4 className=\"text-muted-foreground border-b px-3 py-2 text-sm\">\n          Calendars\n        </h4>\n        <ul>\n          {group.calendars.map((calendar) => {\n            const isVisible = !hiddenCalendarIds.includes(calendar.id);\n            return (\n              <li\n                key={calendar.id}\n                className=\"flex items-center gap-2 px-3 py-2 text-sm\"\n              >\n                <span\n                  className={cn(\n                    \"flex size-3 shrink-0 items-center justify-center rounded-xs\",\n                    CALENDAR_SWATCH_CLASSES[calendar.color],\n                    !isVisible && \"opacity-40\",\n                  )}\n                >\n                  {calendar.isSubscribed && (\n                    <Rss className=\"size-2 text-white\" />\n                  )}\n                </span>\n                <span\n                  className={cn(\n                    \"min-w-0 flex-1 truncate\",\n                    !isVisible && \"opacity-50\",\n                  )}\n                >\n                  {calendar.name}\n                </span>\n                {onHiddenCalendarIdsChange && (\n                  <Button\n                    variant=\"ghost\"\n                    size=\"icon\"\n                    className=\"size-7\"\n                    aria-label={`${isVisible ? \"Hide\" : \"Show\"} ${calendar.name}`}\n                    onClick={() => toggleVisibility(calendar.id)}\n                  >\n                    {isVisible ? (\n                      <Eye className=\"size-4\" />\n                    ) : (\n                      <EyeOff className=\"size-4\" />\n                    )}\n                  </Button>\n                )}\n              </li>\n            );\n          })}\n        </ul>\n      </section>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "categories": [
    "calendar",
    "settings"
  ],
  "type": "registry:component"
}