Skip to main content

Backtop

Appears after scrolling past a threshold and scrolls back to the top on click, using the native scrollTo({ behavior: 'smooth' }) (degrades automatically under prefers-reduced-motion).

Basic usage​

Basic (scroll down to see it)

Last stop position: (not scrolled)

import { useState } from 'react'
import { LhBacktop, useLhLocale } from '@lh-ui/react'

export default function Demo() {
const { t } = useLhLocale()
const [lastStop, setLastStop] = useState<number | null>(null)

return (
<div className="demo-col">
<LhBacktop right={24} bottom={24} />
<LhBacktop right={24} bottom={88} visibilityHeight={80} smooth={false} />
<LhBacktop
right={24}
bottom={152}
onScrollend={(scrollTop) => setLastStop(scrollTop)}
/>
<p>
{t('docs.backtop.lastStop')}
{lastStop === null ? t('docs.backtop.notScrolled') : `${lastStop}px`}
</p>
<LhBacktop right={24} bottom={216}>{t('docs.backtop.topText')}</LhBacktop>
</div>
)
}

Backtop Props​

PropDescriptionTypeOptionsDefault
targetScroll container (selector or element; defaults to the viewport)string | HTMLElement——
visibilityHeightScroll distance (px) before the button appearsnumber—200
rightOffset from the right edge (px)number—40
bottomOffset from the bottom edge (px)number—40
smoothWhether to scroll smoothlyboolean—true
childrenCustom button content (replaces the default up-arrow icon)ReactNode—Up-arrow icon
sxLayout shorthandSxProps——

Backtop events​

EventDescriptionCallback signatureVue equivalent
onClickEmitted when the button is clicked (the scroll-to-top runs afterwards)(event: MouseEvent<HTMLButtonElement>) => void@click
onScrollendEmitted when scrolling stops, carrying the current scroll distance(scrollTop: number) => void@scrollend
Vue equivalent

target / visibilityHeight / right / bottom / smooth match lh-ui-plus's LhBackTop props; React has no slots, so plus's #default slot is merged into children, and @click / @scrollend map to onClick / onScrollend.

scrollend compatibility

onScrollend prefers the native scrollend event (Chromium 114+ / Safari 18.2+); older browsers fall back to scroll + a 150ms debounce. Commonly used for anchor syncing and lazy data loading.

Accessibility

A real <button>; the icon-only form is named via the locale key backtop.label ("Back to top" by default). When hidden the button is removed from the tree entirely and takes no spot in the tab order.

Styles​

import '@lh-ui/react/style/backtop'