From 4d5973f1d94bb458373d7c9dd137ceb698ff6bb0 Mon Sep 17 00:00:00 2001 From: David Walter Date: Thu, 16 Nov 2023 17:27:48 +0100 Subject: [PATCH] Fix Alert Button not triggering References #11 --- Sources/CustomAlert/AlertButtonStyle.swift | 4 ---- Sources/CustomAlert/Views/CustomAlert.swift | 16 +++++++++++++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Sources/CustomAlert/AlertButtonStyle.swift b/Sources/CustomAlert/AlertButtonStyle.swift index 5753f1c..ccf0ced 100644 --- a/Sources/CustomAlert/AlertButtonStyle.swift +++ b/Sources/CustomAlert/AlertButtonStyle.swift @@ -32,10 +32,6 @@ public struct AlertButtonStyle: ButtonStyle { .padding(buttonConfiguration.padding) .frame(maxHeight: maxHeight) .background(background(configuration: configuration)) - .simultaneousGesture(TapGesture().onEnded { _ in - guard isEnabled else { return } - isPresented = false - }) } @ViewBuilder diff --git a/Sources/CustomAlert/Views/CustomAlert.swift b/Sources/CustomAlert/Views/CustomAlert.swift index be9f37e..91c6883 100644 --- a/Sources/CustomAlert/Views/CustomAlert.swift +++ b/Sources/CustomAlert/Views/CustomAlert.swift @@ -109,7 +109,7 @@ struct CustomAlert: View where Content: View, Actions: View { .frame(height: height) _VariadicView.Tree(ContentLayout(), content: actions) - .buttonStyle(.alert(isPresented: $isPresented)) + .modifier(AlertButton(isPresented: $isPresented)) .captureSize($actionsSize) } .frame(minWidth: minWidth, maxWidth: maxWidth) @@ -136,6 +136,20 @@ struct ContentLayout: _VariadicView_ViewRoot { } } +private struct AlertButton: ViewModifier { + @Environment(\.isEnabled) var isEnabled + @Binding var isPresented: Bool + + func body(content: Content) -> some View { + content + .simultaneousGesture(TapGesture().onEnded { _ in + guard isEnabled else { return } + isPresented = false + }, including: .all) + .buttonStyle(.alert(isPresented: $isPresented)) + } +} + struct CustomAlert_Previews: PreviewProvider { static var previews: some View { CustomAlert(title: Text("Preview"), isPresented: .constant(true)) {