Skip to content

Commit ba6b3d0

Browse files
committed
feat(content-nav): optimize active ID handling and improve scroll behavior in navigation
1 parent 4db2c50 commit ba6b3d0

2 files changed

Lines changed: 11 additions & 8 deletions

File tree

packages/components/src/content-nav/composables/useActiveSync.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export function useActiveSync(options: ContentNavActiveSyncOptions) {
2525
const preferredReducedMotion = usePreferredReducedMotion()
2626

2727
function setActiveId(value: string | undefined) {
28+
if (activeId.value === value) {
29+
return
30+
}
31+
2832
if (options.activeId?.value === undefined) {
2933
localActiveId.value = value
3034
}
@@ -137,7 +141,6 @@ export function useActiveSync(options: ContentNavActiveSyncOptions) {
137141
scrollHeight: getContentNavScrollHeight(scrollRoot),
138142
},
139143
anchors,
140-
items: options.items.value,
141144
activeOffset: options.activeOffset?.value,
142145
})
143146

packages/components/src/content-nav/defaults.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,22 @@ export function defaultContentNavActiveResolver(options: {
3434
scrollHeight: number
3535
}
3636
anchors: Array<{ id: string; el: HTMLElement }>
37-
items: ContentNavItem[]
3837
activeOffset?: number
3938
}) {
40-
const { viewport, anchors, items } = options
39+
const { viewport, anchors } = options
4140

42-
if (!anchors.length || !items.length) {
43-
return items[0]?.id
41+
if (!anchors.length) {
42+
return
4443
}
4544

45+
const canScroll = viewport.scrollHeight - viewport.clientHeight > 2
4646
const isAtBottom = viewport.scrollTop + viewport.clientHeight >= viewport.scrollHeight - 2
47-
if (isAtBottom) {
48-
return items[items.length - 1]?.id
47+
if (canScroll && isAtBottom) {
48+
return anchors[anchors.length - 1]?.id
4949
}
5050

5151
const threshold = viewport.top + Math.max(0, options.activeOffset ?? 120)
52-
let activeId = items[0]?.id
52+
let activeId = anchors[0]?.id
5353

5454
for (const anchor of anchors) {
5555
const rect = anchor.el.getBoundingClientRect()

0 commit comments

Comments
 (0)