diff --git a/Demo/Demo/Alerts/CustomAlerts.swift b/Demo/Demo/Alerts/CustomAlerts.swift index 160fb6e..232f1e4 100644 --- a/Demo/Demo/Alerts/CustomAlerts.swift +++ b/Demo/Demo/Alerts/CustomAlerts.swift @@ -22,7 +22,7 @@ struct CustomAlerts: View { DetailLabel("Custom Config", detail: "CustomAlert with heavily modified styling") } .customAlert("Preview", isPresented: $showAlert) { - Text("Content") + CustomContent() } actions: { MultiButton { Button { @@ -94,6 +94,25 @@ struct CustomAlerts: View { } } +struct CustomContent: View { + @Environment(\.alertDismiss) private var alertDismiss + @Environment(\.customAlertConfiguration) private var configuration + + var body: some View { + VStack(alignment: .leading) { + Text("Content") + + Button { + alertDismiss() + } label: { + Text("Custom Dismiss Button") + } + .buttonStyle(.bordered) + .tint(configuration.button.tintColor) + } + } +} + extension CustomAlertConfiguration { static var myConfig: CustomAlertConfiguration = .create { configuration in configuration.background = .blurEffect(.dark) diff --git a/Sources/CustomAlert/Environment/Environment+AlertDismissAction.swift b/Sources/CustomAlert/Environment/Environment+AlertDismissAction.swift index d0e40c1..c39b0e3 100644 --- a/Sources/CustomAlert/Environment/Environment+AlertDismissAction.swift +++ b/Sources/CustomAlert/Environment/Environment+AlertDismissAction.swift @@ -8,10 +8,38 @@ import SwiftUI /// An action that dismisses a custom alert presentation. +/// +/// Use the `alertDismiss` environment value to get the instance +/// of this structure for a given `Environment`. Then call the instance +/// to perform the dismissal. You call the instance directly because +/// it defines a ``AlertDismissAction/callAsFunction()`` +/// method that Swift calls when you call the instance. public struct AlertDismissAction { let action: () -> Void - func callAsFunction() { + /// Dismisses the alert if it is currently presented. + /// + /// Don't call this method directly. SwiftUI calls it for you when you + /// call the ``AlertDismissAction`` structure that you get from the + /// `Environment`: + /// + /// ```swift + /// private struct SheetContents: View { + /// @Environment(\.alertDismiss) private var alertDismiss + /// + /// var body: some View { + /// Button("Done") { + /// alertDismiss() // Implicitly calls dismiss.callAsFunction() + /// } + /// } + /// } + /// ``` + /// + /// For information about how Swift uses the `callAsFunction()` method to + /// simplify call site syntax, see + /// [Methods with Special Names](https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#ID622) + /// in *The Swift Programming Language*. + public func callAsFunction() { action() } } diff --git a/Sources/CustomAlert/Views/CustomAlert.swift b/Sources/CustomAlert/Views/CustomAlert.swift index ccd8b98..188d753 100644 --- a/Sources/CustomAlert/Views/CustomAlert.swift +++ b/Sources/CustomAlert/Views/CustomAlert.swift @@ -157,12 +157,12 @@ public struct CustomAlert: View where Content: View, Actions: } #endif } - .onAlertDismiss { - isPresented = false - } .buttonStyle(.alert) .captureSize($actionsSize) } + .onAlertDismiss { + isPresented = false + } .frame(minWidth: minWidth, maxWidth: maxWidth) .background(BackgroundView(background: configuration.alert.background)) .cornerRadius(configuration.alert.cornerRadius)