@@ -11,8 +11,8 @@ import { usePrefixConfig, useComponentConfig, useLayout } from '../../hooks';
1111import { registerComponentMate } from '../../utils' ;
1212
1313export interface DAnchorItem {
14- title : React . ReactNode ;
1514 href : string ;
15+ title ?: React . ReactNode ;
1616 target ?: string ;
1717}
1818
@@ -76,7 +76,7 @@ function Anchor<T extends DAnchorItem>(props: DAnchorProps<T>, ref: React.Forwar
7676 let nearestEl : [ string , number ] | undefined ;
7777 const reduceLinks = ( arr : DNestedChildren < T > [ ] ) => {
7878 arr . forEach ( ( { href, children } ) => {
79- const el = document . querySelector ( href ) ;
79+ const el = document . getElementById ( href ) ;
8080 if ( el ) {
8181 const top = el . getBoundingClientRect ( ) . top ;
8282 // Add 1 because `getBoundingClientRect` return decimal
@@ -165,10 +165,10 @@ function Anchor<T extends DAnchorItem>(props: DAnchorProps<T>, ref: React.Forwar
165165 const pageTop = pageEl . getBoundingClientRect ( ) . top ;
166166
167167 const scrollTop = pageEl . scrollTop ;
168- window . location . hash = href ;
168+ window . location . hash = `# ${ href } ` ;
169169 pageEl . scrollTop = scrollTop ;
170170
171- const el = document . querySelector ( href ) ;
171+ const el = document . getElementById ( href ) ;
172172 if ( el ) {
173173 const top = el . getBoundingClientRect ( ) . top ;
174174 const scrollTop = top - pageTop + pageEl . scrollTop - dDistance ;
@@ -192,7 +192,7 @@ function Anchor<T extends DAnchorItem>(props: DAnchorProps<T>, ref: React.Forwar
192192 'is-active' : linkHref === activeHref ,
193193 } ) }
194194 style = { { paddingLeft : 16 + level * 16 } }
195- href = { linkHref }
195+ href = { `# ${ linkHref } ` }
196196 target = { linkTarget }
197197 onClick = { ( e ) => {
198198 e . preventDefault ( ) ;
@@ -201,7 +201,7 @@ function Anchor<T extends DAnchorItem>(props: DAnchorProps<T>, ref: React.Forwar
201201 handleLinkClick ( linkHref ) ;
202202 } }
203203 >
204- { linkTitle }
204+ { linkTitle ?? linkHref }
205205 </ a >
206206 </ li >
207207 { children && getNodes ( children , level + 1 ) }
0 commit comments