Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add super button style. #67

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 43 additions & 12 deletions Sources/Compound/BaseStyles/CompoundButtonStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,18 @@ public extension ButtonStyle where Self == CompoundButtonStyle {
/// Default button style for standalone buttons.
public struct CompoundButtonStyle: ButtonStyle {
@Environment(\.isEnabled) private var isEnabled
@Environment(\.colorScheme) private var colorScheme

var kind: Kind
public enum Kind {
/// A plain button with no background.
case plain
/// A stroked button that uses colour to highlight important actions.
case `super`
/// A filled button usually representing the default action.
case primary
/// A stroked button usually representing alternate actions.
case secondary
/// A plain button with no background. This will be renamed to `tertiary` in the future.
case plain
}

var size: Size
Expand Down Expand Up @@ -69,15 +72,19 @@ public struct CompoundButtonStyle: ButtonStyle {
return 14
}
}

private var maxWidth: CGFloat? {
switch kind {
switch kind { // This is wrong, it should be switching on size.
case .super, .primary, .secondary:
return .infinity
case .plain:
return nil
case .primary, .secondary:
return .infinity
}
}

private var pressedOpacity: Double {
colorScheme == .light ? 0.3 : 0.6
}

public func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
Expand All @@ -96,28 +103,43 @@ public struct CompoundButtonStyle: ButtonStyle {
@ViewBuilder
private func makeBackground(configuration: Self.Configuration) -> some View {
switch kind {
case .plain:
EmptyView()
case .super:
if isEnabled {
ZStack {
Capsule().fill(.compound.bgCanvasDefault)
Capsule().fill(LinearGradient(gradient: Color.compound.gradientSuperButton,
startPoint: .bottomLeading, endPoint: .topTrailing))
.opacity(0.04)
Capsule().stroke(LinearGradient(gradient: Color.compound.gradientSuperButton,
startPoint: .bottomLeading, endPoint: .topTrailing))
}
.compositingGroup()
.opacity(configuration.isPressed ? pressedOpacity : 1)
} else {
Capsule().stroke(buttonColor(configuration: configuration))
}
case .primary:
Capsule().fill(buttonColor(configuration: configuration))
case .secondary:
Capsule().stroke(buttonColor(configuration: configuration))
case .plain:
EmptyView()
}
}

private var contentShape: AnyShape {
switch kind {
case .super, .primary, .secondary:
return AnyShape(Capsule())
case .plain:
return AnyShape(Rectangle())
case .primary, .secondary:
return AnyShape(Capsule())
}
}

private func buttonColor(configuration: Self.Configuration) -> Color {
guard isEnabled else { return .compound.bgActionPrimaryDisabled }
if configuration.role == .destructive {
return .compound.bgCriticalPrimary.opacity(configuration.isPressed ? 0.6 : 1)
return .compound.bgCriticalPrimary.opacity(configuration.isPressed ? pressedOpacity : 1)
} else {
return configuration.isPressed ? .compound.bgActionPrimaryPressed : .compound.bgActionPrimaryRest
}
Expand All @@ -129,7 +151,7 @@ public struct CompoundButtonStyle: ButtonStyle {
} else {
guard isEnabled else { return .compound.textDisabled }
let textColor: Color = configuration.role == .destructive ? .compound.textCriticalPrimary : .compound.textActionPrimary
return textColor.opacity(kind == .plain && configuration.isPressed ? 0.6 : 1.0)
return textColor.opacity(configuration.isPressed ? pressedOpacity : 1)
}
}
}
Expand All @@ -151,14 +173,23 @@ public struct CompoundButtonStyle_Previews: PreviewProvider, PrefireProvider {

Section {
plain
.padding(.bottom) // Only for the snapshot.
} header: {
Header(title: "Plain")
}
}
.previewLayout(.fixed(width: 390, height: 975))
}

public static func states(_ size: CompoundButtonStyle.Size) -> some View {
VStack {
Button("Super") { }
.buttonStyle(.compound(.super, size: size))

Button("Disabled") { }
.buttonStyle(.compound(.super, size: size))
.disabled(true)

Button("Primary") { }
.buttonStyle(.compound(.primary, size: size))

Expand Down
3 changes: 3 additions & 0 deletions Sources/Compound/Colors/CompoundColors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ public struct CompoundColors {
/// This token is a placeholder and hasn't been finalised.
@available(iOS, deprecated: 17.0, message: "This token should be generated by now.")
public let _textOwnPill = compound.colorGreen1100

// MARK: - Gradients
public let gradientSuperButton = Gradient(colors: [compound.colorBlue900, compound.colorGreen1100])
}

private extension UITraitCollection {
Expand Down
Loading