From e8010b950528712edd4cb022da60ba98cbb85b04 Mon Sep 17 00:00:00 2001 From: Daniel Saidi Date: Wed, 18 Dec 2024 23:56:56 +0100 Subject: [PATCH] Add variadic component support to LinkText --- RELEASE_NOTES.md | 5 +++ ...t+Style.swift => LinkText+LinkStyle.swift} | 26 +++++------ Sources/SwiftUIKit/Text/LinkText.swift | 45 ++++++++++--------- 3 files changed, 42 insertions(+), 34 deletions(-) rename Sources/SwiftUIKit/Text/{LinkText+Style.swift => LinkText+LinkStyle.swift} (71%) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index cba220255d..37338d8b72 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -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. diff --git a/Sources/SwiftUIKit/Text/LinkText+Style.swift b/Sources/SwiftUIKit/Text/LinkText+LinkStyle.swift similarity index 71% rename from Sources/SwiftUIKit/Text/LinkText+Style.swift rename to Sources/SwiftUIKit/Text/LinkText+LinkStyle.swift index 0aa41762c5..16242ea61d 100644 --- a/Sources/SwiftUIKit/Text/LinkText+Style.swift +++ b/Sources/SwiftUIKit/Text/LinkText+LinkStyle.swift @@ -1,5 +1,5 @@ // -// LinkText.swift +// LinkText+LinkStyle.swift // SwiftUIKit // // Created by Daniel Saidi on 2022-07-31. @@ -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, @@ -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() } @@ -55,19 +55,19 @@ 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 } } @@ -75,8 +75,8 @@ private extension LinkText.Style { 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 } } } diff --git a/Sources/SwiftUIKit/Text/LinkText.swift b/Sources/SwiftUIKit/Text/LinkText.swift index b349419c55..c9e77f5bf4 100644 --- a/Sources/SwiftUIKit/Text/LinkText.swift +++ b/Sources/SwiftUIKit/Text/LinkText.swift @@ -20,7 +20,7 @@ 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: @@ -28,11 +28,19 @@ public struct LinkText: View { 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) @@ -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) } } } @@ -81,7 +90,7 @@ private extension LinkText { var markdownText: LocalizedStringKey { .init(stringLiteral: components.map { - $0.markdown(style) + $0.markdown(linkStyle) }.joined()) } } @@ -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(".") - ]) + ) } } @@ -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) } }