Skip to content
This repository was archived by the owner on Apr 21, 2026. It is now read-only.

Commit a781aae

Browse files
dpataninJ12934
andcommitted
refactor for more react like style
Co-authored-by: Jannik Hollenbach <jannik@hollenbach.de>
1 parent 8ac36ef commit a781aae

4 files changed

Lines changed: 73 additions & 59 deletions

File tree

src/components/MenuMobile.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,25 @@ const MenuMobile = props => {
99
<Menu
1010
right
1111
id="slide"
12-
width={"100vw"}
13-
className={`main-menu-mobile ${props.active ? "open" : ""}`}
14-
customBurgerIcon={<img src={hamburgerIcon} alt="menu"/>}
12+
width={'100vw'}
13+
className={`main-menu-mobile ${props.active ? 'open' : ''}`}
14+
customBurgerIcon={<img src={hamburgerIcon} alt="menu" />}
1515
styles={{
1616
bmBurgerBars: {
17-
height: "3px"
18-
}
17+
height: '3px',
18+
},
1919
}}
2020
>
21-
{menuLinks.map(link => (
22-
<Link to={link.link} key={link.name}>{link.name}</Link>
21+
{menuLinks.map((link) => (
22+
<li key={link.name}>
23+
{link.external ? (
24+
<a href={link.link} target="_blank" rel="noopener noreferrer">
25+
{link.name}
26+
</a>
27+
) : (
28+
<Link to={link.link}>{link.name}</Link>
29+
)}
30+
</li>
2331
))}
2432
</Menu>
2533
);
@@ -33,6 +41,7 @@ export default props => (
3341
siteMetadata {
3442
menuLinks {
3543
name
44+
external
3645
link
3746
}
3847
}

src/components/Sidebar.js

Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,51 @@
1-
import React, { Fragment } from 'react';
1+
import React, { Fragment, useState } from 'react';
22
import { Link } from 'gatsby';
33

4-
// Toggles sidebar menus, only one may be open at a time
5-
function toggleMenu(id) {
6-
const element = document.getElementById(id);
4+
/**
5+
* Interactive Sidebar with toggleable categories.
6+
*
7+
* @requires categories of form: [ { name: "foobar", entires: []} ]
8+
*/
9+
const Sidebar = ({ categories = [], currentPathname }) => {
10+
const [selectedCategory, selectCategory] = useState(() => {
11+
for (const { categoryName, entries } of categories) {
12+
for (const entry of entries) {
13+
if (
14+
currentPathname === `/integrations/${entry.node.frontmatter.path}`
15+
) {
16+
return categoryName;
17+
}
18+
}
19+
}
720

8-
const scanners = document.getElementById('Scanners');
9-
const persistenceProviders = document.getElementById('Persistence Providers');
10-
const hooks = document.getElementById('Hooks');
21+
throw new Error(
22+
`Could not find matching Category for path ${currentPathname}`
23+
);
24+
});
1125

12-
if (element !== scanners) scanners.style.display = 'none';
13-
if (element !== persistenceProviders)
14-
persistenceProviders.style.display = 'none';
15-
if (element !== hooks) hooks.style.display = 'none';
16-
17-
element.style.display = element.style.display === 'block' ? 'none' : 'block';
18-
}
19-
20-
// Determine the naming of Sidebar categories
21-
function getCategoryTitle(category) {
22-
let categoryTitle = '';
23-
24-
switch (category[0].node.frontmatter.category) {
25-
case 'scanner':
26-
categoryTitle = 'Scanners';
27-
break;
28-
29-
case 'hook':
30-
categoryTitle =
31-
category[0].node.frontmatter.type === 'persistenceProvider'
32-
? 'Persistence Providers'
33-
: 'Hooks';
34-
break;
35-
36-
default:
37-
categoryTitle = 'Unknown Category';
38-
break;
39-
}
40-
41-
return categoryTitle;
42-
}
43-
44-
const Sidebar = (props) => {
4526
return (
4627
<nav className="sidebar">
47-
{props.dataArray.map((category, index) => (
48-
<Fragment key={index}>
49-
<h1
50-
onClick={() => toggleMenu(getCategoryTitle(category))}
28+
{categories.map(({ categoryName, entries }) => (
29+
<Fragment key={categoryName}>
30+
<button
31+
onClick={() =>
32+
selectCategory(
33+
selectedCategory === categoryName ? null : categoryName
34+
)
35+
}
5136
className="sidebar-header"
5237
>
53-
{getCategoryTitle(category)}
54-
</h1>
38+
{categoryName}
39+
</button>
5540
<ul
56-
id={getCategoryTitle(category)}
41+
id={categoryName}
42+
name="Category"
5743
className="list-unstyled components show"
44+
style={{
45+
display: selectedCategory === categoryName ? 'block' : 'none',
46+
}}
5847
>
59-
{category.map((element) => (
48+
{entries.map((element) => (
6049
<li key={element.node.frontmatter.title}>
6150
<Link
6251
to={`/integrations/${element.node.frontmatter.path}`}

src/scss/components/_sidebar.scss

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ a {
9292
-khtml-user-select: none; /* Konqueror HTML */
9393
-moz-user-select: none; /* Old versions of Firefox */
9494
-ms-user-select: none; /* Internet Explorer/Edge */
95-
user-select: none; /* Non-prefixed version, currently supported by Chrome, Edge, Opera and Firefox */
95+
user-select: none; /* Non-prefixed version, currently supported by Chrome, Edge, Opera and Firefox */
9696
}
9797
.sidebar-header:hover {
9898
cursor: pointer;
@@ -106,3 +106,13 @@ a {
106106
//Instead of the line below you could use @include transition($transition-1, $transition-2, $transition-3, $transition-4, $transition-5, $transition-6, $transition-7, $transition-8, $transition-9, $transition-10)
107107
transition: all 0.3s;
108108
}
109+
110+
// removing button style
111+
button {
112+
background: none;
113+
color: inherit;
114+
border: none;
115+
padding: 0;
116+
cursor: pointer;
117+
outline: inherit;
118+
}

src/templates/integration.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import { graphql } from 'gatsby';
33
import Layout from '../components/Layout';
44
import ScannerExamples from '../components/ScannerExamples.js';
5-
import Sidebar from '../components/Sidebar.js'
5+
import Sidebar from '../components/Sidebar.js';
66

77
const Integration = (props) => {
88
const { title } = props.data.markdownRemark.frontmatter;
@@ -15,8 +15,14 @@ const Integration = (props) => {
1515
return (
1616
<Layout bodyClass="integration">
1717
<div className="sidebar-wrapper">
18-
<Sidebar dataArray={[scanners, persistenceProviders, hooks]}/>
19-
18+
<Sidebar
19+
categories={[
20+
{ categoryName: 'Scanners', entries: scanners },
21+
{ categoryName: 'Persistence Providers', entries: persistenceProviders },
22+
{ categoryName: 'Hooks', entries: hooks },
23+
]}
24+
currentPathname={props.location.pathname}
25+
/>
2026
<div id="content">
2127
<div className="container-fluid" id="integration-doc">
2228
<h1 className="title">{title}</h1>

0 commit comments

Comments
 (0)