Components

Chat

A conversation with incoming and outgoing bubbles, a typing indicator and an input bar. Built in all 5 Effect UI styles, in light and dark mode.

Chat in Prism, light mode
Chat in Prism, dark mode
Prism ProLiquid Glass on living colour.
Chat in Ledger, light mode
Chat in Ledger, dark mode
Ledger FreeCalm, precise, trustworthy.
Chat in Bounce, light mode
Chat in Bounce, dark mode
Bounce ProChunky, tactile, joyful.
Chat in Folio, light mode
Chat in Folio, dark mode
Folio ProEditorial and literary.
Chat in Bloom, light mode
Chat in Bloom, dark mode
Bloom ProSoft, organic, calming.

Source: Ledger

ChatView.swift in the free Ledger style. It needs Ledger's Theme.swift, which comes with the style: join early access on the home page and it's emailed to you.

import SwiftUI

// MARK: - Sample data

private enum ChatItem: Identifiable {
    case day(String)
    case text(id: Int, mine: Bool, body: String, time: String? = nil, reaction: String? = nil)
    case request(id: Int, mine: Bool, time: String?)
    case file(id: Int, mine: Bool, time: String?)
    case typing

    var id: String {
        switch self {
        case .day(let s): return "day-\(s)"
        case .text(let id, _, _, _, _), .request(let id, _, _), .file(let id, _, _): return "m\(id)"
        case .typing: return "typing"
        }
    }
}

private let conversation: [ChatItem] = [
    .day("Yesterday · Nov 26"),
    .text(id: 1, mine: false, body: "Con Edison came in at $92.18 for November. Up from $71.40 in October."),
    .text(id: 2, mine: true, body: "The heating went on mid-month. Split it the usual way?"),
    .text(id: 3, mine: false, body: "Half each. Sending the request now.", time: "8:14 PM"),
    .request(id: 4, mine: false, time: nil),
    .text(id: 5, mine: true, body: "Paid. Thanks for keeping track.", time: "8:21 PM", reaction: "hand.thumbsup.fill"),
    .day("Today · Nov 27"),
    .text(id: 6, mine: false, body: "Groceries are at $412.80 of $600.00 with three days left."),
    .text(id: 7, mine: true, body: "Saturday’s Costco run was most of it. We have $187.20 left, so we’re fine."),
    .file(id: 8, mine: true, time: "6:02 PM"),
    .text(id: 9, mine: false, body: "Good. I’ll cook on Friday then. Pasta, nothing fancy.", time: "6:10 PM"),
    .typing,
]

// MARK: - Screen

struct ChatView: View {
    @State private var draft = "I’ll pick up bread on the way home."
    @FocusState private var composing: Bool

    var body: some View {
        VStack(spacing: 0) {
            header
            ScrollView {
                LazyVStack(alignment: .leading, spacing: Theme.Space.sm) {
                    ForEach(conversation) { item in
                        row(item)
                    }
                }
                .padding(.horizontal, Theme.Space.margin)
                .padding(.vertical, Theme.Space.lg)
            }
            .defaultScrollAnchor(.bottom)
            .scrollIndicators(.hidden)
            .scrollDismissesKeyboard(.interactively)
        }
        .paperBackground()
        .safeAreaInset(edge: .bottom, spacing: 0) { inputBar }
    }

    // MARK: Header

    private var header: some View {
        VStack(spacing: 0) {
            HStack(spacing: Theme.Space.md) {
                IconButton(symbol: "chevron.left")
                Avatar(initials: "JR", size: 40)
                VStack(alignment: .leading, spacing: Theme.Space.xxs) {
                    Text("Jonah Reyes")
                        .ledgerType(.headlineMd)
                        .foregroundStyle(Theme.onSurface)
                    Text("Online · 3 shared budgets")
                        .ledgerType(.bodySm)
                        .foregroundStyle(Theme.onSurfaceVariant)
                }
                Spacer(minLength: Theme.Space.sm)
                IconButton(symbol: "ellipsis")
            }
            .padding(.horizontal, Theme.Space.margin)
            .padding(.top, Theme.Space.sm)
            .padding(.bottom, Theme.Space.md)
            Hairline(strong: true)
        }
        .background(Theme.surface)
    }

    // MARK: Rows

    @ViewBuilder
    private func row(_ item: ChatItem) -> some View {
        switch item {
        case .day(let label):
            HStack(spacing: Theme.Space.sm) {
                MicroLabel(label)
                Hairline()
            }
            .padding(.vertical, Theme.Space.md)

        case let .text(_, mine, body, time, reaction):
            MessageRow(mine: mine, time: time, reaction: reaction) {
                Text(body)
                    .ledgerType(.bodyMd)
                    .foregroundStyle(Theme.onSurface)
                    .fixedSize(horizontal: false, vertical: true)
                    .padding(.horizontal, Theme.Space.md)
                    .padding(.vertical, 10)
            }

        case let .request(_, mine, time):
            MessageRow(mine: mine, time: time, reaction: nil) {
                RequestCard()
            }

        case let .file(_, mine, time):
            MessageRow(mine: mine, time: time, reaction: nil) {
                FileAttachment()
            }

        case .typing:
            MessageRow(mine: false, time: nil, reaction: nil) {
                TypingDots()
            }
            .accessibilityLabel("Jonah is typing")
        }
    }

    // MARK: Input

    private var inputBar: some View {
        VStack(spacing: 0) {
            Hairline()
            HStack(spacing: Theme.Space.sm) {
                IconButton(symbol: "paperclip")
                    .accessibilityLabel("Attach")
                TextField("Message Jonah", text: $draft, axis: .vertical)
                    .ledgerType(.bodyMd)
                    .foregroundStyle(Theme.onSurface)
                    .lineLimit(1...3)
                    .focused($composing)
                    .ledgerField(focused: composing)
                Button {
                    draft = ""
                } label: {
                    Image(systemName: "arrow.up")
                        .font(.system(size: 15, weight: .semibold))
                        .foregroundStyle(Theme.onPrimary)
                        .frame(width: 36, height: 36)
                        .background(
                            RoundedRectangle(cornerRadius: Theme.Radius.md, style: .continuous)
                                .fill(Theme.primary)
                        )
                }
                .buttonStyle(.plain)
                .disabled(draft.trimmingCharacters(in: .whitespaces).isEmpty)
                .opacity(draft.trimmingCharacters(in: .whitespaces).isEmpty ? 0.38 : 1)
                .animation(Theme.Motion.stateChange, value: draft.isEmpty)
                .accessibilityLabel("Send")
            }
            .padding(.horizontal, Theme.Space.margin)
            .padding(.vertical, Theme.Space.sm)
        }
        .background(Theme.surface.ignoresSafeArea(edges: .bottom))
    }
}

// MARK: - Bubble

private struct MessageRow<Content: View>: View {
    let mine: Bool
    let time: String?
    let reaction: String?
    @ViewBuilder var content: Content

    private var shape: UnevenRoundedRectangle {
        UnevenRoundedRectangle(
            topLeadingRadius: Theme.Radius.md,
            bottomLeadingRadius: mine ? Theme.Radius.md : 2,
            bottomTrailingRadius: mine ? 2 : Theme.Radius.md,
            topTrailingRadius: Theme.Radius.md,
            style: .continuous
        )
    }

    var body: some View {
        VStack(alignment: mine ? .trailing : .leading, spacing: Theme.Space.xs) {
            content
                .background(shape.fill(mine ? Theme.primaryTint : Theme.surfaceRaised))
                .overlay(shape.strokeBorder(mine ? Color.clear : Theme.rule, lineWidth: 0.5))
                .overlay(alignment: mine ? .bottomLeading : .bottomTrailing) {
                    if let reaction {
                        ReactionChip(symbol: reaction)
                            .offset(x: mine ? -10 : 10, y: 14)
                    }
                }
                .padding(.bottom, reaction == nil ? 0 : Theme.Space.md)
            if let time {
                Text(time)
                    .font(.system(size: 11, weight: .regular, design: .monospaced))
                    .foregroundStyle(Theme.onSurfaceMuted)
                    .padding(.horizontal, Theme.Space.xxs)
            }
        }
        .frame(maxWidth: 288, alignment: mine ? .trailing : .leading)
        .frame(maxWidth: .infinity, alignment: mine ? .trailing : .leading)
        .padding(.bottom, time == nil ? 0 : Theme.Space.sm)
    }
}

private struct ReactionChip: View {
    let symbol: String

    var body: some View {
        HStack(spacing: Theme.Space.xxs) {
            Image(systemName: symbol)
                .font(.system(size: 11, weight: .semibold))
            Text("1")
                .font(.system(size: 11, weight: .semibold, design: .monospaced))
        }
        .foregroundStyle(Theme.onSurface)
        .padding(.horizontal, 6)
        .frame(height: 22)
        .background(RoundedRectangle(cornerRadius: Theme.Radius.sm, style: .continuous).fill(Theme.surfaceRaised))
        .overlay(RoundedRectangle(cornerRadius: Theme.Radius.sm, style: .continuous).strokeBorder(Theme.rule, lineWidth: 0.5))
        .accessibilityLabel("Jonah reacted with thumbs up")
    }
}

/// Split request preview: a small ruled receipt inside the bubble.
private struct RequestCard: View {
    var body: some View {
        VStack(alignment: .leading, spacing: 0) {
            HStack(spacing: Theme.Space.sm) {
                Image(systemName: "arrow.left.arrow.right")
                    .font(.system(size: 11, weight: .semibold))
                    .foregroundStyle(Theme.onSurfaceMuted)
                MicroLabel("Split request")
                Spacer()
                MicroLabel("Paid", color: Theme.onSurface)
            }
            .padding(.bottom, Theme.Space.sm)
            Hairline(strong: true)
            line("Con Edison · Nov", Money.format(92.18, decimals: 2), muted: true)
            Hairline()
            line("Your half", Money.format(46.09, decimals: 2), muted: false)
            Hairline()
            Text("Due Dec 1 · Shared: 41 Bergen St")
                .ledgerType(.bodySm)
                .foregroundStyle(Theme.onSurfaceMuted)
                .padding(.top, Theme.Space.sm)
        }
        .padding(Theme.Space.md)
        .frame(width: 248)
    }

    private func line(_ title: String, _ amount: String, muted: Bool) -> some View {
        HStack {
            Text(title)
                .ledgerType(.bodySm)
                .foregroundStyle(muted ? Theme.onSurfaceVariant : Theme.onSurface)
            Spacer()
            Text(amount)
                .ledgerType(.numeralMd)
                .foregroundStyle(Theme.onSurface)
        }
        .frame(height: 36)
    }
}

private struct FileAttachment: View {
    var body: some View {
        HStack(spacing: Theme.Space.md) {
            RoundedRectangle(cornerRadius: Theme.Radius.tile, style: .continuous)
                .fill(Theme.surfaceRaised)
                .frame(width: 36, height: 36)
                .overlay(
                    Image(systemName: "tablecells")
                        .font(.system(size: 15, weight: .semibold))
                        .foregroundStyle(Theme.onSurface)
                )
                .overlay(
                    RoundedRectangle(cornerRadius: Theme.Radius.tile, style: .continuous)
                        .strokeBorder(Theme.rule, lineWidth: 0.5)
                )
            VStack(alignment: .leading, spacing: Theme.Space.xxs) {
                Text("groceries-november.csv")
                    .font(.system(size: 13, weight: .semibold, design: .monospaced))
                    .foregroundStyle(Theme.onSurface)
                    .lineLimit(1)
                Text("18 rows · 2 KB")
                    .ledgerType(.bodySm)
                    .monospacedDigit()
                    .foregroundStyle(Theme.onSurfaceVariant)
            }
        }
        .padding(Theme.Space.md)
    }
}

/// Static, not looping: three dots stepping down in ink.
private struct TypingDots: View {
    var body: some View {
        HStack(spacing: 5) {
            ForEach([1.0, 0.6, 0.3], id: \.self) { o in
                Circle()
                    .fill(Theme.onSurfaceVariant)
                    .frame(width: 6, height: 6)
                    .opacity(o)
            }
        }
        .padding(.horizontal, Theme.Space.md)
        .frame(height: 36)
    }
}

#Preview("Light") {
    ChatView()
}

#Preview("Dark") {
    ChatView().preferredColorScheme(.dark)
}

More screens