Skip to main content

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​

Sticky top / bottom (scroll the page down)
Sticky top bar

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

Sticky bottom bar

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​

PropDescriptionTypeOptionsDefault
offsetTopDistance (px) from the viewport top that triggers sticky topnumber—0
offsetBottomDistance (px) from the viewport bottom that triggers sticky bottom (priority over offsetTop)number—0
targetScroll container to listen on (selector or element; defaults to the viewport)string | HTMLElement——
childrenAffix content (plus's default slot)ReactNode——
sxLayout shorthandSxProps——

Affix events​

EventDescriptionCallback signatureVue equivalent
onChangeEmitted when the fixed state toggles (entering or leaving sticky top / bottom)(fixed: boolean) => void@change
Vue equivalent

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.

No layout jump

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.

Accessibility

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'