Attachment
Native attachment composition, package imports, and editable Swift source.
Preview


Use it
Import SwiftCN to use CNAttachment directly. This complete example also runs in the native component gallery.
import SwiftUI
import SwiftCN
struct AttachmentExample: View {
var body: some View {
VStack(alignment: .leading, spacing: 12) {
CNAttachment(url: URL(string: "https://github.com/cbethin/swiftcn")!, title: "Design notes", detail: "Markdown · 12 KB")
}
}
}Native behavior
Uses native Link for explicit opening. The component does not read file contents. The host owns security-scoped access, file import, upload transport, and removal.
Style and compose
Visual recipes use .tw classes. Append classes: to override a recipe locally, or change the recipe in TWGlobalRules.named for your subtree. Native layout and interaction modifiers remain SwiftUI APIs. For control colors, append cn-tint-[primary], another theme token, or an interpolated Color.
Motion recipes are editable too: control-motion uses a 150 ms smooth response, disclosure-motion uses a 250 ms spring, and feedback-motion uses a 200 ms smooth response. Set a recipe to animate-none in your global rules to turn it off. Presets respect Reduce Motion. Static components stay still; native scrolling, drag tracking, and platform presentation keep their own gestures.
Action groups and card, dialog, and questionnaire footers stack when their content does not fit. The native layout preserves the same child controls during this change.
View builders preserve native child content. Native Button roles, labels, keyboard shortcuts, submit actions, and presentation modifiers still work. A string recipe does not replace a native control API or a typed chart mark.
Own the source
Copy the complete file below into your app alongside import SwiftCN. The file contains related composable parts from the same family. Its dependencies use public package APIs, so copied and imported components can coexist.
To own the styling engine too, download the complete source bundle. It contains every component, the .tw engine, its recipes, and the MIT license. Keep those files in one module and remove import SwiftCN from the usage example.
import SwiftUI
import SwiftCN
/// Opening and removal are explicit actions. This view never accesses file contents on its own.
public struct CNAttachment<Preview: View>: View {
private let url: URL
private let title: String
private let detail: String?
private let progress: Double?
private let onRemove: (() -> Void)?
private let classes: TWClasses
private let preview: Preview
public init(url: URL, title: String, detail: String? = nil, progress: Double? = nil, classes: TWClasses = "",
onRemove: (() -> Void)? = nil, @ViewBuilder preview: () -> Preview) {
self.url = url; self.title = title; self.detail = detail; self.progress = progress
self.classes = classes; self.onRemove = onRemove; self.preview = preview()
}
public init(url: URL, title: String, detail: String? = nil, progress: Double? = nil, classes: TWClasses = "",
onRemove: (() -> Void)? = nil) where Preview == Image {
self.init(url: url, title: title, detail: detail, progress: progress, classes: classes, onRemove: onRemove) {
Image(systemName: "doc")
}
}
public var body: some View {
HStack(alignment: .center, spacing: 12) {
preview.accessibilityHidden(true)
VStack(alignment: .leading, spacing: 4) {
Link(title, destination: url).buttonStyle(.tw("button-link px-0"))
if let detail { Text(detail).tw("text-xs text-mutedForeground") }
if let progress { CNProgress("Upload progress", value: progress) }
}
if let onRemove {
CNButton(variant: .ghost, size: .icon, action: onRemove) { Image(systemName: "xmark") }
.accessibilityLabel("Remove \(title)")
}
}.tw(cn("attachment", classes))
}
}