Skip to content

Commit 64bd7b3

Browse files
committed
Add border style to capsule style
1 parent 7040a04 commit 64bd7b3

File tree

3 files changed

+113
-38
lines changed

3 files changed

+113
-38
lines changed

Sources/TagKit/Views/TagCapsule.swift

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public struct TagCapsule: View {
3535
.foregroundColor(style.foregroundColor)
3636
.materialCapsuleBackground(with: style.backgroundMaterial)
3737
.background(Capsule().fill(style.backgroundColor))
38-
.padding(style.borderWidth)
38+
.padding(style.border.width)
3939
.background(Capsule()
4040
.strokeBorder(
41-
style.borderColor,
42-
lineWidth: style.borderWidth
41+
style.border.color,
42+
lineWidth: style.border.width
4343
)
4444
)
4545
.compositingGroup()
@@ -81,23 +81,42 @@ private extension View {
8181
TagCapsule("standard-selected")
8282
.tagCapsuleStyle(.standardSelected)
8383
}
84-
85-
TagCapsule("spider-man")
86-
.tagCapsuleStyle(.init(
87-
foregroundColor: .black,
88-
backgroundColor: .red,
89-
borderColor: .blue,
90-
borderWidth: 4,
91-
padding: .init(
92-
top: 10,
93-
leading: 20,
94-
bottom: 12,
95-
trailing: 20
96-
)
97-
))
98-
.shadow(radius: 0, y: 2)
84+
HStack {
85+
TagCapsule("spider-man")
86+
.tagCapsuleStyle(.spiderman)
87+
TagCapsule("spider-man-selected")
88+
.tagCapsuleStyle(.spidermanSelected)
89+
}
9990
}
10091
.padding(.top, 250)
10192
}
10293
}
10394
}
95+
96+
private extension TagCapsuleStyle {
97+
98+
static var spiderman: Self {
99+
.init(
100+
foregroundColor: .black,
101+
backgroundColor: .red,
102+
backgroundMaterial: .thin,
103+
border: .init(
104+
color: .blue,
105+
width: 4
106+
),
107+
padding: .init(
108+
top: 10,
109+
leading: 20,
110+
bottom: 12,
111+
trailing: 20
112+
)
113+
)
114+
}
115+
116+
static var spidermanSelected: Self {
117+
var style = Self.spiderman
118+
style.backgroundMaterial = .none
119+
style.shadow = .standardSelected
120+
return style
121+
}
122+
}

Sources/TagKit/Views/TagCapsuleStyle.swift

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,22 @@ import SwiftUI
1919
/// use a primary foreground color, a clear background color
2020
/// and a material background.
2121
public struct TagCapsuleStyle {
22-
22+
2323
/// Create a new tag capsule style.
2424
///
2525
/// - Parameters:
2626
/// - foregroundColor: The foreground color, by default `.primary`.
2727
/// - backgroundColor: The background color, by default `.clear`.
2828
/// - backgroundMaterial: The background material, by default `.ultraThin`.
29-
/// - borderColor: The border color, by default `.clear`.
30-
/// - borderWidth: The border width, by default `1`.
29+
/// - border: The border style, by default ``Border/standard``.
3130
/// - shadow: The shadow style, by default ``Shadow/standard``.
3231
/// - padding: The intrinsic padding to apply, by default a small padding.
3332
public init(
3433
foregroundColor: Color = .primary,
3534
backgroundColor: Color = .clear,
3635
backgroundMaterial: Material? = .ultraThin,
37-
borderColor: Color = .clear,
38-
borderWidth: Double = 1,
39-
shadowStyle: Shadow = .standard,
36+
border: Border = .standard,
37+
shadow: Shadow = .standard,
4038
padding: EdgeInsets? = nil
4139
) {
4240
var defaultPadding: EdgeInsets
@@ -49,10 +47,32 @@ public struct TagCapsuleStyle {
4947
self.foregroundColor = foregroundColor
5048
self.backgroundColor = backgroundColor
5149
self.backgroundMaterial = backgroundMaterial
52-
self.borderColor = borderColor
53-
self.borderWidth = borderWidth
50+
self.border = border
51+
self.shadow = shadow
5452
self.padding = padding ?? defaultPadding
55-
self.shadow = shadowStyle
53+
}
54+
55+
@available(*, deprecated, message: "Use the new borderStyle initializer instead.")
56+
public init(
57+
foregroundColor: Color = .primary,
58+
backgroundColor: Color = .clear,
59+
backgroundMaterial: Material? = .ultraThin,
60+
borderColor: Color = .clear,
61+
borderWidth: Double = 1,
62+
shadow: Shadow = .standard,
63+
padding: EdgeInsets? = nil
64+
) {
65+
self.init(
66+
foregroundColor: foregroundColor,
67+
backgroundColor: backgroundColor,
68+
backgroundMaterial: backgroundMaterial,
69+
border: .init(
70+
color: borderColor,
71+
width: borderWidth
72+
),
73+
shadow: shadow,
74+
padding: padding
75+
)
5676
}
5777

5878
/// The foreground color.
@@ -64,19 +84,41 @@ public struct TagCapsuleStyle {
6484
/// The background material.
6585
public var backgroundMaterial: Material?
6686

67-
/// The border color.
68-
public var borderColor: Color
69-
70-
/// The border width.
71-
public var borderWidth: Double
87+
/// The border style.
88+
public var border: Border
7289

73-
// The shadow style.
90+
/// The shadow style.
7491
public var shadow: Shadow
7592

7693
/// The padding to apply to the text.
7794
public var padding: EdgeInsets
7895
}
7996

97+
public extension TagCapsuleStyle {
98+
99+
struct Border {
100+
101+
/// Create a new tag capsule border style.
102+
///
103+
/// - Parameters:
104+
/// - color: The border color, by default `.clear`.
105+
/// - width: The border width, by default `1`.
106+
public init(
107+
color: Color = .clear,
108+
width: Double = 1
109+
) {
110+
self.color = color
111+
self.width = width
112+
}
113+
114+
/// The border color.
115+
public var color: Color
116+
117+
/// The border width.
118+
public var width: Double
119+
}
120+
}
121+
80122
public extension TagCapsuleStyle {
81123

82124
struct Shadow {
@@ -125,14 +167,15 @@ public extension TagCapsuleStyle {
125167
static var standardSelected: Self {
126168
.init(
127169
backgroundMaterial: .regular,
128-
shadowStyle: .standardSelected
170+
border: .standardSelected,
171+
shadow: .standardSelected
129172
)
130173
}
131174
}
132175

133176
public extension TagCapsuleStyle.Shadow {
134177

135-
/// The standard style.
178+
/// The standard shadow style.
136179
static var standard: Self {
137180
.init()
138181
}
@@ -146,6 +189,19 @@ public extension TagCapsuleStyle.Shadow {
146189
}
147190
}
148191

192+
public extension TagCapsuleStyle.Border {
193+
194+
/// The standard border style.
195+
static var standard: Self {
196+
.init()
197+
}
198+
199+
/// The standard, selected border style.
200+
static var standardSelected: Self {
201+
.init()
202+
}
203+
}
204+
149205
public extension TagCapsuleStyle {
150206

151207
@available(*, deprecated, renamed: "standard")

Sources/TagKit/Views/TagEditList.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,16 @@ private extension TagCapsuleStyle {
212212
.init(
213213
foregroundColor: .black,
214214
backgroundColor: .red,
215-
borderWidth: 4
215+
border: .init(width: 4)
216216
)
217217
}
218218

219219
static var customSelected: TagCapsuleStyle {
220220
.init(
221221
foregroundColor: .black,
222222
backgroundColor: .red,
223-
borderColor: .blue,
224-
borderWidth: 4
223+
border: .init(width: 4),
224+
shadow: .standardSelected
225225
)
226226
}
227227
}

0 commit comments

Comments
 (0)