Skip to content

Commit 1b382e3

Browse files
committed
Update buttons with liquid glass styling and LocalizedStringKey support
1 parent 08b29dc commit 1b382e3

2 files changed

Lines changed: 85 additions & 75 deletions

File tree

‎Sources/BlocKit/Button/PrimaryButton.swift‎

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,64 @@
88
import SwiftUI
99

1010
public struct PrimaryGradientButton: View {
11-
private var title: String
12-
private var action: () -> ()
13-
private var color: Color
14-
15-
@Environment(\.colorScheme) var scheme
16-
17-
var gradient: [Color] {
18-
[color.prominence(scheme: scheme), color.prominence(scheme: scheme).opacity(0.8)]
19-
}
20-
21-
public init(_ title: String, _ color: Color, _ action: @escaping () -> Void) {
22-
self.title = title
23-
self.color = color
24-
self.action = action
25-
}
26-
27-
public var body: some View {
28-
Button(action: action) {
29-
Text(title)
30-
.foregroundColor(.traitsBackground)
31-
.buttonText()
11+
private var title: LocalizedStringKey
12+
private var action: () -> ()
13+
private var color: Color
14+
15+
public init(_ title: LocalizedStringKey, _ color: Color, _ action: @escaping () -> Void) {
16+
self.title = title
17+
self.color = color
18+
self.action = action
19+
}
20+
21+
public var body: some View {
22+
Button(action: action) {
23+
Text(title)
24+
.font(.callout)
25+
.bold()
26+
.fontWidth(.expanded)
27+
.foregroundStyle(Color.white)
28+
.frame(maxWidth: .infinity)
29+
}
30+
.controlSize(.large)
31+
.buttonStyle(.glassProminent)
32+
.tint(color)
33+
.buttonBorderShape(.roundedRectangle(radius: 16))
34+
.accessibilityLabel(title)
35+
.accessibilityAddTraits(.isButton)
3236
}
33-
.buttonStyle(PrimaryButtonStyle({ LinearGradient(gradient: Gradient(colors: gradient), startPoint: .top, endPoint: .bottom).cornerRadius(Constants.cornerRadius) }))
34-
}
3537
}
3638

3739
public struct PrimaryButton: View {
38-
private var title: String
39-
private var action: () -> ()
40-
41-
public init(_ title: String, _ action: @escaping () -> Void) {
42-
self.title = title
43-
self.action = action
44-
}
45-
46-
public var body: some View {
47-
Button(action: action) {
48-
Text(title)
49-
.foregroundColor(.white)
50-
.buttonText()
40+
private var title: LocalizedStringKey
41+
private var action: () -> ()
42+
43+
public init(_ title: LocalizedStringKey, _ action: @escaping () -> Void) {
44+
self.title = title
45+
self.action = action
46+
}
47+
48+
public var body: some View {
49+
Button(action: action) {
50+
Text(title)
51+
.font(.callout)
52+
.bold()
53+
.fontWidth(.expanded)
54+
.foregroundStyle(Color.white)
55+
.frame(maxWidth: .infinity)
56+
}
57+
.controlSize(.large)
58+
.buttonStyle(.glassProminent)
59+
.buttonBorderShape(.roundedRectangle(radius: 16))
60+
.accessibilityLabel(title)
61+
.accessibilityAddTraits(.isButton)
5162
}
52-
.buttonStyle(PrimaryButtonStyle({ Color.accentColor.cornerRadius(Constants.cornerRadius) }))
53-
}
5463
}
5564

56-
struct ColoredButton_Previews: PreviewProvider {
57-
static var previews: some View {
65+
#Preview {
5866
VStack {
59-
PrimaryButton("Evaluate", {})
67+
PrimaryButton("Evaluate", {})
68+
PrimaryGradientButton("Next", .blue, {})
6069
}
61-
}
70+
.padding()
6271
}

‎Sources/BlocKit/Button/SecondaryButton.swift‎

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,64 +9,65 @@ import SwiftUI
99

1010
public struct SecondaryGradientButton: View {
1111
private var color: Color
12-
private var title: String
12+
private var title: LocalizedStringKey
1313
private var action: () -> ()
1414
private var disabled: Bool
15-
16-
@Environment(\.colorScheme) var scheme
17-
18-
var gradient: [Color] {
19-
if disabled {
20-
return [.gray, .gray.opacity(0.8)]
21-
} else {
22-
return [color.prominence(scheme: scheme), color.prominence(scheme: scheme).opacity(0.8)]
23-
}
24-
}
25-
26-
public init(title: String, color: Color, disabled: Bool, _ action: @escaping () -> Void) {
15+
16+
public init(title: LocalizedStringKey, color: Color, disabled: Bool = false, _ action: @escaping () -> Void) {
2717
self.title = title
2818
self.color = color
2919
self.action = action
3020
self.disabled = disabled
3121
}
32-
22+
3323
public var body: some View {
3424
Button(action: action) {
3525
Text(title)
36-
.foregroundColor(color.prominence(scheme: scheme, reverse: true))
37-
.buttonText()
26+
.font(.callout)
27+
.bold()
28+
.fontWidth(.expanded)
29+
.foregroundStyle(Color.primary)
30+
.frame(maxWidth: .infinity)
3831
}
39-
.buttonStyle(SecondaryButtonStyle {
40-
RoundedRectangle(cornerRadius: Constants.cornerRadius)
41-
.stroke(LinearGradient(gradient: Gradient(colors: gradient), startPoint: .top, endPoint: .bottom), lineWidth: 1.5)
42-
})
32+
.controlSize(.large)
33+
.buttonStyle(.glass)
34+
.buttonBorderShape(.roundedRectangle(radius: 16))
35+
.tint(disabled ? .gray : color)
36+
.accessibilityLabel(title)
37+
.accessibilityAddTraits(.isButton)
4338
}
4439
}
4540

4641
public struct SecondaryButton: View {
47-
private var title: String
42+
private var title: LocalizedStringKey
4843
private var action: () -> ()
49-
50-
public init(_ title: String, _ action: @escaping () -> Void) {
44+
45+
public init(_ title: LocalizedStringKey, _ action: @escaping () -> Void) {
5146
self.title = title
5247
self.action = action
5348
}
54-
49+
5550
public var body: some View {
5651
Button(action: action) {
5752
Text(title)
58-
.foregroundColor(.primary)
59-
.buttonText()
53+
.font(.callout)
54+
.bold()
55+
.fontWidth(.expanded)
56+
.foregroundStyle(Color.primary)
57+
.frame(maxWidth: .infinity)
6058
}
61-
.buttonStyle(SecondaryButtonStyle {
62-
RoundedRectangle(cornerRadius: Constants.cornerRadius)
63-
.stroke(Color.accentColor, lineWidth: 1)
64-
})
59+
.controlSize(.large)
60+
.buttonStyle(.glass)
61+
.buttonBorderShape(.roundedRectangle(radius: 16))
62+
.accessibilityLabel(title)
63+
.accessibilityAddTraits(.isButton)
6564
}
6665
}
6766

68-
struct SecondaryButton_Previews: PreviewProvider {
69-
static var previews: some View {
70-
SecondaryButton("Try again", {})
67+
#Preview {
68+
VStack {
69+
SecondaryButton("Try Again", {})
70+
SecondaryGradientButton(title: "Show Score", color: .blue, disabled: false, {})
7171
}
72+
.padding()
7273
}

0 commit comments

Comments
 (0)