JGuang

Anchor

Anchor · Ant Design

Preview
npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23

Anchor

Hyperlinks to scroll on one page.

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

  • Basic · basic

  • Horizontal Anchor · horizontal

  • Static Anchor · static

  • Customize the onClick event · onClick

  • Customize the anchor highlight · customizeHighlight

  • Set Anchor scroll offset · targetOffset

  • Listening for anchor link change · onChange

  • Replace href in history · replace

  • Custom semantic dom styling · style-class

API

Common props ref:Common props

Anchor Props

PropertyDescriptionTypeDefaultVersionGlobal Config
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>-6.0.0
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>-6.0.0
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
targetOffsetCustomize scroll offset for this anchor link. It takes precedence over the targetOffset prop of the Anchor componentnumber-6.4.0

We recommend using the items form instead.

PropertyDescriptionTypeDefaultVersion
hrefThe target of hyperlinkstring-
targetSpecifies where to display the linked URLstring-
titleThe content of hyperlinkReactNode-
targetOffsetCustomize scroll offset for this anchor link. It takes precedence over the targetOffset prop of the Anchor componentnumber-6.4.0

Semantic DOM

Semantic nodeNative descriptionVersion
rootRoot element with layout positioning, padding, margin, background color and other basic styles6.0.0
itemLink item element with padding, text color, hover states, transition animations and other styles6.0.0
itemTitleTitle text element with font styles, color changes, text decoration, transition effects and other styles6.0.0
indicatorIndicator element with width, height, background color, position changes, transition animations and other styles6.0.0

Semantic description source: vendor/components/anchor/demo/_semantic.tsx

Design Token

Types, inheritance and declarations retain the pinned native source. Defaults are computed by the native theme and may be overridden by ConfigProvider; this table does not invent static defaults.

TokenTypeNative description
linkPaddingBlocknumberVertical padding of link
linkPaddingInlineStartnumberHorizontal padding of link

Declaration source: vendor/components/anchor/style/index.ts

import type { CSSObject } from '@ant-design/cssinjs';

import { unit } from '@ant-design/cssinjs';

import { resetComponent, textEllipsis } from '../../style';

import type { FullToken, GenerateStyle, GetDefaultToken } from '../../theme/internal';

import { genStyleHooks, mergeToken } from '../../theme/internal';

export interface ComponentToken {
  /**
   * @desc 链接纵向内间距
   * @descEN Vertical padding of link
   */
  linkPaddingBlock: number;
  /**
   * @desc 链接横向内间距
   * @descEN Horizontal padding of link
   */
  linkPaddingInlineStart: number;
}

/**
 * @desc Anchor 组件的 Token
 * @descEN Token for Anchor component
 */
interface AnchorToken extends FullToken<'Anchor'> {
  /**
   * @desc 容器块偏移量
   * @descEN Holder block offset
   */
  holderOffsetBlock: number;
  /**
   * @desc 次级锚点块内间距
   * @descEN Secondary anchor block padding
   */
  anchorPaddingBlockSecondary: number | string;
  /**
   * @desc 锚点球大小
   * @descEN Anchor ball size
   */
  anchorBallSize: number | string;
  /**
   * @desc 锚点标题块
   * @descEN Anchor title block
   */
  anchorTitleBlock: number | string;
}

FAQ

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

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

Shared native props

Tips: The following generic properties apply to most antd components; those not supported are described separately.

PropertyDescriptionTypeDefault
styleThe additional styleCSSProperties-
classNameThe additional css classstring-
rootClassNameClassName on the root elementstring-
autoFocusAuto focus when component mounted, only effective for focusable elements like forms, links, etc.booleanfalse

Install and reproduce an example

npm install antd@6.6.5 @ant-design/icons@6.3.4 dayjs@1.11.23

Use ConfigProvider and native App; configure dayjs locale separately. Each example lists its complete source files and additional pinned dependencies in View code. Example names select demos, not universal component props. See the resource setup guide for theme, locale and SSR.

Native type references

The source viewer includes unchanged native implementations and the following source declarations. Inherited types remain in their native packages. English source comments are preserved as code reference.

  • vendor/components/anchor/Anchor.tsx
  • vendor/components/anchor/AnchorLink.tsx
  • vendor/components/anchor/index.tsx
  • vendor/components/anchor/style/index.ts

Source and license

https://github.com/ant-design/ant-design/tree/4a39f54842eade4e565ab336ef6097cd7e723cdd

Original bilingual prose and executable examples are retained. Website-only embeds and token tables are expanded locally. Ant Design is MIT; separately listed dependency notices also apply. Upload previews block outbound requests and do not provide a backend.