diff --git a/src/components/DocsLayout/index.tsx b/src/components/DocsLayout/index.tsx index 6d09c2f..34ee9f7 100644 --- a/src/components/DocsLayout/index.tsx +++ b/src/components/DocsLayout/index.tsx @@ -26,7 +26,7 @@ const DocsLayout = ({ children }: PropsWithChildren<{}>) => { `) return ( - +
@@ -39,7 +39,7 @@ const DocsLayout = ({ children }: PropsWithChildren<{}>) => { {` `} Gatsby - +
) } diff --git a/src/components/DocsLayout/styles.tsx b/src/components/DocsLayout/styles.tsx index 5bc63b3..cc0bb79 100644 --- a/src/components/DocsLayout/styles.tsx +++ b/src/components/DocsLayout/styles.tsx @@ -7,7 +7,7 @@ export const Main = styled.div` ` export const MainContent = styled.main` - flex: 1 0 auto; + flex: 1 1 auto; & > :first-child { margin-top: 0; diff --git a/src/components/DocsSidebar/index.tsx b/src/components/DocsSidebar/index.tsx index caabcbf..e3245ca 100644 --- a/src/components/DocsSidebar/index.tsx +++ b/src/components/DocsSidebar/index.tsx @@ -1,26 +1,29 @@ import * as R from "ramda" -import React from "react" +import React, { useState } from "react" import Tree from "react-treeview" import { useStaticQuery, graphql } from "gatsby" - -export interface DocsSidebarProps { - readonly width: string | number -} +import * as SC from "./styles" interface IFile { title: string type: "file" + path: string } interface IFolder { title: string type: "folder" + path: string children: IFileOrFolder[] } type IFileOrFolder = IFile | IFolder -const traverse = ([head, ...tail]: string[]): IFileOrFolder => { +const traverse = ( + [head, ...tail]: string[], + basePath = "/docs" +): IFileOrFolder => { + const path = basePath + "/" + head const isFile = !tail.length if (isFile) { // probably not more than one dot @@ -28,20 +31,24 @@ const traverse = ([head, ...tail]: string[]): IFileOrFolder => { return { title: name, type: "file", + path, } } return { title: head, type: "folder", - children: [traverse(tail)], + path, + children: [traverse(tail, path)], } } const generateFolder = ({ title, + path, targets, }: { title: IFolder["title"] + path: IFile["path"] targets: IFolder[] }): IFolder => { const children = join(R.chain(target => target.children, targets)) @@ -49,13 +56,21 @@ const generateFolder = ({ return { title, type: "folder", + path, children, } } -const generateFile = ({ title }: { title: IFile["title"] }): IFile => { +const generateFile = ({ + title, + path, +}: { + title: IFile["title"] + path: IFile["path"] +}): IFile => { return { title, + path, type: "file", } } @@ -68,12 +83,12 @@ const join = ([head, ...tail]: IFileOrFolder[]): IFileOrFolder[] => { tail ) const targets = [head, ...similarFs] - const { title } = head + const { title, path } = head const current = head.type === "folder" - ? generateFolder({ title, targets: targets as IFolder[] }) - : generateFile({ title }) + ? generateFolder({ title, path, targets: targets as IFolder[] }) + : generateFile({ title, path }) return [current, ...join(remaining)] } @@ -116,17 +131,34 @@ const ALL_DOCS = graphql` const plantTree = (item: IFileOrFolder) => { if (item.type === "file") { - return
{item.title}
+ return ( + + {item.title} + + ) + } + + return +} + +function Folder({ item }: { item: IFolder }) { + const [collapsed, setCollapse] = useState(false) + + function toggleCollapse() { + setCollapse(prevState => !prevState) } return ( - {item.title}}> + {item.title}} + > {item.children.map(plantTree)} ) } -const DocsSidebar = ({ width }: DocsSidebarProps) => { +const DocsSidebar = () => { const docs = useStaticQuery(ALL_DOCS) const objects = docs.allFile.edges.map(({ node: file }: IFileQuery) => @@ -136,7 +168,11 @@ const DocsSidebar = ({ width }: DocsSidebarProps) => { console.log(results) - return
{results.map(node => plantTree(node))}
+ return ( + + {results.map(node => plantTree(node))} + + ) } export default DocsSidebar diff --git a/src/components/DocsSidebar/style.tsx b/src/components/DocsSidebar/style.tsx deleted file mode 100644 index 8b67f01..0000000 --- a/src/components/DocsSidebar/style.tsx +++ /dev/null @@ -1,6 +0,0 @@ -import styled from "styled-components" - -export const DocsSidebarScroller = styled.div` - display: flex; - flex-direction: column; -` diff --git a/src/components/DocsSidebar/styles.tsx b/src/components/DocsSidebar/styles.tsx new file mode 100644 index 0000000..ea98111 --- /dev/null +++ b/src/components/DocsSidebar/styles.tsx @@ -0,0 +1,18 @@ +import styled from "styled-components" +import { Link } from "gatsby" + +export const DocsSidebarWrapper = styled.div` + box-sizing: border-box; + padding: 0 16px; + flex: 0 0 300px; +` + +export const PageLink = styled(Link)` + display: block; + padding: 4px 0; + color: #1c61df; + + &.active { + font-weight: 700; + } +` diff --git a/src/components/Layout/layout.scss b/src/components/Layout/layout.scss index f12a80c..a5c52e9 100644 --- a/src/components/Layout/layout.scss +++ b/src/components/Layout/layout.scss @@ -48,11 +48,20 @@ body { .tree-view_item { display: flex; + cursor: pointer; +} + +.tree-view_item, +.tree-view_children > div { + padding: 4px 0; } /* style for the children nodes container */ .tree-view_children { margin-left: 16px; + display: flex; + flex-direction: column; + align-items: flex-start; } .tree-view_children-collapsed {