Skip to content

Commit

Permalink
fix: change sidebar from hash location to routes
Browse files Browse the repository at this point in the history
  • Loading branch information
barelyhuman committed Dec 29, 2024
1 parent 42b7c92 commit 4baebe5
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions src/components/Sidebar.jsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
import { useEffect, useState } from "preact/hooks";
import { useLocation } from "preact-iso";

export function Sidebar({ items = [], initialValue = "" } = {}) {
const [activeKey, setActiveKey] = useState(initialValue);
export function Sidebar({ items = [] } = {}) {
const location = useLocation();

useEffect(() => {
const hash = window.location.hash.replace(/^#/, "");

if (hash != activeKey) {
setActiveKey(hash.replace(/^#/, ""));
}
}, []);
const activeKey = items.find((d) => "/" + d.key === location.path);

return (
<ul class="sticky top-[50px] flex flex-col gap-4">
{items.map((i) => (
<SidebarItem
item={i}
active={i.key == activeKey}
onPress={() => setActiveKey(i.key)}
/>
<SidebarItem item={i} active={activeKey && i.key == activeKey.key} />
))}
</ul>
);
}

function SidebarItem({ item, active, onPress }) {
function SidebarItem({ item, active }) {
return (
<li class="w-fit">
<a href={"/" + item.key} onClick={onPress} class="group text-zinc-600">
<a href={"/" + item.key} class="group text-zinc-600">
{item.label}
<span
class={`block max-w-0 group-hover:max-w-full transition-all duration-500 h-0.5 bg-black ${
Expand Down

0 comments on commit 4baebe5

Please sign in to comment.