|
| 1 | +// |
| 2 | +// LinkExample.swift |
| 3 | +// Shared |
| 4 | + |
| 5 | +import Foundation |
| 6 | +#if OPENSWIFTUI |
| 7 | +import OpenSwiftUI |
| 8 | +#else |
| 9 | +import SwiftUI |
| 10 | +#endif |
| 11 | + |
| 12 | +private let linkDestination = URL(string: "https://example.com")! |
| 13 | + |
| 14 | +struct LinkExample: View { |
| 15 | + private let localizedTitle: LocalizedStringKey = "Localized title" |
| 16 | + private let title = "String title" |
| 17 | + |
| 18 | + var body: some View { |
| 19 | + VStack(alignment: .leading, spacing: 16) { |
| 20 | + Link(localizedTitle, destination: linkDestination) |
| 21 | + Link(title, destination: linkDestination) |
| 22 | + Link(destination: linkDestination) { |
| 23 | + HStack(spacing: 8) { |
| 24 | + Color.orange |
| 25 | + .frame(width: 16, height: 16) |
| 26 | + Text("Custom label") |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + .padding() |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +struct LinkButtonStyleExample: View { |
| 35 | + var body: some View { |
| 36 | + VStack(alignment: .leading, spacing: 16) { |
| 37 | + Link("Default style", destination: linkDestination) |
| 38 | + Link("Automatic style", destination: linkDestination) |
| 39 | + .buttonStyle(.automatic) |
| 40 | + Link("Plain style", destination: linkDestination) |
| 41 | + .buttonStyle(.plain) |
| 42 | + Link("Bordered style", destination: linkDestination) |
| 43 | + .buttonStyle(.bordered) |
| 44 | + Link("Custom style", destination: linkDestination) |
| 45 | + .buttonStyle(ExampleLinkButtonStyle()) |
| 46 | + #if os(macOS) |
| 47 | + Link("Explicit link style", destination: linkDestination) |
| 48 | + .buttonStyle(.link) |
| 49 | + Button("Link button style") {} |
| 50 | + .buttonStyle(.link) |
| 51 | + #endif |
| 52 | + } |
| 53 | + .padding() |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +private struct ExampleLinkButtonStyle: ButtonStyle { |
| 58 | + func makeBody(configuration: Configuration) -> some View { |
| 59 | + configuration.label |
| 60 | + .foregroundStyle(.white) |
| 61 | + .padding(8) |
| 62 | + .background(.purple) |
| 63 | + .opacity(configuration.isPressed ? 0.5 : 1) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +struct LinkAppearanceExample: View { |
| 68 | + var body: some View { |
| 69 | + VStack(alignment: .leading, spacing: 16) { |
| 70 | + Link("Default appearance", destination: linkDestination) |
| 71 | + Link("Tinted link", destination: linkDestination) |
| 72 | + .tint(.purple) |
| 73 | + Link("Foreground style", destination: linkDestination) |
| 74 | + .foregroundStyle(.orange) |
| 75 | + Link(destination: linkDestination) { |
| 76 | + Text("Styled label") |
| 77 | + .font(.headline) |
| 78 | + .foregroundStyle(.green) |
| 79 | + } |
| 80 | + } |
| 81 | + .padding() |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +struct LinkShapeStyleExample: View { |
| 86 | + var body: some View { |
| 87 | + VStack(alignment: .leading, spacing: 16) { |
| 88 | + Text("Link foreground style") |
| 89 | + .foregroundStyle(.link) |
| 90 | + Rectangle() |
| 91 | + .fill(.link) |
| 92 | + .frame(height: 24) |
| 93 | + } |
| 94 | + .padding() |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +#Preview("LinkExample") { |
| 99 | + LinkExample() |
| 100 | +} |
| 101 | + |
| 102 | +#Preview("LinkButtonStyleExample") { |
| 103 | + LinkButtonStyleExample() |
| 104 | +} |
| 105 | + |
| 106 | +#Preview("LinkAppearanceExample") { |
| 107 | + LinkAppearanceExample() |
| 108 | +} |
| 109 | + |
| 110 | +#Preview("LinkShapeStyleExample") { |
| 111 | + LinkShapeStyleExample() |
| 112 | +} |
0 commit comments