Back to Ant Design

Anchor

components/anchor/index.en-US.md

6.3.74.9 KB
Original Source

When To Use

For displaying anchor hyperlinks on page and jumping between them.

Notes for developers

After version 4.24.0, we rewrite Anchor use FC, Some methods of obtaining ref and calling internal instance methods will invalid.

Examples

<!-- prettier-ignore -->

<code src="./demo/basic.tsx" iframe="200">Basic</code> <code src="./demo/horizontal.tsx" iframe="200">Horizontal Anchor</code> <code src="./demo/static.tsx" >Static Anchor</code> <code src="./demo/onClick.tsx">Customize the onClick event</code> <code src="./demo/customizeHighlight.tsx">Customize the anchor highlight</code> <code src="./demo/targetOffset.tsx" iframe="200">Set Anchor scroll offset</code> <code src="./demo/onChange.tsx">Listening for anchor link change</code> <code src="./demo/replace.tsx" iframe="200">Replace href in history</code> <code src="./demo/legacy-anchor.tsx" debug>Deprecated JSX demo</code> <code src="./demo/style-class.tsx" iframe="200" version="6.0.0">Custom semantic dom styling</code> <code src="./demo/component-token.tsx" iframe="800" debug>Component Token</code>

API

Common props ref:Common props

Anchor Props

PropertyDescriptionTypeDefaultVersion
affixFixed mode of Anchorboolean | Omit<AffixProps, 'offsetTop' | 'target' | 'children'>trueobject: 5.19.0
boundsBounding distance of anchor areanumber5
classNamesCustomize class for each semantic structure inside the component. Supports object or function.Record<SemanticDOM, string> | (info: { props })=> Record<SemanticDOM, string>-
getContainerScrolling container() => HTMLElement() => window
getCurrentAnchorCustomize the anchor highlight(activeLink: string) => string-
offsetTopPixels to offset from top when calculating position of scrollnumber0
showInkInFixedWhether show ink-square when affix={false}booleanfalse
stylesCustomize inline style for each semantic structure inside the component. Supports object or function.Record<SemanticDOM, CSSProperties> | (info: { props })=> Record<SemanticDOM, CSSProperties>-
targetOffsetAnchor scroll offset, default as offsetTop, examplenumber-
onChangeListening for anchor link change(currentActiveLink: string) => void
onClickSet the handler to handle click event(e: MouseEvent, link: object) => void-
itemsData configuration option content, support nesting through children{ key, href, title, target, children }[] see-5.1.0
directionSet Anchor directionvertical | horizontalvertical5.2.0
replaceReplace items' href in browser history instead of pushing itbooleanfalse5.7.0

AnchorItem

PropertyDescriptionTypeDefaultVersion
keyThe unique identifier of the Anchor Linkstring | number-
hrefThe target of hyperlinkstring
targetSpecifies where to display the linked URLstring
titleThe content of hyperlinkReactNode
childrenNested Anchor Link, Attention: This attribute does not support horizontal orientationAnchorItem[]-
replaceReplace item href in browser history instead of pushing itbooleanfalse5.7.0

We recommend using the items form instead.

PropertyDescriptionTypeDefaultVersion
hrefThe target of hyperlinkstring
targetSpecifies where to display the linked URLstring
titleThe content of hyperlinkReactNode

Semantic DOM

<code src="./demo/_semantic.tsx" simplify="true"></code>

Design Token

<ComponentTokenTable component="Anchor"></ComponentTokenTable>

FAQ

In version 5.25.0+, the :target pseudo-class of the destination element does not take effect as expected after anchor navigation. {#faq-target-pseudo-class}

For the purpose of page performance optimization, the implementation of anchor navigation has been changed from window.location.href to window.history.pushState/replaceState. Since pushState/replaceState does not trigger a page reload, the browser will not automatically update the matching state of the :target pseudo-class. To resolve this issue, you can manually construct the full URL: href = window.location.origin + window.location.pathname + '#xxx'.

Related issues: #53143 #54255