Ant Design Svelte

Anchor

Navigation through document sections with scroll tracking, indentation and history.

When to use

Anchor links the table of contents to sections of a long page and shows the active section as you scroll. It is suitable for documentation, ordering conditions and articles with strong title identifiers. To navigate between pages, use Menu.

Transitions and active section

Each item contains a key, href and title. An address like #section must match an existing id. In vertical mode, children creates a nested branch; horizontal mode displays only the top level.

Script Setting Example
Table of contents with nested paragraphs items, children Vertical list
One row of sections direction="horizontal" Horizontal list
Own logic of the active item getCurrentAnchor Own lighting
Reaction to section change onChange Changing section
Address and transition history replace Story

onClick receives a MouseEvent and link information. onChange reports the active href and can be triggered during normal scrolling. Do not replace the handler for sending data or confirming an action with it.

Container, docking and padding

Window scrolling is used without settings. For a local scope, getContainer returns its HTMLElement; Unlike Affix.target, this getter's public type does not allow null. Bind the node using bind:this, as in the examples. A generic getter can be set via ConfigProvider.getTargetContainer.

offsetTop defines the docking position. targetOffset sets the target offset when transitioning and defining the current section; the value of a specific item takes precedence. bounds—tolerance for determining the active partition. If the header is fixed, set the indentation according to its actual height: otherwise the title will appear below it.

Affix accepts a boolean or Affix settings without offsetTop, target and children. false leaves the list on the stream; showInkInFixed keeps the indicator in this mode. Securing in a container also shows a CSS wrapper: the scroll target itself does not clip the fixed node to the borders of the card.

Examples

Project Description
Project delivery
Payment for the project

Vertical navigation

Nested items and section highlighting when scrolling the selected container. Pinning is disabled to isolate the card.

Project Description
Project delivery
Payment for the project

Horizontal navigation

Horizontal Anchor uses a flat list. The long menu scrolls within its own area.

Project Description
Project delivery
Payment for the project

Without fastening

affix=false leaves navigation in the stream. showInkInFixed controls the indicator; Partition tracking is maintained.

Project Description
Project delivery
Payment for the project
Links have not been clicked yet

Click processing

preventDefault prevents the URL from being written to history. Anchor continues to scroll the container and reports the selected link.

Project Description
Project delivery
Payment for the project

Own lighting

getCurrentAnchor sets the active link regardless of actual scrolling. Selecting the backlight remains the state of the example.

Area above sections
Project Description
Project delivery
Payment for the project

Target offset

targetOffset leaves space above the section. For one link, its own targetOffset takes precedence over the general one.

Project Description
Project delivery
Payment for the project
No events

Changing the active partition

onChange reflects transitions between sections when clicked and manually scrolled. The story is limited to the last five events.

Project Description
Project delivery
Payment for the project

Transition history

replace changes the current history entry instead of adding a new one. This example intentionally updates fragment in the address bar.

Project Description
Project delivery
Payment for the project

Semantic styles

The styles function styles root, item, itemTitle and indicator. When changing direction, the function receives the current props.

Project Description
Project delivery
Payment for the project

Component tokens

The local theme changes the vertical and horizontal padding of links via theme.components.Anchor.

Start of document
Document details

Securing in a container

affix accepts Affix parameters. Local containing block and rootClassName restrict sticky navigation to the card area.

Empty record restores the standard design:

Project Description
Project delivery
Payment for the project

Anchor Settings

General semantic defaults, replacing record and recalculating the indicator after changing indents.

The link inherits the Anchor owner prefix.
Empty item.prefixCls selects a common provider prefix.
Non-empty item.prefixCls specifies the full custom prefix.
General: suite; class: project-anchor; section: —

For two links with explicit item.prefixCls, the design is specified separately via class. Changing the prefix preserves the links and the position of the indicator.

Anchor and Link Prefixes

General and local prefixCls, item inheritance and explicit empty prefix. The indicator continues to follow the active link.

Order Description
Delivery terms
Payment methods

Order sections

Saved review example with local scrolling of description, delivery and payment.

API

Examples are checked against the current ant.design page and the public API of the installed package. Full definitions of Props are in @woodger/antd.

Topics of official examples
ThemeExample or limitation
BasicVertical list and nested links; fastening separately.
Horizontal AnchorFlat horizontal menu.
Static Anchoraffix=false and indicator control.
Customize the onClick eventCancel URL changes without canceling scrolling.
Customize the anchor highlightgetCurrentAnchor and backlight state.
Set Anchor scroll offsetGeneral and individual target indent.
Listening for anchor link changeHistory onChange.
Replace href in historyreplaceState and pushState.
Custom semantic dom stylingclassNames object and styles function.
CSS prefixLocal Anchor, inherited links and independent item.prefixCls.
Main parameters of Anchor
PropertyDescriptionTypeDefault
prefixClsFull local CSS prefix. The empty string and undefined share the same provider prefix.stringConfigProvider.prefixCls + "-anchor"
itemsLinks; children create nesting only with vertical. items takes precedence over children-snippet.readonly AnchorItem[]—
directionMenu direction; in horizontal, nested children are not displayed.'vertical' | 'horizontal'vertical
affixFastening via Affix; false leaves Anchor in the thread.boolean | Omit<AffixProps, ...>true
showInkInFixedShows the indicator when affix=false.booleanfalse
getContainerTracking and scrolling area; getter is called in the browser.() => HTMLElement | Window() => window
offsetTopReinforcement offset and fallback target offset.number0
targetOffsetTarget offset; The targetOffset of an individual link takes precedence.numberoffsetTop
boundsActive section definition tolerance in px.number5
getCurrentAnchorOverrides the highlighted link; onChange gets the original href.(activeLink: string) => string—
onClickpreventDefault prevents writing a URL; Anchor scrolling is preserved.(event, { title, href }) => void—
onChangeSelected section when clicked or scrolled.(href: string) => void—
replaceReplaces history entry; replace of an individual item takes precedence.booleanfalse
classNamesSemantic slots classes.AnchorSlots | (info) => AnchorSlots—
stylesCSS lines semantic slots.AnchorSlots | (info) => AnchorSlots—
nativeElementbind:nativeElement returns the outer div Anchor.HTMLDivElement—

Link parameters

AnchorItem specifies key, href, title, target, class, replace, targetOffset, prefixCls and children. title accepts text, number or Snippet.

item.prefixCls: undefined inherits the allowed Anchor prefix; the empty string selects a common provider prefix with the suffix -anchor; a non-empty string specifies the full link prefix. To explicitly override item you need custom CSS. Semantic callbacks Anchor receive the original local props.prefixCls.

ConfigProvider.anchor

AnchorConfig: classstyleclassNamesstyles. class/style - strings; classNames/styles - slots object or function with current props.

Classes add up. Priority of root styles: defaults.styles.root → defaults.style → local styles.root → local style. The local styles of the remaining slots override the defaults. A nested anchor replaces the entire record, an empty object resets the settings. When changing semantic styles, the position of the indicator is recalculated using the current link.

Semantic DOM

Anchor creates an outer div and internal a links with real href. Semantic slots: root, item, itemTitle, indicator. styles accepts CSS strings; functions receive current props. General defaults are set via ConfigProvider.anchor.

The keyboard uses regular Tab and Enter links. When horizontal, the menu scrolls following the active link. Decorative indicator; The component does not assign a separate aria-current for the active link.

Registration tokens

Both component tokens work via ConfigProvider theme.components.Anchor. The card shows local padding; The overall colors and typography follow the theme of the section.

Component tokens
TokenPurpose
linkPaddingBlockVertical indent of the link.
linkPaddingInlineStartNested level indentation.
Common tokens of the current topic
TokenCurrent value
colorPrimary#2f54eb
colorSplitrgba(5,5,5,0.06)
colorTextrgba(0,0,0,0.88)
fontFamily-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji'
fontSize14
lineHeight1.5714285714285714
lineTypesolid
lineWidth1
lineWidthBold2
motionDurationSlow0.3s
paddingXXS4

FAQ

Why do the examples scroll the card and not the entire page?

Each getContainer returns a separate area. In the application, you can leave the default window or select your own scroll container.

Why does the pinned menu go beyond the target border?

target specifies the observation area. The fixed node drawing constraint requires CSS composition; it is saved in the pinning example.

How to undo a URL change?

Call preventDefault in onClick. Scrolling and active link detection will continue to work.

Why doesn't the :target design work?

History changes programmatically. The state of the active section is available through onChange and getCurrentAnchor.

Is it possible to make a nested horizontal list?

horizontal does not display children. For hierarchy use vertical.