File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ import React , { type ReactNode } from 'react' ;
2
+ import Category from '@theme-original/DocSidebarItem/Category' ;
3
+ import type CategoryType from '@theme/DocSidebarItem/Category' ;
4
+ import type { WrapperProps } from '@docusaurus/types' ;
5
+ import { useHistory } from '@docusaurus/router' ;
6
+
7
+ type Props = WrapperProps < typeof CategoryType > ;
8
+
9
+ export default function CategoryWrapper ( props : Props ) : ReactNode {
10
+ const history = useHistory ( ) ;
11
+ const categoryRef = React . useRef < HTMLDivElement > ( null ) ;
12
+
13
+ React . useEffect ( ( ) => {
14
+ if ( ! categoryRef . current || ! props . item . href ) return ;
15
+
16
+ const handleClick = ( e : MouseEvent ) => {
17
+ const target = e . target as HTMLElement ;
18
+ const categoryLink = categoryRef . current ?. querySelector ( 'a.menu__link--sublist' ) ;
19
+
20
+ if ( ! categoryLink || ! categoryLink . contains ( target ) ) {
21
+ return ;
22
+ }
23
+
24
+ const clickedOnLink = target . tagName === 'A' || target . closest ( 'a' ) === categoryLink ;
25
+
26
+ if ( clickedOnLink ) {
27
+ e . preventDefault ( ) ;
28
+ history . push ( props . item . href ) ;
29
+ }
30
+ } ;
31
+
32
+ const element = categoryRef . current ;
33
+ element . addEventListener ( 'click' , handleClick ) ;
34
+
35
+ return ( ) => {
36
+ element . removeEventListener ( 'click' , handleClick ) ;
37
+ } ;
38
+ } , [ props . item . href , history ] ) ;
39
+
40
+ return (
41
+ < div ref = { categoryRef } >
42
+ < Category { ...props } />
43
+ </ div >
44
+ ) ;
45
+ }
You can’t perform that action at this time.
0 commit comments