Files
jarvis/Jarvis/Views/Shared/SourceChips.swift

43 lines
1.2 KiB
Swift
Raw Normal View History

// SourceChips.swift
// Jarvis names the first 2 sources, collapses the rest into "+N".
import SwiftUI
struct SourceChips: View {
/// Source names in display order.
let names: [String]
/// Total number of sources (may exceed `names.count`).
let total: Int
var maxNamed: Int = 2
private var named: [String] { Array(names.prefix(maxNamed)) }
private var overflow: Int { max(0, total - named.count) }
var body: some View {
HStack(spacing: 6) {
ForEach(named, id: \.self) { name in
chip(name)
}
if overflow > 0 {
chip("+\(overflow)", muted: true)
}
}
}
private func chip(_ text: String, muted: Bool = false) -> some View {
Text(text)
.font(.system(size: 12, weight: .bold))
.foregroundStyle(muted ? Color(hex: "777777") : Color(hex: "CCCCCC"))
.padding(.horizontal, 9)
.padding(.vertical, 5)
.background(Palette.surface2)
.clipShape(Capsule())
}
}
#Preview {
SourceChips(names: ["Daily Monitor", "NilePost", "The EastAfrican"], total: 7)
.padding()
.background(Color.black)
}