5
5
* LICENSE file in the root directory of this source tree.
6
6
*/
7
7
8
- import React from 'react' ;
8
+ import React , { useEffect } from 'react' ;
9
9
import Link from '@docusaurus/Link' ;
10
10
import useBaseUrl from '@docusaurus/useBaseUrl' ;
11
11
import isInternalUrl from '@docusaurus/isInternalUrl' ;
@@ -40,11 +40,18 @@ export default function NavbarNavLink({
40
40
const normalizedHref = useBaseUrl ( href , { forcePrependBaseUrl : true } ) ;
41
41
const isExternalLink = label && href && ! isInternalUrl ( href ) ;
42
42
43
- const clickLink = ( e ) => {
44
- if ( e . target ?. lang ) {
45
- localStorage . setItem ( '_lang_user_' , e . target . lang ) ;
46
- }
47
- }
43
+ useEffect ( ( ) => {
44
+ const handleClick = ( event ) => {
45
+ if ( event . target . tagName === 'A' && event . target . lang ) {
46
+ localStorage . setItem ( '_lang_user_' , event . target . lang ) ;
47
+ }
48
+ } ;
49
+ document . addEventListener ( 'click' , handleClick ) ;
50
+
51
+ return ( ) => {
52
+ document . removeEventListener ( 'click' , handleClick ) ;
53
+ } ;
54
+ } , [ ] )
48
55
49
56
// Link content is set through html XOR label
50
57
const linkContentProps = html
@@ -68,7 +75,6 @@ export default function NavbarNavLink({
68
75
href = { prependBaseUrlToHref ? normalizedHref : href }
69
76
{ ...props }
70
77
{ ...linkContentProps }
71
- onClick = { clickLink }
72
78
/>
73
79
) ;
74
80
}
@@ -85,7 +91,6 @@ export default function NavbarNavLink({
85
91
} ) }
86
92
{ ...props }
87
93
{ ...linkContentProps }
88
- onClick = { clickLink }
89
94
/>
90
95
) ;
91
96
}
0 commit comments