|
| 1 | +import SwiftUI |
| 2 | + |
| 3 | +/// The visual variant of the POS button. |
| 4 | +enum POSButtonVariant { |
| 5 | + case filled |
| 6 | + case outlined |
| 7 | +} |
| 8 | + |
| 9 | +/// The size variant of the POS button. |
| 10 | +enum POSButtonSize { |
| 11 | + case normal |
| 12 | + case extraSmall |
| 13 | +} |
| 14 | + |
| 15 | +/// A unified button style for POS that supports different variants and sizes. |
| 16 | +struct POSButtonStyle: ButtonStyle { |
| 17 | + @Environment(\.colorScheme) var colorScheme |
| 18 | + @Environment(\.isEnabled) var isEnabled |
| 19 | + |
| 20 | + let variant: POSButtonVariant |
| 21 | + let size: POSButtonSize |
| 22 | + |
| 23 | + init(variant: POSButtonVariant = .filled, |
| 24 | + size: POSButtonSize = .normal) { |
| 25 | + self.variant = variant |
| 26 | + self.size = size |
| 27 | + } |
| 28 | + |
| 29 | + func makeBody(configuration: Configuration) -> some View { |
| 30 | + HStack { |
| 31 | + Spacer() |
| 32 | + configuration.label |
| 33 | + Spacer() |
| 34 | + } |
| 35 | + .padding(.vertical, size.padding.vertical) |
| 36 | + .padding(.horizontal, size.padding.horizontal) |
| 37 | + .font(size.font) |
| 38 | + .background(backgroundColor) |
| 39 | + .foregroundColor(foregroundColor) |
| 40 | + .overlay(borderOverlay) |
| 41 | + .cornerRadius(POSButtonStyleConstants.framedButtonCornerRadius) |
| 42 | + } |
| 43 | + |
| 44 | + private var backgroundColor: Color { |
| 45 | + switch (variant, isEnabled) { |
| 46 | + case (.filled, true): |
| 47 | + .posPrimaryContainer |
| 48 | + case (.filled, false): |
| 49 | + .posDisabledContainer |
| 50 | + case (.outlined, _): |
| 51 | + .clear |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private var foregroundColor: Color { |
| 56 | + switch (variant, isEnabled) { |
| 57 | + case (.filled, true): |
| 58 | + .posOnPrimaryContainer |
| 59 | + case (.filled, false): |
| 60 | + .posOnDisabledContainer |
| 61 | + case (.outlined, true): |
| 62 | + .posOnSurface |
| 63 | + case (.outlined, false): |
| 64 | + .posOnDisabledContainer |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @ViewBuilder |
| 69 | + private var borderOverlay: some View { |
| 70 | + if variant == .outlined { |
| 71 | + RoundedRectangle(cornerRadius: POSButtonStyleConstants.framedButtonCornerRadius) |
| 72 | + .stroke(isEnabled ? Color.posInverseSurface : .posDisabledContainer, |
| 73 | + lineWidth: 2) |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// MARK: - POSButtonSize Extensions |
| 79 | + |
| 80 | +private extension POSButtonSize { |
| 81 | + var padding: (vertical: CGFloat, horizontal: CGFloat) { |
| 82 | + switch self { |
| 83 | + case .normal: |
| 84 | + (vertical: 24, horizontal: 24) |
| 85 | + case .extraSmall: |
| 86 | + (vertical: 8, horizontal: 16) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + var font: POSFontStyle { |
| 91 | + switch self { |
| 92 | + case .normal: |
| 93 | + .posBodyEmphasized |
| 94 | + case .extraSmall: |
| 95 | + .posDetailEmphasized |
| 96 | + } |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// MARK: - Preview |
| 101 | + |
| 102 | +#if DEBUG |
| 103 | + |
| 104 | +struct POSButtonStyle_Previews: View { |
| 105 | + var body: some View { |
| 106 | + ScrollView { |
| 107 | + VStack(alignment: .leading, spacing: 32) { |
| 108 | + previewSection(title: "Filled Buttons - Normal", |
| 109 | + variant: .filled, size: .normal) |
| 110 | + |
| 111 | + previewSection(title: "Filled Buttons - Extra Small", |
| 112 | + variant: .filled, size: .extraSmall) |
| 113 | + |
| 114 | + previewSection(title: "Outlined Buttons - Normal", |
| 115 | + variant: .outlined, size: .normal) |
| 116 | + |
| 117 | + previewSection(title: "Outlined Buttons - Extra Small", |
| 118 | + variant: .outlined, size: .extraSmall) |
| 119 | + |
| 120 | + // Example with long text |
| 121 | + VStack(alignment: .leading, spacing: 16) { |
| 122 | + Text("Long Text Examples") |
| 123 | + .font(.headline) |
| 124 | + |
| 125 | + Button("This is a very long button text that might wrap to multiple lines") {} |
| 126 | + .buttonStyle(POSButtonStyle(variant: .filled, size: .normal)) |
| 127 | + |
| 128 | + Button("Long text in small size button that might need to wrap") {} |
| 129 | + .buttonStyle(POSButtonStyle(variant: .outlined, size: .extraSmall)) |
| 130 | + } |
| 131 | + } |
| 132 | + .padding() |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private func previewSection(title: String, variant: POSButtonVariant, size: POSButtonSize) -> some View { |
| 137 | + VStack(alignment: .leading, spacing: 16) { |
| 138 | + Text(title) |
| 139 | + .font(.headline) |
| 140 | + |
| 141 | + Button("Enabled Button") {} |
| 142 | + .buttonStyle(POSButtonStyle(variant: variant, size: size)) |
| 143 | + |
| 144 | + Button("Disabled Button") {} |
| 145 | + .buttonStyle(POSButtonStyle(variant: variant, size: size)) |
| 146 | + .disabled(true) |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +#Preview("Button Styles") { |
| 152 | + POSButtonStyle_Previews() |
| 153 | +} |
| 154 | + |
| 155 | +#endif |
0 commit comments