Skip to content

Commit ae2457a

Browse files
Merge pull request #41 from componentskit/sucard
SUCard
2 parents b87030e + a708a5c commit ae2457a

File tree

7 files changed

+118
-27
lines changed

7 files changed

+118
-27
lines changed

Examples/DemosApp/DemosApp/ComponentsPreview/PreviewPages/CardPreview.swift

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ struct CardPreview: View {
88
var body: some View {
99
VStack {
1010
PreviewWrapper(title: "UIKit") {
11-
UKCard(model: self.model, content: cardContent)
11+
UKCard(model: self.model, content: self.ukCardContent)
1212
.preview
1313
}
14+
PreviewWrapper(title: "SwiftUI") {
15+
SUCard(model: self.model, content: self.suCardContent)
16+
}
1417
Form {
1518
Picker("Background Color", selection: self.$model.backgroundColor) {
1619
Text("Default").tag(Optional<UniversalColor>.none)
@@ -38,30 +41,42 @@ struct CardPreview: View {
3841
}
3942
}
4043
}
41-
}
4244

43-
#Preview {
44-
CardPreview()
45-
}
45+
// MARK: - Helpers
46+
47+
private func ukCardContent() -> UIView {
48+
let titleLabel = UILabel()
49+
titleLabel.text = "Card"
50+
titleLabel.font = UniversalFont.mdHeadline.uiFont
51+
titleLabel.textColor = UniversalColor.foreground.uiColor
52+
titleLabel.numberOfLines = 0
4653

47-
// MARK: - Helpers
54+
let subtitleLabel = UILabel()
55+
subtitleLabel.text = "Card is a container for text, images, and other content."
56+
subtitleLabel.font = UniversalFont.mdBody.uiFont
57+
subtitleLabel.textColor = UniversalColor.secondaryForeground.uiColor
58+
subtitleLabel.numberOfLines = 0
4859

49-
private func cardContent() -> UIView {
50-
let titleLabel = UILabel()
51-
titleLabel.text = "Card"
52-
titleLabel.font = UniversalFont.mdHeadline.uiFont
53-
titleLabel.textColor = UniversalColor.foreground.uiColor
54-
titleLabel.numberOfLines = 0
60+
let stackView = UIStackView(arrangedSubviews: [titleLabel, subtitleLabel])
61+
stackView.axis = .vertical
62+
stackView.spacing = 8
5563

56-
let subtitleLabel = UILabel()
57-
subtitleLabel.text = "Card is a container for text, images, and other content."
58-
subtitleLabel.font = UniversalFont.mdBody.uiFont
59-
subtitleLabel.textColor = UniversalColor.secondaryForeground.uiColor
60-
subtitleLabel.numberOfLines = 0
64+
return stackView
65+
}
6166

62-
let stackView = UIStackView(arrangedSubviews: [titleLabel, subtitleLabel])
63-
stackView.axis = .vertical
64-
stackView.spacing = 8
67+
private func suCardContent() -> some View {
68+
VStack(alignment: .leading, spacing: 8) {
69+
Text("Card")
70+
.foregroundStyle(UniversalColor.foreground.color)
71+
.font(UniversalFont.mdHeadline.font)
6572

66-
return stackView
73+
Text("Card is a container for text, images, and other content.")
74+
.foregroundStyle(UniversalColor.secondaryForeground.color)
75+
.font(UniversalFont.mdBody.font)
76+
}
77+
}
78+
}
79+
80+
#Preview {
81+
CardPreview()
6782
}

Sources/ComponentsKit/Components/Card/CardVM.swift renamed to Sources/ComponentsKit/Components/Card/Models/CardVM.swift

File renamed without changes.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import SwiftUI
2+
3+
/// A SwiftUI component that serves as a container for provided content.
4+
///
5+
/// - Example:
6+
/// ```swift
7+
/// SUCard(
8+
/// model: .init(),
9+
/// content: {
10+
/// Text("This is the content of the card.")
11+
/// }
12+
/// )
13+
/// ```
14+
public struct SUCard<Content: View>: View {
15+
// MARK: - Properties
16+
17+
/// A model that defines the appearance properties.
18+
public let model: CardVM
19+
20+
@ViewBuilder private let content: () -> Content
21+
22+
// MARK: - Initialization
23+
24+
/// Initializer.
25+
///
26+
/// - Parameters:
27+
/// - model: A model that defines the appearance properties.
28+
/// - content: The content that is displayed in the card.
29+
public init(
30+
model: CardVM,
31+
content: @escaping () -> Content
32+
) {
33+
self.model = model
34+
self.content = content
35+
}
36+
37+
// MARK: - Body
38+
39+
public var body: some View {
40+
self.content()
41+
.padding(self.model.contentPaddings.edgeInsets)
42+
.background(self.model.preferredBackgroundColor.color)
43+
.background(UniversalColor.background.color)
44+
.cornerRadius(self.model.cornerRadius.value)
45+
.overlay(
46+
RoundedRectangle(cornerRadius: self.model.cornerRadius.value)
47+
.stroke(UniversalColor.divider.color, lineWidth: 1.0)
48+
)
49+
.shadow(self.model.shadow)
50+
}
51+
}

Sources/ComponentsKit/Components/Card/UKCard.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import UIKit
77
/// ```swift
88
/// let banner = UKCard(
99
/// model: .init(),
10-
/// content: { _ in
10+
/// content: {
1111
/// let label = UILabel()
1212
/// label.text = "This is the content of the card."
1313
/// label.numberOfLines = 0
1414
/// return label
1515
/// }
1616
/// )
17+
/// ```
1718
open class UKCard: UIView, UKComponent {
1819
// MARK: - Typealiases
1920

Sources/ComponentsKit/Helpers/Paddings.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Foundation
1+
import SwiftUI
22

33
/// Defines padding values for each edge.
44
public struct Paddings: Hashable {
@@ -50,3 +50,16 @@ public struct Paddings: Hashable {
5050
self.trailing = padding
5151
}
5252
}
53+
54+
// MARK: - SwiftUI Helpers
55+
56+
extension Paddings {
57+
var edgeInsets: EdgeInsets {
58+
return EdgeInsets(
59+
top: self.top,
60+
leading: self.leading,
61+
bottom: self.bottom,
62+
trailing: self.trailing
63+
)
64+
}
65+
}

Sources/ComponentsKit/Modal/SwiftUI/ModalContent.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ struct ModalContent<VM: ModalVM, Header: View, Body: View, Footer: View>: View {
6363
.clipShape(RoundedRectangle(
6464
cornerRadius: self.model.cornerRadius.value
6565
))
66-
.padding(.top, self.model.outerPaddings.top)
67-
.padding(.leading, self.model.outerPaddings.leading)
68-
.padding(.bottom, self.model.outerPaddings.bottom)
69-
.padding(.trailing, self.model.outerPaddings.trailing)
66+
.padding(self.model.outerPaddings.edgeInsets)
7067
}
7168

7269
private var bodyTopPadding: CGFloat {

Sources/ComponentsKit/Shared/Types/Shadow.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import SwiftUI
12
import UIKit
23

34
/// Defines shadow options for components.
@@ -61,3 +62,16 @@ extension UIView {
6162
self.layer.shadowOpacity = 1
6263
}
6364
}
65+
66+
// MARK: - SwiftUI + Shadow
67+
68+
extension View {
69+
func shadow(_ shadow: Shadow) -> some View {
70+
self.shadow(
71+
color: shadow.color.color,
72+
radius: shadow.radius,
73+
x: shadow.offset.width,
74+
y: shadow.offset.height
75+
)
76+
}
77+
}

0 commit comments

Comments
 (0)