Skip to content
ProLifetime access for $199, paid once, every future release included
Truffle UI

Back to Top

FreeNew

A button that appears once the reader has scrolled and takes them back up, with reading progress drawn around its edge and six shapes.

Installation

pnpm dlx shadcn@latest add @viewport/back-to-top

First time? Add the @viewport registry to components.json. Setup takes one minute.

Usage

The code behind the preview above.

back-to-top-demo.tsx
"use client";

import { useEffect, useRef } from "react";
import { BackToTop } from "@/components/viewport/back-to-top";

const SECTIONS = [
  ["Start with the grid", "Lay the page out on eight columns and let the gutters do the talking. Most of what reads as polish is just consistent spacing."],
  ["Set the type once", "Pick a scale, write it down and stop reaching for one-off sizes. Headings two steps apart are easier to scan than headings one step apart."],
  ["Colour comes last", "Design in greys until the hierarchy works. An accent is a pointer: if everything is pointing, nothing is."],
  ["Motion with a job", "Animate what changed, where it changed. Two hundred milliseconds is long enough to notice and short enough not to wait for."],
  ["Then test it small", "Open it on the oldest phone in the drawer. Whatever breaks there is what your visitors have been quietly putting up with."],
];

function Tile({ name, children }: { name: string; children: React.ReactNode }) {
  return (
    <div className="relative h-28 min-w-0 overflow-hidden rounded-xl border border-border bg-card">
      <span className="absolute top-2.5 left-3 text-[11px] font-medium text-muted-foreground">{name}</span>
      <div aria-hidden className="absolute top-9 left-3 grid w-1/2 gap-1.5">
        <span className="h-1.5 rounded-full bg-muted" />
        <span className="h-1.5 w-4/5 rounded-full bg-muted" />
        <span className="h-1.5 w-3/5 rounded-full bg-muted" />
      </div>
      {children}
    </div>
  );
}

export default function BackToTopDemo() {
  const scroller = useRef<HTMLDivElement>(null);

  // Start a little way down, so the button and its progress ring are there to look at.
  useEffect(() => {
    if (scroller.current) scroller.current.scrollTop = 420;
  }, []);

  return (
    <div className="@container flex w-full max-w-3xl min-w-0 flex-col gap-3">
      <div className="relative overflow-hidden rounded-2xl border border-border bg-card shadow-sm">
        <div className="flex items-center gap-1.5 border-b border-border px-4 py-2.5">
          <span aria-hidden className="size-2.5 rounded-full bg-border" />
          <span aria-hidden className="size-2.5 rounded-full bg-border" />
          <span aria-hidden className="size-2.5 rounded-full bg-border" />
          <span className="ml-2 truncate rounded-md bg-muted px-3 py-0.5 text-[11px] text-muted-foreground">viewport.studio/journal/five-rules</span>
        </div>
        <div ref={scroller} tabIndex={0} role="region" aria-label="Article: scroll to try the button" className="h-80 overflow-y-auto overscroll-contain px-6 py-7 outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset @md:px-10">
          <p className="text-xs font-medium tracking-wide text-muted-foreground uppercase">Journal</p>
          <h3 className="mt-1.5 text-2xl font-semibold tracking-tight text-balance">Five rules we keep coming back to</h3>
          {SECTIONS.map(([title, body], i) => (
            <section key={title} className="mt-7">
              <h4 className="text-[15px] font-semibold">
                <span className="mr-2 text-muted-foreground tabular-nums">0{i + 1}</span>
                {title}
              </h4>
              <p className="mt-1.5 max-w-prose text-sm/6 text-muted-foreground">{body}</p>
              <div aria-hidden className="mt-4 h-24 rounded-xl bg-muted/60" />
            </section>
          ))}
        </div>
        <BackToTop contained target={scroller} threshold={160} progress variant="outline" color="var(--primary)" />
      </div>

      <div className="grid min-w-0 grid-cols-2 gap-3 @xl:grid-cols-3">
        <Tile name="Outline with label">
          <BackToTop contained target={scroller} threshold={0} shape="rounded" variant="outline" label="TO TOP" offset={12} />
        </Tile>
        <Tile name="Corner">
          <BackToTop contained target={scroller} threshold={0} shape="corner" color="#4353ff" textColor="#fff" progress />
        </Tile>
        <Tile name="Square">
          <BackToTop contained target={scroller} threshold={0} shape="square" icon="triangle" label="To Top" color="#17b67c" textColor="#fff" offset={12} />
        </Tile>
        <Tile name="Soft, stacked">
          <BackToTop contained target={scroller} threshold={0} shape="rounded" variant="soft" stacked label="TOP" color="#f59e0b" offset={12} />
        </Tile>
        <Tile name="Bare">
          <BackToTop contained target={scroller} threshold={0} shape="bare" icon="chevrons" label="Scroll to Top" offset={10} />
        </Tile>
        <Tile name="Pill with progress">
          <BackToTop contained target={scroller} threshold={0} shape="pill" icon="chevron" label="Top" progress offset={12} />
        </Tile>
        <Tile name="Middle right">
          <BackToTop contained target={scroller} threshold={0} shape="circle" label="TO TOP" position="middle-right" color="#e5484d" textColor="#fff" offset={10} />
        </Tile>
        <Tile name="Top right, vertical">
          <BackToTop contained target={scroller} threshold={0} shape="rounded" stacked label="TOP" position="top-right" variant="soft" color="#7c5cff" offset={10} />
        </Tile>
      </div>
    </div>
  );
}

Props

PropTypeDefault
targetReact.RefObject<HTMLElement | null>

The element that scrolls. Leave it out to follow the page.

—
thresholdnumber

How far down, in pixels, before the button appears. `0` keeps it on screen.

320
shape"circle" | "rounded" | "square" | "pill" | "corner" | "bare"

`corner` docks a quarter circle into the corner; `bare` is just an icon and a label.

"circle"
variant"filled" | "outline" | "soft"

`filled` uses `color`, `outline` is a bordered card and `soft` is a tint of `color`.

"filled"
icon"arrow" | "chevron" | "chevrons" | "triangle" | React.ReactNode

The glyph, or your own node.

"arrow"
labelstring

Text next to the icon. Without it the button is icon-only and named "Back to top".

—
stackedboolean

Puts the label under the icon instead of beside it.

false
progressboolean

Draws how far the page has been read around the edge of the button.

false
position"bottom-right" | "bottom-left" | "bottom-center" | "middle-right" | "middle-left" | "top-right" | "top-left"

Where it floats: a bottom corner or centre, the middle of an edge, or a top corner.

"bottom-right"
offsetnumber

Distance from the edges in pixels.

16
containedboolean

Positions it inside the nearest `relative` parent instead of the window.

false
colorstring

The accent: the fill, the tint or the progress stroke, depending on `variant`.

"var(--foreground)"
textColorstring

Color of the icon and label on a `filled` button.

"var(--background)"
onClick() => void

Called after the scroll to the top has started.

—
classNamestring—

Accessibility

  • A real button named “Back to top”, or by its label when it has one.
  • It stays on screen while it has keyboard focus, so it never disappears from under a Tab press.
  • The progress stroke is decorative and hidden from screen readers.
  • Under reduced motion it fades instead of springing, and the page jumps to the top instead of gliding.