@@ -47,22 +47,21 @@ export function useActiveSidebarLinks(container: Ref<HTMLElement>, marker: Ref<H
4747 let prevActiveLink : HTMLAnchorElement | null = null ;
4848
4949 function activateLink ( hash : string ) {
50- deactiveLink ( prevActiveLink ) ;
50+ prevActiveLink && deactiveLink ( prevActiveLink ) ;
5151
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+ }
6665 }
6766
6867 function deactiveLink ( link : HTMLElement ) {
@@ -96,7 +95,7 @@ function getPageOffset() {
9695function getAnchorTop ( anchor : HTMLAnchorElement ) {
9796 const pageOffset = getPageOffset ( ) ;
9897 try {
99- return anchor . parentElement . offsetTop - pageOffset - 15 ;
98+ return anchor . parentElement ? .offsetTop ? anchor . parentElement . offsetTop - pageOffset - 15 : 0 ;
10099 } catch ( e ) {
101100 return 0 ;
102101 }
0 commit comments