Skip to content

Commit fe0a5bc

Browse files
Merge pull request #56 from componentskit/SUBadge
SUBadge
2 parents 0db1162 + 7fa7518 commit fe0a5bc

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import ComponentsKit
2+
import SwiftUI
3+
import UIKit
4+
5+
struct BadgePreview: View {
6+
@State private var model = BadgeVM {
7+
$0.title = "Badge"
8+
}
9+
10+
var body: some View {
11+
VStack {
12+
PreviewWrapper(title: "SwiftUI") {
13+
SUBadge(model: self.model)
14+
}
15+
Form {
16+
Picker("Font", selection: self.$model.font) {
17+
Text("Default").tag(Optional<UniversalFont>.none)
18+
Text("Small").tag(UniversalFont.smButton)
19+
Text("Medium").tag(UniversalFont.mdButton)
20+
Text("Large").tag(UniversalFont.lgButton)
21+
Text("Custom: system bold of size 16").tag(UniversalFont.system(size: 16, weight: .bold))
22+
}
23+
ComponentOptionalColorPicker(selection: self.$model.color)
24+
ComponentRadiusPicker(selection: self.$model.cornerRadius) {
25+
Text("Custom: 4px").tag(ComponentRadius.custom(4))
26+
}
27+
Picker("Style", selection: self.$model.style) {
28+
Text("Filled").tag(BadgeVM.Style.filled)
29+
Text("Light").tag(BadgeVM.Style.light)
30+
}
31+
}
32+
}
33+
}
34+
}
35+
36+
#Preview {
37+
BadgePreview()
38+
}

Examples/DemosApp/DemosApp/Core/App.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ struct App: View {
88
NavigationLinkWithTitle("Alert") {
99
AlertPreview()
1010
}
11+
NavigationLinkWithTitle("Badge") {
12+
BadgePreview()
13+
}
1114
NavigationLinkWithTitle("Button") {
1215
ButtonPreview()
1316
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Foundation
2+
3+
extension BadgeVM {
4+
/// Defines the available visual styles for a badge.
5+
public enum Style: Equatable {
6+
case filled
7+
case light
8+
}
9+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import SwiftUI
2+
3+
/// A model that defines the appearance properties for a badge component.
4+
public struct BadgeVM: ComponentVM {
5+
/// The text displayed on the badge.
6+
public var title: String = ""
7+
8+
/// The color of the badge.
9+
public var color: ComponentColor?
10+
11+
/// The visual style of the badge.
12+
///
13+
/// Defaults to `.filled`.
14+
public var style: Style = .filled
15+
16+
/// The font used for the badge's text.
17+
///
18+
/// Defaults to `.smButton`.
19+
public var font: UniversalFont = .smButton
20+
21+
/// The corner radius of the badge.
22+
///
23+
/// Defaults to `.medium`.
24+
public var cornerRadius: ComponentRadius = .medium
25+
26+
/// Paddings for the badge.
27+
public var paddings: Paddings = .init(horizontal: 10, vertical: 8)
28+
29+
/// Initializes a new instance of `BadgeVM` with default values.
30+
public init() {}
31+
}
32+
33+
// MARK: Helpers
34+
35+
extension BadgeVM {
36+
/// Returns the background color of the badge based on its style.
37+
var backgroundColor: UniversalColor {
38+
switch self.style {
39+
case .filled:
40+
return self.color?.main ?? .content2
41+
case .light:
42+
return self.color?.background ?? .content1
43+
}
44+
}
45+
46+
/// Returns the foreground color of the badge based on its style.
47+
var foregroundColor: UniversalColor {
48+
switch self.style {
49+
case .filled:
50+
return self.color?.contrast ?? .foreground
51+
case .light:
52+
return self.color?.main ?? .foreground
53+
}
54+
}
55+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import SwiftUI
2+
3+
/// A SwiftUI component that displays a badge.
4+
public struct SUBadge: View {
5+
// MARK: Properties
6+
7+
/// A model that defines the appearance properties.
8+
public var model: BadgeVM
9+
10+
// MARK: Initialization
11+
12+
/// Initializes a new instance of `SUBadge`.
13+
/// - Parameter model: A model that defines the appearance properties.
14+
public init(model: BadgeVM) {
15+
self.model = model
16+
}
17+
18+
// MARK: Body
19+
20+
public var body: some View {
21+
Text(self.model.title)
22+
.font(self.model.font.font)
23+
.padding(self.model.paddings.edgeInsets)
24+
.foregroundStyle(self.model.foregroundColor.color)
25+
.background(self.model.backgroundColor.color)
26+
.clipShape(
27+
RoundedRectangle(cornerRadius: self.model.cornerRadius.value())
28+
)
29+
}
30+
}

0 commit comments

Comments
 (0)