Path of NavigationStack inside NavigationSplitView emptied on change

848 views Asked by At

I have the following view inside WindowGroup:

struct SidebarSelection: View {
    
    @State var selection: Int?
    @State var path: [Int] = []
    
    var body: some View {
        NavigationSplitView {
            List(1...20, id: \.self, selection: $selection) { number in
                    Text("I like \(number)")
            }
        } detail: {
            NavigationStack(path: $path) {
                VStack {
                    Image(systemName: "x.squareroot")
                        .imageScale(.large)
                        .foregroundColor(.accentColor)
                    Text("This is the NavigationStack root")
                }
                .padding()
                .navigationDestination(for: Int.self) { number in
                    Text("You chose \(number)")
                }
            }
        }
        .onChange(of: selection) { newValue in
            print("You clicked \(newValue ?? 0)")
            if let newValue {
                path.append(newValue)
                print("New path \(path)")
            }
        }
        .onChange(of: path) { newValue in
            print("Path changed to \(path)")
        }
    }
}

If I run this on macOS and click:

  1. „I like 5“
  2. „I like 6“
  3. „I like 7“

I would expect the detail view to show:

  1. „You chose 5“
  2. „You chose 6“
  3. „You chose 7“

And the console to show:

You clicked 5
New path [5]
Path changed to [5]
You clicked 6
New path [5, 6]
Path changed to [5, 6]
You clicked 7
New path [5, 6, 7]
Path changed to [5, 6, 7]

What I actually see in the detail view is:

  1. „You chose 5“
  2. „This is the NavigationStack root“
  3. „You chose 7“

And the console shows:

New path [5]
Path changed to [5]
You clicked 6
New path [5, 6]
Path changed to []
You clicked 7
New path [7]
Path changed to [7]

I think this is a bug and have filed a feedback and also asked on the Apple forums half a year ago but the problem persists.

Am I doing something wrong? Does anyone know what emptied the path and/or how to properly use a NavigationStack inside a NavigationSplitView?

0

There are 0 answers