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.
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.
History, design and availability
For local fragment links, the component changes the address via pushState or replaceState. replace can be set to the entire Anchor or an individual item. target refers to a link and allows you to open a regular address in a different way.
Calling event.preventDefault() on onClick cancels the history change, but not the Anchor's internal scrolling. Therefore, documentation cards can be scrolled without changing the address of the entire page. This history does not trigger a native fragment transition: use application state, not just CSS :target, to style the current section.
Links retain native Tab and Enter. The component does not automatically add aria-current; If necessary, set the available label for the current section in your own composition. Root, item, itemTitle and indicator are configured via classNames/styles; styles values are CSS strings. General defaults are provided by ConfigProvider.anchor, theme indents are provided by Anchor component design tokens.
prefixCls specifies the full local Anchor prefix. The empty string and undefined use ConfigProvider.prefixCls with the -anchor suffix. For item, the rules are different: a missing prefixCls inherits the allowed prefix of the Anchor owner, an empty string returns the provider's prefix, a non-empty string sets its own full link prefix. Explicit item overrides require separate CSS, as in example of prefixes. Local Anchor.prefixCls does not rename Affix; theme.cssVar.prefix separately sets the namespace of theme variables.
Inheritance of the local prefix by links is a deliberate difference from Ant Design 6.6.8: there, items without an explicit prefix retain the provider prefix, which can disrupt the design and position of the indicator. Here the indicator follows the active link and with a separate item.prefixCls.
Examples
Vertical navigation
Nested items and section highlighting when scrolling the selected container. Pinning is disabled to isolate the card.
<script lang="ts">
import { Anchor } from '@woodger/antd';
let area = $state<HTMLDivElement>();
</script>
<style>
.layout {
display: grid;
grid-template-columns: minmax(100px, 130px) minmax(0, 1fr);
gap: 16px;
}
.area {
height: 180px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
border-radius: 8px;
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
overflow-wrap: anywhere;
}
.section:last-child { min-height: 240px; }
@media (max-width: 640px) { .layout { grid-template-columns: minmax(0, 1fr); } }
</style>
<div class="layout">
<Anchor
affix={false}
getContainer={() => area!}
onClick={event => event.preventDefault()}
items={([
{ key: 'intro', href: '#anchor-basic-intro', title: "Description" },
{
key: 'delivery', href: '#anchor-basic-delivery', title: "Delivery",
children: [{ key: 'payment', href: '#anchor-basic-payment', title: "Payment" }]
}
])}
showInkInFixed
/>
<div
class="area"
bind:this={area}
role="region"
aria-label={("Basic Anchor Sections")}
tabindex="0"
>
<div id="anchor-basic-intro" class="section">{("Project Description")}</div>
<div id="anchor-basic-delivery" class="section">{("Project delivery")}</div>
<div id="anchor-basic-payment" class="section">{("Payment for the project")}</div>
</div>
</div>
Horizontal navigation
Horizontal Anchor uses a flat list. The long menu scrolls within its own area.
<script lang="ts">
import { Anchor } from '@woodger/antd';
let area = $state<HTMLDivElement>();
</script>
<style>
.layout {
display: grid;
grid-template-columns: minmax(100px, 130px) minmax(0, 1fr);
gap: 16px;
}
.area {
height: 180px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
border-radius: 8px;
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
overflow-wrap: anywhere;
}
.section:last-child { min-height: 240px; }
@media (max-width: 640px) { .layout { grid-template-columns: minmax(0, 1fr); } }
.layout { grid-template-columns: minmax(0, 1fr); }
</style>
<div class="layout">
<Anchor
affix={false}
getContainer={() => area!}
onClick={event => event.preventDefault()}
items={([
{ key: 'intro', href: '#anchor-horizontal-intro', title: "Description" },
{ key: 'delivery', href: '#anchor-horizontal-delivery', title: "Delivery" },
{ key: 'payment', href: '#anchor-horizontal-payment', title: "Payment" }
])}
direction="horizontal"
showInkInFixed
/>
<div class="area" bind:this={area} role="region" aria-label={("Sections horizontal Anchor")} tabindex="0">
<div id="anchor-horizontal-intro" class="section">{("Project Description")}</div>
<div id="anchor-horizontal-delivery" class="section">{("Project delivery")}</div>
<div id="anchor-horizontal-payment" class="section">{("Payment for the project")}</div>
</div>
</div>
Without fastening
affix=false leaves navigation in the stream. showInkInFixed controls the indicator; Partition tracking is maintained.
<script lang="ts">
import { Anchor, Checkbox } from '@woodger/antd';
let area = $state<HTMLDivElement>();
let ink = $state(false);
</script>
<style>
.layout {
display: grid;
grid-template-columns: minmax(100px, 130px) minmax(0, 1fr);
gap: 16px;
}
.area {
height: 180px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
border-radius: 8px;
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
overflow-wrap: anywhere;
}
.section:last-child { min-height: 240px; }
.controls {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px;
margin-bottom: 16px;
}
@media (max-width: 640px) { .layout { grid-template-columns: minmax(0, 1fr); } }
</style>
<div
class="controls"
><Checkbox
bind:checked={ink}
>{("Static Anchor Indicator")}</Checkbox></div>
<div class="layout">
<Anchor
affix={false}
getContainer={() => area!}
onClick={event => event.preventDefault()}
items={([
{ key: 'intro', href: '#anchor-static-intro', title: "Description" },
{ key: 'delivery', href: '#anchor-static-delivery', title: "Delivery" },
{ key: 'payment', href: '#anchor-static-payment', title: "Payment" }
])}
showInkInFixed={ink}
/>
<div class="area" bind:this={area} role="region" aria-label={("static Anchor Sections")} tabindex="0">
<div id="anchor-static-intro" class="section">{("Project Description")}</div>
<div id="anchor-static-delivery" class="section">{("Project delivery")}</div>
<div id="anchor-static-payment" class="section">{("Payment for the project")}</div>
</div>
</div>
Click processing
preventDefault prevents the URL from being written to history. Anchor continues to scroll the container and reports the selected link.
<script lang="ts">
import { Anchor } from '@woodger/antd';
let area = $state<HTMLDivElement>();
let clicked = $state('');
</script>
<style>
.layout {
display: grid;
grid-template-columns: minmax(100px, 130px) minmax(0, 1fr);
gap: 16px;
}
.area {
height: 180px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
border-radius: 8px;
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
overflow-wrap: anywhere;
}
.section:last-child { min-height: 240px; }
output {
display: block;
margin-top: 16px;
overflow-wrap: anywhere;
}
@media (max-width: 640px) { .layout { grid-template-columns: minmax(0, 1fr); } }
</style>
<div class="layout">
<Anchor
affix={false}
getContainer={() => area!}
onClick={(event, link) => {
event.preventDefault();
clicked = link.href;
}}
items={([
{ key: 'intro', href: '#anchor-click-intro', title: "Description" },
{ key: 'delivery', href: '#anchor-click-delivery', title: "Delivery" },
{ key: 'payment', href: '#anchor-click-payment', title: "Payment" }
])}
/>
<div
class="area"
bind:this={area}
role="region"
aria-label={("Sections click Anchor")}
tabindex="0"
>
<div id="anchor-click-intro" class="section">{("Project Description")}</div>
<div id="anchor-click-delivery" class="section">{("Project delivery")}</div>
<div id="anchor-click-payment" class="section">{("Payment for the project")}</div>
</div>
</div>
<output aria-label={("Pressing Anchor")}>{(clicked
|| ("Links have not been clicked yet"))}</output>
Own lighting
getCurrentAnchor sets the active link regardless of actual scrolling. Selecting the backlight remains the state of the example.
<script lang="ts">
import { Anchor } from '@woodger/antd';
let area = $state<HTMLDivElement>();
let highlighted = $state('#anchor-highlight-intro');
let resolveHighlight = $derived.by(() => {
const href = highlighted;
return () => href;
});
</script>
<style>
.layout {
display: grid;
grid-template-columns: minmax(100px, 130px) minmax(0, 1fr);
gap: 16px;
}
.area {
height: 180px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
border-radius: 8px;
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
overflow-wrap: anywhere;
}
.section:last-child { min-height: 240px; }
.controls {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px;
margin-bottom: 16px;
}
@media (max-width: 640px) { .layout { grid-template-columns: minmax(0, 1fr); } }
</style>
<div class="controls">
<label>{("Illuminated section ")}<select bind:value={highlighted}>
<option value="#anchor-highlight-intro">{("Description")}</option>
<option value="#anchor-highlight-delivery">{("Delivery")}</option>
<option value="#anchor-highlight-payment">{("Payment")}</option>
</select></label>
</div>
<div class="layout">
<Anchor
affix={false}
getContainer={() => area!}
onClick={event => event.preventDefault()}
items={([
{ key: 'intro', href: '#anchor-highlight-intro', title: "Description" },
{ key: 'delivery', href: '#anchor-highlight-delivery', title: "Delivery" },
{ key: 'payment', href: '#anchor-highlight-payment', title: "Payment" }
])}
showInkInFixed
getCurrentAnchor={resolveHighlight}
/>
<div class="area" bind:this={area} role="region" aria-label={("Sections highlight Anchor")} tabindex="0">
<div id="anchor-highlight-intro" class="section">{("Project Description")}</div>
<div id="anchor-highlight-delivery" class="section">{("Project delivery")}</div>
<div id="anchor-highlight-payment" class="section">{("Payment for the project")}</div>
</div>
</div>
Target offset
targetOffset leaves space above the section. For one link, its own targetOffset takes precedence over the general one.
<script lang="ts">
import { Anchor } from '@woodger/antd';
let area = $state<HTMLDivElement>();
</script>
<style>
.layout {
display: grid;
grid-template-columns: minmax(100px, 130px) minmax(0, 1fr);
gap: 16px;
}
.area {
height: 180px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
border-radius: 8px;
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
overflow-wrap: anywhere;
}
.section:last-child { min-height: 240px; }
@media (max-width: 640px) { .layout { grid-template-columns: minmax(0, 1fr); } }
</style>
<div class="layout">
<Anchor
affix={false}
getContainer={() => area!}
onClick={event => event.preventDefault()}
items={([
{ key: 'intro', href: '#anchor-offset-intro', title: "Indent 32px" },
{ key: 'delivery', href: '#anchor-offset-delivery', title: "Margin 64px", targetOffset: 64 },
{ key: 'payment', href: '#anchor-offset-payment', title: "Again 32px" }
])}
showInkInFixed
targetOffset={32}
/>
<div class="area" bind:this={area} role="region" aria-label={("offset Anchor sections")} tabindex="0">
<div style="
height: 64px;
padding: 8px;
box-sizing: border-box;
">
{("Area above sections")}
</div>
<div id="anchor-offset-intro" class="section">{("Project Description")}</div>
<div id="anchor-offset-delivery" class="section">{("Project delivery")}</div>
<div id="anchor-offset-payment" class="section">{("Payment for the project")}</div>
</div>
</div>
Changing the active partition
onChange reflects transitions between sections when clicked and manually scrolled. The story is limited to the last five events.
<script lang="ts">
import { Anchor } from '@woodger/antd';
let area = $state<HTMLDivElement>();
let events = $state<string[]>([]);
</script>
<style>
.layout {
display: grid;
grid-template-columns: minmax(100px, 130px) minmax(0, 1fr);
gap: 16px;
}
.area {
height: 180px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
border-radius: 8px;
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
overflow-wrap: anywhere;
}
.section:last-child { min-height: 240px; }
output {
display: block;
margin-top: 16px;
overflow-wrap: anywhere;
}
@media (max-width: 640px) { .layout { grid-template-columns: minmax(0, 1fr); } }
</style>
<div class="layout">
<Anchor
affix={false}
getContainer={() => area!}
onClick={event => event.preventDefault()}
items={([
{ key: 'intro', href: '#anchor-change-intro', title: "Description" },
{ key: 'delivery', href: '#anchor-change-delivery', title: "Delivery" },
{ key: 'payment', href: '#anchor-change-payment', title: "Payment" }
])}
showInkInFixed
onChange={value => { events = [...events, value || "Home"].slice(-5); }}
/>
<div class="area" bind:this={area} role="region" aria-label={("Sections change Anchor")} tabindex="0">
<div id="anchor-change-intro" class="section">{("Project Description")}</div>
<div id="anchor-change-delivery" class="section">{("Project delivery")}</div>
<div id="anchor-change-payment" class="section">{("Payment for the project")}</div>
</div>
</div>
<output aria-label={("History of Anchor sections")}>{(events.join(' → ')
|| ("No events"))}</output>
Transition history
replace changes the current history entry instead of adding a new one. This example intentionally updates fragment in the address bar.
<script lang="ts">
import { Anchor, Checkbox } from '@woodger/antd';
let area = $state<HTMLDivElement>();
let replace = $state(true);
</script>
<style>
.layout {
display: grid;
grid-template-columns: minmax(100px, 130px) minmax(0, 1fr);
gap: 16px;
}
.area {
height: 180px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
border-radius: 8px;
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
overflow-wrap: anywhere;
}
.section:last-child { min-height: 240px; }
.controls {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px;
margin-bottom: 16px;
}
@media (max-width: 640px) { .layout { grid-template-columns: minmax(0, 1fr); } }
.layout { grid-template-columns: minmax(0, 1fr); }
</style>
<div class="controls">
<Checkbox bind:checked={replace}>
{("Replace history record Anchor")}
</Checkbox>
</div>
<div class="layout">
<Anchor
affix={false}
getContainer={() => area!}
items={([
{ key: 'intro', href: '#anchor-history-intro', title: "Description" },
{ key: 'delivery', href: '#anchor-history-delivery', title: "Delivery" },
{ key: 'payment', href: '#anchor-history-payment', title: "Payment" }
])}
replace={replace}
showInkInFixed
/>
<div class="area" bind:this={area} role="region" aria-label={("Sections history Anchor")} tabindex="0">
<div id="anchor-history-intro" class="section">{("Project Description")}</div>
<div id="anchor-history-delivery" class="section">{("Project delivery")}</div>
<div id="anchor-history-payment" class="section">{("Payment for the project")}</div>
</div>
</div>
Semantic styles
The styles function styles root, item, itemTitle and indicator. When changing direction, the function receives the current props.
<script lang="ts">
import { Anchor, Checkbox } from '@woodger/antd';
let area = $state<HTMLDivElement>();
let horizontal = $state(false);
</script>
<style>
.layout {
display: grid;
grid-template-columns: minmax(100px, 130px) minmax(0, 1fr);
gap: 16px;
}
.area {
height: 180px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
border-radius: 8px;
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
overflow-wrap: anywhere;
}
.section:last-child { min-height: 240px; }
.controls {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 12px;
margin-bottom: 16px;
}
@media (max-width: 640px) { .layout { grid-template-columns: minmax(0, 1fr); } }
.layout { grid-template-columns: minmax(0, 1fr); }
</style>
<div class="controls">
<Checkbox bind:checked={horizontal}>
{("Horizontal semantic Anchor")}
</Checkbox>
</div>
<div class="layout">
<Anchor
affix={false}
getContainer={() => area!}
onClick={event => event.preventDefault()}
items={([
{ key: 'intro', href: '#anchor-semantic-intro', title: "Description" },
{ key: 'delivery', href: '#anchor-semantic-delivery', title: "Delivery" },
{ key: 'payment', href: '#anchor-semantic-payment', title: "Payment" }
])}
showInkInFixed
direction={horizontal ? 'horizontal' : 'vertical'}
classNames={{ root: 'semantic-anchor-root', itemTitle: 'semantic-anchor-title' }}
styles={({ props }) => ({
root: `
padding: 12px;
border: 1px solid var(--ant-color-primary-border);
border-radius: 8px;
`,
item: props.direction === 'horizontal'
? 'padding-inline:8px'
: 'padding-block:2px',
itemTitle: 'font-weight:600',
indicator: 'background:var(--ant-color-success)'
})}
/>
<div class="area" bind:this={area} role="region" aria-label={("Sections semantic Anchor")} tabindex="0">
<div id="anchor-semantic-intro" class="section">{("Project Description")}</div>
<div id="anchor-semantic-delivery" class="section">{("Project delivery")}</div>
<div id="anchor-semantic-payment" class="section">{("Payment for the project")}</div>
</div>
</div>
Component tokens
The local theme changes the vertical and horizontal padding of links via theme.components.Anchor.
<script lang="ts">
import { Anchor, ConfigProvider } from '@woodger/antd';
let area = $state<HTMLDivElement>();
</script>
<style>
.layout {
display: grid;
grid-template-columns: minmax(100px, 130px) minmax(0, 1fr);
gap: 16px;
}
.area {
height: 180px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
border-radius: 8px;
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
overflow-wrap: anywhere;
}
.section:last-child { min-height: 240px; }
@media (max-width: 640px) { .layout { grid-template-columns: minmax(0, 1fr); } }
</style>
<ConfigProvider theme={{
cssVar: {}, components: {
Anchor: {
linkPaddingBlock: 10,
linkPaddingInlineStart: 24
}
}
}}>
<div class="layout">
<Anchor
affix={false}
getContainer={() => area!}
onClick={event => event.preventDefault()}
items={([
{ key: 'intro', href: '#anchor-tokens-intro', title: "Description" },
{ key: 'delivery', href: '#anchor-tokens-delivery', title: "Delivery" },
{ key: 'payment', href: '#anchor-tokens-payment', title: "Payment" }
])}
showInkInFixed
/>
<div class="area" bind:this={area} role="region" aria-label={("Sections tokens Anchor")} tabindex="0">
<div id="anchor-tokens-intro" class="section">{("Project Description")}</div>
<div id="anchor-tokens-delivery" class="section">{("Project delivery")}</div>
<div id="anchor-tokens-payment" class="section">{("Payment for the project")}</div>
</div>
</div>
</ConfigProvider>
Securing in a container
affix accepts Affix parameters. Local containing block and rootClassName restrict sticky navigation to the card area.
<script lang="ts">
import { Anchor } from '@woodger/antd';
let area = $state<HTMLDivElement>();
</script>
<style>
.frame {
transform: translate(0);
isolation: isolate;
overflow: hidden;
border-radius: 8px;
}
.area {
height: 220px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
}
.frame :global(.fixed-local-anchor) { top: 8px !important; }
.section {
min-height: 220px;
padding: 16px;
box-sizing: border-box;
}
.section:last-child { min-height: 300px; }
</style>
<div class="frame">
<div
class="area"
bind:this={area}
role="region"
aria-label={("Fixed Anchor")}
tabindex="0"
>
<div style="height:48px"></div>
<Anchor affix={{ rootClassName: 'fixed-local-anchor' }} offsetTop={8}
getContainer={() => area!} direction="horizontal" targetOffset={48}
onClick={event => event.preventDefault()}
items={([
{ key: 'intro', href: '#anchor-fixed-intro', title: "Home" },
{ key: 'details', href: '#anchor-fixed-details', title: "Details" }
])}
style="background:var(--ant-color-bg-container)"
/>
<div id="anchor-fixed-intro" class="section">{("Start of document")}</div>
<div id="anchor-fixed-details" class="section">{("Document details")}</div>
</div>
</div>
Empty record restores the standard design:
Anchor Settings
General semantic defaults, replacing record and recalculating the indicator after changing indents.
<script lang="ts">
import { Anchor, Checkbox, ConfigProvider } from '@woodger/antd';
let area = $state<HTMLDivElement>();
let spacious = $state(false);
const items = [
{ key: 'intro', href: '#anchor-provider-intro', title: "Description" },
{ key: 'delivery', href: '#anchor-provider-delivery', title: "Delivery" },
{ key: 'payment', href: '#anchor-provider-payment', title: "Payment" }
];
</script>
<style>
.settings { margin-bottom: 24px; }
.examples {
display: grid;
gap: 20px;
}
.area {
height: 180px;
overflow: auto;
border-radius: 8px;
background: var(--ant-color-fill-quaternary);
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
}
.section:last-child { min-height: 240px; }
</style>
<div class="settings">
<Checkbox bind:checked={spacious}>
{("Increase link indentation Anchor")}
</Checkbox>
</div>
<ConfigProvider anchor={{
class: 'provider-anchor', style: 'border-radius:8px',
classNames: { root: 'context-anchor-root' },
styles: () => ({
root: 'border-radius:4px',
item: `padding-block:${spacious ? 12 : 2}px`,
itemTitle: 'font-weight:600',
indicator: 'background:#237804'
})
}}>
<div class="examples">
<Anchor items={(items)} affix={false} showInkInFixed getContainer={() => area!}
onClick={event => event.preventDefault()} class="local-anchor" style="border-radius:16px"
classNames={{ root: 'local-anchor-root' }} styles={{ root: 'border-radius:12px' }} />
<ConfigProvider anchor={{}}>
<p>{("Empty record restores the standard design:")}</p>
<Anchor items={(items)} affix={false} getContainer={() => area!}
onClick={event => event.preventDefault()} class="reset-anchor" />
</ConfigProvider>
<div bind:this={area} class="area" role="region" aria-label={("Anchor Configuration Sections")} tabindex="0">
<div id="anchor-provider-intro" class="section">{("Project Description")}</div>
<div id="anchor-provider-delivery" class="section">{("Project delivery")}</div>
<div id="anchor-provider-payment" class="section">{("Payment for the project")}</div>
</div>
</div>
</ConfigProvider>
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.
<script lang="ts">
import { Anchor, Checkbox, ConfigProvider, type AnchorItem } from '@woodger/antd';
let custom = $state(true);
let local = $state(true);
let area = $state<HTMLDivElement>();
let active = $state('');
const prefix = $derived(custom ? 'suite' : 'ant');
const items: AnchorItem[] = $derived([
{ key: 'owner', href: '#anchor-prefix-owner', title: "Inheritance" },
{
key: 'provider', href: '#anchor-prefix-provider', title: "Provider", prefixCls: '',
class: `prefix-anchor-override ${active === '#anchor-prefix-provider' ? 'is-active' : ''}`
},
{
key: 'custom', href: '#anchor-prefix-custom', title: "Own prefix", prefixCls: 'item-anchor',
class: `prefix-anchor-override ${active === '#anchor-prefix-custom' ? 'is-active' : ''}`
}
]);
</script>
<style>
.example {
display: grid;
gap: 20px;
min-width: 0;
}
.controls {
display: flex;
flex-wrap: wrap;
gap: 12px;
}
.layout {
display: grid;
grid-template-columns: minmax(120px, 150px) minmax(0, 1fr);
gap: 16px;
}
.area {
height: 180px;
overflow: auto;
background: var(--ant-color-fill-quaternary);
border-radius: 8px;
}
.section {
min-height: 180px;
padding: 16px;
box-sizing: border-box;
overflow-wrap: anywhere;
}
.section:last-child { min-height: 240px; }
.layout :global(.prefix-anchor-override) { padding: 4px 0 4px 16px; }
.layout :global(.prefix-anchor-override > a) {
display: block;
color: var(--ant-color-text);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.layout :global(.prefix-anchor-override.is-active > a) { color: var(--ant-color-primary); }
p { margin: 0; }
output { overflow-wrap: anywhere; }
@media (max-width: 640px) { .layout { grid-template-columns: minmax(0, 1fr); } }
</style>
<div class="example">
<div class="controls">
<Checkbox bind:checked={custom}>{("General prefix Anchor suite")}</Checkbox>
<Checkbox bind:checked={local}>{("Local prefix Anchor")}</Checkbox>
</div>
<ConfigProvider prefixCls={prefix} theme={{ cssVar: { prefix: 'anchor-theme' } }}>
<div class="layout">
<Anchor
items={(items)}
prefixCls={local ? 'project-anchor' : ''}
class="prefix-anchor-main"
affix={false}
showInkInFixed
replace
getContainer={() => area!}
onChange={href => { active = href; }}
/>
<div class="area" bind:this={area} role="region"
aria-label={("Anchor sections with prefix")} tabindex="0">
<div id="anchor-prefix-owner" class="section">
{("The link inherits the Anchor owner prefix.")}
</div>
<div id="anchor-prefix-provider" class="section">
{("Empty item.prefixCls selects a common provider prefix.")}
</div>
<div id="anchor-prefix-custom" class="section">
{("Non-empty item.prefixCls specifies the full custom prefix.")}
</div>
</div>
</div>
</ConfigProvider>
<output
aria-label={("Prefix Anchor")}
>{("\n General: ")}{prefix}{("; class: ")}{(local ? 'project-anchor' : prefix + '-anchor')}{("; section: ")}{(active || '—')}
</output>
<p>
{("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.")}
</p>
</div>
Order sections
Saved review example with local scrolling of description, delivery and payment.
<script lang="ts">
import { Anchor } from '@woodger/antd';
import { onMount } from 'svelte';
let navigation = $state<HTMLDivElement>();
let navigationVisible = $state(false);
onMount(() => {
if (!navigation) {
return;
}
const observer = new IntersectionObserver((
[entry]
) => { navigationVisible = entry.intersectionRatio === 1; }, { threshold: 1 });
observer.observe(navigation);
return () => observer.disconnect();
});
let container = $state<HTMLDivElement>();
let current = $state('');
</script>
<style>
output {
display: block;
margin-top: 16px;
}
</style>
<div bind:this={navigation}>
<Anchor
affix={false}
showInkInFixed
getCurrentAnchor={navigationVisible ? undefined : () => ''}
direction="horizontal"
getContainer={() => container!}
targetOffset={8}
items={([
{ key: 'details', href: '#anchor-demo-details', title: "Description" },
{ key: 'delivery', href: '#anchor-demo-delivery', title: "Delivery" },
{ key: 'payment', href: '#anchor-demo-payment', title: "Payment" }
])}
onClick={event => event.preventDefault()}
onChange={value => { current = value; }}
/>
</div>
<div
bind:this={container}
style="
height: 180px;
overflow: auto;
margin-top: 12px;
background: var(--ant-color-fill-quaternary);
"
>
<div id="anchor-demo-details" style="
height: 180px;
padding: 16px;
box-sizing: border-box;
">
{("Order Description")}
</div>
<div id="anchor-demo-delivery" style="
height: 180px;
padding: 16px;
box-sizing: border-box;
">
{("Delivery terms")}
</div>
<div id="anchor-demo-payment" style="
height: 220px;
padding: 16px;
box-sizing: border-box;
">
{("Payment methods")}
</div>
</div>
<output aria-label={("Current section Anchor")}>{current}</output>
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.
| Theme | Example or limitation |
|---|---|
| Basic | Vertical list and nested links; fastening separately. |
| Horizontal Anchor | Flat horizontal menu. |
| Static Anchor | affix=false and indicator control. |
| Customize the onClick event | Cancel URL changes without canceling scrolling. |
| Customize the anchor highlight | getCurrentAnchor and backlight state. |
| Set Anchor scroll offset | General and individual target indent. |
| Listening for anchor link change | History onChange. |
| Replace href in history | replaceState and pushState. |
| Custom semantic dom styling | classNames object and styles function. |
| CSS prefix | Local Anchor, inherited links and independent item.prefixCls. |
| Property | Description | Type | Default |
|---|---|---|---|
prefixCls | Full local CSS prefix. The empty string and undefined share the same provider prefix. | string | ConfigProvider.prefixCls + "-anchor" |
items | Links; children create nesting only with vertical. items takes precedence over children-snippet. | readonly AnchorItem[] | — |
direction | Menu direction; in horizontal, nested children are not displayed. | 'vertical' | 'horizontal' | vertical |
affix | Fastening via Affix; false leaves Anchor in the thread. | boolean | Omit<AffixProps, ...> | true |
showInkInFixed | Shows the indicator when affix=false. | boolean | false |
getContainer | Tracking and scrolling area; getter is called in the browser. | () => HTMLElement | Window | () => window |
offsetTop | Reinforcement offset and fallback target offset. | number | 0 |
targetOffset | Target offset; The targetOffset of an individual link takes precedence. | number | offsetTop |
bounds | Active section definition tolerance in px. | number | 5 |
getCurrentAnchor | Overrides the highlighted link; onChange gets the original href. | (activeLink: string) => string | — |
onClick | preventDefault prevents writing a URL; Anchor scrolling is preserved. | (event, { title, href }) => void | — |
onChange | Selected section when clicked or scrolled. | (href: string) => void | — |
replace | Replaces history entry; replace of an individual item takes precedence. | boolean | false |
classNames | Semantic slots classes. | AnchorSlots | (info) => AnchorSlots | — |
styles | CSS lines semantic slots. | AnchorSlots | (info) => AnchorSlots | — |
nativeElement | bind: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.
| Token | Purpose |
|---|---|
linkPaddingBlock | Vertical indent of the link. |
linkPaddingInlineStart | Nested level indentation. |
| Token | Current value |
|---|---|
colorPrimary | #2f54eb |
colorSplit | rgba(5,5,5,0.06) |
colorText | rgba(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' |
fontSize | 14 |
lineHeight | 1.5714285714285714 |
lineType | solid |
lineWidth | 1 |
lineWidthBold | 2 |
motionDurationSlow | 0.3s |
paddingXXS | 4 |
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.