ToastUI icon indicating copy to clipboard operation
ToastUI copied to clipboard

Is there a function to hide the toast by clicking on the outside now?

Open coder-free opened this issue 1 year ago • 1 comments

I want click outside to dismiss toast

coder-free avatar Mar 26 '23 14:03 coder-free

Hi @coder-free,

I don't have any plan to add this feature at the moment. ToastUI is implemented by presenting an internal UIWindow consists of your provided SwiftUI view entirely. Therefore, it's a little bit tricky to implement this directly in the internal UIWindow.

Please feel free to open a PR if you have an example implementation.

quanshousio avatar Apr 22 '24 20:04 quanshousio

Hi @coder-free,

Technically, you can implement this in your SwiftUI view by handling tap gesture that occurred outside of your view and then dismiss it.

struct TapOutsideToDismissToastExample: View {
  @State private var presentingToast = false

  var body: some View {
    Button {
      presentingToast = true
    } label: {
      Text("Tap me")
    }
    .toast(isPresented: $presentingToast) {
      ToastView("Tap anywhere to dismiss")
        .toastViewStyle(.information)
        .background(
          Color.clear.contentShape(Rectangle())
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .onTapGesture {
              presentingToast = false
            }
        )
    }
  }
}

Please don't hesitate to reopen this issue if you have any further questions.

quanshousio avatar Aug 10 '24 18:08 quanshousio