'macOS: How to prevent that showing an alert dismisses the Popover it comes from

In a macOS App I use .popovers to show and edit cell details. If the user deletes specific content I want to show a warning .alert. But showing the alert always dismisses the Popover it originated from. How can I prevent that?

This should work as Apple is using it e.g. in Calendar, when you delete an attachment in a calendar entry.

Here is a simple demo code showing the issue:

struct ContentView: View {
    
    @State private var showPopover = false
    @State private var showAlert = false
    
    var body: some View {
        
        VStack {
            
            Button("Show popover") { showPopover = true }
            
            .popover(isPresented: $showPopover, arrowEdge: .leading) {
                VStack {
                    Text("Popover")
                    Button("Delete someting") { showAlert = true}
                }
                .padding()
                .frame(minWidth: 100, minHeight: 100)
            }
            .alert("Really delete?", isPresented: $showAlert) { }
            // ^ dismisses the popover immediately
        }
        .frame(minWidth: 600, minHeight: 400)
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source