Card
Native card composition, package imports, and editable Swift source.
Preview


Use it
Import SwiftCN to use CNCard directly. This complete example also runs in the native component gallery.
import SwiftUI
import SwiftCN
struct CardExample: View {
@State private var text = "Design system"
var body: some View {
VStack(alignment: .leading, spacing: 12) {
CNCard {
CNCardHeader { CNCardTitle("New workspace"); CNCardDescription("Keep your team in sync.") }
CNCardContent { CNInput("Workspace name", text: $text) }
CNCardFooter { CNButton("Create", action: {}) }
}
}
}
}Native behavior
Compose CNCardHeader, CNCardTitle, CNCardDescription, CNCardContent, and CNCardFooter in a native stack. Footer actions stack when space is limited. The card itself adds no activation gesture.
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
/// A composable native VStack with editable `card` defaults.
public struct CNCard<Content: View>: View {
private let classes: TWClasses
private let spacing: CGFloat
private let content: Content
public init(_ classes: TWClasses = "", spacing: CGFloat = 0, @ViewBuilder content: () -> Content) {
self.classes = classes
self.spacing = spacing
self.content = content()
}
public var body: some View {
VStack(alignment: .leading, spacing: spacing) { content }
.frame(maxWidth: .infinity, alignment: .leading)
.tw(cn("card p-0 w-full", classes))
}
}
/// A composable native VStack with editable `card-header` defaults.
public struct CNCardHeader<Content: View>: View {
private let classes: TWClasses
private let spacing: CGFloat
private let content: Content
public init(_ classes: TWClasses = "", spacing: CGFloat = 6, @ViewBuilder content: () -> Content) {
self.classes = classes
self.spacing = spacing
self.content = content()
}
public var body: some View {
VStack(alignment: .leading, spacing: spacing) { content }
.frame(maxWidth: .infinity, alignment: .leading)
.tw(cn("card-header", classes))
}
}
/// A composable native VStack with editable `card-content` defaults.
public struct CNCardContent<Content: View>: View {
private let classes: TWClasses
private let spacing: CGFloat
private let content: Content
public init(_ classes: TWClasses = "", spacing: CGFloat = 12, @ViewBuilder content: () -> Content) {
self.classes = classes
self.spacing = spacing
self.content = content()
}
public var body: some View {
VStack(alignment: .leading, spacing: spacing) { content }
.frame(maxWidth: .infinity, alignment: .leading)
.tw(cn("card-content", classes))
}
}
/// A native action row that stacks when needed, with editable `card-footer` defaults.
public struct CNCardFooter<Content: View>: View {
private let classes: TWClasses
private let spacing: CGFloat
private let content: Content
public init(_ classes: TWClasses = "", spacing: CGFloat = 12, @ViewBuilder content: () -> Content) {
self.classes = classes
self.spacing = spacing
self.content = content()
}
public var body: some View {
CNAdaptiveActionLayout(spacing: spacing) { content }
.frame(maxWidth: .infinity, alignment: .leading)
.tw(cn("card-footer", classes))
}
}
/// Styled content with an optional localized Text convenience initializer.
public struct CNCardTitle<Content: View>: View {
private let classes: TWClasses
private let content: Content
public init(_ classes: TWClasses = "", @ViewBuilder content: () -> Content) {
self.classes = classes
self.content = content()
}
public init(_ title: LocalizedStringKey, classes: TWClasses = "") where Content == Text {
self.classes = classes
content = Text(title)
}
public var body: some View { content.tw(cn("card-title", classes)) }
}
/// Styled content with an optional localized Text convenience initializer.
public struct CNCardDescription<Content: View>: View {
private let classes: TWClasses
private let content: Content
public init(_ classes: TWClasses = "", @ViewBuilder content: () -> Content) {
self.classes = classes
self.content = content()
}
public init(_ title: LocalizedStringKey, classes: TWClasses = "") where Content == Text {
self.classes = classes
content = Text(title)
}
public var body: some View { content.tw(cn("card-description", classes)) }
}