Skip to content

Commit 00342c1

Browse files
committed
Add double scrollTo to fix scroll restoration inconsistency
1 parent 457ea7f commit 00342c1

File tree

1 file changed

+7
-0
lines changed
  • Sources/LiveViewNative/Views/Layout Containers/Collection Containers

1 file changed

+7
-0
lines changed

Sources/LiveViewNative/Views/Layout Containers/Collection Containers/List.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ struct List<Root: RootRegistry>: View {
208208
}
209209
}
210210
}
211+
.opacity(didAttemptRestoration ? 1 : 0) // prevent flickering when restoring
211212
.task {
212213
defer { didAttemptRestoration = true }
213214

@@ -216,6 +217,12 @@ struct List<Root: RootRegistry>: View {
216217
else { return }
217218

218219
scrollProxy.scrollTo(restoreID, anchor: .top)
220+
221+
// wait for the next runloop and try to scroll again
222+
// it seems that the scroll won't work if the elements are not laid-out yet
223+
try! await Task.sleep(for: .nanoseconds(0))
224+
225+
scrollProxy.scrollTo(restoreID, anchor: .top)
219226
}
220227
}
221228
}

0 commit comments

Comments
 (0)