File tree Expand file tree Collapse file tree 1 file changed +7
-17
lines changed Expand file tree Collapse file tree 1 file changed +7
-17
lines changed Original file line number Diff line number Diff line change 1- import  {  useEffect ,   useState   }  from  "preact/hooks " ; 
1+ import  {  useLocation   }  from  "preact-iso " ; 
22
3- export  function  Sidebar ( {  items =  [ ] ,  initialValue  =   ""  }  =  { } )  { 
4-   const  [ activeKey ,   setActiveKey ]   =   useState ( initialValue ) ; 
3+ export  function  Sidebar ( {  items =  [ ]  }  =  { } )  { 
4+   const  location   =   useLocation ( ) ; 
55
6-   useEffect ( ( )  =>  { 
7-     const  hash  =  window . location . hash . replace ( / ^ # / ,  "" ) ; 
8- 
9-     if  ( hash  !=  activeKey )  { 
10-       setActiveKey ( hash . replace ( / ^ # / ,  "" ) ) ; 
11-     } 
12-   } ,  [ ] ) ; 
6+   const  activeKey  =  items . find ( ( d )  =>  "/"  +  d . key  ===  location . path ) ; 
137
148  return  ( 
159    < ul  class = "sticky top-[50px] flex flex-col gap-4" > 
1610      { items . map ( ( i )  =>  ( 
17-         < SidebarItem 
18-           item = { i } 
19-           active = { i . key  ==  activeKey } 
20-           onPress = { ( )  =>  setActiveKey ( i . key ) } 
21-         /> 
11+         < SidebarItem  item = { i }  active = { activeKey  &&  i . key  ==  activeKey . key }  /> 
2212      ) ) } 
2313    </ ul > 
2414  ) ; 
2515} 
2616
27- function  SidebarItem ( {  item,  active,  onPress  } )  { 
17+ function  SidebarItem ( {  item,  active } )  { 
2818  return  ( 
2919    < li  class = "w-fit" > 
30-       < a  href = { "/"  +  item . key }  onClick = { onPress }   class = "group text-zinc-600" > 
20+       < a  href = { "/"  +  item . key }  class = "group text-zinc-600" > 
3121        { item . label } 
3222        < span 
3323          class = { `block max-w-0 group-hover:max-w-full transition-all duration-500 h-0.5 bg-black ${  
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments