Skip to content

Conversation ​

A chat transcript for AI assistants: user and assistant messages, reasoning, sources, copy and feedback actions, a typing indicator and a "scroll to latest" button.

Installation ​

bash
npx bootcn add conversation
bash
pnpm dlx bootcn add conversation
bash
yarn dlx bootcn add conversation
bash
bunx --bun bootcn add conversation

The CLI also installs the design tokens and these components: code-block. It prints the <link> and <script> tags to add to your layout.

Usage ​

Render past messages on the server and add new ones with Bootcn.conversation.append. A streamed reply:

js
const root = document.querySelector('[data-bootcn-conversation]');
Bootcn.conversation.setTyping(root, true);
const reply = Bootcn.conversation.append(root, { role: 'assistant', text: '' });
let text = '';
for await (const chunk of stream) {
  text += chunk;
  Bootcn.conversation.setText(reply, text);
}
Bootcn.conversation.setTyping(root, false);

Pair it with the Prompt input component, or install the AI chat block, which connects the two.

JavaScript API ​

  • Markup: data-bootcn-conversation on the section, the scrolling role="log" element with data-bootcn-conversation-log, the typing indicator (data-bootcn-conversation-typing), the "scroll to latest" button (data-bootcn-conversation-latest), a status region (data-bootcn-conversation-status) and one <template> per role (data-bootcn-conversation-template="user" / "assistant").
  • Bootcn.conversation.append(root, { role: 'user' | 'assistant', text }) adds a message from the template and returns it. text becomes paragraphs (blank lines split them) and is never parsed as HTML; html instead of text inserts markup as is, so pass only markup you built or sanitized.
  • setText(message, text) replaces a message's text, for streamed replies.
  • setTyping(root, true | false) shows the typing indicator and marks the log aria-busy (screen readers then read the finished reply once).
  • scrollToLatest(root, smooth), clear(root), init() / destroy().
  • The log stays at the latest message while the reader is there; after scrolling up, the "scroll to latest" button appears instead.
  • Events (bubble): bootcn:conversation:copy (detail.text), bootcn:conversation:feedback (detail.value: 'up', 'down' or null), bootcn:conversation:regenerate.

Accessibility ​

  • The transcript is a role="log" region, so new messages are announced. While a reply is generated, aria-busy holds the announcement until it is complete.
  • Every message names its author ("You" is visually hidden).
  • The log is focusable for keyboard scrolling; message actions are buttons with names, and the feedback buttons use aria-pressed.
  • Reasoning uses a native <details> disclosure.
  • The typing dots stop moving with prefers-reduced-motion.

Dependencies ​

  • Bootstrap 5.3+ CSS.
  • themes/tokens/tokens.css.
  • The code-block component.

Source ​

The exact files the CLI installs (conversation@1.0.0). View on GitHub · Report an issue

html
<section
  class="bootcn-conversation"
  aria-label="Conversation"
  data-bootcn-conversation
  style="--bootcn-conversation-height: 26rem"
>
  <div
    class="bootcn-conversation__log"
    role="log"
    aria-label="Messages"
    tabindex="0"
    data-bootcn-conversation-log
  >
    <article class="bootcn-message bootcn-message--user">
      <div class="bootcn-message__body">
        <p class="bootcn-message__author visually-hidden">You</p>
        <div class="bootcn-message__content">
          <p>How do I add a dialog to my PHP page?</p>
        </div>
      </div>
    </article>
    <article class="bootcn-message bootcn-message--assistant">
      <span class="bootcn-message__avatar" aria-hidden="true"
        ><svg
          class="bootcn-icon"
          width="16"
          height="16"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
          focusable="false"
        >
          <path d="M12 3l1.9 5.1L19 10l-5.1 1.9L12 17l-1.9-5.1L5 10l5.1-1.9z" />
          <path d="M19 15l.8 2.2L22 18l-2.2.8L19 21l-.8-2.2L16 18l2.2-.8z" /></svg
      ></span>
      <div class="bootcn-message__body">
        <p class="bootcn-message__author">Assistant</p>
        <details class="bootcn-reasoning">
          <summary class="bootcn-reasoning__summary">
            Thought for 3 seconds
            <svg
              class="bootcn-icon bootcn-reasoning__chevron"
              width="16"
              height="16"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
              focusable="false"
            >
              <path d="m6 9 6 6 6-6" />
            </svg>
          </summary>
          <div class="bootcn-reasoning__content">
            <p>
              The user wants a modal dialog. Bootcn ships it as a component built on Bootstrap's
              modal, so the CLI command and the markup are enough.
            </p>
          </div>
        </details>
        <div class="bootcn-message__content">
          <p>Install the component with the CLI:</p>
          <figure class="bootcn-code-block" data-bootcn-code-block>
            <figcaption class="bootcn-code-block__header">
              <span class="bootcn-code-block__title">Terminal</span>
              <button
                type="button"
                class="bootcn-code-block__copy"
                aria-label="Copy code"
                data-bootcn-code-block-copy
              >
                <svg
                  class="bootcn-icon bootcn-code-block__copy-icon"
                  width="16"
                  height="16"
                  viewBox="0 0 24 24"
                  fill="none"
                  stroke="currentColor"
                  stroke-width="2"
                  stroke-linecap="round"
                  stroke-linejoin="round"
                  aria-hidden="true"
                  focusable="false"
                >
                  <rect x="8" y="8" width="13" height="13" rx="2" />
                  <path d="M16 8V5a2 2 0 0 0-2-2H5a2 2 0 0 0-2 2v9a2 2 0 0 0 2 2h3" />
                </svg>
                <svg
                  class="bootcn-icon bootcn-code-block__done-icon"
                  width="16"
                  height="16"
                  viewBox="0 0 24 24"
                  fill="none"
                  stroke="currentColor"
                  stroke-width="2"
                  stroke-linecap="round"
                  stroke-linejoin="round"
                  aria-hidden="true"
                  focusable="false"
                >
                  <path d="M20 6 9 17l-5-5" />
                </svg>
              </button>
            </figcaption>
            <pre
              class="bootcn-code-block__pre"
              tabindex="0"
            ><code>npx bootcn add dialog</code></pre>
            <span class="visually-hidden" role="status" data-bootcn-code-block-status></span>
          </figure>
          <p>
            Then include <code>components/ui/dialog.html</code> where you need it, and load
            <code>dialog.css</code> and the Bootstrap bundle on the page.
          </p>
        </div>
        <div class="bootcn-message__sources">
          <span class="bootcn-message__sources-label">Sources</span>
          <ul class="bootcn-message__source-list">
            <li>
              <a class="bootcn-message__source" href="https://bootcn.dev/components/dialog"
                >bootcn.dev</a
              >
            </li>
            <li>
              <a
                class="bootcn-message__source"
                href="https://getbootstrap.com/docs/5.3/components/modal/"
              >
                getbootstrap.com
              </a>
            </li>
          </ul>
        </div>
        <div class="bootcn-message__actions" role="group" aria-label="Message actions">
          <button
            type="button"
            class="bootcn-message__action"
            aria-label="Copy"
            data-bootcn-message-copy
          >
            <svg
              class="bootcn-icon bootcn-message__copy-icon"
              width="16"
              height="16"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
              focusable="false"
            >
              <rect x="8" y="8" width="13" height="13" rx="2" />
              <path d="M16 8V5a2 2 0 0 0-2-2H5a2 2 0 0 0-2 2v9a2 2 0 0 0 2 2h3" />
            </svg>
            <svg
              class="bootcn-icon bootcn-message__done-icon"
              width="16"
              height="16"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
              focusable="false"
            >
              <path d="M20 6 9 17l-5-5" />
            </svg>
          </button>
          <button
            type="button"
            class="bootcn-message__action"
            aria-label="Regenerate"
            data-bootcn-message-regenerate
          >
            <svg
              class="bootcn-icon"
              width="16"
              height="16"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
              focusable="false"
            >
              <path d="M21 12a9 9 0 0 1-15.5 6.2L3 16" />
              <path d="M3 21v-5h5" />
              <path d="M3 12a9 9 0 0 1 15.5-6.2L21 8" />
              <path d="M21 3v5h-5" />
            </svg>
          </button>
          <button
            type="button"
            class="bootcn-message__action"
            aria-label="Good response"
            aria-pressed="false"
            data-bootcn-message-feedback="up"
          >
            <svg
              class="bootcn-icon"
              width="16"
              height="16"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
              focusable="false"
            >
              <path d="M7 10v11" />
              <path
                d="M15 5.9 14 10h5.8a2 2 0 0 1 2 2.3l-1.4 7A2 2 0 0 1 18.4 21H7V10l4-8a3 3 0 0 1 4 3.9z"
              />
              <path d="M3 10h4v11H3z" />
            </svg>
          </button>
          <button
            type="button"
            class="bootcn-message__action"
            aria-label="Bad response"
            aria-pressed="false"
            data-bootcn-message-feedback="down"
          >
            <svg
              class="bootcn-icon"
              width="16"
              height="16"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
              focusable="false"
            >
              <path d="M17 14V3" />
              <path
                d="M9 18.1 10 14H4.2a2 2 0 0 1-2-2.3l1.4-7A2 2 0 0 1 5.6 3H17v11l-4 8a3 3 0 0 1-4-3.9z"
              />
              <path d="M21 14h-4V3h4z" />
            </svg>
          </button>
        </div>
      </div>
    </article>
    <article class="bootcn-message bootcn-message--user">
      <div class="bootcn-message__body">
        <p class="bootcn-message__author visually-hidden">You</p>
        <div class="bootcn-message__content">
          <p>Can it close when I press Escape?</p>
        </div>
      </div>
    </article>
    <article class="bootcn-message bootcn-message--assistant">
      <span class="bootcn-message__avatar" aria-hidden="true"
        ><svg
          class="bootcn-icon"
          width="16"
          height="16"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
          focusable="false"
        >
          <path d="M12 3l1.9 5.1L19 10l-5.1 1.9L12 17l-1.9-5.1L5 10l5.1-1.9z" />
          <path d="M19 15l.8 2.2L22 18l-2.2.8L19 21l-.8-2.2L16 18l2.2-.8z" /></svg
      ></span>
      <div class="bootcn-message__body">
        <p class="bootcn-message__author">Assistant</p>
        <div class="bootcn-message__content">
          <p>
            Yes. Bootstrap's modal closes on <kbd>Esc</kbd> and returns focus to the button that
            opened it. Add <code>data-bs-backdrop="static"</code> if clicks outside should not close
            it.
          </p>
        </div>
        <div class="bootcn-message__actions" role="group" aria-label="Message actions">
          <button
            type="button"
            class="bootcn-message__action"
            aria-label="Copy"
            data-bootcn-message-copy
          >
            <svg
              class="bootcn-icon bootcn-message__copy-icon"
              width="16"
              height="16"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
              focusable="false"
            >
              <rect x="8" y="8" width="13" height="13" rx="2" />
              <path d="M16 8V5a2 2 0 0 0-2-2H5a2 2 0 0 0-2 2v9a2 2 0 0 0 2 2h3" />
            </svg>
            <svg
              class="bootcn-icon bootcn-message__done-icon"
              width="16"
              height="16"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
              focusable="false"
            >
              <path d="M20 6 9 17l-5-5" />
            </svg>
          </button>
          <button
            type="button"
            class="bootcn-message__action"
            aria-label="Regenerate"
            data-bootcn-message-regenerate
          >
            <svg
              class="bootcn-icon"
              width="16"
              height="16"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
              focusable="false"
            >
              <path d="M21 12a9 9 0 0 1-15.5 6.2L3 16" />
              <path d="M3 21v-5h5" />
              <path d="M3 12a9 9 0 0 1 15.5-6.2L21 8" />
              <path d="M21 3v5h-5" />
            </svg>
          </button>
          <button
            type="button"
            class="bootcn-message__action"
            aria-label="Good response"
            aria-pressed="false"
            data-bootcn-message-feedback="up"
          >
            <svg
              class="bootcn-icon"
              width="16"
              height="16"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
              focusable="false"
            >
              <path d="M7 10v11" />
              <path
                d="M15 5.9 14 10h5.8a2 2 0 0 1 2 2.3l-1.4 7A2 2 0 0 1 18.4 21H7V10l4-8a3 3 0 0 1 4 3.9z"
              />
              <path d="M3 10h4v11H3z" />
            </svg>
          </button>
          <button
            type="button"
            class="bootcn-message__action"
            aria-label="Bad response"
            aria-pressed="false"
            data-bootcn-message-feedback="down"
          >
            <svg
              class="bootcn-icon"
              width="16"
              height="16"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
              focusable="false"
            >
              <path d="M17 14V3" />
              <path
                d="M9 18.1 10 14H4.2a2 2 0 0 1-2-2.3l1.4-7A2 2 0 0 1 5.6 3H17v11l-4 8a3 3 0 0 1-4-3.9z"
              />
              <path d="M21 14h-4V3h4z" />
            </svg>
          </button>
        </div>
      </div>
    </article>
    <div
      class="bootcn-message bootcn-message--assistant bootcn-message--typing"
      data-bootcn-conversation-typing
      hidden
    >
      <span class="bootcn-message__avatar" aria-hidden="true"></span>
      <div class="bootcn-message__body">
        <span class="bootcn-typing" aria-hidden="true"
          ><span></span><span></span><span></span
        ></span>
        <span class="visually-hidden">Assistant is typing</span>
      </div>
    </div>
  </div>
  <button type="button" class="bootcn-conversation__latest" data-bootcn-conversation-latest hidden>
    <svg
      class="bootcn-icon"
      width="16"
      height="16"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      stroke-width="2"
      stroke-linecap="round"
      stroke-linejoin="round"
      aria-hidden="true"
      focusable="false"
    >
      <path d="M12 5v14" />
      <path d="m19 12-7 7-7-7" />
    </svg>
    <span class="visually-hidden">Scroll to the latest message</span>
  </button>
  <p class="visually-hidden" role="status" data-bootcn-conversation-status></p>
  <template data-bootcn-conversation-template="user">
    <article class="bootcn-message bootcn-message--user">
      <div class="bootcn-message__body">
        <p class="bootcn-message__author visually-hidden">You</p>
        <div class="bootcn-message__content"></div>
      </div>
    </article>
  </template>
  <template data-bootcn-conversation-template="assistant">
    <article class="bootcn-message bootcn-message--assistant">
      <span class="bootcn-message__avatar" aria-hidden="true"
        ><svg
          class="bootcn-icon"
          width="16"
          height="16"
          viewBox="0 0 24 24"
          fill="none"
          stroke="currentColor"
          stroke-width="2"
          stroke-linecap="round"
          stroke-linejoin="round"
          aria-hidden="true"
          focusable="false"
        >
          <path d="M12 3l1.9 5.1L19 10l-5.1 1.9L12 17l-1.9-5.1L5 10l5.1-1.9z" />
          <path d="M19 15l.8 2.2L22 18l-2.2.8L19 21l-.8-2.2L16 18l2.2-.8z" /></svg
      ></span>
      <div class="bootcn-message__body">
        <p class="bootcn-message__author">Assistant</p>
        <div class="bootcn-message__content"></div>
        <div class="bootcn-message__actions" role="group" aria-label="Message actions">
          <button
            type="button"
            class="bootcn-message__action"
            aria-label="Copy"
            data-bootcn-message-copy
          >
            <svg
              class="bootcn-icon bootcn-message__copy-icon"
              width="16"
              height="16"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
              focusable="false"
            >
              <rect x="8" y="8" width="13" height="13" rx="2" />
              <path d="M16 8V5a2 2 0 0 0-2-2H5a2 2 0 0 0-2 2v9a2 2 0 0 0 2 2h3" />
            </svg>
            <svg
              class="bootcn-icon bootcn-message__done-icon"
              width="16"
              height="16"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
              focusable="false"
            >
              <path d="M20 6 9 17l-5-5" />
            </svg>
          </button>
          <button
            type="button"
            class="bootcn-message__action"
            aria-label="Regenerate"
            data-bootcn-message-regenerate
          >
            <svg
              class="bootcn-icon"
              width="16"
              height="16"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
              focusable="false"
            >
              <path d="M21 12a9 9 0 0 1-15.5 6.2L3 16" />
              <path d="M3 21v-5h5" />
              <path d="M3 12a9 9 0 0 1 15.5-6.2L21 8" />
              <path d="M21 3v5h-5" />
            </svg>
          </button>
          <button
            type="button"
            class="bootcn-message__action"
            aria-label="Good response"
            aria-pressed="false"
            data-bootcn-message-feedback="up"
          >
            <svg
              class="bootcn-icon"
              width="16"
              height="16"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
              focusable="false"
            >
              <path d="M7 10v11" />
              <path
                d="M15 5.9 14 10h5.8a2 2 0 0 1 2 2.3l-1.4 7A2 2 0 0 1 18.4 21H7V10l4-8a3 3 0 0 1 4 3.9z"
              />
              <path d="M3 10h4v11H3z" />
            </svg>
          </button>
          <button
            type="button"
            class="bootcn-message__action"
            aria-label="Bad response"
            aria-pressed="false"
            data-bootcn-message-feedback="down"
          >
            <svg
              class="bootcn-icon"
              width="16"
              height="16"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
              focusable="false"
            >
              <path d="M17 14V3" />
              <path
                d="M9 18.1 10 14H4.2a2 2 0 0 1-2-2.3l1.4-7A2 2 0 0 1 5.6 3H17v11l-4 8a3 3 0 0 1-4-3.9z"
              />
              <path d="M21 14h-4V3h4z" />
            </svg>
          </button>
        </div>
      </div>
    </article>
  </template>
</section>
css
.bootcn-conversation {
  --bootcn-conversation-height: 32rem;
  position: relative;
  width: 42rem;
  max-width: 100%;
}

.bootcn-conversation__log {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  height: var(--bootcn-conversation-height);
  padding: 1rem;
  overflow-y: auto;
  overscroll-behavior: contain;
  border-radius: var(--bootcn-radius-lg);
  outline: 0;
}

.bootcn-conversation__log:focus-visible {
  box-shadow: var(--bootcn-focus-ring);
}

.bootcn-conversation__latest {
  position: absolute;
  bottom: 1rem;
  left: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  border: var(--bootcn-border-width) solid var(--bootcn-border-color);
  border-radius: 9999px;
  background-color: var(--bootcn-background);
  box-shadow: var(--bootcn-shadow-md);
  color: var(--bootcn-foreground);
  transform: translateX(-50%);
}

.bootcn-conversation__latest[hidden] {
  display: none;
}

.bootcn-conversation__latest:hover {
  background-color: var(--bootcn-accent);
}

.bootcn-conversation__latest:focus-visible {
  outline: 0;
  box-shadow: var(--bootcn-focus-ring);
}

/* Messages */
.bootcn-message {
  display: flex;
  gap: 0.75rem;
  min-width: 0;
}

.bootcn-message[hidden] {
  display: none;
}

.bootcn-message__body {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  min-width: 0;
}

.bootcn-message--assistant .bootcn-message__body {
  flex: 1;
}

.bootcn-message--user {
  justify-content: flex-end;
}

.bootcn-message--user .bootcn-message__body {
  max-width: 80%;
  padding: 0.625rem 1rem;
  border-radius: var(--bootcn-radius-xl);
  background-color: var(--bootcn-secondary);
  color: var(--bootcn-secondary-foreground);
}

.bootcn-message__avatar {
  display: inline-flex;
  flex: none;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  border: var(--bootcn-border-width) solid var(--bootcn-border-color);
  border-radius: 9999px;
  background-color: var(--bootcn-background);
  color: var(--bootcn-foreground);
}

.bootcn-message__author {
  margin: 0.375rem 0 0;
  font-size: 0.875rem;
  font-weight: 600;
  line-height: 1.25rem;
}

.bootcn-message__content {
  font-size: 0.9375rem;
  line-height: 1.65;
  overflow-wrap: anywhere;
}

.bootcn-message__content > :first-child {
  margin-top: 0;
}

.bootcn-message__content > :last-child {
  margin-bottom: 0;
}

.bootcn-message__content p,
.bootcn-message__content ul,
.bootcn-message__content ol {
  margin: 0 0 0.75rem;
}

.bootcn-message__content :not(pre) > code {
  padding: 0.125rem 0.375rem;
  border-radius: var(--bootcn-radius-sm);
  background-color: var(--bootcn-muted);
  color: inherit;
  font-size: 0.85em;
}

.bootcn-message__content .bootcn-code-block {
  width: 100%;
  margin: 0 0 0.75rem;
}

/* Reasoning: a native disclosure. */
.bootcn-reasoning {
  color: var(--bootcn-muted-foreground);
  font-size: 0.875rem;
}

.bootcn-reasoning__summary {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  border-radius: var(--bootcn-radius-sm);
  cursor: pointer;
  list-style: none;
}

.bootcn-reasoning__summary::-webkit-details-marker {
  display: none;
}

.bootcn-reasoning__summary:hover {
  color: var(--bootcn-foreground);
}

.bootcn-reasoning__summary:focus-visible {
  outline: 0;
  box-shadow: var(--bootcn-focus-ring);
}

.bootcn-reasoning__chevron {
  transition: transform var(--bootcn-transition-duration) var(--bootcn-transition-timing);
}

.bootcn-reasoning[open] .bootcn-reasoning__chevron {
  transform: rotate(180deg);
}

.bootcn-reasoning__content {
  margin-top: 0.5rem;
  padding-left: 0.75rem;
  border-left: 2px solid var(--bootcn-border-color);
}

.bootcn-reasoning__content p {
  margin: 0;
}

/* Sources */
.bootcn-message__sources {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.8125rem;
}

.bootcn-message__sources-label {
  color: var(--bootcn-muted-foreground);
}

.bootcn-message__source-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.375rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

.bootcn-message__source {
  display: inline-flex;
  padding: 0.125rem 0.625rem;
  border: var(--bootcn-border-width) solid var(--bootcn-border-color);
  border-radius: 9999px;
  color: var(--bootcn-foreground);
  text-decoration: none;
}

.bootcn-message__source:hover {
  background-color: var(--bootcn-accent);
}

.bootcn-message__source:focus-visible {
  outline: 0;
  box-shadow: var(--bootcn-focus-ring);
}

/* Actions */
.bootcn-message__actions {
  display: flex;
  gap: 0.125rem;
  margin-left: -0.375rem;
}

.bootcn-message__action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  border: 0;
  border-radius: var(--bootcn-radius);
  background: transparent;
  color: var(--bootcn-muted-foreground);
}

.bootcn-message__action:hover {
  background-color: var(--bootcn-accent);
  color: var(--bootcn-foreground);
}

.bootcn-message__action:focus-visible {
  outline: 0;
  box-shadow: var(--bootcn-focus-ring);
}

.bootcn-message__action[aria-pressed='true'] {
  color: var(--bootcn-foreground);
}

.bootcn-message__action[aria-pressed='true'] .bootcn-icon {
  fill: currentColor;
  fill-opacity: 0.2;
}

.bootcn-message__done-icon,
.bootcn-message__action.is-copied .bootcn-message__copy-icon {
  display: none;
}

.bootcn-message__action.is-copied .bootcn-message__done-icon {
  display: inline-block;
}

/* Typing indicator */
.bootcn-message--typing .bootcn-message__avatar {
  visibility: hidden;
}

.bootcn-typing {
  display: inline-flex;
  gap: 0.25rem;
  align-items: center;
  height: 2rem;
}

.bootcn-typing span {
  width: 0.375rem;
  height: 0.375rem;
  border-radius: 9999px;
  background-color: var(--bootcn-muted-foreground);
  animation: bootcn-typing 1.2s infinite ease-in-out;
}

.bootcn-typing span:nth-child(2) {
  animation-delay: 0.15s;
}

.bootcn-typing span:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes bootcn-typing {
  0%,
  80%,
  100% {
    opacity: 0.3;
    transform: translateY(0);
  }

  40% {
    opacity: 1;
    transform: translateY(-0.125rem);
  }
}

@media (prefers-reduced-motion: reduce) {
  .bootcn-typing span {
    animation: none;
  }

  .bootcn-reasoning__chevron {
    transition: none;
  }
}
js
(function () {
  'use strict';

  const ns = (window.Bootcn = window.Bootcn || {});
  if (ns.conversation) return; // already loaded: idempotent

  const ROOT = '[data-bootcn-conversation]';
  const LOG = '[data-bootcn-conversation-log]';
  const NEAR_BOTTOM = 48; // px: "at the latest message" below this distance
  const timers = new WeakMap();
  let listening = false;

  // Clipboard API, else a hidden textarea and execCommand (older browsers,
  // pages without clipboard permission).
  function writeClipboard(text) {
    if (navigator.clipboard && window.isSecureContext) {
      return navigator.clipboard.writeText(text).then(
        function () {
          return true;
        },
        function () {
          return legacyCopy(text);
        },
      );
    }
    return Promise.resolve(legacyCopy(text));
  }

  function legacyCopy(text) {
    const area = document.createElement('textarea');
    area.value = text;
    area.setAttribute('readonly', '');
    area.style.position = 'fixed';
    area.style.opacity = '0';
    document.body.appendChild(area);
    const active = document.activeElement;
    area.select();
    let ok;
    try {
      ok = document.execCommand('copy');
    } catch {
      ok = false;
    }
    area.remove();
    if (active && active.focus) active.focus();
    return ok;
  }

  // Announces text in a live region, even when it is the same as last time.
  function announce(region, text) {
    if (!region) return;
    region.textContent = '';
    setTimeout(function () {
      region.textContent = text;
    }, 50);
  }

  function logOf(root) {
    return root && root.querySelector(LOG);
  }

  function atBottom(log) {
    return log.scrollHeight - log.scrollTop - log.clientHeight <= NEAR_BOTTOM;
  }

  function updateLatest(root) {
    const log = logOf(root);
    const button = root.querySelector('[data-bootcn-conversation-latest]');
    if (log && button) button.hidden = atBottom(log);
  }

  function reducedMotion() {
    return window.matchMedia('(prefers-reduced-motion: reduce)').matches;
  }

  function scrollToLatest(root, smooth) {
    const log = logOf(root);
    if (!log) return;
    log.scrollTo({
      top: log.scrollHeight,
      behavior: smooth && !reducedMotion() ? 'smooth' : 'auto',
    });
    updateLatest(root);
  }

  // Plain text into paragraphs (blank lines split them); never parsed as HTML.
  function setText(message, text) {
    const content = message.querySelector('.bootcn-message__content');
    if (!content) return;
    content.replaceChildren();
    String(text)
      .split(/\n{2,}/)
      .filter(function (part) {
        return part.trim() !== '';
      })
      .forEach(function (part) {
        const p = document.createElement('p');
        p.textContent = part.trim();
        content.appendChild(p);
      });
    const root = message.closest(ROOT);
    if (root && root.dataset.bootcnStick === 'true') scrollToLatest(root, false);
  }

  // Adds a message from the user or the assistant template. `html` is
  // inserted as is: pass only markup you built or sanitized.
  function append(root, options) {
    const log = logOf(root);
    if (!log) return null;
    const role = options && options.role === 'user' ? 'user' : 'assistant';
    const template = root.querySelector('[data-bootcn-conversation-template="' + role + '"]');
    if (!template) return null;
    const message = template.content.firstElementChild.cloneNode(true);
    const stick = atBottom(log);
    const typing = log.querySelector('[data-bootcn-conversation-typing]');
    log.insertBefore(message, typing);
    const content = message.querySelector('.bootcn-message__content');
    if (options && typeof options.html === 'string') content.innerHTML = options.html;
    else setText(message, (options && options.text) || '');
    root.dataset.bootcnStick = String(stick);
    if (stick || role === 'user') scrollToLatest(root, false);
    else updateLatest(root);
    return message;
  }

  function setTyping(root, typing) {
    const log = logOf(root);
    if (!log) return;
    const indicator = log.querySelector('[data-bootcn-conversation-typing]');
    const stick = atBottom(log);
    if (indicator) indicator.hidden = !typing;
    if (typing) log.setAttribute('aria-busy', 'true');
    else log.removeAttribute('aria-busy');
    if (stick) scrollToLatest(root, false);
  }

  function clear(root) {
    const log = logOf(root);
    if (!log) return;
    log
      .querySelectorAll('.bootcn-message:not([data-bootcn-conversation-typing])')
      .forEach(function (message) {
        message.remove();
      });
    setTyping(root, false);
    updateLatest(root);
  }

  function copy(button) {
    const message = button.closest('.bootcn-message');
    const root = button.closest(ROOT);
    const content = message && message.querySelector('.bootcn-message__content');
    if (!content || !root) return;
    const text = content.innerText.trim();
    writeClipboard(text).then(function (ok) {
      if (!ok) return;
      button.classList.add('is-copied');
      clearTimeout(timers.get(button));
      timers.set(
        button,
        setTimeout(function () {
          button.classList.remove('is-copied');
        }, 2000),
      );
      announce(root.querySelector('[data-bootcn-conversation-status]'), 'Copied to the clipboard.');
      message.dispatchEvent(
        new CustomEvent('bootcn:conversation:copy', { bubbles: true, detail: { text: text } }),
      );
    });
  }

  function feedback(button) {
    const group = button.closest('.bootcn-message__actions');
    const pressed = button.getAttribute('aria-pressed') !== 'true';
    group.querySelectorAll('[data-bootcn-message-feedback]').forEach(function (other) {
      other.setAttribute('aria-pressed', 'false');
    });
    button.setAttribute('aria-pressed', String(pressed));
    button.dispatchEvent(
      new CustomEvent('bootcn:conversation:feedback', {
        bubbles: true,
        detail: { value: pressed ? button.getAttribute('data-bootcn-message-feedback') : null },
      }),
    );
  }

  function onClick(event) {
    const target = event.target.closest && event.target.closest(ROOT + ' button');
    if (!target) return;
    const root = target.closest(ROOT);
    if (target.hasAttribute('data-bootcn-conversation-latest')) {
      scrollToLatest(root, true);
      const log = logOf(root);
      if (log) log.focus({ preventScroll: true });
    } else if (target.hasAttribute('data-bootcn-message-copy')) {
      copy(target);
    } else if (target.hasAttribute('data-bootcn-message-feedback')) {
      feedback(target);
    } else if (target.hasAttribute('data-bootcn-message-regenerate')) {
      target.dispatchEvent(new CustomEvent('bootcn:conversation:regenerate', { bubbles: true }));
    }
  }

  function onScroll(event) {
    const log = event.target;
    if (!(log instanceof Element) || !log.matches(LOG)) return;
    const root = log.closest(ROOT);
    if (!root) return;
    root.dataset.bootcnStick = String(atBottom(log));
    updateLatest(root);
  }

  function init() {
    if (!listening) {
      document.addEventListener('click', onClick);
      document.addEventListener('scroll', onScroll, true); // scroll does not bubble
      listening = true;
    }
    document.querySelectorAll(ROOT).forEach(function (root) {
      scrollToLatest(root, false);
      root.dataset.bootcnStick = 'true';
    });
  }

  function destroy() {
    if (!listening) return;
    document.removeEventListener('click', onClick);
    document.removeEventListener('scroll', onScroll, true);
    listening = false;
  }

  ns.conversation = {
    init: init,
    destroy: destroy,
    append: append,
    setText: setText,
    setTyping: setTyping,
    scrollToLatest: scrollToLatest,
    clear: clear,
  };

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', init);
  } else {
    init();
  }
})();

Released under the MIT License. Not affiliated with Bootstrap or shadcn/ui.