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

- Kind: Component (Navigation)
- Docs: https://ui.minidev.pro/docs/back-to-top
- Source (registry JSON with the full file): https://ui.minidev.pro/r/back-to-top.json
- License: MIT. One file, yours to edit after install.

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

## Installation

### shadcn CLI (copies the source into your project)

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

Writes `components/ui/back-to-top.tsx` and installs its npm dependencies. Older CLI versions: `npx shadcn@latest add https://ui.minidev.pro/r/back-to-top.json`.

### npm package

```bash
npm i minidev-ui-kit lucide-react
```

Add the tokens to your global stylesheet once:

```css
@import "tailwindcss";
@import "minidev-ui-kit/styles.css";
@source "../node_modules/minidev-ui-kit";
```

Next.js also needs `transpilePackages: ["minidev-ui-kit"]` in next.config.

## Import

After the shadcn install:

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

From the npm package:

```tsx
import { BackToTop } from "minidev-ui-kit/ui/back-to-top"
```

## Usage

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

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

## Props

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `threshold` | `number` | `400` | no | Show after this many pixels of scroll. |
| `target` | `React.RefObject<HTMLElement \| null>` | - | no | Element to scroll; defaults to the window. |
| `className` | `string` | - | no |  |

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

```tsx
const ROWS = Array.from({ length: 30 }, (_, i) => "INV-" + (2041 - i) + " · " + ["Northwind Traders", "Helix Biotech", "Blue Harbor Co.", "Meridian Freight", "Summit Dental Group"][i % 5])

function PanelDemo() {
  const ref = React.useRef<HTMLDivElement>(null)
  return (
    <div className="relative w-full max-w-xs">
      <div ref={ref} className="h-64 overflow-y-auto rounded-2xl border border-border bg-surface">
        <ul className="divide-y divide-border text-sm">
          {ROWS.map((row) => <li key={row} className="px-4 py-2 text-fg">{row}</li>)}
        </ul>
      </div>
      <BackToTop target={ref} threshold={120} className="absolute right-3 bottom-3" />
    </div>
  )
}
```

### Reading pane

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

```tsx
const PARAGRAPHS = [
  "Lumen reconciles bank transfers against open invoices every hour. A transfer is matched when the amount and the reference line up, and the invoice is marked as paid without anyone touching it.",
  "When a payment arrives without a reference, it lands in the unmatched queue. From there you can attach it to an invoice by hand or split it across several invoices for the same customer.",
  "Partial payments keep the invoice open and show the remaining balance. Reminders only go out for the balance, so customers who paid most of a bill are not asked for the full amount again.",
  "Refunds reverse the match. The invoice goes back to open, the payment returns to the queue, and the audit log records who did it and why.",
  "Every match, split and refund is exported nightly to your accounting system as a journal entry, so the books and the dashboard never drift apart.",
  "Multi currency accounts are matched in the account's own currency. The invoice keeps its original amount and the exchange difference is posted as a separate line, so nothing is rounded away.",
  "If a bank connection drops, Lumen keeps the last successful sync and shows a banner on the invoices page. Reconnecting replays the missed days, and no transfer is matched twice.",
  "Everything here also works for card payments through the customer portal, which arrive already referenced and are matched on the spot.",
]

function ArticleDemo() {
  const ref = React.useRef<HTMLElement>(null)
  return (
    <div className="relative w-full max-w-md">
      <article ref={ref} className="h-72 overflow-y-auto rounded-2xl border border-border bg-surface p-5">
        <h3 className="text-base font-medium text-fg">How bank sync matches payments</h3>
        {PARAGRAPHS.map((p) => <p key={p} className="mt-3 text-sm leading-[1.6] text-fg-muted">{p}</p>)}
      </article>
      <BackToTop target={ref} threshold={200} className="absolute right-4 bottom-4 size-9" />
    </div>
  )
}
```

## Keyboard interactions

| Key | Action |
| --- | --- |
| `Enter` or `Space` | Scrolls to the top and moves focus to the page's main content or first heading. |
| `Tab` | Reaches 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.

## FAQ

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

## Rules for generating code

- Use semantic tokens only (bg, surface, sunken, border, fg, fg-muted, fg-subtle, ink, accent, success, warning, danger). Never raw hex or palette steps like blue-500.
- Shadows only from the set: shadow-xs, shadow-sm, shadow-raised, shadow-md, shadow-lg, shadow-overlay.
- Numbers use tabular-nums. Dark mode is the `dark` class on <html>; components never branch on theme.

## Dependencies

- npm: `lucide-react`

## Related

- [Back Link](https://ui.minidev.pro/docs/back-link/md): Small back navigation link with a left arrow icon, muted text that darkens on hover and a visible focus ring. Defaults to the label Back and accepts any href.
- [Breadcrumb Ellipsis](https://ui.minidev.pro/docs/breadcrumb-ellipsis/md): Ellipsis marker for collapsing the middle of long breadcrumb trails. Decorative and hidden from assistive tech, sized to sit between breadcrumb separators.
- [Breadcrumb](https://ui.minidev.pro/docs/breadcrumb/md): Breadcrumb navigation from an items array, with chevron separators, linked ancestors and the current page marked with aria-current. Built with React.
- [Collapsible Sidebar](https://ui.minidev.pro/docs/collapsible-sidebar/md): Sidebar with a toggle button on its edge that collapses it to a narrow rail and expands it again. The button label updates for screen readers.
- [Command Item](https://ui.minidev.pro/docs/command-item/md): Row for command menus with a label, an optional keyboard shortcut in a kbd tag and an active highlight state. Use it to build custom command lists.
- [Command Palette](https://ui.minidev.pro/docs/command-palette/md): Cmd K style command palette in a dialog with a search field that filters commands as you type, runs the chosen action and closes. Shows an empty state.

---

All 557 components: https://ui.minidev.pro/llms.txt (full text: https://ui.minidev.pro/llms-full.txt).
