Button Group
Native button group composition, package imports, and editable Swift source.
Preview


Use it
Import SwiftCN to use CNButtonGroup directly. This complete example also runs in the native component gallery.
import SwiftUI
import SwiftCN
struct ButtonGroupExample: View {
var body: some View {
VStack(alignment: .leading, spacing: 12) {
CNButtonGroup {
CNButton("Previous", variant: .outline, action: {})
CNButton("Next", variant: .outline, action: {})
CNDropdownMenu("More") { CNDropdownMenuItem("Export") {} }
}
}
}
}Native behavior
Group arbitrary native controls horizontally or vertically. Horizontal groups stack when their ideal widths exceed the available space. The same child controls and their local state remain in the layout.
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
public enum CNButtonVariant: String, CaseIterable, Sendable {
case primary, secondary, outline, destructive, ghost, link
public var classes: TWClasses { TWClasses("button-\(rawValue)") }
}
public enum CNButtonSize: Sendable {
case regular, small, large, icon
public var classes: TWClasses {
switch self {
case .regular: ""
case .small: "control-sm"
case .large: "control-lg"
case .icon:
// Icon labels are narrower than the platform's touch target.
#if os(iOS)
"rounded-full min-w-[44]"
#else
"rounded-full min-w-[32]"
#endif
}
}
}
/// Native Button: roles, keyboard shortcuts, disabled state and activation stay with SwiftUI.
public struct CNButton<Label: View>: View {
private let role: ButtonRole?
private let variant: CNButtonVariant
private let size: CNButtonSize
private let classes: TWClasses
private let action: () -> Void
private let label: Label
@FocusState private var isFocused: Bool
public init(role: ButtonRole? = nil, variant: CNButtonVariant = .primary, size: CNButtonSize = .regular,
classes: TWClasses = "", action: @escaping () -> Void, @ViewBuilder label: () -> Label) {
self.role = role; self.variant = variant; self.size = size; self.classes = classes
self.action = action; self.label = label()
}
public init(_ title: LocalizedStringKey, role: ButtonRole? = nil, variant: CNButtonVariant = .primary,
size: CNButtonSize = .regular, classes: TWClasses = "", action: @escaping () -> Void) where Label == Text {
self.init(role: role, variant: variant, size: size, classes: classes, action: action) { Text(title) }
}
public var body: some View {
Button(role: role, action: action) { label }
.focused($isFocused)
.buttonStyle(.tw(cn(variant.classes, size.classes, classes), state: .init(isFocused: isFocused)))
}
}
public struct CNButtonGroup<Content: View>: View {
private let axis: Axis
private let classes: TWClasses
private let content: Content
public init(axis: Axis = .horizontal, classes: TWClasses = "", @ViewBuilder content: () -> Content) {
self.axis = axis; self.classes = classes; self.content = content()
}
public var body: some View {
let layout = axis == .horizontal ? AnyLayout(CNAdaptiveActionLayout(spacing: 4)) : AnyLayout(VStackLayout(spacing: 4))
layout { content }.tw(cn("button-group", classes))
}
}