# Share Dialog (/docs/agent-ui/components/share-dialog)

Use a share dialog to put a **Share** button beside a chat. Its dialog chooses between **Only you** and **Anyone with the link**, and shows the link you make.

<Example caption="Press Share, choose Anyone with the link, then press Create link. Press Copy link to copy it, or Stop sharing to make the chat private again. Press Done or Escape to close the dialog.">
  <ShareDialogExample />

  ```tsx tab="React"
  "use client";
  import { useState } from "react";
  import { ShareDialog } from "@/components/ui/share-dialog";

  const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

  export default function ShareDialogExample() {
    const [visibility, setVisibility] = useState<"private" | "link">("private");
    const [link, setLink] = useState<string>();

    return (
      <ShareDialog
        visibility={visibility}
        onVisibilityChange={setVisibility}
        link={link}
        onCreateLink={async () => {
          await wait(800);
          setLink("https://example.com/share/billing-export-retries");
        }}
        onRevoke={async () => {
          await wait(400);
          setLink(undefined);
          setVisibility("private");
        }}
      />
    );
  }
  ```

  ```vue tab="Vue"
  <script setup lang="ts">
  import { ref } from "vue";
  import { ShareDialog } from "@/components/ui/vue/share-dialog";

  const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

  const visibility = ref<"private" | "link">("private");
  const link = ref<string>();

  async function createLink() {
    await wait(800);
    link.value = "https://example.com/share/billing-export-retries";
  }

  async function revoke() {
    await wait(400);
    link.value = undefined;
    visibility.value = "private";
  }
  </script>

  <template>
    <ShareDialog v-model:visibility="visibility" :link="link" :on-create-link="createLink" :on-revoke="revoke" />
  </template>
  ```

  ```svelte tab="Svelte"
  <script lang="ts">
    import { ShareDialog } from "@/components/ui/svelte/share-dialog/index.js";

    const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

    let visibility: "private" | "link" = $state("private");
    let link: string | undefined = $state();
  </script>

  <ShareDialog
    bind:visibility
    {link}
    onCreateLink={async () => {
      await wait(800);
      link = "https://example.com/share/billing-export-retries";
    }}
    onRevoke={async () => {
      await wait(400);
      link = undefined;
      visibility = "private";
    }}
  />
  ```

  ```tsx tab="Solid"
  import { createSignal } from "solid-js";
  import { ShareDialog } from "@/components/ui/solid/share-dialog";

  const wait = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

  export default function ShareDialogExample() {
    const [visibility, setVisibility] = createSignal<"private" | "link">("private");
    const [link, setLink] = createSignal<string>();

    return (
      <ShareDialog
        visibility={visibility()}
        onVisibilityChange={setVisibility}
        link={link()}
        onCreateLink={async () => {
          await wait(800);
          setLink("https://example.com/share/billing-export-retries");
        }}
        onRevoke={async () => {
          await wait(400);
          setLink(undefined);
          setVisibility("private");
        }}
      />
    );
  }
  ```
</Example>

## Installation [#installation]

```bash tab="React"
npx shadcn@latest add @kind-ui/share-dialog
```

```bash tab="Web Components"
npx shadcn@latest add @kind-ui/share-dialog-web
```

```bash tab="Vue"
npx shadcn@latest add @kind-ui/share-dialog-vue
```

```bash tab="Svelte"
npx shadcn@latest add @kind-ui/share-dialog-svelte
```

```bash tab="Solid"
npx shadcn@latest add @kind-ui/share-dialog-solid
```

## Composition [#composition]

```text
ShareDialog
├── ShareDialogTrigger
├── ShareDialogBackdrop
└── ShareDialogContent
    ├── ShareDialogTitle
    ├── ShareDialogDescription
    ├── ShareDialogVisibility
    ├── ShareDialogCreate
    ├── ShareDialogLink
    ├── ShareDialogCopy
    ├── ShareDialogError
    ├── ShareDialogRevoke
    └── ShareDialogClose
```

`ShareDialogTrigger` is the **Share** button, named by `label`. `ShareDialogContent` is a modal dialog over `ShareDialogBackdrop`, titled by `dialogLabel` ("Share chat") and described by `ShareDialogDescription`, which says what the choice means. `ShareDialogVisibility` is a UI SDK Segmented Control with the two choices, and `ShareDialogLink` the link in a read-only UI SDK Input. `ShareDialogCreate`, `ShareDialogCopy` and `ShareDialogRevoke` are **Create link**, **Copy link** and **Stop sharing**, `ShareDialogError` says what went wrong, and `ShareDialogClose` is **Done**. To arrange the parts yourself, pass them as children of `ShareDialog`.

## Usage [#usage]

### Who Can Open the Chat [#who-can-open-the-chat]

A chat starts private. Pass `defaultVisibility`, or `visibility` with `onVisibilityChange`, to set it yourself. Choosing **Anyone with the link** only changes `visibility`: nothing is shared until there's a link.

### Making the Link [#making-the-link]

The component never builds a URL. While `visibility` is `"link"`, there's no `link` and `onCreateLink` is set, the dialog shows **Create link**. Make the link in `onCreateLink`, on your server, and pass it back as `link`:

```tsx
<ShareDialog
  visibility={visibility}
  onVisibilityChange={setVisibility}
  link={chat.shareUrl}
  onCreateLink={() => shareChat(chat.id)}
  onRevoke={() => unshareChat(chat.id)}
/>
```

Return a promise to keep **Create link** pending until it settles: it reads **Creating link** beside a running mark. If it rejects, the dialog shows "Couldn't create a link", tells screen readers, and offers **Create link** again.

Once there's a `link`, the dialog shows it with **Copy link**, which copies it and reads **Copied** for 2 seconds. `onCopy` runs first, and `details.cancel()` keeps it from writing to the clipboard.

### Stop Sharing [#stop-sharing]

**Stop sharing** shows while there's a `link` and `onRevoke` is set. Revoke the link in `onRevoke`, then clear `link`, and usually set `visibility` back to `"private"`, as the example does. A returned promise keeps it pending, reading **Stopping**, and a rejection shows and announces "Couldn't stop sharing".

### Presentation and Motion [#presentation-and-motion]

With `presentation="auto"`, the default, the dialog is centered on the page, and a bottom sheet on small touch screens. Pass `"dialog"` or `"sheet"` to choose one. Escape, **Done** or a press outside closes it, and focus returns to **Share**. Pass `defaultOpen`, or `open` with `onOpenChange`, to open it yourself.

The dialog rises in at full motion. Under `prefers-reduced-motion`, or with `motion="reduced"`, it only fades, over 120 ms, and with `motion="none"` it opens at once.

### Shared View [#shared-view]

Someone who opens a share link should read the chat, not continue it. Render the [conversation](./conversation.mdx) with [messages](./message.mdx) that have no callbacks, so there's nothing to edit or regenerate, under a [notice](./notice.mdx) with a button that starts their own copy:

<Example caption="A shared copy of a chat: the messages have no actions, and Continue this chat sits in the notice above them.">
  <SharedViewExample />

  ```tsx tab="React"
  import {
    Conversation,
    ConversationContent,
    ConversationRow,
    ConversationViewport,
  } from "@/components/ui/conversation";
  import { Message, MessageContent } from "@/components/ui/message";
  import { Notice } from "@/components/ui/notice";

  const messages = [
    { id: "m1", role: "user", text: "The nightly export failed again. Can you take a look?" },
    {
      id: "m2",
      role: "assistant",
      text: "Every run since Wednesday stopped after 30 seconds waiting on the reports service, which got slower, not down.",
    },
    { id: "m3", role: "user", text: "So we just raise the timeout?" },
    {
      id: "m4",
      role: "assistant",
      text: "That gets tonight's run through. I'd also retry each page with backoff and alert when a run takes over an hour.",
    },
  ] as const;

  export default function SharedViewExample() {
    return (
      <div className="flex w-full max-w-xl flex-col gap-3">
        <Notice
          label="This is a shared copy of a chat."
          actions={
            <button type="button" data-kind-button="" className="self-start">
              Continue this chat
            </button>
          }
        />
        <Conversation
          aria-label="Shared chat"
          messageCount={messages.length}
          className="h-72 rounded-xl border border-[var(--kind-color-border)]"
        >
          <ConversationViewport>
            <ConversationContent>
              {messages.map((message, index) => (
                <ConversationRow key={message.id} value={message.id} index={index}>
                  <Message
                    aria-label={message.role === "user" ? "User message" : "Assistant message"}
                    messageRole={message.role}
                    text={message.text}
                  >
                    <MessageContent>{message.text}</MessageContent>
                  </Message>
                </ConversationRow>
              ))}
            </ConversationContent>
          </ConversationViewport>
        </Conversation>
      </div>
    );
  }
  ```

  ```vue tab="Vue"
  <script setup lang="ts">
  import {
    Conversation,
    ConversationContent,
    ConversationRow,
    ConversationViewport,
  } from "@/components/ui/vue/conversation";
  import { Message, MessageContent } from "@/components/ui/vue/message";
  import { Notice } from "@/components/ui/vue/notice";

  const messages = [
    { id: "m1", role: "user", text: "The nightly export failed again. Can you take a look?" },
    {
      id: "m2",
      role: "assistant",
      text: "Every run since Wednesday stopped after 30 seconds waiting on the reports service, which got slower, not down.",
    },
    { id: "m3", role: "user", text: "So we just raise the timeout?" },
    {
      id: "m4",
      role: "assistant",
      text: "That gets tonight's run through. I'd also retry each page with backoff and alert when a run takes over an hour.",
    },
  ] as const;
  </script>

  <template>
    <div class="flex w-full max-w-xl flex-col gap-3">
      <Notice label="This is a shared copy of a chat.">
        <template #actions>
          <button type="button" data-kind-button="" class="self-start">Continue this chat</button>
        </template>
      </Notice>
      <Conversation
        aria-label="Shared chat"
        :message-count="messages.length"
        class="h-72 rounded-xl border border-[var(--kind-color-border)]"
      >
        <ConversationViewport>
          <ConversationContent>
            <ConversationRow v-for="(message, index) in messages" :key="message.id" :value="message.id" :index="index">
              <Message
                :aria-label="message.role === 'user' ? 'User message' : 'Assistant message'"
                :message-role="message.role"
                :text="message.text"
              >
                <MessageContent>{{ message.text }}</MessageContent>
              </Message>
            </ConversationRow>
          </ConversationContent>
        </ConversationViewport>
      </Conversation>
    </div>
  </template>
  ```

  ```svelte tab="Svelte"
  <script lang="ts">
    import {
      Conversation,
      ConversationContent,
      ConversationRow,
      ConversationViewport,
    } from "@/components/ui/svelte/conversation/index.js";
    import { Message, MessageContent } from "@/components/ui/svelte/message/index.js";
    import { Notice } from "@/components/ui/svelte/notice/index.js";

    const messages = [
      { id: "m1", role: "user", text: "The nightly export failed again. Can you take a look?" },
      {
        id: "m2",
        role: "assistant",
        text: "Every run since Wednesday stopped after 30 seconds waiting on the reports service, which got slower, not down.",
      },
      { id: "m3", role: "user", text: "So we just raise the timeout?" },
      {
        id: "m4",
        role: "assistant",
        text: "That gets tonight's run through. I'd also retry each page with backoff and alert when a run takes over an hour.",
      },
    ] as const;
  </script>

  <div class="flex w-full max-w-xl flex-col gap-3">
    <Notice label="This is a shared copy of a chat.">
      {#snippet actions()}
        <button type="button" data-kind-button="" class="self-start">Continue this chat</button>
      {/snippet}
    </Notice>
    <Conversation
      aria-label="Shared chat"
      messageCount={messages.length}
      class="h-72 rounded-xl border border-[var(--kind-color-border)]"
    >
      <ConversationViewport>
        <ConversationContent>
          {#each messages as message, index (message.id)}
            <ConversationRow value={message.id} {index}>
              <Message
                aria-label={message.role === "user" ? "User message" : "Assistant message"}
                messageRole={message.role}
                text={message.text}
              >
                <MessageContent>{message.text}</MessageContent>
              </Message>
            </ConversationRow>
          {/each}
        </ConversationContent>
      </ConversationViewport>
    </Conversation>
  </div>
  ```

  ```tsx tab="Solid"
  import { For } from "solid-js";
  import {
    Conversation,
    ConversationContent,
    ConversationRow,
    ConversationViewport,
  } from "@/components/ui/solid/conversation";
  import { Message, MessageContent } from "@/components/ui/solid/message";
  import { Notice } from "@/components/ui/solid/notice";

  const messages = [
    { id: "m1", role: "user", text: "The nightly export failed again. Can you take a look?" },
    {
      id: "m2",
      role: "assistant",
      text: "Every run since Wednesday stopped after 30 seconds waiting on the reports service, which got slower, not down.",
    },
    { id: "m3", role: "user", text: "So we just raise the timeout?" },
    {
      id: "m4",
      role: "assistant",
      text: "That gets tonight's run through. I'd also retry each page with backoff and alert when a run takes over an hour.",
    },
  ] as const;

  export default function SharedViewExample() {
    return (
      <div class="flex w-full max-w-xl flex-col gap-3">
        <Notice
          label="This is a shared copy of a chat."
          actions={
            <button type="button" data-kind-button="" class="self-start">
              Continue this chat
            </button>
          }
        />
        <Conversation
          aria-label="Shared chat"
          messageCount={messages.length}
          class="h-72 rounded-xl border border-[var(--kind-color-border)]"
        >
          <ConversationViewport>
            <ConversationContent>
              <For each={messages} keyed={(message) => message.id}>
                {(message, index) => (
                  <ConversationRow value={message().id} index={index()}>
                    <Message
                      aria-label={message().role === "user" ? "User message" : "Assistant message"}
                      messageRole={message().role}
                      text={message().text}
                    >
                      <MessageContent>{message().text}</MessageContent>
                    </Message>
                  </ConversationRow>
                )}
              </For>
            </ConversationContent>
          </ConversationViewport>
        </Conversation>
      </div>
    );
  }
  ```
</Example>

## API Reference [#api-reference]

### ShareDialogOptions

| Prop                  | Type                                                                                                        | Description                                                                                                                                                                                                                                                                                                                                                                                                                         |
| --------------------- | ----------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `label?`              | `string \| undefined`                                                                                       | Names the trigger, and the component for agents. Default: `"Share"`                                                                                                                                                                                                                                                                                                                                                                 |
| `dialogLabel?`        | `string \| undefined`                                                                                       | Titles the dialog. Default: `"Share chat"`                                                                                                                                                                                                                                                                                                                                                                                          |
| `visibility?`         | `"link" \| "private"`                                                                                       | Who can open the chat. Passing it controls it: change it in `onVisibilityChange`.                                                                                                                                                                                                                                                                                                                                                   |
| `defaultVisibility?`  | `"link" \| "private"`                                                                                       | Who can open the chat at first, when `visibility` isn't passed. Default: `"private"`                                                                                                                                                                                                                                                                                                                                                |
| `onVisibilityChange?` | `((visibility: ShareVisibility, details: ChangeDetails<ShareVisibilityChangeReason>) => void) \| undefined` | Called when a person or `setVisibility` chooses who can open the chat, with why.                                                                                                                                                                                                                                                                                                                                                    |
| `link?`               | `string \| undefined`                                                                                       | The chat's share link, which the host makes. The component never builds one: while `visibility` is `"link"` it shows this, read-only, with Copy link.                                                                                                                                                                                                                                                                               |
| `onCreateLink?`       | `((details: ShareDialogActionDetails) => unknown) \| undefined`                                             | Called when Create link is pressed. Shows Create link while set, `visibility` is `"link"` and there's no `link`. Make the link and pass it as `link`; return a promise to keep Create link pending until it settles. If it rejects or throws, the dialog shows and announces "Couldn't create a link", and Create link is offered again. A call that resolves without a new `link` keeps Create link disabled until `link` changes. |
| `onRevoke?`           | `((details: ShareDialogActionDetails) => unknown) \| undefined`                                             | Called when Stop sharing is pressed. Shows Stop sharing while set and there's a `link`. Revoke the link, then clear `link` (and usually set `visibility` to `"private"`); a promise keeps it pending until it settles. If it rejects or throws, the dialog shows and announces "Couldn't stop sharing". A call that resolves without clearing `link` keeps Stop sharing disabled until `link` changes.                              |
| `onCopy?`             | `((link: string, details: ChangeDetails<"copy-press" \| "imperative">) => void) \| undefined`               | Called before Copy link writes `link` to the clipboard. `details.cancel()` keeps it from writing.                                                                                                                                                                                                                                                                                                                                   |
| `agentConfirm?`       | `boolean \| undefined`                                                                                      | Whether an agent needs a person's go-ahead for every action, since sharing makes a chat visible to others. Renders `data-agent-confirm` on the root. Default: `true`                                                                                                                                                                                                                                                                |
| `open?`               | `boolean \| undefined`                                                                                      | Whether the dialog is open. Passing it controls it.                                                                                                                                                                                                                                                                                                                                                                                 |
| `defaultOpen?`        | `boolean \| undefined`                                                                                      | Whether the dialog is open at first, when `open` isn't passed. Default: `false`                                                                                                                                                                                                                                                                                                                                                     |
| `onOpenChange?`       | `((open: boolean, details: ChangeDetails<DialogOpenChangeReason>) => void) \| undefined`                    | Called when the dialog opens or closes, with what caused it.                                                                                                                                                                                                                                                                                                                                                                        |
| `presentation?`       | `"auto" \| "dialog" \| "sheet"`                                                                             | How the dialog presents. `"auto"` is a bottom sheet on small touch screens and a centered dialog elsewhere. Default: `"auto"`                                                                                                                                                                                                                                                                                                       |
| `motion?`             | `"auto" \| "none" \| "reduced"`                                                                             | The dialog rises in at full motion and only fades, in 120 ms, under reduced motion. `"auto"` follows `prefers-reduced-motion`. Default: `"auto"`                                                                                                                                                                                                                                                                                    |

Vue, Svelte and Solid take the same props. Controlled props and callbacks are spelled as each framework expects:

| React                              | Vue                                        | Svelte                                  | Solid                              |
| ---------------------------------- | ------------------------------------------ | --------------------------------------- | ---------------------------------- |
| `visibility`, `onVisibilityChange` | `v-model:visibility`, `@visibility-change` | `bind:visibility`, `onVisibilityChange` | `visibility`, `onVisibilityChange` |
| `open`, `onOpenChange`             | `v-model:open`, `@open-change`             | `bind:open`, `onOpenChange`             | `open`, `onOpenChange`             |
| `onCreateLink`                     | `@create-link`                             | `onCreateLink`                          | `onCreateLink`                     |
| `onRevoke`                         | `@revoke`                                  | `onRevoke`                              | `onRevoke`                         |
| `onCopy`                           | `@copy`                                    | `onCopy`                                | `onCopy`                           |

### Agent [#agent]

An agent connected with `createAgent` finds the dialog by its `label` and reads `open` and `visibility`. `open()` is offered while the dialog is closed, and the others while it's open: `setVisibility` while nothing is pending, `createLink` and `revoke` while their buttons show, and `copyLink` while there's a link.

Making a chat public is a consequential change, so with `agentConfirm`, on by default, every agent action needs a person's go-ahead. Pass `agentConfirm={false}` only when your app asks for it another way.

### ShareDialogAgentActions

| Prop            | Type                                                      | Description                                                                                       |
| --------------- | --------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
| `open`          | `(() => boolean) \| undefined`                            | Opens the dialog. Offered while it's closed.                                                      |
| `close`         | `(() => boolean) \| undefined`                            | Closes the dialog. Offered while it's open.                                                       |
| `setVisibility` | `((visibility: ShareVisibility) => boolean) \| undefined` | Chooses who can open the chat, as a person would. Offered while the dialog is open.               |
| `createLink`    | `((event?: Event) => void) \| undefined`                  | Calls `onCreateLink`. Offered while the dialog is open, Create link shows and nothing is pending. |
| `copyLink`      | `((event?: Event) => Promise<boolean>) \| undefined`      | Copies `link`. Offered while the dialog is open and shows the link.                               |
| `revoke`        | `((event?: Event) => void) \| undefined`                  | Calls `onRevoke`. Offered while the dialog is open, Stop sharing shows and nothing is pending.    |

<Caption>
  Runs in React, Vue, Svelte, Solid and Web Components. On `<kind-share-dialog>`, the props are kebab-case attributes, so `dialogLabel` is `dialog-label`. The `can-create-link` attribute offers Create link, which fires `createlink`, and `can-revoke` offers Stop sharing, which fires `revoke`. `onVisibilityChange` is `sharevisibilitychange`, `onCopy` is `linkcopy` and `onOpenChange` is `openchange`. Set `link` once you've made one. The element fills in its trigger and dialog when it has no children.
</Caption>
