Stateful Submit Button

Stateful Submit Button

A submit button that runs an async action and shows every state in one shape. It squeezes into a spinner while loading, then draws a green checkmark on success or shakes red with Try again on failure.

Luiz Mello
  • button
  • loading
  • success

snippet.swift246 lines

5 files • Scroll to see all →
/// A submit button that tells the whole story of an async action in one shape:////// **idle** → tap → **loading** (the pill squeezes into a circle with a spinner)/// → **success** (turns green, a checkmark draws itself, then it relaxes back)/// or **failure** (turns red, shakes, and offers "Try again").////// The action is `async throws`: return normally for success, throw for failure.public struct StatefulSubmitButton: View {    let title: String    var systemImage: String?    var successTitle: String    var failureTitle: String    var gradient: [Color]    var height: CGFloat    var resetDelay: Duration    let action: () async throws -> Void​    private enum Phase: Equatable { case idle, loading, success, failure }​    @State private var phase: Phase = .idle​// ... (click "Show full code" to see more)
Viewing: StatefulSubmitButton.swift•1 of 5