Skip to content

Partial Sheet Not Triggering When Using @Published Variable in ViewModel #178

@IAMTHEBURT

Description

@IAMTHEBURT

I'm having difficulty triggering a partial sheet from a @published variable within my ViewModel. However, the same partial sheet triggers as expected when using a @State variable within the View.
Here is the code snippet that is not working as expected:

class MainViewModel: ObservableObject {
    @Published var isPresented: Bool = false
}

struct ContentView: View {
    @StateObject var vm: MainViewModel = MainViewModel()
    
    var body: some View {
        ZStack{
            VStack {
                Image(systemName: "globe")
                    .imageScale(.large)
                    .foregroundColor(.accentColor)
                    .onTapGesture {
                        vm.isPresented.toggle()
                    }
                
                Text("Hello, world!")
            }
            .padding()
            .partialSheet(isPresented: $vm.isPresented) {
                Text("Some content")
            }
        }
        .attachPartialSheetToRoot()
    }
}

When tapping the "globe" image, the partial sheet is not triggered, despite vm.isPresented being toggled.

Expected Behavior:

I expect the partial sheet to be presented when the @published variable isPresented is toggled in the ViewModel.

Actual Behavior:

The partial sheet is not being presented when the @published variable isPresented is toggled in the ViewModel.

Could you please help me understand why this is happening? Is this a bug or am I missing something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions