GlassChatBubble
Message bubble with role, streaming state, text selection and copy.
- Content
- iOS 26 · iPadOS 26 · macOS 26
- Version 1.1.0
glass-chat-bubble
Content on an opaque, readable surface. Glass is kept for controls.
CaptureInstallation
spglass add glass-chat-bubbleCopies the sources and their dependencies into your project. Set up the CLI.
.package(url: "https://git.spboucher.ai/sp-glasskit.git", from: "1.1.0")
import SPGlassKit| 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
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. |
| Copy | A copy button sits under finished messages of the assistant, the system and tools. The user's own messages keep the context menu only (GlassChatBubbleMetrics.showsCopyButton). |
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. |
| Fenced code and quotes | A block between two lines of three backticks is set in a monospaced inset that scrolls sideways (the word after the opening fence is shown as its language); consecutive lines starting with > are set as a quote. Nothing else is interpreted: no inline styles, no links, no HTML. rendersBlocks: false shows the text as it is. |
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. |
rendersBlocks | Bool | true | Sets fenced code and quotes apart. Ignored with usesMonospacedFont. |
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
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.
usesMonospacedFontapplies 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.
Example
import SwiftUI
import SPGlassKit
public struct GlassChatBubbleExample: GlassExample {
public static let componentID = "glass-chat-bubble"
/// Fictitious conversation: no model produced these messages.
@State private var messages = [
GlassChatMessage(id: "1", role: .system, text: "Sample data — this conversation is fictitious."),
GlassChatMessage(id: "2", role: .user, text: "Can you summarise the sample quarterly note in two sentences? 🙂"),
GlassChatMessage(id: "3", role: .assistant, text: "Certainly. The sample note reports steady demand and a slightly lower margin. It expects the next quarter to look similar, with one new product line."),
GlassChatMessage(id: "4", role: .tool, text: "{\n \"tool\": \"sample_lookup\",\n \"rows\": 3,\n \"a_very_long_key_without_any_space_to_test_wrapping_inside_the_bubble\": true\n}"),
GlassChatMessage(id: "4b", role: .assistant, text: "The note says:\n> Demand stayed steady over the quarter.\nAs a filter:\n```swift\nlet steady = notes.filter { $0.demand == .steady }\n```"),
GlassChatMessage(id: "5", role: .user, text: "هل يمكنك كتابة الخلاصة بالعربية؟"),
GlassChatMessage(id: "6", role: .assistant, text: "The sample answer is still being writ", state: .streaming),
GlassChatMessage(id: "7", role: .assistant, text: "", state: .streaming),
GlassChatMessage(id: "8", role: .assistant, text: "A partial sample answer that stopped.", state: .failed(reason: "Sample failure: the connection was interrupted.")),
]
@State private var lastCopied = "Nothing copied yet"
@State private var retryCount = 0
public init() {}
public var body: some View {
ScrollView {
LazyVStack(spacing: 12) {
ForEach(messages) { message in
GlassChatBubble(
message,
usesMonospacedFont: message.role == .tool,
onCopy: { text in lastCopied = "Copied \(text.count) characters (kept inside the example)" },
onRetry: { retryCount += 1 }
)
}
Text("\(lastCopied) · retried \(retryCount) times")
.font(.caption)
.foregroundStyle(.secondary)
}
.padding()
}
}
}
#Preview("GlassChatBubble") { GlassChatBubbleExample().glassTheme(.aurora) }
Registry entry
| Identifier | glass-chat-bubble |
|---|---|
| Version | 1.1.0 |
| Depends on | glass-foundation |
| Frameworks | SwiftUI |
| Public symbols | GlassChatRole, GlassChatMessageState, GlassChatMessage, GlassChatBubbleMetrics, GlassChatBlock, GlassChatBlocks, GlassChatBubble |
| Licence | MIT |
| Manifest | manifest.json |
Files
AI/AI/GlassChatBubble.swift18557 bytes · sha256 e3843a61610b…