Sexy confetti

I would have assumed that Apple would ship their iMessage confetti as a SwiftUI component. But they don’t. I don’t know why. They don’t want our apps to have rizz, I guess.

Every other confetti library I tried looked wrong, just like 3 pieces of hay flying around or something. All I wanted was a view showering down with the exact confetti iMessage uses, nothing to configure.

So I went to Google, found an old write-up with the right idea, and discovered the code no longer compiles. Damn. I then dove through the documentation to bring everything up to date and wrapped it into a single SwiftUI view called UnionConfetti. On a real device it looks like this:

UnionConfetti raining down when a button is tappedThe same effect, on iOS.

How to use

One boolean, one overlay:

import SwiftUI
import UnionConfetti
 
struct ContentView: View {
    @State var showConfetti = false
 
    var body: some View {
        Button("Rain on me") {
            showConfetti = true
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .overlay(ConfettiView(isPresented: $showConfetti))
    }
}

That’s the whole API. Flip showConfetti to true and it rains; it resets itself when the last piece lands. Drop the overlay on top of anything worth celebrating, like a successful payment:

Confetti firing on a successful Apple Pay paymentConfetti on a completed payment.

There’s more it could do — blur the layer behind it, expose a few knobs — but the whole point was a view that needs none of that. It rains the confetti, then gets out of the way.

UnionConfetti is on GitHub. Get the package.

Note

Originally published on ITNEXT in December 2024, and rewritten for this site.