Components
← All components

Scroll Restore

stimeo--scroll-restore

Persists and restores an inner scroll region's position across Turbo navigations.

  • Rails / Turbo

The stimeo--scroll-restore controller has no APG widget pattern — it is a pure state-preservation utility. Turbo swaps the whole body on navigation, so an inner scroll container is rebuilt with scrollTop reset to 0. Rather than hand-write a controller for this in every app, it saves the offset under a stable key in sessionStorage and restores it on connect, so it survives both Turbo Drive navigations and full reloads within the tab session. Keying by key (falling back to the element id) makes it multi-instance safe. The scroll listener is internal and passive, coalesced through requestAnimationFrame, and flushes synchronously on disconnect (Turbo included). axis selects vertical / horizontal / both. It sets no ARIA/CSS and never moves focus.

running

Scroll both lists, then press Tear down & rebuild. The left list (with stimeo--scroll-restore) returns to where you were; the plain right list jumps back to the top — exactly what happens to an inner scroll region on a Turbo navigation. The position also survives a real page navigation and a browser reload (within the tab session).

With Scroll Restore

  1. Row 1
  2. Row 2
  3. Row 3
  4. Row 4
  5. Row 5
  6. Row 6
  7. Row 7
  8. Row 8
  9. Row 9
  10. Row 10
  11. Row 11
  12. Row 12
  13. Row 13
  14. Row 14
  15. Row 15
  16. Row 16
  17. Row 17
  18. Row 18
  19. Row 19
  20. Row 20

Without (plain)

  1. Row 1
  2. Row 2
  3. Row 3
  4. Row 4
  5. Row 5
  6. Row 6
  7. Row 7
  8. Row 8
  9. Row 9
  10. Row 10
  11. Row 11
  12. Row 12
  13. Row 13
  14. Row 14
  15. Row 15
  16. Row 16
  17. Row 17
  18. Row 18
  19. Row 19
  20. Row 20