A floating link back to the top of the page. It is a server component with no JavaScript: the link points to #top, appears after a scroll threshold through a scroll-driven animation, and the progress ring fills on the same scroll timeline. Five variants: default circle, pill with text, minimal icon, progress ring and text.
Browser Support
Shows the button after scrolling.
Enables smooth scrolling while the link is active.
Preview
Each box is its own scroll area: the button follows the nearest scroller, so here it reacts to the box, on a real page to the page.
Scroll down to see the button appear.
Installation
Usage
import { ScrollToTop } from "@/components/ui/scroll-to-top"
export default function Page() {
return (
<main>
{/* Your page content */}
<ScrollToTop />
</main>
)
}Variants
Default
Circular button with an arrow. Three sizes: sm, md, lg.
<ScrollToTop />
<ScrollToTop size="sm" />
<ScrollToTop size="lg" />Pill
Elongated button with a text label.
Scroll down to see the button appear.
<ScrollToTopPill label="Back to top" />Minimal
Icon-only button without background.
<ScrollToTopMinimal />Progress
Circular button with a ring that shows how far the page is scrolled.
<ScrollToTopProgress />
<ScrollToTopProgress progressColor="var(--chart-2)" strokeWidth={4} />Text
Text link with a small progress circle.
Scroll down to see the button appear.
<ScrollToTopText label="Back to top" />Examples
Custom threshold
The button fades in over 80 px of scroll after the threshold.
<ScrollToTop threshold={500} />Custom position
Override the default corner with your own classes.
<ScrollToTop className="left-6 right-auto" />
<ScrollToTop className="bottom-20" />Inside a scroll area
In a scrolling box, place the link inside the box: a click scrolls only the box and leaves the page where it is. href stays as the fallback without JavaScript.
<div className="relative h-96 overflow-y-auto scroll-smooth">
<span id="list-top" />
{items}
<div className="sticky bottom-0 h-0">
<ScrollToTop href="#list-top" className="absolute bottom-4 right-4" />
</div>
</div>Props
Common props
| Prop | Type | Default | Description |
|---|---|---|---|
| threshold | number | 300 | Scroll distance in px before the button appears |
| behavior | ScrollBehavior | "smooth" | "smooth" makes the page scroll smoothly when the link is used |
| label | string | "Scroll to top" | Accessible name, and the visible text in Pill and Text |
| href | string | "#top" | Fallback anchor without JavaScript. #top scrolls to the top of the document |
| target | string | HTMLElement | RefObject | — | Scroll container to send to the top: selector, element or ref. By default the nearest scrolling ancestor, else the page |
| animationDuration | number | 300 | Duration of the hover and press transitions in ms |
| className | string | - | Additional CSS classes |
ScrollToTop
| Prop | Type | Default | Description |
|---|---|---|---|
| size | "sm" | "md" | "lg" | "md" | Button size |
ScrollToTopProgress
| Prop | Type | Default | Description |
|---|---|---|---|
| progressColor | string | var(--primary) | Progress ring color |
| progressBgColor | string | var(--muted) | Color of the ring track |
| strokeWidth | number | 3 | Ring stroke width in px |
useScrollToTop hook
The components do not use it anymore. It stays for custom client implementations that need the scroll position in JavaScript:
"use client"
import { useScrollToTop } from "@/components/ui/scroll-to-top"
function ReadingProgress() {
const { scrollProgress } = useScrollToTop(300)
return <progress value={scrollProgress} max={100} />
}