@@ -17,7 +17,19 @@ function reset() {
17
17
}
18
18
19
19
function getElAndScroll ( ) {
20
- const element = document . getElementById ( hashFragment ) ;
20
+ let element = null ;
21
+ if ( hashFragment === '#' ) {
22
+ element = document . body ;
23
+ } else {
24
+ // check for element with matching id before assume '#top' is the top of the document
25
+ // see https://html.spec.whatwg.org/multipage/browsing-the-web.html#target-element
26
+ const id = hashFragment . replace ( '#' , '' ) ;
27
+ element = document . getElementById ( id ) ;
28
+ if ( element === null && hashFragment === '#top' ) {
29
+ element = document . body ;
30
+ }
31
+ }
32
+
21
33
if ( element !== null ) {
22
34
scrollFunction ( element ) ;
23
35
@@ -63,13 +75,13 @@ export function genericHashLink(As) {
63
75
function handleClick ( e ) {
64
76
reset ( ) ;
65
77
if ( props . onClick ) props . onClick ( e ) ;
66
- if ( typeof props . to === 'string' ) {
67
- hashFragment = props . to . split ( '#' ) . slice ( 1 ) . join ( '#' ) ;
78
+ if ( typeof props . to === 'string' && props . to . includes ( '#' ) ) {
79
+ hashFragment = `# ${ props . to . split ( '#' ) . slice ( 1 ) . join ( '#' ) } ` ;
68
80
} else if (
69
81
typeof props . to === 'object' &&
70
82
typeof props . to . hash === 'string'
71
83
) {
72
- hashFragment = props . to . hash . replace ( '#' , '' ) ;
84
+ hashFragment = props . to . hash ;
73
85
}
74
86
if ( hashFragment !== '' ) {
75
87
scrollFunction =
0 commit comments