|
| 1 | +import SwiftUI |
| 2 | + |
| 3 | +/// Styled button variants for Ocean theme |
| 4 | +public struct OceanButton: View { |
| 5 | + public enum Variant { |
| 6 | + case primary // Accent background, dark text |
| 7 | + case secondary // Transparent, bordered |
| 8 | + case danger // Error color, bordered |
| 9 | + } |
| 10 | + |
| 11 | + let title: String |
| 12 | + let icon: String? |
| 13 | + let variant: Variant |
| 14 | + let action: () -> Void |
| 15 | + |
| 16 | + @Environment(\.isEnabled) private var isEnabled |
| 17 | + |
| 18 | + public init( |
| 19 | + _ title: String, |
| 20 | + icon: String? = nil, |
| 21 | + variant: Variant = .primary, |
| 22 | + action: @escaping () -> Void |
| 23 | + ) { |
| 24 | + self.title = title |
| 25 | + self.icon = icon |
| 26 | + self.variant = variant |
| 27 | + self.action = action |
| 28 | + } |
| 29 | + |
| 30 | + public var body: some View { |
| 31 | + Button(action: action) { |
| 32 | + HStack(spacing: 6) { |
| 33 | + if let icon = icon { |
| 34 | + Text(icon) |
| 35 | + } |
| 36 | + Text(title) |
| 37 | + .font(Ocean.ui(13, weight: .medium)) |
| 38 | + } |
| 39 | + .padding(.horizontal, 16) |
| 40 | + .padding(.vertical, 8) |
| 41 | + .background(background) |
| 42 | + .foregroundColor(foregroundColor) |
| 43 | + .cornerRadius(Ocean.buttonRadius) |
| 44 | + .overlay( |
| 45 | + RoundedRectangle(cornerRadius: Ocean.buttonRadius) |
| 46 | + .stroke(borderColor, lineWidth: variant == .primary ? 0 : 1) |
| 47 | + ) |
| 48 | + } |
| 49 | + .buttonStyle(.plain) |
| 50 | + .opacity(isEnabled ? 1 : 0.5) |
| 51 | + } |
| 52 | + |
| 53 | + private var background: Color { |
| 54 | + guard isEnabled else { return Ocean.surface } |
| 55 | + switch variant { |
| 56 | + case .primary: return Ocean.accent |
| 57 | + case .secondary, .danger: return .clear |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private var foregroundColor: Color { |
| 62 | + guard isEnabled else { return Ocean.textDim } |
| 63 | + switch variant { |
| 64 | + case .primary: return Ocean.bg |
| 65 | + case .secondary: return Ocean.textDim |
| 66 | + case .danger: return Ocean.error |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private var borderColor: Color { |
| 71 | + guard isEnabled else { return Ocean.border } |
| 72 | + switch variant { |
| 73 | + case .primary: return .clear |
| 74 | + case .secondary: return Ocean.border |
| 75 | + case .danger: return Ocean.borderError |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// MARK: - Preview |
| 81 | + |
| 82 | +#if DEBUG |
| 83 | +struct OceanButton_Previews: PreviewProvider { |
| 84 | + static var previews: some View { |
| 85 | + VStack(spacing: 16) { |
| 86 | + OceanButton("Launch", icon: "▶", variant: .primary) {} |
| 87 | + OceanButton("Cancel", variant: .secondary) {} |
| 88 | + OceanButton("Stop", icon: "■", variant: .danger) {} |
| 89 | + |
| 90 | + Divider() |
| 91 | + |
| 92 | + OceanButton("Disabled", variant: .primary) {} |
| 93 | + .disabled(true) |
| 94 | + OceanButton("Disabled", variant: .secondary) {} |
| 95 | + .disabled(true) |
| 96 | + } |
| 97 | + .padding(40) |
| 98 | + .background(Ocean.bg) |
| 99 | + } |
| 100 | +} |
| 101 | +#endif |
0 commit comments