@@ -47,22 +47,21 @@ export function useActiveSidebarLinks(container: Ref<HTMLElement>, marker: Ref<H
47
47
let prevActiveLink : HTMLAnchorElement | null = null ;
48
48
49
49
function activateLink ( hash : string ) {
50
- deactiveLink ( prevActiveLink ) ;
50
+ prevActiveLink && deactiveLink ( prevActiveLink ) ;
51
51
52
- // const activeLink = (prevActiveLink =
53
- // hash == null
54
- // ? null
55
- // : (container.value.querySelector(
56
- // `.devui-item a[href="${decodeURIComponent(hash)}"]`
57
- // ) as HTMLAnchorElement))
58
- // if (activeLink) {
59
- // activeLink.classList.add('active')
60
- // marker.value.style.opacity = '1'
61
- // marker.value.style.top = `${activeLink.offsetTop}px`
62
- // } else {
63
- // marker.value.style.opacity = '0'
64
- // marker.value.style.top = '33px'
65
- // }
52
+ const activeLink = ( prevActiveLink =
53
+ hash == null
54
+ ? null
55
+ : container . value && ( container . value . querySelector ( `.devui-item a[href="${ decodeURIComponent ( hash ) } "]` ) as HTMLAnchorElement ) ) ;
56
+ if ( marker . value )
57
+ if ( activeLink ) {
58
+ activeLink . classList . add ( 'active' ) ;
59
+ marker . value . style . opacity = '1' ;
60
+ marker . value . style . top = `${ activeLink . offsetTop } px` ;
61
+ } else {
62
+ marker . value . style . opacity = '0' ;
63
+ marker . value . style . top = '33px' ;
64
+ }
66
65
}
67
66
68
67
function deactiveLink ( link : HTMLElement ) {
@@ -96,7 +95,7 @@ function getPageOffset() {
96
95
function getAnchorTop ( anchor : HTMLAnchorElement ) {
97
96
const pageOffset = getPageOffset ( ) ;
98
97
try {
99
- return anchor . parentElement . offsetTop - pageOffset - 15 ;
98
+ return anchor . parentElement ? .offsetTop ? anchor . parentElement . offsetTop - pageOffset - 15 : 0 ;
100
99
} catch ( e ) {
101
100
return 0 ;
102
101
}
0 commit comments