-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathSidebar.js
More file actions
91 lines (82 loc) · 2.58 KB
/
Sidebar.js
File metadata and controls
91 lines (82 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import React, { Fragment, useContext, useState, memo } from 'react';
import { Link } from 'gatsby';
import classnames from 'classnames';
import SidebarTreeList from './SidebarTreeList';
import { LayoutContext } from './Layout';
import FilterBar from './FilterBar';
import { useFilteredTree } from '../hooks';
import * as css from './Sidebar.module.css';
export const Sidebar = memo(({ children, title, show, setShow = () => {} }) => {
const { headerScrolled } = useContext(LayoutContext);
if (show === null) {
return <div key="placeholder" className={css.root} />;
}
return (
<div
key="root"
className={classnames(css.root, {
[css.show]: show,
[css.headerScrolled]: headerScrolled
})}>
<div
className={css.toggleButton}
onClick={() => setShow(!show)}
onKeyDown={(e) => e.keyCode === 13 && setShow(!show)}
role={'button'}
tabIndex={'0'}>
{title && <span className={css.toggleLabel}>{title}</span>}
{show ? '×' : '+'}
</div>
{show && (
<Fragment>
{title && <h2 className={css.title}>{title}</h2>}
{children}
</Fragment>
)}
</div>
);
});
export const SidebarTree = memo(({ tree, title, show, useSerif, setShow }) => {
const [searchTerm, setSearchTerm] = useState('');
const filtered = useFilteredTree(tree, searchTerm);
return (
<Sidebar title={title} show={show} setShow={setShow}>
<FilterBar
placeholder={'Filter'}
onChange={(e) => setSearchTerm(e.target.value)}
onClick={(e) => setSearchTerm('')}
searchTerm={searchTerm}
/>
<div className={css.listWrapper}>
<SidebarTreeList tree={filtered} useSerif={useSerif} />
</div>
</Sidebar>
);
});
export const SidebarTableOfContents = memo(
({ items = [], title, show, useSerif, setShow }) => {
const { currentHeading } = useContext(LayoutContext);
return (
<Sidebar title={title} show={show} setShow={setShow}>
<div className={css.listWrapper}>
<ul>
{items.map((item, index) => {
const isCurrent = currentHeading === item.url.replace('#', '');
return (
<li
key={`item-${index}`}
className={classnames(css.tocItem, {
[css.active]: isCurrent
})}>
<Link to={item.url}>
<h4>{item.title}</h4>
</Link>
</li>
);
})}
</ul>
</div>
</Sidebar>
);
}
);