Anchor
Scroll-spy table of contents that highlights the heading at the top of a scroll container.
import { Anchor } from '@lobehub/ui/base-ui';Navigation
Basic
Loading preview
APIs
Anchor properties
activeKeynull | stringgetContainer() => HTMLElement | Window | null | undefineditemsRequiredAnchorItem[]offsetnumberDefaults to
0.onChange(key: string | null) => voidonClick(event: MouseEvent<HTMLAnchorElement>, item: AnchorItem) => voidrefnull | (instance: HTMLElement | null) => void | (() => VoidOrUndefinedOnly) | DO_NOT_USE_OR_YOU_WILL_BE_FIRED_CALLBACK_REF_RETURN_VALUES[keyof DO_NOT_USE_OR_YOU_WILL_BE_FIRED_CALLBACK_REF_RETURN_VALUES] | RefObject<HTMLElement | null>Allows getting a ref to the component instance. Once the component unmounts, React will set `ref.current` to `null` (or call the ref with `null` if you passed a callback ref).
Also accepts all native HTML and ARIA attributes.
The active item is the last target whose top has passed offset pixels below the top of the container. getContainer returns the scroll element (default window) and is only called in effects, so it is SSR-safe. Clicking a link smooth-scrolls (instant under reduced motion) and keeps that link active until the scroll ends; call event.preventDefault() in onClick to handle navigation yourself.
Migrating from antd Anchor
affix,bounds,showInkInFixed,targetOffset,replaceanddirectionare not supported; the list is always vertical with no ink bar.onChangereceives the itemkey(not thehref);activeKeycontrols the highlight.
Helpful?