@@ -3,11 +3,8 @@ import { tv } from 'tailwind-variants'
3
3
import type { FC , MouseEvent } from 'react'
4
4
5
5
import { getIsInteractive } from '~/atoms/is-interactive'
6
- import { useWrappedElement } from '~/providers/shared/WrappedElementProvider'
7
6
import { clsxm } from '~/utils/helper'
8
7
9
- import { escapeSelector } from './escapeSelector'
10
-
11
8
const styles = tv ( {
12
9
base : clsxm (
13
10
'leading-normal mb-[1.5px] text-neutral-content inline-block relative max-w-full min-w-0' ,
@@ -24,19 +21,19 @@ export interface ITocItem {
24
21
title : string
25
22
anchorId : string
26
23
index : number
24
+
25
+ $heading : HTMLHeadingElement
27
26
}
28
27
29
28
export const TocItem : FC < {
30
- title : string
31
- anchorId : string
32
- depth : number
29
+ heading : ITocItem
30
+
33
31
active : boolean
34
32
rootDepth : number
35
33
onClick ?: ( i : number , $el : HTMLElement | null , anchorId : string ) => void
36
- index : number
37
- // containerRef?: RefObject<HTMLDivElement>
38
34
} > = memo ( ( props ) => {
39
- const { index, active, depth, title, rootDepth, onClick, anchorId } = props
35
+ const { active, rootDepth, onClick, heading } = props
36
+ const { $heading, anchorId, depth, index, title } = heading
40
37
41
38
const $ref = useRef < HTMLAnchorElement > ( null )
42
39
@@ -49,12 +46,18 @@ export const TocItem: FC<{
49
46
history . replaceState ( state , '' , `#${ anchorId } ` )
50
47
} , [ active , anchorId ] )
51
48
49
+ useEffect ( ( ) => {
50
+ if ( active ) {
51
+ $ref . current ?. scrollIntoView ( { behavior : 'smooth' } )
52
+ }
53
+ } , [ ] )
54
+
52
55
const renderDepth = useMemo ( ( ) => {
53
56
const result = depth - rootDepth
54
57
55
58
return result
56
59
} , [ depth , rootDepth ] )
57
- const $article = useWrappedElement ( )
60
+
58
61
return (
59
62
< a
60
63
ref = { $ref }
@@ -76,13 +79,10 @@ export const TocItem: FC<{
76
79
onClick = { useCallback (
77
80
( e : MouseEvent ) => {
78
81
e . preventDefault ( )
79
- const $el = $article ?. querySelector (
80
- `#${ escapeSelector ( anchorId ) } ` ,
81
- ) as any as HTMLElement
82
82
83
- onClick ?.( index , $el , anchorId )
83
+ onClick ?.( index , $heading , anchorId )
84
84
} ,
85
- [ onClick , index , $article , anchorId ] ,
85
+ [ onClick , index , $heading , anchorId ] ,
86
86
) }
87
87
title = { title }
88
88
>
0 commit comments