# Meet with people

Notion Calendar's F and P dialog, to meet with someone or show a teammate's calendar.

Live example, interactive at https://calendarcn.xyz/docs/guides/meet-with-people. Its source:

```tsx title="src/examples/people-picker-demo.tsx"
"use client";

import * as React from "react";

import { CalendarProvider } from "@/components/calendar-provider";
import { PeoplePicker } from "@/components/people-picker";
import { WeekView } from "@/components/week-view";
import { Button } from "@/components/ui/button";
import type { PeoplePickerMode } from "@/components/calendar-types";
import { DEMO_CONTACTS, generateContactEvents } from "@/lib/mock-events";
import { createSampleEvents } from "@/lib/sample-events";

export function PeoplePickerDemo() {
  // The provider reads its initial events once, so create them once
  const [initialEvents] = React.useState(() => createSampleEvents(new Date()));
  // Other people's events, by email: the sample data's
  const [contactEvents] = React.useState(() => generateContactEvents());
  const [open, setOpen] = React.useState(false);
  const [mode, setMode] = React.useState<PeoplePickerMode>("meet-with");

  const openPicker = (nextMode: PeoplePickerMode) => {
    setMode(nextMode);
    setOpen(true);
  };

  return (
    <CalendarProvider
      defaultEvents={initialEvents}
      // The people the picker searches
      contacts={DEMO_CONTACTS}
      contactEvents={contactEvents}
      defaultNumberOfDays={5}
    >
      <div className="flex h-[600px] flex-col">
        <div className="flex min-h-12 shrink-0 flex-wrap items-center gap-2 border-b px-3 py-2">
          <Button
            variant="outline"
            size="sm"
            onClick={() => openPicker("meet-with")}
          >
            Meet with...
          </Button>
          <Button
            variant="outline"
            size="sm"
            onClick={() => openPicker("teammate")}
          >
            Show teammate calendar...
          </Button>
        </div>
        <div className="min-h-0 flex-1">
          <WeekView scrollToHour={8} />
        </div>
      </div>
      {/* Buttons here. In your app, F and P can open it too:
          useCalendarShortcuts({ onMeetWith, onShowTeammateCalendar }) */}
      <PeoplePicker open={open} onOpenChange={setOpen} mode={mode} />
    </CalendarProvider>
  );
}
```

`PeoplePicker` is the dialog the [live demo](/) opens with `F` ("Meet with...") and `P` ("Show teammate calendar..."), and from the same commands in the [Command Menu](/docs/components/command-menu). It searches the provider's `contacts` by name or email, and offers a typed address no contact has. Picking someone meets with them or shows their calendar through [`useCalendarPeople()`](/docs/components/calendar-core#usecalendarpeople), so it goes inside `CalendarProvider`. In the preview, each button opens it in one of its two modes, with the [sample data](/docs/reference/sample-data)'s people.

## Installation

```bash
npx shadcn@latest add @calendarcn/people-picker
```

Or install it by hand:

1. Install the items it builds on: `@calendarcn/calendar-core`
2. Add the shadcn/ui components it uses: `npx shadcn@latest add command dialog kbd`
3. Copy these files into your project, keeping the paths under your components, hooks and lib folders:
   - [`components/people-picker.tsx`](https://github.com/vmnog/calendarcn/blob/main/src/components/people-picker.tsx)

## Usage

```tsx
import { useCalendarShortcuts } from "@/hooks/use-calendar-shortcuts";
import { CalendarProvider } from "@/components/calendar-provider";
import { PeoplePicker } from "@/components/people-picker";
import { WeekView } from "@/components/week-view";
import type { PeoplePickerMode } from "@/components/calendar-types";

export function Schedule() {
  return (
    <CalendarProvider
      defaultEvents={events}
      contacts={contacts}
      contactEvents={contactEvents}
    >
      <CalendarWithPeople />
    </CalendarProvider>
  );
}

function CalendarWithPeople() {
  const [open, setOpen] = React.useState(false);
  const [mode, setMode] = React.useState<PeoplePickerMode>("meet-with");
  const openPicker = (next: PeoplePickerMode) => {
    setMode(next);
    setOpen(true);
  };

  // F and P open it, as in Notion Calendar
  useCalendarShortcuts({
    onMeetWith: () => openPicker("meet-with"),
    onShowTeammateCalendar: () => openPicker("teammate"),
  });

  return (
    <>
      <WeekView />
      <PeoplePicker open={open} onOpenChange={setOpen} mode={mode} />
    </>
  );
}
```

- `"meet-with"` adds the person to the meeting: their events show beside yours and the times you're all free are highlighted. An event you create meanwhile invites them. See [Meeting with people](/docs/components/calendar-core#meeting-with-people).
- `"teammate"` shows their calendar beside yours, without a meeting.

The people already added are left out of the list. The arrow keys move through it, `Enter` picks, and `Esc` closes the picker, which opens empty the next time. The [Calendar Sidebar](/docs/components/calendar-sidebar) lists who was picked, each removable.

## Examples

### Your own list of who was picked

The people are provider state, so a list of your own stays in sync with the picker: here each person in the meeting is a button that takes them out.

Live example, interactive at https://calendarcn.xyz/docs/guides/meet-with-people. Its source:

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

import * as React from "react";
import { XIcon } from "lucide-react";

import { useCalendarPeople } from "@/components/calendar-people";
import { CalendarProvider } from "@/components/calendar-provider";
import { PeoplePicker } from "@/components/people-picker";
import { WeekView } from "@/components/week-view";
import { Button } from "@/components/ui/button";
import { DEMO_CONTACTS, generateContactEvents } from "@/lib/mock-events";
import { createSampleEvents } from "@/lib/sample-events";

export function PeoplePickerListDemo() {
  // The provider reads its initial events once, so create them once
  const [initialEvents] = React.useState(() => createSampleEvents(new Date()));
  // Other people's events, by email: the sample data's
  const [contactEvents] = React.useState(() => generateContactEvents());

  return (
    <CalendarProvider
      defaultEvents={initialEvents}
      contacts={DEMO_CONTACTS}
      contactEvents={contactEvents}
      // Start in a meeting with Alex
      defaultMeetWith={["alex@example.com"]}
      defaultNumberOfDays={5}
    >
      <div className="flex h-[600px] flex-col">
        <MeetingBar />
        <div className="min-h-0 flex-1">
          <WeekView scrollToHour={8} />
        </div>
      </div>
    </CalendarProvider>
  );
}

/** Who the meeting is with, each removable: your own list, without a sidebar */
function MeetingBar() {
  const [open, setOpen] = React.useState(false);
  // The people are provider state, so the picker and this list stay in sync
  const { meetWith, contacts, removeMeetWith } = useCalendarPeople();

  return (
    <div className="flex h-12 shrink-0 items-center gap-2 overflow-x-auto border-b px-3">
      <Button variant="outline" size="sm" onClick={() => setOpen(true)}>
        Meet with...
      </Button>
      {meetWith.map((email) => (
        <Button
          key={email}
          variant="secondary"
          size="sm"
          onClick={() => removeMeetWith(email)}
        >
          {contacts.find((contact) => contact.email === email)?.name ?? email}
          <XIcon />
          <span className="sr-only">Stop meeting with them</span>
        </Button>
      ))}
      {/* The people already in the meeting are left out of its list */}
      <PeoplePicker open={open} onOpenChange={setOpen} mode="meet-with" />
    </div>
  );
}
```

### Teammates, loaded as they're picked

Control `teammates` to know who was picked, and load their events in `onTeammatesChange`.

Live example, interactive at https://calendarcn.xyz/docs/guides/meet-with-people. Its source:

```tsx title="src/examples/people-picker-teammates-demo.tsx"
"use client";

import * as React from "react";

import { CalendarProvider } from "@/components/calendar-provider";
import { PeoplePicker } from "@/components/people-picker";
import { WeekView } from "@/components/week-view";
import { Button } from "@/components/ui/button";
import type { DetailedCalendarEvent } from "@/components/calendar-types";
import { DEMO_CONTACTS, generateContactEvents } from "@/lib/mock-events";
import { createSampleEvents } from "@/lib/sample-events";

export function PeoplePickerTeammatesDemo() {
  // The provider reads its initial events once, so create them once
  const [initialEvents] = React.useState(() => createSampleEvents(new Date()));
  const [open, setOpen] = React.useState(false);
  // Your app owns whose calendars show: the picker reports each change
  const [teammates, setTeammates] = React.useState<string[]>([]);
  // Their events by email, loaded as people are added
  const [contactEvents, setContactEvents] = React.useState<
    Record<string, DetailedCalendarEvent[]>
  >({});

  const handleTeammatesChange = (emails: string[]) => {
    setTeammates(emails);
    // Stands in for a request to your calendar API for these people's events
    const everyone = generateContactEvents();
    setContactEvents(
      Object.fromEntries(emails.map((email) => [email, everyone[email] ?? []])),
    );
  };

  return (
    <CalendarProvider
      defaultEvents={initialEvents}
      contacts={DEMO_CONTACTS}
      teammates={teammates}
      onTeammatesChange={handleTeammatesChange}
      contactEvents={contactEvents}
      defaultNumberOfDays={5}
    >
      <div className="flex h-[600px] flex-col">
        <div className="flex h-12 shrink-0 items-center gap-3 border-b px-3">
          <Button variant="outline" size="sm" onClick={() => setOpen(true)}>
            Show teammate calendar...
          </Button>
          <p className="min-w-0 truncate text-sm text-muted-foreground">
            {teammates.length > 0 ? teammates.join(", ") : "Nobody yet"}
          </p>
        </div>
        <div className="min-h-0 flex-1">
          <WeekView scrollToHour={8} />
        </div>
      </div>
      {/* "teammate" shows their calendar, without a meeting */}
      <PeoplePicker open={open} onOpenChange={setOpen} mode="teammate" />
    </CalendarProvider>
  );
}
```

## API Reference

### PeoplePicker

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `open` (required) | `boolean` | – | Whether the picker is open. |
| `onOpenChange` (required) | `(open: boolean) => void` | – | Called when the picker opens or closes: on Escape, a click outside, or once someone is picked. |
| `mode` | `"meet-with" \| "teammate"` | `"meet-with"` | Meet with the picked person, or show their calendar. Also sets the placeholder: "Meet with..." or "Show teammate calendar...". |
