Scroll top

A floating button that appears once the page has scrolled down and takes the reader back to the top. Use it on long pages and long scrolling panels.

  • vig-scroll-top
Import
import { ScrollTop } from '@vigdx/ui/scroll-top';

Page

Place one vig-scroll-top anywhere in the app; it watches the page and shows past threshold pixels (default 400), fixed at the bottom inline-end corner. variant colors it.

This page has one: scroll down past 400 pixels and it appears in the bottom corner.

Scroll container

target="parent" watches the element the button is a direct child of; the button sticks to its bottom.

Release note 1. Scroll down: the button appears once this box scrolled past 120 pixels, and takes you and the focus back to the top.

Release note 2. Scroll down: the button appears once this box scrolled past 120 pixels, and takes you and the focus back to the top.

Release note 3. Scroll down: the button appears once this box scrolled past 120 pixels, and takes you and the focus back to the top.

Release note 4. Scroll down: the button appears once this box scrolled past 120 pixels, and takes you and the focus back to the top.

Release note 5. Scroll down: the button appears once this box scrolled past 120 pixels, and takes you and the focus back to the top.

Release note 6. Scroll down: the button appears once this box scrolled past 120 pixels, and takes you and the focus back to the top.

Release note 7. Scroll down: the button appears once this box scrolled past 120 pixels, and takes you and the focus back to the top.

Release note 8. Scroll down: the button appears once this box scrolled past 120 pixels, and takes you and the focus back to the top.

Release note 9. Scroll down: the button appears once this box scrolled past 120 pixels, and takes you and the focus back to the top.

Release note 10. Scroll down: the button appears once this box scrolled past 120 pixels, and takes you and the focus back to the top.

Release note 11. Scroll down: the button appears once this box scrolled past 120 pixels, and takes you and the focus back to the top.

Release note 12. Scroll down: the button appears once this box scrolled past 120 pixels, and takes you and the focus back to the top.

Styling hooks

Internal elements carry a stable data-part and the component's data-scope; both are public API, so removing one is a breaking change. Select a part by both, e.g. [data-scope="scroll-top"][data-part="root"] { … }: the pair stays within this component when kit components nest, and also reaches parts in overlay panels, which render outside the host. State is exposed through aria-* and, where listed, a data-state; literal values are checked by the API gate, values computed from the component's state follow its documented types. Other data-* attributes listed below (such as data-opening while an overlay animates) are API too. See Customizing.

Scope: [data-scope="scroll-top"]

ScrollTop styling hooks
Part Element data-state
root The vig-scroll-top host (fixed, or sticky with parent ). —
trigger The button. visible, hidden
icon The arrow icon. —

Accessibility

  • A native <button> named by labels.ariaLabel (locale default "Scroll to top"). While hidden it is visibility: hidden: out of the tab order and the accessibility tree.
  • After scrolling, focus moves to the top of the content so keyboard and screen-reader users continue from there: focusTarget, else the page's <main> (the usual skip-link target) or the scroll container, given tabindex="-1" for that move when it is not focusable (removed once focus leaves it). Focus moves the same way when the button hides while focused.
  • Scrolling is smooth, and instant for users who prefer reduced motion.
  • The page is watched by one IntersectionObserver per threshold on a 1px sentinel that every scroll-top shares (appended to <body>), not by a scroll listener. In a cross-origin iframe, which ignores the observer's margin, one shared passive scroll listener answers instead. When <body> rather than the document scrolls, place a target="parent" scroll-top inside the scrolling element. Nothing runs on the server, where the button renders hidden.

API

ScrollTop

vig-scroll-top component

A floating button that appears once the page (or, with target="parent", the scrolling element it is placed in) has scrolled more than threshold pixels. It scrolls back to the top — smoothly, instantly for users who prefer reduced motion — and moves focus to the top of the content, so keyboard and screen-reader users continue from there: focusTarget, else the page's <main> (or the scrolling element), made focusable with tabindex="-1" when it is not.

Example :
<vig-scroll-top />

The page is watched with an IntersectionObserver on one sentinel that every scroll-top shares (no scroll listener; in a cross-origin iframe, which ignores the observer's margin, one shared passive scroll listener); a parent with a passive scroll listener on that element. A page whose <body> is the scrolling element needs target="parent" inside it. Hidden on the server and until the first check. When the button hides while it has focus (Home pressed on it), focus moves the same way.

ScrollTop props
Name Type Default Description
focusTargetHTMLElement | undefinedundefined

Where focus goes after scrolling up; defaults to <main> (page) or the scrolling element.

labelsPartial<ScrollTopLabels>—

Texts (i18n): the button's accessible name; locale default "Scroll to top".

targetScrollTopTarget'window'

window (default): the page; parent: the element the scroll-top is a direct child of.

thresholdnumber400

Pixels scrolled before the button appears.

variantVariant'secondary'
—

Are you sure?