One automatically handles scroll restoration to provide a native browser-like experience. When you navigate to a new page, scroll resets to the top. When you press back/forward, your previous scroll position is restored.
The ScrollBehavior component lets you customize this behavior for special cases.
Default Behavior
Out of the box, One handles:
Hash scrolling: Scrolls to #element-id when present in the URL
New page navigation: Scrolls to top when navigating to a new route
Back/forward navigation: Restores the previous scroll position
Session persistence: Scroll positions are saved to sessionStorage
You don’t need to add ScrollBehavior unless you want to change this behavior.
Usage
Add ScrollBehavior once in your root layout:
app/_layout.tsx
import{ ScrollBehavior, Slot }from'one'
exportdefaultfunctionLayout(){
return(
<>
<ScrollBehavior/>
<Slot/>
</>
)
}
Props
Prop
Type
Default
Description
disable
boolean | 'restore'
false
Disable scroll behavior entirely or just restoration
// Disable only scroll restoration on back/forward
// (still scrolls to top on new navigation)
<ScrollBehaviordisable="restore"/>
Scroll Groups
For complex layouts like tabs or dashboards, you may want child routes to share a scroll position. Use the useScrollGroup hook to create a scroll group:
app/dashboard/_layout.tsx
import{ Slot, useScrollGroup }from'one'
exportdefaultfunctionDashboardLayout(){
// All routes under /dashboard will share scroll position
useScrollGroup()
return(
<div>
<Sidebar/>
<main>
<Slot/>
</main>
</div>
)
}
Now when navigating between /dashboard/overview and /dashboard/settings, the scroll position is preserved instead of resetting to the top.