edudnyk / SheeKit

Customize and resize sheets in SwiftUI with SheeKit. Utilise the power of `UISheetPresentationController` and other UIKit features.
MIT License
91 stars 7 forks source link

`onDismiss` handler is not called when the sheet dismisses itself #7

Closed FelixLisczyk closed 2 years ago

FelixLisczyk commented 2 years ago

Thanks for the great library! I've noticed an issue where SheeKit's onDismiss handler is not called when the sheet dismisses itself by executing the dismiss closure provided by the SwiftUI environment. Here is a quick example:

import SheeKit
import SwiftUI

struct ContentView: View {

    @State private var isPresentingSheet: Bool = false

    var body: some View {
        Button("Present Sheet") {
            isPresentingSheet = true
        }
        .shee(isPresented: $isPresentingSheet, onDismiss: {
            print("Dismiss")
        }, content: {
            SheetView()
        })
    }
}

struct SheetView: View {

    @Environment(\.dismiss) private var dismiss

    var body: some View {
        Button("Close") {
            dismiss()
        }
    }
}

When I replace shee with a regular sheet, the callback works fine.

edudnyk commented 2 years ago

Hi @FelixLisczyk,

This does not work due the fact that DismissAction struct in SwiftUI has internal constructor, and it's not possible to instantiate it in the consumer code.

Please replace

@Environment(\.dismiss) private var dismiss

with

@Environment(\.shee_dismiss) private var dismiss
FelixLisczyk commented 2 years ago

Thank you for the explanation, @edudnyk !