Destructive action (delete)
Confirming a delete: a heavy, unmistakable thud that says "this is significant".
import SwiftUI
struct DeleteButton: View {
let onDelete: () -> Void
@State private var confirming = false
@State private var deleted = 0
var body: some View {
Button(role: .destructive) {
confirming = true
} label: {
Label("Delete item", systemImage: "trash")
}
.confirmationDialog("Delete this item?", isPresented: $confirming) {
Button("Delete", role: .destructive) {
deleted += 1
onDelete()
}
}
.sensoryFeedback(.impact(weight: .heavy), trigger: deleted)
}
} let haptic = UIImpactFeedbackGenerator(style: .heavy)
func confirmDelete() {
haptic.prepare()
let alert = UIAlertController(title: "Delete this item?",
message: nil,
preferredStyle: .actionSheet)
alert.addAction(UIAlertAction(title: "Delete", style: .destructive) { _ in
haptic.impactOccurred()
// perform deletion
})
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
present(alert, animated: true)
}