|
| 1 | +import React, { Fragment, useState } from 'react'; |
| 2 | +import { Link } from 'gatsby'; |
| 3 | +import { IoIosArrowDown, IoIosArrowForward } from 'react-icons/io'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Interactive Sidebar with toggleable categories. |
| 7 | + * |
| 8 | + * @requires categories of form: [ { name: "foobar", entires: []} ] |
| 9 | + */ |
| 10 | +const Sidebar = ({ categories = [], currentPathname }) => { |
| 11 | + const [selectedCategory, selectCategory] = useState(() => { |
| 12 | + for (const { categoryName, entries } of categories) { |
| 13 | + for (const entry of entries) { |
| 14 | + if (currentPathname.includes(entry.node.frontmatter.path)) { |
| 15 | + return categoryName; |
| 16 | + } |
| 17 | + } |
| 18 | + } |
| 19 | + return null; |
| 20 | + }); |
| 21 | + |
| 22 | + return ( |
| 23 | + <nav className="sidebar"> |
| 24 | + {categories.map(({ categoryName, entries }) => ( |
| 25 | + <Fragment key={categoryName}> |
| 26 | + <button |
| 27 | + onClick={() => |
| 28 | + selectCategory( |
| 29 | + selectedCategory === categoryName ? null : categoryName |
| 30 | + ) |
| 31 | + } |
| 32 | + className="sidebar-category" |
| 33 | + > |
| 34 | + {categoryName} |
| 35 | + <IoIosArrowDown |
| 36 | + className="arrow" |
| 37 | + style={{ |
| 38 | + display: selectedCategory === categoryName ? 'block' : 'none', |
| 39 | + }} |
| 40 | + /> |
| 41 | + <IoIosArrowForward |
| 42 | + className="arrow" |
| 43 | + style={{ |
| 44 | + display: selectedCategory === categoryName ? 'none' : 'block', |
| 45 | + }} |
| 46 | + /> |
| 47 | + </button> |
| 48 | + <ul |
| 49 | + id={categoryName} |
| 50 | + name="Category" |
| 51 | + className="list-unstyled components show" |
| 52 | + style={{ |
| 53 | + display: selectedCategory === categoryName ? 'block' : 'none', |
| 54 | + }} |
| 55 | + > |
| 56 | + {entries.map((element) => ( |
| 57 | + <li |
| 58 | + key={element.node.frontmatter.title} |
| 59 | + className="sidebar-element" |
| 60 | + > |
| 61 | + <Link |
| 62 | + to={`/integrations/${element.node.frontmatter.path}`} |
| 63 | + activeClassName="active-Link" |
| 64 | + > |
| 65 | + {element.node.frontmatter.title} |
| 66 | + </Link> |
| 67 | + </li> |
| 68 | + ))} |
| 69 | + </ul> |
| 70 | + </Fragment> |
| 71 | + ))} |
| 72 | + </nav> |
| 73 | + ); |
| 74 | +}; |
| 75 | + |
| 76 | +export default Sidebar; |
0 commit comments