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
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
| Prop | Description | Type | Options | Default |
|---|---|---|---|---|
| target | Scroll container (selector or element; defaults to the viewport) | string | HTMLElement | — | — |
| visibilityHeight | Scroll distance (px) before the button appears | number | — | 200 |
| right | Offset from the right edge (px) | number | — | 40 |
| bottom | Offset from the bottom edge (px) | number | — | 40 |
| smooth | Whether to scroll smoothly | boolean | — | true |
| children | Custom button content (replaces the default up-arrow icon) | ReactNode | — | Up-arrow icon |
| sx | Layout shorthand | SxProps | — | — |
Backtop events
| Event | Description | Callback signature | Vue equivalent |
|---|---|---|---|
| onClick | Emitted when the button is clicked (the scroll-to-top runs afterwards) | (event: MouseEvent<HTMLButtonElement>) => void | @click |
| onScrollend | Emitted when scrolling stops, carrying the current scroll distance | (scrollTop: number) => void | @scrollend |
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.
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.
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'