-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathSiteMobileCategory.tsx
52 lines (49 loc) · 1.58 KB
/
SiteMobileCategory.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
import { useEffect, useRef } from "react"
import { CategoryWithEntries } from "@ourworldindata/utils"
import { SiteNavigationToggle } from "./SiteNavigationToggle.js"
import { SiteNavigationTopic } from "./SiteNavigationTopic.js"
import { allTopicsInCategory } from "./gdocs/utils.js"
export const SiteMobileCategory = ({
category,
isActive,
toggleCategory,
}: {
category: CategoryWithEntries
isActive: boolean
toggleCategory: (category: CategoryWithEntries) => void
}) => {
const categoryRef = useRef<HTMLLIElement>(null)
useEffect(() => {
if (isActive && categoryRef.current) {
categoryRef.current.scrollIntoView({ behavior: "smooth" })
}
}, [isActive])
return (
<li
key={category.slug}
className="SiteMobileCategory"
ref={categoryRef}
>
<SiteNavigationToggle
ariaLabel={
isActive ? `Collapse ${category}` : `Expand ${category}`
}
isActive={isActive}
onToggle={() => toggleCategory(category)}
dropdown={
<ul>
{allTopicsInCategory(category).map((topic) => (
<SiteNavigationTopic
key={topic.slug}
topic={topic}
/>
))}
</ul>
}
withCaret={true}
>
{category.name}
</SiteNavigationToggle>
</li>
)
}