SPB Git

Repository Public MIT

glass-chat-bubble.md 3.5 KB

# GlassChatBubble

Identifier glass-chat-bubble
Module SPGlassAI
Kind content-component — text on an opaque bubble; no glass
Platforms iOS 26, iPadOS 26, macOS 26
Example Examples/Gallery/AI/GlassChatBubbleExample.swift

UI only: the component displays a message value. It contains no AI provider, no key and no network code.

# Minimal usage

swift
GlassChatBubble(GlassChatMessage(id: "1", role: .assistant, text: "Hello."))

# Variants

Variant Use
role: .user Trailing edge, filled with the theme's primary colour, labelled "You".
role: .assistant, .system, .tool Leading edge, opaque themed surface, labelled "Assistant", "System", "Tool".
state: .streaming A "Receiving response" indicator follows the text; the copy button is hidden until the text is final.
state: .failed(reason:) A warning symbol with the reason (or a generic message) under the bubble, and "Retry" when onRetry is given.
usesMonospacedFont: true Code-like content such as tool output.

# Parameters

Parameter Type Default Meaning
message GlassChatMessage — id, role, text, state.
maxWidthFraction Double 0.8 Largest share of the container's width the bubble takes; clamped to 0.4…1.
usesMonospacedFont Bool false Monospaced text.
onCopy (@MainActor (String) -> Void)? nil Receives the text on copy. When nil the text is written to the system pasteboard.
onRetry (@MainActor () -> Void)? nil Shows "Retry" on failed messages.

# States

Complete, streaming, failed. A streaming message with empty text shows the indicator alone. After a copy the button shows a checkmark and "Copied" for 1.5 seconds.

# Advanced example

swift
ScrollView {
    LazyVStack(spacing: 12) {
        ForEach(conversation.messages) { message in      // [GlassChatMessage], updated by the host while streaming
            GlassChatBubble(
                message,
                maxWidthFraction: 0.75,
                usesMonospacedFont: message.role == .tool,
                onRetry: message.role == .assistant ? { conversation.regenerate(message.id) } : nil
            )
        }
    }
    .padding()
}
.defaultScrollAnchor(.bottom)

# Accessibility

  • The role is written above the bubble and read with the text ("Assistant, …"): it never depends on colour or side.
  • The text is selectable (textSelection(.enabled)); copy is also in the context menu.
  • While streaming the element has the "updates frequently" trait and the indicator has a text label; its symbol animation stops with Reduce Motion.
  • Failure is a symbol plus text. The copy button is labelled "Copy" / "Copied" and has a 44-point target on iOS.
  • Alignment uses leading and trailing, so the layout mirrors in right-to-left languages; right-to-left text inside a bubble keeps its natural direction.

# Limits

  • Plain text only: no Markdown rendering, no syntax highlighting, no images. usesMonospacedFont applies to the whole message.
  • Very long messages are laid out in full; put the bubbles in a lazy stack and keep the conversation in the host.
  • Unbroken strings (long URLs, identifiers) wrap by character.
  • The bubble does not scroll the conversation or keep it pinned to the bottom: that is the container's role.
  • Layout with long, code-like, emoji and right-to-left text is in the example and compiles; it was not inspected in a running application during verification.
Files main
Menu

Theme