# Ask popover

Ask about any selection in an app: an Ask button at the selection, the answer streaming in a popover right there, and a way to keep it as a thread.

> chatcn Pro: needs a Pro license (https://chatcn.dev/pro). Install from the `@chatcn-pro` registry with your license token; setup at https://chatcn.dev/docs/chatcn-pro.

Example: Ask popover. A handbook page: a sentence is selected, Ask opens a popover right there, and the answer streams in it. Preview: https://chatcn.dev/preview/ask-popover-demo

## Installation

```bash
npx shadcn@latest add @chatcn-pro/ask-popover
```

## Usage

```tsx
import { AskPopover } from "@/components/ui/chatcn/pro/ask-popover"

<AskPopover
  messages={messages}
  status={status}
  onAsk={(question, quote) => ask(question, { quote })}
  onDismiss={resetConversation}
  onPromote={(quote) => openThread(quote, messages)}
>
  <Article />
</AskPopover>
```

Wrap any content. When the reader selects at least `minLength` characters inside it, a small Ask button appears at the end of the selection. It opens a popover anchored there: the quote on top, a question box, and the answer streaming in place, so the reader never leaves the page. The selection stays marked on every line it spans while the popover is open. The popover opens below the selection when there is room, otherwise above, and is never taller than the room it has. Once there is an answer, Continue in a thread hands the quote to `onPromote`. Start a fresh `messages` list for each quote, and clear it in `onDismiss`.

## Examples

### States

Example: States. Opened on a selection, answered with Continue in a thread, and a failed answer. Preview: https://chatcn.dev/preview/ask-popover-states

## Props

| Prop | Type | Notes |
| --- | --- | --- |
| `children` | `ReactNode` | The content people select from |
| `messages` | `ChatcnMessage[]` | The thread, in the kit's message contract |
| `status`, `error`, `onStop`, `onRetry` | `"ready" | "submitted" | "streaming" | "error"` | Send becomes Stop while it streams; an error offers Try again |
| `value`, `defaultValue`, `onValueChange` | `string` | The draft, controlled or not |
| `onAsk` | `(question, quote) => void` | A question about the quoted text |
| `onDismiss` | `() => void` | The popover closed |
| `onPromote` | `(quote) => void` | Continue in a full thread. Hidden when not set |
| `minLength` | `number` | Shortest selection that offers Ask. Default 3 |
| `defaultQuote` | `string` | Open on mount around `[data-ask-anchor]`, with this quote |
| `labels` | `Partial<AskPopoverLabels>` | Every string, for translation |

## Keyboard

| Key | Action |
| --- | --- |
| Tab | The Ask button follows the content in tab order while text is selected |
| Enter, Shift Enter | Ask, new line |
| Escape | Close the popover |

## Accessibility

- The popover is a dialog named by its quote, so a screen reader hears what is being asked about.
- Opening moves focus to the question box.
- The marked selection is decorative; the quote is repeated in text at the top of the popover.
- A click outside does not close it while an answer is still streaming.

## Motion

The Ask button enters with a short rise; the popover opens from the edge nearest the selection in 200 ms. Under reduced motion both appear without movement.

Source: https://chatcn.dev/docs/components/ask-popover