Skip to content

Commit

Permalink
Only highlight the first sidecar item
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonRomano committed Dec 20, 2024
1 parent d82bf80 commit 4c99485
Showing 1 changed file with 38 additions and 23 deletions.
61 changes: 38 additions & 23 deletions src/components/sidecar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,56 @@ interface SidecarProps {
inViewHeaderIDs: string[];
}

// If there's less items than this, the sidecar content won't render
// it does not make sense to have a single item in the sidecar.
const MIN_SIDECAR_ITEMS = 2;

export default function Sidecar({
className,
title,
items,
inViewHeaderIDs,
}: SidecarProps) {
// Calculate the first header that's in view
var activeHeaderID: null | string;
for (const item of items) {
if (inViewHeaderIDs.includes(item.id.substring(1))) {
activeHeaderID = item.id;
break;
}
}
return (
<div className={classNames(s.sidecar, className)}>
<H6 className={s.title}>{title}</H6>
<ul>
{items.map(({ id, title, depth }) => {
// TODO: make just be the first one that appears
const active = inViewHeaderIDs.includes(id.substring(1));
return (
<li
key={`${id}${active}`}
className={classNames({ [s.active]: active })}
style={
{
"--depth": depth,
} as React.CSSProperties
}
>
{/* Intentionally using an a tag and not next/link:
{items.length > MIN_SIDECAR_ITEMS && (
<>
<H6 className={s.title}>{title}</H6>
<ul>
{items.map(({ id, title, depth }) => {
const active = id === activeHeaderID;
return (
<li
key={`${id}${active}`}
className={classNames({ [s.active]: active })}
style={
{
"--depth": depth,
} as React.CSSProperties
}
>
{/* Intentionally using an a tag and not next/link:
as we want our :target selectors to trigger here.
See: https://github.com/vercel/next.js/issues/51346
Also, we're remaining on the same page always here,
so no client-side routing handing is needed. */}
<a href={id}>
<P weight={active ? "medium" : "regular"}>{title}</P>
</a>
</li>
);
})}
</ul>
<a href={id}>
<P weight={active ? "medium" : "regular"}>{title}</P>
</a>
</li>
);
})}
</ul>
</>
)}
</div>
);
}

0 comments on commit 4c99485

Please sign in to comment.