Skip to content

Commit e8010b9

Browse files
committed
Add variadic component support to LinkText
1 parent eb0f64a commit e8010b9

File tree

3 files changed

+42
-34
lines changed

3 files changed

+42
-34
lines changed

RELEASE_NOTES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ SwiftUIKit makes its best effort to honor semver, but breaking changes can occur
44

55

66

7+
## 5.0.1
8+
9+
This patch adds variadic component support to `LinkText` and makes it possible for each link component to specify its own style.
10+
11+
712
## 5.0.1
813

914
This patch updates `LinkText` to work with Swift 6, after a post 5.0 Swift concurrency change made it stop compiling.

Sources/SwiftUIKit/Text/LinkText+Style.swift renamed to Sources/SwiftUIKit/Text/LinkText+LinkStyle.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// LinkText.swift
2+
// LinkText+LinkStyle.swift
33
// SwiftUIKit
44
//
55
// Created by Daniel Saidi on 2022-07-31.
@@ -16,12 +16,12 @@ public extension LinkText {
1616
/// apply to all links within the view.
1717
///
1818
/// This style can be applied by using the view modifier
19-
/// ``SwiftUICore/View/linkTextStyle(_:)``.
19+
/// ``SwiftUICore/View/linkTextLinkStyle(_:)``.
2020
///
2121
/// To style link color, line heights, etc. just use the
2222
/// standard SwiftUI view modifiers. A link will use the
2323
/// `.accentColor` while texts use the `.foregroundStyle`.
24-
struct Style {
24+
struct LinkStyle {
2525

2626
public init(
2727
bold: Bool = false,
@@ -36,7 +36,7 @@ public extension LinkText {
3636
}
3737
}
3838

39-
public extension LinkText.Style {
39+
public extension LinkText.LinkStyle {
4040

4141
/// The standard, plain link text style.
4242
static var standard: Self { .init() }
@@ -55,28 +55,28 @@ public extension LinkText.Style {
5555

5656
public extension View {
5757

58-
/// Apply a ``LinkText/Style`` to the view.
59-
func linkTextStyle(
60-
_ style: LinkText.Style
58+
/// Apply a ``LinkText/LinkStyle`` to the view.
59+
func linkTextLinkStyle(
60+
_ style: LinkText.LinkStyle
6161
) -> some View {
62-
self.environment(\.linkTextStyle, style)
62+
self.environment(\.linkTextLinkStyle, style)
6363
}
6464
}
6565

66-
private extension LinkText.Style {
66+
private extension LinkText.LinkStyle {
6767

6868
struct Key: EnvironmentKey {
6969

70-
public static var defaultValue: LinkText.Style {
70+
static var defaultValue: LinkText.LinkStyle {
7171
.standard
7272
}
7373
}
7474
}
7575

7676
public extension EnvironmentValues {
7777

78-
var linkTextStyle: LinkText.Style {
79-
get { self [LinkText.Style.Key.self] }
80-
set { self [LinkText.Style.Key.self] = newValue }
78+
var linkTextLinkStyle: LinkText.LinkStyle {
79+
get { self [LinkText.LinkStyle.Key.self] }
80+
set { self [LinkText.LinkStyle.Key.self] = newValue }
8181
}
8282
}

Sources/SwiftUIKit/Text/LinkText.swift

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,27 @@ import SwiftUI
2020
/// You can also use a ``LinkText/Style`` to style the links
2121
/// further, making them bold or italic.
2222
public struct LinkText: View {
23-
23+
2424
/// Create a link text.
2525
///
2626
/// - Parameters:
2727
/// - components: The components to render.
2828
public init(_ components: [Component]) {
2929
self.components = components
3030
}
31+
32+
/// Create a link text.
33+
///
34+
/// - Parameters:
35+
/// - components: The components to render.
36+
public init(_ components: Component...) {
37+
self.components = components
38+
}
3139

3240
private let components: [Component]
3341

34-
@Environment(\.linkTextStyle)
35-
private var style
42+
@Environment(\.linkTextLinkStyle)
43+
private var linkStyle
3644

3745
public var body: some View {
3846
Text(markdownText)
@@ -48,20 +56,21 @@ public extension LinkText {
4856
case text(String)
4957

5058
/// A link with a text and a link.
51-
case link(String, URL?)
59+
case link(String, URL?, LinkText.LinkStyle? = nil)
5260
}
5361
}
5462

5563
private extension LinkText.Component {
5664

5765
func markdown(
58-
_ style: LinkText.Style
66+
_ viewStyle: LinkText.LinkStyle
5967
) -> String {
6068
switch self {
6169
case .text(let text): text
62-
case .link(let text, let url): "[\(text)](\(url?.absoluteString ?? ""))"
63-
.markdownBold(if: style.bold)
64-
.markdownItalic(if: style.italic)
70+
case .link(let text, let url, let style):
71+
"[\(text)](\(url?.absoluteString ?? ""))"
72+
.markdownBold(if: (style ?? viewStyle).bold)
73+
.markdownItalic(if: (style ?? viewStyle).italic)
6574
}
6675
}
6776
}
@@ -81,7 +90,7 @@ private extension LinkText {
8190

8291
var markdownText: LocalizedStringKey {
8392
.init(stringLiteral: components.map {
84-
$0.markdown(style)
93+
$0.markdown(linkStyle)
8594
}.joined())
8695
}
8796
}
@@ -91,13 +100,13 @@ private extension LinkText {
91100
struct PreviewText: View {
92101

93102
var body: some View {
94-
LinkText([
103+
LinkText(
95104
.text("You must accept our "),
96105
.link("terms & conditions", .init(string: "https://danielsaidi.com")),
97106
.text(". Read more on our "),
98107
.link("website", .init(string: "https://danielsaidi.com")),
99108
.text(".")
100-
])
109+
)
101110
}
102111
}
103112

@@ -109,25 +118,19 @@ private extension LinkText {
109118
.accentColor(.green)
110119

111120
PreviewText()
112-
.font(.body.italic())
113-
.linkTextStyle(.bold)
121+
.linkTextLinkStyle(.bold)
114122

115123
PreviewText()
116-
.font(.body)
117-
.linkTextStyle(.italic)
124+
.linkTextLinkStyle(.italic)
118125

119126
PreviewText()
120-
.font(.body)
121-
.linkTextStyle(.boldItalic)
127+
.linkTextLinkStyle(.boldItalic)
122128

123129
PreviewText()
124130
.font(.headline.italic())
125131

126132
PreviewText()
127-
.linkTextStyle(.italic)
128-
129-
PreviewText()
130-
.accentColor(.orange)
133+
.tint(.orange)
131134
.lineSpacing(10)
132135
}
133136
}

0 commit comments

Comments
 (0)