skip to content
mpkcn

~/components/back-to-top

Back to Top

A small square button that appears after the first screen and scrolls back up, with a reduced-motion jump and optional focus target.

$ make build # scroll down

[01] build step 1 ok

[02] build step 2 ok

[03] build step 3 ok

[04] build step 4 ok

[05] build step 5 ok

[06] build step 6 ok

[07] build step 7 ok

[08] build step 8 ok

[09] build step 9 ok

[10] build step 10 ok

[11] build step 11 ok

[12] build step 12 ok

[13] build step 13 ok

[14] build step 14 ok

[15] build step 15 ok

[16] build step 16 ok

[17] build step 17 ok

[18] build step 18 ok

[19] build step 19 ok

[20] build step 20 ok

[21] build step 21 ok

[22] build step 22 ok

[23] build step 23 ok

[24] build step 24 ok

[25] build step 25 ok

[26] build step 26 ok

[27] build step 27 ok

[28] build step 28 ok

[29] build step 29 ok

[30] build step 30 ok

[31] build step 31 ok

[32] build step 32 ok

[33] build step 33 ok

[34] build step 34 ok

[35] build step 35 ok

[36] build step 36 ok

[37] build step 37 ok

[38] build step 38 ok

[39] build step 39 ok

[40] build step 40 ok

Installation

terminal
npx mpkcn add back-to-top
deps:none beyond react· tested with react@19.3.0

Usage

Render it once, in your root layout. A small square button fades in at the bottom right after the reader scrolls past most of the first screen.

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

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en" suppressHydrationWarning>
      <body>
        <main id="main">{children}</main>
        <BackToTop focusSelector="#main" />
      </body>
    </html>
  );
}

Examples

Inside a scroll container

Pass a ref as target and fixed={false} to watch and scroll that element, with the button placed in the nearest positioned parent. Here it shows after 120px.

$ make build # scroll down

[01] build step 1 ok

[02] build step 2 ok

[03] build step 3 ok

[04] build step 4 ok

[05] build step 5 ok

[06] build step 6 ok

[07] build step 7 ok

[08] build step 8 ok

[09] build step 9 ok

[10] build step 10 ok

[11] build step 11 ok

[12] build step 12 ok

[13] build step 13 ok

[14] build step 14 ok

[15] build step 15 ok

[16] build step 16 ok

[17] build step 17 ok

[18] build step 18 ok

[19] build step 19 ok

[20] build step 20 ok

[21] build step 21 ok

[22] build step 22 ok

[23] build step 23 ok

[24] build step 24 ok

[25] build step 25 ok

[26] build step 26 ok

[27] build step 27 ok

[28] build step 28 ok

[29] build step 29 ok

[30] build step 30 ok

[31] build step 31 ok

[32] build step 32 ok

[33] build step 33 ok

[34] build step 34 ok

[35] build step 35 ok

[36] build step 36 ok

[37] build step 37 ok

[38] build step 38 ok

[39] build step 39 ok

[40] build step 40 ok

Terminal label

$ make build # scroll down

[01] build step 1 ok

[02] build step 2 ok

[03] build step 3 ok

[04] build step 4 ok

[05] build step 5 ok

[06] build step 6 ok

[07] build step 7 ok

[08] build step 8 ok

[09] build step 9 ok

[10] build step 10 ok

[11] build step 11 ok

[12] build step 12 ok

[13] build step 13 ok

[14] build step 14 ok

[15] build step 15 ok

[16] build step 16 ok

[17] build step 17 ok

[18] build step 18 ok

[19] build step 19 ok

[20] build step 20 ok

[21] build step 21 ok

[22] build step 22 ok

[23] build step 23 ok

[24] build step 24 ok

[25] build step 25 ok

[26] build step 26 ok

[27] build step 27 ok

[28] build step 28 ok

[29] build step 29 ok

[30] build step 30 ok

[31] build step 31 ok

[32] build step 32 ok

[33] build step 33 ok

[34] build step 34 ok

[35] build step 35 ok

[36] build step 36 ok

[37] build step 37 ok

[38] build step 38 ok

[39] build step 39 ok

[40] build step 40 ok

Props

PropTypeDefaultDescription
targetRefObject<HTMLElement | null>windowScroll container to watch and scroll. Leave empty to use the page.
fixedbooleantruePin the button to the bottom right of the viewport. false places it in the nearest positioned parent instead (use with target).
thresholdnumber0.8Show the button after this much scroll: below 1 is a share of the visible height, otherwise px.
labelstring"Back to top"Accessible name (and tooltip) of the button.
focusSelectorstring—Element to focus after scrolling up, e.g. "#main". Keyboard users then continue from the top.
ignoreReducedMotionbooleanfalseScroll smoothly even when the user prefers reduced motion. Use sparingly.
childrenReactNode"↑"Button content.
classNamestring—Extra classes for the button.

Accessibility

  • It is a real button with an aria-label and a visible focus outline.
  • While hidden it is inert: out of the tab order and the accessibility tree, so no one tabs onto an invisible control.
  • Scrolling is smooth by default and jumps instantly under prefers-reduced-motion: reduce. The fade-in also turns off.
  • Use focusSelector to move keyboard focus to the top of the page after scrolling; otherwise focus would land on body once the button hides. Elements that are not focusable get tabindex="-1" first.