Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update table of content scroll + hading indentation #203

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 31 additions & 21 deletions apps/frontpage/components/docs/table-of-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { inView } from 'framer-motion';
import type { Dispatch, FC, SetStateAction } from 'react';
import { useEffect, useState } from 'react';
import { useScrollDirection } from 'react-use-scroll-direction';
import { ScrollArea } from '../ui/scroll-area';
import { ScrollBar } from '../ui/scroll-area';
import { useDocs } from '../../app/docs/provider';
import * as ScrollAreaPrimitive from '@radix-ui/react-scroll-area';

interface Heading {
id: string;
Expand Down Expand Up @@ -41,23 +42,26 @@ export const TableOfContent: FC = () => {

return (
<nav className="sticky top-[72px] hidden w-[228px] self-start xl:block">
<ScrollArea className="h-[calc(100vh-72px)] w-full">
<div className="py-12">
<div className="block h-8 text-sm font-bold">On this page</div>
<ul className="mt-1">
{headings?.map((heading) => {
return (
<Element
heading={heading}
isInView={isInView}
key={heading.id}
setIsInView={setIsInView}
/>
);
})}
</ul>
</div>
</ScrollArea>
<ScrollAreaPrimitive.Root className="relative h-[calc(100vh-72px)] w-full overflow-hidden">
<ScrollAreaPrimitive.Viewport className="h-full w-full rounded-[inherit]">
<div className="py-12 pl-1 pr-4">
<div className="block h-8 text-sm font-bold">On this page</div>
<ul className="mt-1">
{headings?.map((heading) => {
return (
<Element
heading={heading}
isInView={isInView}
key={heading.id}
setIsInView={setIsInView}
/>
);
})}
</ul>
</div>
</ScrollAreaPrimitive.Viewport>
<ScrollBar className="pb-6 pt-12" />
</ScrollAreaPrimitive.Root>
</nav>
);
};
Expand Down Expand Up @@ -87,11 +91,17 @@ const Element: FC<ElementProps> = ({ heading, isInView, setIsInView }) => {
const active = isInView.length > 0 ? isInView[0].includes(heading.id) : false;

return (
<li key={heading.id}>
<li
key={heading.id}
className={cn(
'mb-2 w-[210px] overflow-hidden text-ellipsis',
heading.level === 3 && 'pl-5',
heading.level === 4 && 'pl-10',
)}
>
<a
className={cn(
'mb-3 flex w-full items-center text-sm text-zinc-700 transition-colors hover:text-blue-500 dark:text-slate-400 dark:hover:text-blue-500',
heading.level > 2 && 'ml-5',
'text-sm text-zinc-700 transition-colors hover:text-blue-500 dark:text-slate-400 dark:hover:text-blue-500',
active && 'text-blue-500',
)}
href={`#${heading.id}`}
Expand Down