|
| 1 | +// |
| 2 | +// RadixBadge.swift |
| 3 | +// RadixUI |
| 4 | +// |
| 5 | +// Created by Amir Mohammadi on 11/28/1403 AP. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | + |
| 10 | +public struct RadixBadge: ViewModifier { |
| 11 | + |
| 12 | + @Environment(\.colorScheme) private var colorScheme |
| 13 | + |
| 14 | + private var variant: RadixBadgeVariant |
| 15 | + private var radius: RadixElementShapeRadius |
| 16 | + private var color: RadixAutoColor |
| 17 | + |
| 18 | + init( |
| 19 | + variant: RadixBadgeVariant, |
| 20 | + radius: RadixElementShapeRadius, |
| 21 | + color: RadixAutoColor |
| 22 | + ) { |
| 23 | + self.variant = variant |
| 24 | + self.radius = radius |
| 25 | + self.color = color |
| 26 | + } |
| 27 | + |
| 28 | + private var badgeColor: [Color] { |
| 29 | + switch variant { |
| 30 | + // 1st Entry is Fill and 2nd is Stroke Colors |
| 31 | + case .outline: [ .clear, color.solid1 ] |
| 32 | + case .soft: [ color.component2, .clear ] |
| 33 | + case .solid: [ color.border3, .clear ] |
| 34 | + case .surface: [ color.background2, color.solid1 ] |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private var fgColor: Color { |
| 39 | + guard color != .blackA else { return .whiteA11 } |
| 40 | + guard color != .whiteA else { return .blackA11 } |
| 41 | + guard variant != .solid else { |
| 42 | + return colorScheme == .light ? color.background2 : color.text2 |
| 43 | + } |
| 44 | + return color.text1 |
| 45 | + } |
| 46 | + |
| 47 | + public func body(content: Content) -> some View { |
| 48 | + content |
| 49 | + .padding(.horizontal, 10) |
| 50 | + .padding(.vertical, 5) |
| 51 | + .background(shape) |
| 52 | + .foregroundStyle(fgColor) |
| 53 | + } |
| 54 | + |
| 55 | + @ViewBuilder |
| 56 | + private var shape: some View { |
| 57 | + switch radius { |
| 58 | + case .none: |
| 59 | + Rectangle() |
| 60 | + .radixShapeFillApplier( |
| 61 | + color: badgeColor.first!, |
| 62 | + width: nil, height: nil |
| 63 | + ) |
| 64 | + .overlay { |
| 65 | + Rectangle() |
| 66 | + .radixShapeBorderApplier( |
| 67 | + color: badgeColor.last!, |
| 68 | + width: nil, height: nil |
| 69 | + ) |
| 70 | + } |
| 71 | + case .large: |
| 72 | + RoundedRectangle(cornerRadius: 8) |
| 73 | + .radixShapeFillApplier( |
| 74 | + color: badgeColor.first!, |
| 75 | + width: nil, height: nil |
| 76 | + ) |
| 77 | + .overlay { |
| 78 | + RoundedRectangle(cornerRadius: 8) |
| 79 | + .radixShapeBorderApplier( |
| 80 | + color: badgeColor.last!, |
| 81 | + width: nil, height: nil |
| 82 | + ) |
| 83 | + } |
| 84 | + case .full: |
| 85 | + Capsule() |
| 86 | + .radixShapeFillApplier( |
| 87 | + color: badgeColor.first!, |
| 88 | + width: nil, height: nil |
| 89 | + ) |
| 90 | + .overlay { |
| 91 | + Capsule() |
| 92 | + .radixShapeBorderApplier( |
| 93 | + color: badgeColor.last!, |
| 94 | + width: nil, height: nil |
| 95 | + ) |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments