1
1
import {
2
+ $ ,
2
3
QwikIntrinsicElements ,
3
4
component$ ,
4
5
useSignal ,
@@ -18,7 +19,7 @@ export const DashboardTableOfContents = component$(
18
19
19
20
return (
20
21
< div class = "space-y-2" >
21
- < p class = "font-medium" > On This Page</ p >
22
+ < div class = "font-medium" > On This Page</ div >
22
23
< Tree headings = { headings } activeItem = { activeHeading . value } />
23
24
</ div >
24
25
) ;
@@ -29,16 +30,15 @@ const useActiveItem = (itemIds: string[]) => {
29
30
const activeId = useSignal < string > ( ) ;
30
31
31
32
useVisibleTask$ ( ( { cleanup } ) => {
32
- const observer = new IntersectionObserver (
33
- ( entries ) => {
34
- entries . forEach ( ( entry ) => {
35
- if ( entry . isIntersecting ) {
36
- activeId . value = entry . target . id ;
37
- }
38
- } ) ;
39
- } ,
40
- { rootMargin : `0% 0% -80% 0%` } ,
41
- ) ;
33
+ const observer = new IntersectionObserver ( ( entries ) => {
34
+ entries . forEach ( ( entry ) => {
35
+ if ( entry . isIntersecting ) {
36
+ console . log ( 'entry: ' , entry . isIntersecting , entry . target . id ) ;
37
+ activeId . value = entry . target . id ;
38
+ }
39
+ } ) ;
40
+ } ) ;
41
+ console . log ( 'observer: ' , observer ) ;
42
42
43
43
itemIds . forEach ( ( id ) => {
44
44
const element = document . getElementById ( id ) ;
@@ -67,13 +67,25 @@ type TreeProps = QwikIntrinsicElements['ul'] & {
67
67
} ;
68
68
69
69
const Tree = component$ < TreeProps > ( ( { headings, level = 1 , activeItem } ) => {
70
+ console . log ( 'level: ' , level ) ;
70
71
return headings . length > 0 && level < 3 ? (
71
72
< ul class = { cn ( 'm-0 list-none' , { 'pl-4' : level !== 1 } ) } >
72
73
{ headings . map ( ( heading ) => {
73
74
return (
74
75
< li key = { heading . id } class = { cn ( 'mt-0 pt-2' ) } >
75
76
< a
76
77
href = { `#${ heading . id } ` }
78
+ onClick$ = { [
79
+ $ ( ( ) => {
80
+ const element = document . getElementById ( heading . id ) ;
81
+ if ( element ) {
82
+ const navbarHeight = 90 ;
83
+ const elementPosition =
84
+ element . getBoundingClientRect ( ) . top + window . scrollY - navbarHeight ;
85
+ window . scrollTo ( { top : elementPosition , behavior : 'auto' } ) ;
86
+ }
87
+ } ) ,
88
+ ] }
77
89
class = { cn (
78
90
heading . level > 2 ? 'ml-4' : null ,
79
91
'inline-block no-underline transition-colors hover:text-foreground' ,
0 commit comments