Anchor
Navigate within one page and highlight the section nearest the top of its scrolling container.
Import
import { HuiAnchor } from '@nscode/handy-ui'
import type { AnchorItem, AnchorChangeContext, AnchorExpose } from '@nscode/handy-ui'
Basic usage
Provide hash hrefs and label content. The example uses a local scroll container.
<script setup lang="ts">
import { HuiAnchor } from '@nscode/handy-ui'
import type { AnchorItem } from '@nscode/handy-ui'
const items: AnchorItem[] = [
{ href: '#anchor-basic-1', label: 'Basic information' },
{ href: '#anchor-basic-2', label: 'Contact details' },
{ href: '#anchor-basic-3', label: 'Account settings' }
]
</script>
<template>
<div class="layout">
<div id="anchor-basic-box" class="box">
<section id="anchor-basic-1" class="section">Basic information</section>
<section id="anchor-basic-2" class="section">Contact details</section>
<section id="anchor-basic-3" class="section">Account settings</section>
</div>
<HuiAnchor :items="items" container="#anchor-basic-box" />
</div>
</template>
<style scoped>
.layout {
display: flex;
gap: 24px;
}
.box {
flex: 1;
height: 240px;
overflow: auto;
border: 1px solid var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
.section {
height: 280px;
padding: 16px;
border-bottom: 1px dashed var(--hui-border-color);
color: var(--hui-text-color-secondary);
}
</style>
Appearance
Compare block and rail styles; showRail can hide the rail and its indicator.
<script setup lang="ts">
import { HuiAnchor } from '@nscode/handy-ui'
import type { AnchorItem } from '@nscode/handy-ui'
const items: AnchorItem[] = [
{ href: '#anchor-type-1', label: 'Basic information' },
{ href: '#anchor-type-2', label: 'Contact details' },
{ href: '#anchor-type-3', label: 'Account settings' }
]
</script>
<template>
<div class="layout">
<div id="anchor-type-box" class="box">
<section id="anchor-type-1" class="section">Basic information</section>
<section id="anchor-type-2" class="section">Contact details</section>
<section id="anchor-type-3" class="section">Account settings</section>
</div>
<div>
<p class="row-label">rail</p>
<HuiAnchor :items="items" type="rail" container="#anchor-type-box" />
</div>
<div>
<p class="row-label">rail, show-rail=false</p>
<HuiAnchor :items="items" :show-rail="false" container="#anchor-type-box" />
</div>
<div>
<p class="row-label">block</p>
<HuiAnchor :items="items" type="block" container="#anchor-type-box" />
</div>
</div>
</template>
<style scoped>
.layout {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
.box {
flex: 1;
min-width: 200px;
height: 240px;
overflow: auto;
border: 1px solid var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
.section {
height: 280px;
padding: 16px;
border-bottom: 1px dashed var(--hui-border-color);
color: var(--hui-text-color-secondary);
}
.row-label {
margin: 0 0 8px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Top offset
targetOffset reserves space for a fixed header. bound adjusts the activation tolerance.
<script setup lang="ts">
import { HuiAnchor } from '@nscode/handy-ui'
import type { AnchorItem } from '@nscode/handy-ui'
const items: AnchorItem[] = [
{ href: '#anchor-offset-1', label: 'Basic information' },
{ href: '#anchor-offset-2', label: 'Contact details' },
{ href: '#anchor-offset-3', label: 'Account settings' }
]
</script>
<template>
<div class="layout">
<div id="anchor-offset-box" class="box">
<div class="header">Fixed header (40px)</div>
<section id="anchor-offset-1" class="section">Basic information</section>
<section id="anchor-offset-2" class="section">Contact details</section>
<section id="anchor-offset-3" class="section">Account settings</section>
</div>
<div>
<p class="row-label">target-offset=40</p>
<HuiAnchor :items="items" :target-offset="40" container="#anchor-offset-box" />
</div>
<div>
<p class="row-label">No offset</p>
<HuiAnchor :items="items" container="#anchor-offset-box" />
</div>
<div>
<p class="row-label">bound=120</p>
<HuiAnchor :items="items" :target-offset="40" :bound="120" container="#anchor-offset-box" />
</div>
</div>
</template>
<style scoped>
.layout {
display: flex;
flex-wrap: wrap;
gap: 24px;
}
.box {
flex: 1;
min-width: 200px;
height: 240px;
overflow: auto;
border: 1px solid var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
.header {
position: sticky;
top: 0;
z-index: 1;
height: 40px;
padding: 0 16px;
line-height: 40px;
background-color: var(--hui-color-primary);
color: var(--hui-color-white);
}
.section {
height: 280px;
padding: 16px;
border-bottom: 1px dashed var(--hui-border-color);
color: var(--hui-text-color-secondary);
}
.row-label {
margin: 0 0 8px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Sticky sidebar
affix uses sticky positioning, with targetOffset as its top inset.
<script setup lang="ts">
import { HuiAnchor } from '@nscode/handy-ui'
import type { AnchorItem } from '@nscode/handy-ui'
const items: AnchorItem[] = [
{ href: '#anchor-affix-1', label: 'Introduction' },
{ href: '#anchor-affix-2', label: 'Usage' },
{ href: '#anchor-affix-3', label: 'FAQ' }
]
</script>
<template>
<div
id="anchor-affix-box"
style="height: 300px; overflow: auto; border: 1px solid var(--hui-border-color)"
>
<div style="display: flex; gap: 24px; padding: 0 16px">
<main style="flex: 1">
<section id="anchor-affix-1" style="height: 400px; padding-top: 16px">Introduction</section>
<section id="anchor-affix-2" style="height: 400px; padding-top: 16px">Usage</section>
<section id="anchor-affix-3" style="height: 400px; padding-top: 16px">FAQ</section>
</main>
<HuiAnchor :items="items" affix :target-offset="16" container="#anchor-affix-box" />
</div>
</div>
</template>
Custom scroll container
Pass a selector for the actual scrolling element, or provide its HTMLElement directly.
<script setup lang="ts">
import { HuiAnchor } from '@nscode/handy-ui'
import type { AnchorItem } from '@nscode/handy-ui'
const items: AnchorItem[] = [
{ href: '#part-1', label: 'Part one' },
{ href: '#part-2', label: 'Part two' },
{ href: '#part-3', label: 'Part three' }
]
</script>
<template>
<div style="display: flex; gap: 24px">
<div
id="anchor-demo-box"
style="flex: 1; height: 300px; overflow: auto; border: 1px solid var(--hui-border-color)"
>
<section id="part-1" style="height: 400px">Part one</section>
<section id="part-2" style="height: 400px">Part two</section>
<section id="part-3" style="height: 400px">Part three</section>
</div>
<HuiAnchor :items="items" container="#anchor-demo-box" />
</div>
</template>
Custom labels
label accepts a VNode as well as a string. This example adds icons.
<script setup lang="ts">
import { h } from 'vue'
import { HuiAnchor, HuiIcon } from '@nscode/handy-ui'
import type { AnchorItem } from '@nscode/handy-ui'
const items: AnchorItem[] = [
{ href: '#anchor-title-1', label: h('span', [h(HuiIcon, { name: 'user' }), ' Profile']) },
{
href: '#anchor-title-2',
label: h('span', [h(HuiIcon, { name: 'setting' }), ' Preferences'])
},
{ href: '#anchor-title-3', label: h('span', [h(HuiIcon, { name: 'bell' }), ' Notifications']) }
]
</script>
<template>
<div style="display: flex; gap: 24px">
<div
id="anchor-title-box"
style="flex: 1; height: 240px; overflow: auto; border: 1px solid var(--hui-border-color)"
>
<section id="anchor-title-1" style="height: 280px; padding: 16px">Profile</section>
<section id="anchor-title-2" style="height: 280px; padding: 16px">Preferences</section>
<section id="anchor-title-3" style="height: 280px; padding: 16px">Notifications</section>
</div>
<HuiAnchor :items="items" container="#anchor-title-box" />
</div>
</template>
Active-section changes
change receives the new href followed by an object containing prevHref. The initial previous href is empty.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiAnchor } from '@nscode/handy-ui'
import type { AnchorItem } from '@nscode/handy-ui'
const items: AnchorItem[] = [
{ href: '#anchor-change-1', label: 'Basic information' },
{ href: '#anchor-change-2', label: 'Contact details' },
{ href: '#anchor-change-3', label: 'Account settings' }
]
const current = ref('')
const prev = ref('')
const handleChange = (currentHref: string, context: { prevHref: string }) => {
current.value = currentHref
prev.value = context.prevHref
}
</script>
<template>
<p style="margin: 0 0 12px">
Current section: {{ current || 'None' }}, Previous section: {{ prev || 'None' }}
</p>
<div style="display: flex; gap: 24px">
<div
id="anchor-change-box"
style="flex: 1; height: 240px; overflow: auto; border: 1px solid var(--hui-border-color)"
>
<section id="anchor-change-1" style="height: 280px; padding: 16px">Basic information</section>
<section id="anchor-change-2" style="height: 280px; padding: 16px">Contact details</section>
<section id="anchor-change-3" style="height: 280px; padding: 16px">Account settings</section>
</div>
<HuiAnchor :items="items" container="#anchor-change-box" @change="handleChange" />
</div>
</template>
Programmatic scrolling
Call scrollToAnchor through a component ref; currentHref exposes the current highlight.
<script setup lang="ts">
import { ref } from 'vue'
import type { AnchorExpose } from '@nscode/handy-ui'
import { HuiAnchor, HuiButton, HuiSpace } from '@nscode/handy-ui'
import type { AnchorItem } from '@nscode/handy-ui'
const anchorRef = ref<AnchorExpose>()
const items: AnchorItem[] = [
{ href: '#anchor-method-1', label: 'Basic information' },
{ href: '#anchor-method-2', label: 'Contact details' },
{ href: '#anchor-method-3', label: 'Account settings' }
]
</script>
<template>
<HuiSpace wrap style="margin-bottom: 12px">
<HuiButton @click="anchorRef?.scrollToAnchor('#anchor-method-1')"
>Go to Basic information</HuiButton
>
<HuiButton @click="anchorRef?.scrollToAnchor('#anchor-method-2')"
>Go to Contact details</HuiButton
>
<HuiButton @click="anchorRef?.scrollToAnchor('#anchor-method-3')"
>Go to Account settings</HuiButton
>
<span>currentHref: {{ anchorRef?.currentHref || 'None' }}</span>
</HuiSpace>
<div style="display: flex; gap: 24px">
<div
id="anchor-method-box"
style="flex: 1; height: 240px; overflow: auto; border: 1px solid var(--hui-border-color)"
>
<section id="anchor-method-1" style="height: 280px; padding: 16px">Basic information</section>
<section id="anchor-method-2" style="height: 280px; padding: 16px">Contact details</section>
<section id="anchor-method-3" style="height: 280px; padding: 16px">Account settings</section>
</div>
<HuiAnchor ref="anchorRef" :items="items" container="#anchor-method-box" />
</div>
</template>
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| items | AnchorItem[] | [] | Flat list of links. |
| targetOffset | number | 0 | Space above the target section, in pixels; also the sticky top offset. |
| container | string | HTMLElement | Window | Browser window | Scroll container element, selector, or window. |
| bound | number | 10 | Extra distance used to determine the active section. |
| affix | boolean | false | Keep the anchor visible with sticky positioning. |
| type | 'block' | 'rail' | 'rail' | Link appearance. |
| showRail | boolean | true | Show the rail and active indicator in rail mode. |
AnchorItem
| Field | Type | Required | Description |
|---|---|---|---|
| href | string | Yes | Hash link to an element in the current document. |
| label | string | VNode | Yes | Link content. The old title field is not supported. |
| target | '_self' | '_blank' | No | Native target attribute. Clicks remain intercepted for in-page scrolling. |
Events
| Event | Arguments | Description |
|---|---|---|
| change | (currentHref: string, context: AnchorChangeContext) | Active link changed. context.prevHref contains the previous link, initially an empty string. Clearing the highlight does not emit change. |
Slots
None. Supply link content through items[].label.
Methods and exposed state
Use a component ref typed as AnchorExpose.
| Name | Type | Description |
|---|---|---|
| currentHref | string | Active href; empty when no section is active. |
| inkVisible | boolean | Whether the rail indicator is visible. |
| scrollToAnchor | (href: string) => void | Smoothly scroll to the target; do nothing when it cannot be found. This method does not directly change the highlight. |
Types
import type { VNode } from 'vue'
type AnchorTarget = string | HTMLElement | Window
type AnchorType = 'block' | 'rail'
interface AnchorItem {
href: string
label: string | VNode
target?: '_self' | '_blank'
}
interface AnchorChangeContext {
prevHref: string
}
interface AnchorExpose {
currentHref: string
inkVisible: boolean
scrollToAnchor: (href: string) => void
}