> ## Documentation Index
> Fetch the complete documentation index at: https://docs.velt.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Popover Setup

<img src="https://mintcdn.com/velt/gAz_vLsG-ukKamYM/gifs/Add-Cell-Comment.gif?s=df0ac829cfa2bf8c2a47cce2d565fc37" alt="" width="1280" height="720" data-path="gifs/Add-Cell-Comment.gif" />

## Step 1: Import Comment components

<Tabs>
  <Tab title="React / Next.js">
    Import the `VeltComments`, `VeltCommentTool`, and `VeltCommentBubble` components.

    ```js theme={null}
    import {
      VeltProvider,
      VeltComments,
      VeltCommentTool,
      VeltCommentBubble
    } from '@veltdev/react';
    ```
  </Tab>

  <Tab title="HTML">
    No imports needed. The Velt SDK is loaded via script tag.
  </Tab>
</Tabs>

## Step 2: Add Comments component with Popover mode

Add the Comments component to the root of your app and enable Popover mode.

This component is required to render comments in your app. Popover mode means comments can be attached to specific target elements (similar to Google Sheets).

<Tabs>
  <Tab title="React / Next.js">
    ```js theme={null}
    <VeltProvider apiKey="API_KEY">
      <VeltComments popoverMode={true}/>
    </VeltProvider>
    ```
  </Tab>

  <Tab title="Other Frameworks">
    Add the comment component to your template close to the root level of your `<body>`.

    ```html theme={null}
    <body>
      <velt-comments popover-mode="true"></velt-comments>
    </body>
    ```
  </Tab>
</Tabs>

## Step 3: Add the Comment Tool component

There are two patterns for adding the Comment Tool:

* Add a Comment Tool next to each element you want to comment on
* Have a single Comment Tool and use it to pin comments on elements

### a. Comment Tool next to each element

Add a Comment Tool near each cell or element you want to comment on. For example, in a table you could add this tool to each cell and show it on hover or right click context menu.

* Add unique DOM ID to each element
* Set the `targetElementId` to match the element's ID
* When users click on the Comment Tool, it attaches a Comment to that element
* If your app is more complex and cannot have unique IDs for each element (e.g., table cell), you could also use `context` to bind the element and comment with custom metadata, which can then be used to filter, group, and render comments that match specific criteria. [Learn more](/async-collaboration/comments/customize-behavior#aggregation)

<img src="https://mintcdn.com/velt/2O8vk8YlVD4Kq-ni/images/comment-tool.png?fit=max&auto=format&n=2O8vk8YlVD4Kq-ni&q=85&s=f6542acc54050b4b607812391c0b97ad" alt="" width="1304" height="856" data-path="images/comment-tool.png" />

Once the Comment is added, you'll see a triangle on the top right corner of the element.

<img src="https://mintcdn.com/velt/vGgIoGfoceFP8dEI/images/popover-comment-show.png?fit=max&auto=format&n=vGgIoGfoceFP8dEI&q=85&s=fb9f1f0e706b14296b86d255dfcb7284" alt="" width="1280" height="720" data-path="images/popover-comment-show.png" />

<Tabs>
  <Tab title="React / Next.js">
    ```jsx theme={null}
    <div className="table">
      <div className="cell" id="cell-id-1">
        <VeltCommentTool targetElementId="cell-id-1" />
      </div>
      <div className="cell" id="cell-id-2">
        <VeltCommentTool targetElementId="cell-id-2" />
      </div>
    </div>
    ```
  </Tab>

  <Tab title="HTML">
    ```html theme={null}
    <div class="table">
      <div class="cell" id="cell-id-1">
        <velt-comment-tool target-element-id="cell-id-1"></velt-comment-tool>
      </div>
      <div class="cell" id="cell-id-2">
        <velt-comment-tool target-element-id="cell-id-2"></velt-comment-tool>
      </div>
    </div>
    ```
  </Tab>
</Tabs>

### b. Single Comment Tool

<img src="https://mintcdn.com/velt/gAz_vLsG-ukKamYM/gifs/freestyle-popover.gif?s=ed078b4a7a751aee1e0f2aab6361b4c2" alt="" width="1280" height="720" data-path="gifs/freestyle-popover.gif" />

Add a Comment Tool in a single location such as the navigation bar.

* Add unique DOM ID and `data-velt-target-comment-element-id` attribute to each element (both with the same value)
* When users click on the Comment Tool and then click on the target element, it attaches a Comment to it
* You can only add one Comment Annotation (thread) per element with this approach
* If your app is more complex and cannot have unique IDs for each element (e.g., table cell), you could also use `context` to bind the element and comment with custom metadata, which can then be used to filter, group, and render comments that match specific criteria. [Learn more](/async-collaboration/comments/customize-behavior#aggregation)

<Warning>
  If you don't add the `data-velt-target-comment-element-id` attribute, you will be adding multiple Comment Annotations on the same element.
</Warning>

<Tabs>
  <Tab title="React / Next.js">
    ```jsx theme={null}
    <div>
      <VeltCommentTool />
      <div className="table">
        <div className="cell" id="cell-id-A" data-velt-target-comment-element-id="cell-id-A">
        </div>
        <div className="cell" id="cell-id-B" data-velt-target-comment-element-id="cell-id-B">
        </div>
      </div>
    </div>
    ```
  </Tab>

  <Tab title="HTML">
    ```html theme={null}
    <div>
      <velt-comment-tool></velt-comment-tool>
      <div class="table">
        <div class="cell" data-velt-target-comment-element-id="cell-id-A" id="cell-id-A">
        </div>
        <div class="cell" data-velt-target-comment-element-id="cell-id-B" id="cell-id-B">
        </div>
      </div>
    </div>
    ```
  </Tab>
</Tabs>

## Step 4: Add Metadata to the Comment

* Render additional data on comments UI
* Create custom UI components
* Enable comment filtering on custom data
* Use the metadata later when processing notifications

### a. Using Comment Tool

<Tabs>
  <Tab title="React / Next.js">
    ```jsx theme={null}
    <VeltCommentTool targetElementId="elementId" context={{ key1: 'value1' }} />
    ```
  </Tab>

  <Tab title="HTML">
    ```html theme={null}
    <velt-comment-tool target-element-id="elementId" context='{"key1": "value1"}'></velt-comment-tool>
    ```
  </Tab>
</Tabs>

### b. Using Comment Add Event Callback

Learn more about it [here](/async-collaboration/comments/customize-behavior#addcontext).

## Step 5: Add the Comment Bubble component (optional)

<img src="https://mintcdn.com/velt/vGgIoGfoceFP8dEI/images/popover-bubble.png?fit=max&auto=format&n=vGgIoGfoceFP8dEI&q=85&s=877e3867de36da7674269cf22f068a7b" alt="" width="1280" height="720" data-path="images/popover-bubble.png" />

<Warning>
  Either use this or the default triangle component. Using both could cause visual UX issues. Turn off the triangle by setting `popoverTriangleComponent` to `false` in the Velt Comments component.
</Warning>

The Comment Bubble component:

* Displays a count of replies for a comment thread
* Must have the same `targetElementId` or `context` as its associated element and comment tool
* Can show either total replies or only unread replies
* Can be placed anywhere in your UI

**Props:**

* `commentCountType`: Which count to display
  * `total`: Total number of replies (default)
  * `unread`: Number of unread replies

<Tabs>
  <Tab title="React / Next.js">
    ```js theme={null}
    <div className="table">
      <div className="cell" id="cell-id-1">
        <VeltCommentTool targetElementId="cell-id-1" />
        <VeltCommentBubble
          commentCountType="unread"
          targetElementId="cell-id-1"
        />
      </div>
      <div className="cell" id="cell-id-2">
        <VeltCommentTool targetElementId="cell-id-2" />
        <VeltCommentBubble
          commentCountType="unread"
          targetElementId="cell-id-2"
        />
      </div>
    </div>
    ```
  </Tab>

  <Tab title="HTML">
    ```html theme={null}
    <div class="table">
      <div class="cell" id="cell-id-1">
        <velt-comment-tool target-element-id="cell-id-1"></velt-comment-tool>
        <velt-comment-bubble
          comment-count-type="unread"
          target-element-id="cell-id-1"
        ></velt-comment-bubble>
      </div>
      <div class="cell" id="cell-id-2">
        <velt-comment-tool target-element-id="cell-id-2"></velt-comment-tool>
        <velt-comment-bubble
          comment-count-type="unread"
          target-element-id="cell-id-2"
        ></velt-comment-bubble>
      </div>
    </div>
    ```
  </Tab>
</Tabs>

### Remove Popover Mode Triangle (optional)

<img src="https://mintcdn.com/velt/vGgIoGfoceFP8dEI/images/popover-comment-pin.png?fit=max&auto=format&n=vGgIoGfoceFP8dEI&q=85&s=1143f721774cb8eed6085c9a0d43079a" alt="" width="1280" height="720" data-path="images/popover-comment-pin.png" />

You can remove the triangle that appears in Popover mode.

By default, the triangle is enabled.

<Tabs>
  <Tab title="React / Next.js">
    ```jsx theme={null}
    <VeltComments popoverTriangleComponent={false}/>
    ```

    API Method:

    ```jsx theme={null}
    const commentElement = client.getCommentElement();
    commentElement.enablePopoverTriangleComponent();
    commentElement.disablePopoverTriangleComponent();
    ```
  </Tab>

  <Tab title="HTML">
    ```html theme={null}
    <velt-comments popover-triangle-component="false"></velt-comments>
    ```

    API Method:

    ```jsx theme={null}
    const commentElement = client.getCommentElement();
    commentElement.enablePopoverTriangleComponent();
    commentElement.disablePopoverTriangleComponent();
    ```
  </Tab>
</Tabs>

## Step 6: Test Integration

Test it out by opening the page with Velt components in your browser.

Click on the Comment Tool and leave a comment on the target element.

<img src="https://mintcdn.com/velt/vGgIoGfoceFP8dEI/images/popover-comment-pin.png?fit=max&auto=format&n=vGgIoGfoceFP8dEI&q=85&s=1143f721774cb8eed6085c9a0d43079a" alt="" width="1280" height="720" data-path="images/popover-comment-pin.png" />

## Complete Example

<Tabs>
  <Tab title="React / Next.js">
    ```jsx theme={null}
    import {
      VeltProvider,
      VeltComments,
      VeltCommentTool,
      VeltCommentBubble
    } from '@veltdev/react';

    export default function App() {
      return (
        <VeltProvider apiKey="API_KEY">
          <VeltComments popoverMode={true} popoverTriangleComponent={true} />

          {/* Pattern a: Comment Tool next to each element */}
          <div className="table">
            <div className="cell" id="cell-1">
              <VeltCommentTool targetElementId="cell-1" />
              <VeltCommentBubble targetElementId="cell-1" />
            </div>
            <div className="cell" id="cell-2">
              <VeltCommentTool targetElementId="cell-2" />
            </div>
          </div>

          {/* Pattern b: Single Comment Tool */}
          <div>
            <VeltCommentTool />
            <div className="table">
              <div className="cell" id="cell-A" data-velt-target-comment-element-id="cell-A">
                Content
              </div>
              <div className="cell" id="cell-B" data-velt-target-comment-element-id="cell-B">
                Content
              </div>
            </div>
          </div>
        </VeltProvider>
      );
    }
    ```
  </Tab>

  <Tab title="HTML">
    ```html theme={null}
    <body>
      <velt-comments popover-mode="true" popover-triangle-component="true"></velt-comments>

      <!-- Pattern a: Comment Tool next to each element -->
      <div class="table">
        <div class="cell" id="cell-1">
          <velt-comment-tool target-element-id="cell-1"></velt-comment-tool>
          <velt-comment-bubble target-element-id="cell-1"></velt-comment-bubble>
        </div>
        <div class="cell" id="cell-2">
          <velt-comment-tool target-element-id="cell-2"></velt-comment-tool>
        </div>
      </div>

      <!-- Pattern b: Single Comment Tool -->
      <div>
        <velt-comment-tool></velt-comment-tool>
        <div class="table">
          <div class="cell" id="cell-A" data-velt-target-comment-element-id="cell-A">
            Content
          </div>
          <div class="cell" id="cell-B" data-velt-target-comment-element-id="cell-B">
            Content
          </div>
        </div>
      </div>
    </body>
    ```
  </Tab>
</Tabs>
