Command palette

@yasinsensoy/web

Web · Components

BackToTop

"Back to top" button: appears in the corner once the page is scrolled by threshold and scrolls smoothly to the top on press.

3 exports

Live example

Preview

Scroll the page down; the button appears in the corner and its ring shows how much you've read.

Code

import { BackToTop } from "@yasinsensoy/web";

<BackToTop threshold={200} />

Events

  1. Events appear here when you interact with the preview.

Import

import { BackToTop } from "@yasinsensoy/web";

BackToTop

componentClient component

"Back to top" button: appears in the corner once the page is scrolled by threshold and scrolls smoothly to the top on press. For long lists, blogs and docs pages.

  • Progress: the ring around it fills with the read part of the page (turned off with progress={false}). Scrolling is processed once per frame and the ring updates without a React render.
  • On scroll up: with showOnScrollUp it appears only while scrolling up; it doesn't cover content while reading down.
  • Accessibility: after going to the top, focus moves to the main element; the screen reader continues from the top. While hidden it isn't in the tab order.
  • Mobile: room is left for the notch and home indicator (safe-area); with the reduced motion preference scrolling is instant.
BackToTop({ threshold, target, position, progress, showOnScrollUp, focusTarget, icon, label, children, tooltip, onClick, className, }: BackToTopProps): string | number | bigint | boolean | JSX.Element | Iterable<ReactNode> | Promise<string | number | bigint | boolean | Iterable<ReactNode> | ReactElement<unknown, string | JSXElementConstructor<any>> | ReactPortal | null | undefined> | null | undefined

Examples

// Root layout or a long page
<BackToTop />

<BackToTop target="#list" position="bottom-left" showOnScrollUp>Back to top</BackToTop>

Position of the BackToTop button on screen.

type BackToTopPosition = "bottom-right" | "bottom-left" | "bottom-center"

Props of the BackToTop component.

NameTypeDefaultDescription
threshold?number400Scroll distance needed for the button to appear (px).
target?ScrollRootScrolling container: an element, a ref or a CSS selector. Defaults to the page. With a container the button sits at the container's corner (the container doesn't have to be relative; the button still stays at the screen corner).
position?BackToTopPosition"bottom-right"Position of the button.
progress?booleantrueShow reading progress in the ring? The ring fills with how much of the page has been read.
showOnScrollUp?booleanfalseVisible only while scrolling up? It doesn't cover content while reading down; it appears when the user wants to go back.
focusTarget?string | false"main"Where focus goes on press. So screen reader and keyboard users can continue from the top of the page, the main element (otherwise the container) is focused by default. false leaves focus where it is.
icon?IconPropup arrowButton icon.
label?string"Başa dön"` (`messages.backToTop`)Screen reader name and tooltip.
children?ReactNodeText shown next to the icon (e.g. "Back to top"); when given the button becomes a pill.
tooltip?TooltipProp | falseTooltip on hover. Defaults to label; false turns it off. See TooltipProp.
onClick?() => voidCalled after scrolling to the top.
className?stringClasses added to the button (e.g. bottom-24 to move it).