Skip to content

AutoHide issue when view removed #264

@vinipazzini

Description

@vinipazzini

Hi,
First of all, thank you for this library, it is the best one for popups in swift UI that I have seen so for.

I stumbled upon this issue where the popup is never dismissed and I managed to make a minimal reproducible code.
I am not sure if I am doing something wrong with the State/Bindings or if this is a limitation of the library.
I debugged the code a bunch and I see that isPresentedRef?.value.wrappedValue = false is called and the binding value changes but .onChange(of: isPresented) { newValue in never get triggered again, I suppose it is due to the lifecycle of the View, but I am not finding a way to fix it.

import SwiftUI
import PopupView

@main
struct PopupExampleApp: App {
    var body: some Scene {
        WindowGroup {
            SimplifiedDemoView()
        }
    }
}

struct SimplifiedDemoView: View {
    @State private var showFirstView = true
    @State private var showPopup = false

    var body: some View {
        VStack {
            Picker("Select a view", selection: $showFirstView) {
                Text("View 1").tag(true)
                Text("View 2").tag(false)
            }
            .pickerStyle(SegmentedPickerStyle())

            if showFirstView {
                ViewWithPopup(showPopup: $showPopup)
            } else {
                Text("This is the second view.")
            }
            Spacer()
        }
        .padding()
    }
}

struct ViewWithPopup: View {
    @Binding var showPopup: Bool

    var body: some View {
        VStack {
            Text("This is the first view.")
            Button("Toggle Popup") {
                showPopup.toggle()
            }
        }
        .popup(isPresented: $showPopup) {
            // This is the content of the popup.
            Text("This is a Popup")
                .foregroundColor(.white)
                .padding()
                .background(Color.black)
                .cornerRadius(8)
        } customize: {
            $0
                .displayMode(.window)
                .type(.floater(verticalPadding: 2, horizontalPadding: 2, useSafeAreaInset: true))
                .position(.top)
                .animation(.spring())
                .closeOnTapOutside(true)
                .autohideIn(2)
        }
    }
}

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions