Skip to content

Instantly share code, notes, and snippets.

@hmlongco
Created May 3, 2026 14:37
Show Gist options
  • Select an option

  • Save hmlongco/8f27f7e1cbc7d3b1fe0e06dd1ba68e7a to your computer and use it in GitHub Desktop.

Select an option

Save hmlongco/8f27f7e1cbc7d3b1fe0e06dd1ba68e7a to your computer and use it in GitHub Desktop.
DSButtonStyle with enums and static ButtonStyle extensions
import SwiftUI
public enum DSButtonStyle: ButtonStyle {
case primary
case secondary
case action
case link
public func makeBody(configuration: Configuration) -> some View {
customize(
configuration.label // common config follows
.font(.body)
.opacity(configuration.isPressed ? 0.8 : 1)
.padding(.horizontal, 14)
.padding(.vertical, 8)
.overlay {
RoundedRectangle(cornerRadius: 10)
.strokeBorder(lineWidth: 1)
}
)
}
@ViewBuilder private func customize(_ label: some View) -> some View {
switch self {
case .primary:
label.foregroundStyle(.primary)
case .secondary:
label.foregroundStyle(.secondary)
case .link:
label.foregroundStyle(.blue).font(.callout) // override font
case .action:
label.foregroundStyle(.green).bold()
}
}
}
extension ButtonStyle where Self == DSButtonStyle {
public static var dsPrimary: Self { .primary }
public static var dsSecondary: Self { .secondary }
public static var dsLink: Self { .link }
public static var dsAction: Self { .action }
}
struct TestView: View {
var body: some View {
Button("Hello, world!") {}
.buttonStyle(.dsAction)
}
}
public struct DSCustomButtonStyle: ButtonStyle {
public func makeBody(configuration: Self.Configuration) -> some View {
configuration.label.font(.headline)
}
}
extension ButtonStyle where Self == DSCustomButtonStyle {
public static var dsCustom: Self { DSCustomButtonStyle() }
}
#Preview {
TestView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment