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 " ;
2
2
3
- export function Sidebar ( { items = [ ] , initialValue = "" } = { } ) {
4
- const [ activeKey , setActiveKey ] = useState ( initialValue ) ;
3
+ export function Sidebar ( { items = [ ] } = { } ) {
4
+ const location = useLocation ( ) ;
5
5
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 ) ;
13
7
14
8
return (
15
9
< ul class = "sticky top-[50px] flex flex-col gap-4" >
16
10
{ 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 } />
22
12
) ) }
23
13
</ ul >
24
14
) ;
25
15
}
26
16
27
- function SidebarItem ( { item, active, onPress } ) {
17
+ function SidebarItem ( { item, active } ) {
28
18
return (
29
19
< 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" >
31
21
{ item . label }
32
22
< span
33
23
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