Files
onspring-api-docs/src/app/components/SideBar.tsx
T

127 lines
3.3 KiB
TypeScript
Raw Normal View History

'use client';
2023-04-14 00:40:25 -05:00
import Link from 'next/link.js';
import { useState } from 'react';
import { useThemeContext } from '../theme';
2023-04-09 14:50:50 -05:00
import { Doc, DocsStructure } from '../types/types.js';
2023-04-07 23:18:45 -05:00
import styles from './SideBar.module.css';
2023-04-09 14:50:50 -05:00
function TreeNode({ doc }: { doc: Doc }) {
const [isExpanded, setIsExpanded] =
useState<boolean>(true);
const hasChildren =
doc.children && doc.children?.length > 0;
return (
<li className={styles.listItem}>
{hasChildren ? (
2023-04-13 09:27:38 -05:00
<div className={styles.expandable}>
<a
href={`#${doc.title
.replaceAll(' ', '-')
.toLowerCase()}`}
className={styles.link}
>
{doc.title}
</a>
2023-04-18 22:51:50 -05:00
<div
className={styles.chevronContainer}
onClick={() => setIsExpanded(!isExpanded)}
>
2023-04-13 09:27:38 -05:00
{isExpanded ? (
<svg
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
fill="currentColor"
className={styles.chevron}
viewBox="0 0 16 16"
>
<path
fillRule="evenodd"
d="M7.646 4.646a.5.5 0 0 1 .708 0l6 6a.5.5 0 0 1-.708.708L8 5.707l-5.646 5.647a.5.5 0 0 1-.708-.708l6-6z"
/>
</svg>
) : (
<svg
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
fill="currentColor"
className={styles.chevron}
viewBox="0 0 16 16"
>
<path
fillRule="evenodd"
d="M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708z"
/>
</svg>
)}
</div>
</div>
) : (
2023-04-10 00:04:49 -05:00
<a
2023-04-12 10:54:07 -05:00
href={`#${doc.title
.replaceAll(' ', '-')
.toLowerCase()}`}
2023-04-10 00:04:49 -05:00
className={styles.link}
>
{doc.title}
</a>
)}
{doc.children &&
doc.children?.length > 0 &&
isExpanded && <ListTree docs={doc.children} />}
</li>
);
}
2023-04-09 14:50:50 -05:00
function ListTree({ docs }: { docs: Doc[] }) {
return (
<ul className={styles.list}>
{docs.map(d => (
<TreeNode key={d.title} doc={d} />
))}
</ul>
);
}
export default function SideBar({
version,
}: {
version: DocsStructure;
}) {
const { theme, setTheme } = useThemeContext();
2023-04-07 23:18:45 -05:00
return (
<div className={styles.container}>
2023-04-20 23:36:52 -05:00
<div className={styles.titleContainer}>
<Link href="/" className={styles.link}>
<h1 className={styles.title}>
<span className={styles.onspring}>
Onspring
</span>{' '}
<span className={styles.api}>API</span>
</h1>
</Link>
2023-04-20 23:36:52 -05:00
<label className={styles.switch}>
<input
2023-04-20 23:36:52 -05:00
title="Toggle dark mode"
onChange={() =>
setTheme(theme === 'light' ? 'dark' : 'light')
}
type="checkbox"
2023-04-21 09:40:29 -05:00
checked={theme === 'dark'}
/>
2023-04-20 23:36:52 -05:00
<span
className={`${styles.slider} ${styles.round}`}
></span>
</label>
</div>
<ListTree docs={version.docs} />
2023-04-07 23:18:45 -05:00
</div>
);
}