on master help sign in
docs: sidebar
commit8cef528b3023b17b0e9f1ef873c8af912841e918 authorMatilde Park <matilde@tlon.io> dateTue 22 Jun 2021, 15:30 (5 years ago) parent26d6dff
2 files changed+92−46
pages/docs.js+42−23 pages/docs/[...slug].js+50−23
pages/docs.js+42−23
@@ -1,4 +1,6 @@
11import Link from "next/link";
2+import { useState } from "react";
3+import classnames from "classnames";
24import { join } from "path";
35import { buildPageTree, getPage } from "../lib/lib";
46import Markdown from "../components/Markdown";
@@ -21,38 +23,52 @@ const breadcrumbs = (posts, paths) => {
2123 return results;
2224};
2325
24−const childPages = (thisLink, children) => (
26+const childPages = (thisLink, children, level = 0) => (
2527 <ul className="pl-1">
2628 {Object.entries(children).map(([childSlug, child]) => (
27− <li>{pageTree(join(thisLink, childSlug), child)}</li>
29+ <li>{pageTree(join(thisLink, childSlug), child, level)}</li>
2830 ))}
2931 </ul>
3032);
3133
32−const pageTree = (thisLink, tree) => (
33− <div>
34− <Link href={thisLink}>
35− <span className="font-medium text-lg cursor-pointer">{tree.title}</span>
36− </Link>
37− <ul className="pl-1">
38− {tree.pages
39− .sort((a, b) => a.weight - b.weight)
40− .map(({ title, slug }) => (
41− <li className="font-thin py-1">
42− <Link href={join(thisLink, slug)}>{title}</Link>
43− </li>
44− ))}
45− </ul>
46− {childPages(thisLink, tree.children)}
47− </div>
48−);
34+const pageTree = (thisLink, tree, level = 0) => {
35+ const [isOpen, toggleTree] = useState(false);
36+
37+ const activeClasses = classnames({
38+ hidden: !isOpen,
39+ });
40+ return (
41+ <>
42+ <span
43+ className={
44+ "font-medium text-gray text-lg cursor-pointer hover:text-black"
45+ }
46+ onClick={() => toggleTree(!isOpen)}
47+ >
48+ {tree.title}
49+ </span>
50+ <div className={activeClasses}>
51+ <ul className={"pl-1"}>
52+ {tree.pages
53+ .sort((a, b) => a.weight - b.weight)
54+ .map(({ title, slug }) => (
55+ <li className="font-thin py-1">
56+ <Link href={join(thisLink, slug)}>{title}</Link>
57+ </li>
58+ ))}
59+ </ul>
60+ {childPages(thisLink, tree.children, level + 1)}
61+ </div>
62+ </>
63+ );
64+};
4965
5066function Sidebar(props) {
5167 return (
5268 <div className="flex flex-col w-72 bg-wall max-h-screen h-screen">
53− <header className="flex justify-between items-center pl-12 pt-8 pb-8">
69+ <header className="flex flex-shrink-0 justify-between items-center pl-12 pt-12 pb-8">
5470 <Link href="/">
55− <a className="type-ui">Urbit</a>
71+ <a className="type-ui text-gray">Urbit.org</a>
5672 </Link>
5773 </header>
5874 <div className="overflow-y-scroll p-12 pt-16">
@@ -66,8 +82,11 @@ function Sidebar(props) {
6682function ContentArea(props) {
6783 return (
6884 <div className="w-full">
69− <header className="flex justify-between items-center px-24 pt-8 pb-8">
70− <div className="type-ui">Install Urbit</div>
85+ <header className="flex justify-between items-center px-24 pt-12 pb-8">
86+ <div className="type-ui">Urbit Documentation</div>
87+ <button className="button-sm bg-wall text-gray">
88+ Search Urbit.org<div className="ml-4 text-lightGray">⌘K</div>
89+ </button>
7190 </header>
7291 <div className="px-24 pb-24 pt-16 flex flex-col w-full max-h-screen h-screen overflow-y-scroll">
7392 <div className="type-ui text-lightGray">{props.breadcrumbs}</div>
pages/docs/[...slug].js+50−23
@@ -1,6 +1,8 @@
11import Head from "next/head";
22import Link from "next/link";
33import { useRouter } from "next/router";
4+import { useState } from "react";
5+import classnames from "classnames";
46import { join } from "path";
57import { getDocs, formatDate, buildPageTree, getPage } from "../../lib/lib";
68import Markdown from "../../components/Markdown";
@@ -23,38 +25,60 @@ const breadcrumbs = (posts, paths) => {
2325 return results;
2426};
2527
26−const childPages = (thisLink, children) => (
28+const childPages = (thisLink, children, level = 0) => (
2729 <ul className="pl-1">
2830 {Object.entries(children).map(([childSlug, child]) => (
29− <li>{pageTree(join(thisLink, childSlug), child)}</li>
31+ <li>{pageTree(join(thisLink, childSlug), child, level)}</li>
3032 ))}
3133 </ul>
3234);
3335
34−const pageTree = (thisLink, tree) => (
35− <div>
36− <Link href={thisLink}>
37− <span className="font-medium text-lg cursor-pointer">{tree.title}</span>
38− </Link>
39− <ul className="pl-1">
40− {tree.pages
41− .sort((a, b) => a.weight - b.weight)
42− .map(({ title, slug }) => (
43− <li className="font-thin py-1">
44− <Link href={join(thisLink, slug)}>{title}</Link>
45− </li>
46− ))}
47− </ul>
48− {childPages(thisLink, tree.children)}
49− </div>
50−);
36+const pageTree = (thisLink, tree, level = 0) => {
37+ const router = useRouter();
38+ const firstCrumb = "/" + router.asPath.split("/").slice(1).join("/");
39+
40+ const thisPage = firstCrumb.includes(thisLink);
41+ const [isOpen, toggleTree] = useState(thisPage);
42+
43+ const activeClasses = classnames({
44+ hidden: !isOpen,
45+ });
46+
47+ const headerClass = classnames({
48+ "text-gray": !thisPage,
49+ });
50+ return (
51+ <>
52+ <span
53+ className={
54+ "font-medium text-lg cursor-pointer hover:text-black " + headerClass
55+ }
56+ onClick={() => toggleTree(!isOpen)}
57+ >
58+ {tree.title}
59+ </span>
60+ <div className={activeClasses}>
61+ <ul className={"pl-1"}>
62+ {tree.pages
63+ .sort((a, b) => a.weight - b.weight)
64+ .map(({ title, slug }) => (
65+ <li className="font-thin py-1">
66+ <Link href={join(thisLink, slug)}>{title}</Link>
67+ </li>
68+ ))}
69+ </ul>
70+ {childPages(thisLink, tree.children, level + 1)}
71+ </div>
72+ </>
73+ );
74+};
5175
5276function Sidebar(props) {
5377 return (
5478 <div className="flex flex-col w-72 bg-wall max-h-screen h-screen">
55− <header className="flex justify-between items-center pl-12 pt-8 pb-8">
79+ <header className="flex flex-shrink-0 justify-between items-center pl-12 pt-12 pb-8">
5680 <Link href="/">
57− <a className="type-ui">Urbit</a>
81+ <a className="type-ui text-gray">Urbit.org</a>
5882 </Link>
5983 </header>
6084 <div className="overflow-y-scroll p-12 pt-16">
@@ -68,8 +92,11 @@ function Sidebar(props) {
6892function ContentArea(props) {
6993 return (
7094 <div className="w-full">
71− <header className="flex justify-between items-center px-24 pt-8 pb-8">
72− <div className="type-ui">Install Urbit</div>
95+ <header className="flex justify-between items-center px-24 pt-12 pb-8">
96+ <div className="type-ui">Urbit Documentation</div>
97+ <button className="button-sm bg-wall text-gray">
98+ Search Urbit.org<div className="ml-4 text-lightGray">⌘K</div>
99+ </button>
73100 </header>
74101 <div className="px-24 pb-24 pt-16 flex flex-col w-full max-h-screen h-screen overflow-y-scroll">
75102 <div className="type-ui text-lightGray">{props.breadcrumbs}</div>