Affix
Sticky pinning on scroll (top or bottom). Uses IntersectionObserver to detect the placeholder leaving the viewport (no scroll-throttling overhead; falls back to a scroll listener when IO is unavailable).
Basic usage
Scroll the page down: the bar pins when it reaches the top of the dashed box and restores when it leaves.
Sticky top state: Not fixed
Scroll the page down: the bar pins when it reaches the top of the dashed box and restores when it leaves.
Sticky bottom state: Not fixed
import { useState } from 'react'
import { LhAffix, useLhLocale } from '@lh-ui/react'
export default function Demo() {
const { t } = useLhLocale()
const [topFixed, setTopFixed] = useState(false)
const [bottomFixed, setBottomFixed] = useState(false)
return (
<div className="demo-col">
<div className="demo-affix-zone">
<LhAffix offsetTop={8} onChange={setTopFixed}>
<div className="demo-affix-bar">{t('docs.affix.topBar')}</div>
</LhAffix>
<p className="demo-affix-text">{t('docs.affix.zoneHint')}</p>
</div>
<p>
{t('docs.affix.topState')}
{topFixed ? t('docs.affix.fixed') : t('docs.affix.unfixed')}
</p>
<div className="demo-affix-zone">
<LhAffix offsetBottom={8} onChange={setBottomFixed}>
<div className="demo-affix-bar">{t('docs.affix.bottomBar')}</div>
</LhAffix>
<p className="demo-affix-text">{t('docs.affix.zoneHint')}</p>
</div>
<p>
{t('docs.affix.bottomState')}
{bottomFixed ? t('docs.affix.fixed') : t('docs.affix.unfixed')}
</p>
</div>
)
}
offsetBottom pins to the bottom (taking priority over offsetTop); target selects the
scroll container to listen on (defaults to the viewport).
Affix Props
| Prop | Description | Type | Options | Default |
|---|---|---|---|---|
| offsetTop | Distance (px) from the viewport top that triggers sticky top | number | — | 0 |
| offsetBottom | Distance (px) from the viewport bottom that triggers sticky bottom (priority over offsetTop) | number | — | 0 |
| target | Scroll container to listen on (selector or element; defaults to the viewport) | string | HTMLElement | — | — |
| children | Affix content (plus's default slot) | ReactNode | — | — |
| sx | Layout shorthand | SxProps | — | — |
Affix events
| Event | Description | Callback signature | Vue equivalent |
|---|---|---|---|
| onChange | Emitted when the fixed state toggles (entering or leaving sticky top / bottom) | (fixed: boolean) => void | @change |
offsetTop / offsetBottom / target / sx match lh-ui-plus's LhAffix props; React has no
slots, so plus's default slot is merged into children, and @change maps to onChange.
While fixed, the placeholder keeps the flow height with an explicit height and the fixed element matches the placeholder width, so content does not jitter while scrolling.
No special role; the fixed state does not affect the document-flow focus order, and the fixed
element's stacking uses --lh-affix-z-index (900 by default).
Styles
import '@lh-ui/react/style/affix'