Keep a section's stack when the sidebar selection changes

In the split-view sidebar layout (macOS, and now iPad and the iPhone
Duo), the detail NavigationStack that a selection change replaces
writes an empty path through its binding on the way out, so going to
another section and back always landed on the section's root. macOS
already behaved this way before the router.

Only the selected section's stack is on screen and can pop, so path
writes for any other section are ignored.
This commit is contained in:
Arkadiusz Fal
2026-10-04 23:32:44 +02:00
parent 5f31610489
commit 4a4bcd6ca8
2 changed files with 31 additions and 1 deletions

View File

@@ -677,6 +677,28 @@ struct NavigationCoordinatorTests {
#expect(router.selection == .bookmarks)
}
@Test("A stack that is no longer selected can't clear itself")
func offscreenStackWriteIgnored() {
let router = NavigationRouter()
router.push(.history)
let homePath = router.path(for: .home)
router.select(.bookmarks)
homePath.wrappedValue = []
#expect(router.stack(for: .home) == [.history])
}
@Test("The selected stack still pops")
func selectedStackPops() {
let router = NavigationRouter()
router.push(.history)
router.path(for: .home).wrappedValue = []
#expect(router.stack(for: .home).isEmpty)
}
@Test("A destination pushed before startup keeps its section")
func deepLinkBeforeStartup() {
let coordinator = NavigationCoordinator()