-
Notifications
You must be signed in to change notification settings - Fork 0
/
_sidebar.tsx
151 lines (145 loc) · 4.03 KB
/
_sidebar.tsx
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// deno-lint-ignore-file no-undef
import { React } from "./_deps.tsx";
import type { PageProps, PagePropsSidebar, PagicLayout } from "./_deps.tsx";
import { classnames, isRelativeLink } from "./_utils.tsx";
const Sidebar: PagicLayout = ({ config, outputPath, sidebar }) => {
if (!sidebar) {
return null;
}
return (
<aside className="sidebar">
<ol className="list_style_none">
{sidebar.map((sidebarItem, index) => (
<FoldableItem
key={index}
config={config}
outputPath={outputPath}
sidebarItem={sidebarItem}
/>
))}
</ol>
</aside>
);
};
const FoldableItem: React.FC<{
config: PageProps["config"];
outputPath: PageProps["outputPath"];
sidebarItem: PagePropsSidebar[0];
}> = (
{ config, outputPath, sidebarItem: { text, link, expanded, children } },
) => {
// deno-lint-ignore no-explicit-any
const olRef = React.useRef<any>(null);
const [fold, setFold] = React.useState(expanded === false);
const [olHeight, setOlHeight] = React.useState(0);
const isActive = link === outputPath;
const foldOl = (fold: boolean) => {
if (olRef.current === null) {
return;
}
const currentHeight = olRef.current.getBoundingClientRect().height;
if (fold) {
setOlHeight(currentHeight);
olRef.current.style.height = `${currentHeight}px`;
setTimeout(() => {
olRef.current.style.height = 0;
setFold(fold);
}, 17);
} else {
if (olHeight === 0) {
olRef.current.style.height = "auto";
const currentHeight = olRef.current.getBoundingClientRect().height;
olRef.current.style.height = 0;
setTimeout(() => {
olRef.current.style.height = `${currentHeight}px`;
}, 0);
setOlHeight(currentHeight);
} else {
olRef.current.style.height = `${olHeight}px`;
}
setFold(fold);
setTimeout(() => {
olRef.current.style.height = "auto";
}, 300);
}
};
const toggleFold = (e: React.MouseEvent) => {
e.preventDefault();
e.stopPropagation();
foldOl(!fold);
};
return (
<li className={children ? (fold ? "fold" : "unfold") : ""}>
<a
href={link
? (isRelativeLink(link) ? `${config.root}${link}` : link)
: "#"}
className={classnames("nav_link", {
active: isActive,
no_link: !link,
})}
onClick={(e) => {
if (link) {
if (children) {
if (isActive) {
toggleFold(e);
} else {
// @ts-ignore need for parse
document.documentElement.classList.remove("show_sidebar");
if (fold) {
foldOl(false);
}
}
} else {
// @ts-ignore need for parse
document.documentElement.classList.remove("show_sidebar");
}
} else {
toggleFold(e);
}
}}
>
{text}
{children && (
<>
<span
className="czs-angle-up-l"
style={{
backgroundImage:
`url("${config.root}assets/czs-angle-up-l.svg")`,
}}
onClick={toggleFold}
/>
<span
className="czs-angle-down-l"
style={{
backgroundImage:
`url("${config.root}assets/czs-angle-down-l.svg")`,
}}
onClick={toggleFold}
/>
</>
)}
</a>
{children && (
<ol
className="list_style_none"
ref={olRef}
style={{
height: expanded === false ? 0 : "auto",
}}
>
{children.map((sidebarItem, index) => (
<FoldableItem
key={index}
config={config}
outputPath={outputPath}
sidebarItem={sidebarItem}
/>
))}
</ol>
)}
</li>
);
};
export default Sidebar;