WebsiteLink.swift (1713B)
1 // 2 // WebsiteLink.swift 3 // damus 4 // 5 // Created by William Casarin on 2023-01-22. 6 // 7 8 import SwiftUI 9 10 struct WebsiteLink: View { 11 let url: URL 12 let style: StyleVariant 13 @Environment(\.openURL) var openURL 14 15 init(url: URL, style: StyleVariant? = nil) { 16 self.url = url 17 self.style = style ?? .normal 18 } 19 20 var body: some View { 21 HStack { 22 Image("link") 23 .resizable() 24 .frame(width: 16, height: 16) 25 .foregroundColor(self.style == .accent ? .white : .gray) 26 .padding(.vertical, 5) 27 .padding([.leading], 10) 28 29 Button(action: { 30 openURL(url) 31 }, label: { 32 Text(link_text) 33 .font(.footnote) 34 .foregroundColor(self.style == .accent ? .white : .accentColor) 35 .truncationMode(.tail) 36 .lineLimit(1) 37 }) 38 .padding(.vertical, 5) 39 .padding([.trailing], 10) 40 } 41 .background( 42 self.style == .accent ? 43 AnyView(RoundedRectangle(cornerRadius: 50).fill(PinkGradient)) 44 : AnyView(Color.clear) 45 ) 46 } 47 48 var link_text: String { 49 url.host ?? url.absoluteString 50 } 51 52 enum StyleVariant { 53 case normal 54 case accent 55 } 56 } 57 58 struct WebsiteLink_Previews: PreviewProvider { 59 static var previews: some View { 60 WebsiteLink(url: URL(string: "https://jb55.com")!) 61 .previewDisplayName("Normal") 62 WebsiteLink(url: URL(string: "https://jb55.com")!, style: .accent) 63 .previewDisplayName("Accent") 64 } 65 }