Skip to content

Commit

Permalink
Fix Alert Button not triggering
Browse files Browse the repository at this point in the history
References #11
  • Loading branch information
divadretlaw committed Nov 16, 2023
1 parent 7ecb679 commit 4d5973f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
4 changes: 0 additions & 4 deletions Sources/CustomAlert/AlertButtonStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion Sources/CustomAlert/Views/CustomAlert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ struct CustomAlert<Content, Actions>: 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)
Expand All @@ -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)) {
Expand Down

0 comments on commit 4d5973f

Please sign in to comment.