Skip to content

Commit 44efd21

Browse files
committed
fix: change sidebar from hash location to routes
1 parent 15e9189 commit 44efd21

File tree

1 file changed

+7
-17
lines changed

1 file changed

+7
-17
lines changed

src/components/Sidebar.jsx

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,23 @@
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 ${

0 commit comments

Comments
 (0)