Skip to content

Back To Top

A back to top button that appears after scrolling, with a ring that fills as the reader progresses. Smooth scroll that respects reduced motion.

src/registry/ui/back-to-top.tsx/r/back-to-top.json

Installation

bash
npx shadcn@latest add @minidev/back-to-top

MiniDev UI is in the shadcn registry directory, so the short form works with no setup. Older CLI versions can use the full URL: npx shadcn@latest add https://ui.minidev.pro/r/back-to-top.json

Usage

tsx
import { BackToTop } from "@/components/ui/back-to-top"

export function Example() {
  return (
    <BackToTop />
  )
}

API reference

BackToTop props
PropTypeDefault
thresholdShow after this many pixels of scroll.number400
targetElement to scroll; defaults to the window.React.RefObject<HTMLElement | null>-
classNamestring-

When to use

Use on long docs, articles and changelogs. Fixed bottom-right; move it with className. Scrolls smoothly unless reduced motion is on, then moves focus to <main> (or the first <h1>) so keyboard users land at the top too. While hidden it is out of the tab order.

Keyboard interactions

Keyboard interactions
KeyAction
EnterorSpaceScrolls to the top and moves focus to the page's main content or first heading.
TabReaches the button only after the page has scrolled past threshold. While hidden it has tabIndex={-1}.

Accessibility

  • The button is labelled "Back to top". While hidden it is aria-hidden and out of the tab order, so screen readers don't find an invisible control.
  • The progress ring and arrow are aria-hidden; the scroll percentage is not announced.
  • Under prefers-reduced-motion: reduce the scroll jumps instead of animating, and the fade and slide are cut by the kit's stylesheet.

Frequently asked questions

How do I use it inside a scrolling panel instead of the window?

Pass a ref to the scrolling element as target and override the fixed position: <BackToTop target={panelRef} className="absolute" />. Put it in a relative wrapper next to the scroller, not inside it, or it scrolls away with the content.

Where does keyboard focus go after the jump?

To the first h1 or h2 inside target; for the window, to <main>, or the first <h1> if there is no <main>. It adds tabindex="-1" to that element if needed, so the next Tab starts from the top of the page instead of from the button.

How do I change when the button appears?

Set threshold in pixels; the default is 400. The ring shows how far the reader has scrolled through the whole page or target.

Dependencies

npm

lucide-react

Built from

No other MiniDev files

Examples

Scrollable panel

Point target at a scroll container and make the button absolute inside the same wrapper; scroll the list to see it appear.

Reading pane

A higher threshold waits until the reader is well into the text, and the ring shows how far through they are.

All navigation components

Learn more