Skip to content

Commit

Permalink
Make AlertDismissAction public
Browse files Browse the repository at this point in the history
  • Loading branch information
divadretlaw committed Sep 8, 2024
1 parent c4b9bdd commit d9de26f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
21 changes: 20 additions & 1 deletion Demo/Demo/Alerts/CustomAlerts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/CustomAlert/Views/CustomAlert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ public struct CustomAlert<Content, Actions>: 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)
Expand Down

0 comments on commit d9de26f

Please sign in to comment.