GooseUI

Components

Scroll To Top


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

Scroll-driven animations

Shows the button after scrolling.

Your browser:
:has()

Enables smooth scrolling while the link is active.

Your browser:

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.

Default

Scroll down to see the button appear.

Pill

Scroll down to see the button appear.

Progress

Scroll down to see the button appear.

Installation

npx shadcn@latest add @gooseui/scroll-to-top
pnpm dlx shadcn@latest add @gooseui/scroll-to-top
yarn dlx shadcn@latest add @gooseui/scroll-to-top
bunx --bun shadcn@latest add @gooseui/scroll-to-top

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.

Scroll down to see the button appear.

<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.

Scroll down to see the button appear.

<ScrollToTopMinimal />

Progress

Circular button with a ring that shows how far the page is scrolled.

Scroll down to see the button appear.

<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

PropTypeDefaultDescription
thresholdnumber300Scroll distance in px before the button appears
behaviorScrollBehavior"smooth""smooth" makes the page scroll smoothly when the link is used
labelstring"Scroll to top"Accessible name, and the visible text in Pill and Text
hrefstring"#top"Fallback anchor without JavaScript. #top scrolls to the top of the document
targetstring | HTMLElement | RefObject—Scroll container to send to the top: selector, element or ref. By default the nearest scrolling ancestor, else the page
animationDurationnumber300Duration of the hover and press transitions in ms
classNamestring-Additional CSS classes

ScrollToTop

PropTypeDefaultDescription
size"sm" | "md" | "lg""md"Button size

ScrollToTopProgress

PropTypeDefaultDescription
progressColorstringvar(--primary)Progress ring color
progressBgColorstringvar(--muted)Color of the ring track
strokeWidthnumber3Ring 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} />
}