feat(sidebar): close on mobile upon clicking a link

This commit is contained in:
Jean-Philippe Sirois
2020-01-18 00:05:45 -05:00
parent ea57ea7815
commit 3f08458b03
2 changed files with 57 additions and 8 deletions
+22 -3
View File
@@ -1,6 +1,8 @@
import { graphql, useStaticQuery } from "gatsby"
import { sort } from "ramda"
import React, { FC, HTMLAttributes } from "react"
import { useLockBodyScroll } from "../../hooks/useLockBodyScroll"
import useSidebar from "../../hooks/useSidebar"
import { IAllArchivesQuery, IFileOrFolder } from "../../types"
import { humanize } from "../../utils"
import { Sidebar } from "../Sidebar"
@@ -19,9 +21,20 @@ const ALL_ARCHIVES = graphql`
}
`
function plantTree(item: IFileOrFolder) {
function Tree({ item }: { item: IFileOrFolder }) {
const { setOpenOnMobile } = useSidebar()
const { unlock } = useLockBodyScroll()
return (
<SC.PageLink key={item.title} to={item.path} activeClassName="active">
<SC.PageLink
key={item.title}
to={item.path}
activeClassName="active"
onClick={() => {
setOpenOnMobile(false)
unlock()
}}
>
{humanize(item.title)}
</SC.PageLink>
)
@@ -32,5 +45,11 @@ export const ArchivesSidebar: FC<HTMLAttributes<HTMLDivElement>> = props => {
const tree = useBuildTree(archives, "/archives")
const sortedTree = sort((a, b) => a.title.localeCompare(b.title), tree)
return <Sidebar {...props}>{sortedTree.map(plantTree)}</Sidebar>
return (
<Sidebar {...props}>
{sortedTree.map(node => (
<Tree item={node} />
))}
</Sidebar>
)
}