Skip to content

Commit

Permalink
Add variadic component support to LinkText
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Dec 18, 2024
1 parent eb0f64a commit e8010b9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 34 deletions.
5 changes: 5 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ SwiftUIKit makes its best effort to honor semver, but breaking changes can occur



## 5.0.1

This patch adds variadic component support to `LinkText` and makes it possible for each link component to specify its own style.


## 5.0.1

This patch updates `LinkText` to work with Swift 6, after a post 5.0 Swift concurrency change made it stop compiling.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// LinkText.swift
// LinkText+LinkStyle.swift
// SwiftUIKit
//
// Created by Daniel Saidi on 2022-07-31.
Expand All @@ -16,12 +16,12 @@ public extension LinkText {
/// apply to all links within the view.
///
/// This style can be applied by using the view modifier
/// ``SwiftUICore/View/linkTextStyle(_:)``.
/// ``SwiftUICore/View/linkTextLinkStyle(_:)``.
///
/// To style link color, line heights, etc. just use the
/// standard SwiftUI view modifiers. A link will use the
/// `.accentColor` while texts use the `.foregroundStyle`.
struct Style {
struct LinkStyle {

public init(
bold: Bool = false,
Expand All @@ -36,7 +36,7 @@ public extension LinkText {
}
}

public extension LinkText.Style {
public extension LinkText.LinkStyle {

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

public extension View {

/// Apply a ``LinkText/Style`` to the view.
func linkTextStyle(
_ style: LinkText.Style
/// Apply a ``LinkText/LinkStyle`` to the view.
func linkTextLinkStyle(
_ style: LinkText.LinkStyle
) -> some View {
self.environment(\.linkTextStyle, style)
self.environment(\.linkTextLinkStyle, style)
}
}

private extension LinkText.Style {
private extension LinkText.LinkStyle {

struct Key: EnvironmentKey {

public static var defaultValue: LinkText.Style {
static var defaultValue: LinkText.LinkStyle {
.standard
}
}
}

public extension EnvironmentValues {

var linkTextStyle: LinkText.Style {
get { self [LinkText.Style.Key.self] }
set { self [LinkText.Style.Key.self] = newValue }
var linkTextLinkStyle: LinkText.LinkStyle {
get { self [LinkText.LinkStyle.Key.self] }
set { self [LinkText.LinkStyle.Key.self] = newValue }
}
}
45 changes: 24 additions & 21 deletions Sources/SwiftUIKit/Text/LinkText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,27 @@ import SwiftUI
/// You can also use a ``LinkText/Style`` to style the links
/// further, making them bold or italic.
public struct LinkText: View {

/// Create a link text.
///
/// - Parameters:
/// - components: The components to render.
public init(_ components: [Component]) {
self.components = components
}

/// Create a link text.
///
/// - Parameters:
/// - components: The components to render.
public init(_ components: Component...) {
self.components = components
}

private let components: [Component]

@Environment(\.linkTextStyle)
private var style
@Environment(\.linkTextLinkStyle)
private var linkStyle

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

/// A link with a text and a link.
case link(String, URL?)
case link(String, URL?, LinkText.LinkStyle? = nil)
}
}

private extension LinkText.Component {

func markdown(
_ style: LinkText.Style
_ viewStyle: LinkText.LinkStyle
) -> String {
switch self {
case .text(let text): text
case .link(let text, let url): "[\(text)](\(url?.absoluteString ?? ""))"
.markdownBold(if: style.bold)
.markdownItalic(if: style.italic)
case .link(let text, let url, let style):
"[\(text)](\(url?.absoluteString ?? ""))"
.markdownBold(if: (style ?? viewStyle).bold)
.markdownItalic(if: (style ?? viewStyle).italic)
}
}
}
Expand All @@ -81,7 +90,7 @@ private extension LinkText {

var markdownText: LocalizedStringKey {
.init(stringLiteral: components.map {
$0.markdown(style)
$0.markdown(linkStyle)
}.joined())
}
}
Expand All @@ -91,13 +100,13 @@ private extension LinkText {
struct PreviewText: View {

var body: some View {
LinkText([
LinkText(
.text("You must accept our "),
.link("terms & conditions", .init(string: "https://danielsaidi.com")),
.text(". Read more on our "),
.link("website", .init(string: "https://danielsaidi.com")),
.text(".")
])
)
}
}

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

PreviewText()
.font(.body.italic())
.linkTextStyle(.bold)
.linkTextLinkStyle(.bold)

PreviewText()
.font(.body)
.linkTextStyle(.italic)
.linkTextLinkStyle(.italic)

PreviewText()
.font(.body)
.linkTextStyle(.boldItalic)
.linkTextLinkStyle(.boldItalic)

PreviewText()
.font(.headline.italic())

PreviewText()
.linkTextStyle(.italic)

PreviewText()
.accentColor(.orange)
.tint(.orange)
.lineSpacing(10)
}
}

0 comments on commit e8010b9

Please sign in to comment.