1
0
mirror of https://github.com/oceanprotocol/docs.git synced 2024-11-26 19:49:26 +01:00
docs/src/components/Sidebar.jsx

112 lines
3.3 KiB
React
Raw Normal View History

2018-11-08 10:44:46 +01:00
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { Link } from 'gatsby'
import styles from './Sidebar.module.scss'
const SidebarLink = ({ link, title, linkClasses }) => {
if (link) {
if (link.match(/^\s?http(s?)/gi)) {
return (
<a href={link} target="_blank" rel="noopener noreferrer">
{title}
</a>
)
} else {
return (
<Link to={link} className={linkClasses}>
{title}
</Link>
)
}
} else {
return title
2018-11-08 10:44:46 +01:00
}
}
const SidebarList = ({ items, location }) => (
2018-11-08 12:33:20 +01:00
<ul className={styles.list}>
2018-11-08 10:44:46 +01:00
{items.map((item, j) => (
<li key={j}>
<SidebarLink
link={item.link}
title={item.title}
linkClasses={
item.link === location.pathname
? styles.active
: styles.link
}
/>
</li>
))}
</ul>
)
export default class Sidebar extends Component {
static propTypes = {
sidebar: PropTypes.string,
2018-11-23 13:49:53 +01:00
location: PropTypes.object.isRequired,
toc: PropTypes.bool,
tableOfContents: PropTypes.string
2018-11-08 10:44:46 +01:00
}
2018-11-23 13:49:53 +01:00
static defaultProps = { location: { pathname: `/` } }
2018-11-08 10:44:46 +01:00
render() {
2018-11-23 13:49:53 +01:00
const { sidebar, location, toc, tableOfContents } = this.props
2018-11-23 13:49:53 +01:00
const sidebarfile = sidebar ? require(`../../data/sidebars/${sidebar}.yml`) : [] // eslint-disable-line
2018-11-08 10:44:46 +01:00
if (!sidebarfile) {
return null
}
return (
2018-11-08 13:21:39 +01:00
<nav className={styles.sidebar}>
2018-11-23 13:49:53 +01:00
{toc ? (
<div>
<h4 className={styles.groupTitle}>On this page</h4>
<div
className={styles.toc}
dangerouslySetInnerHTML={{
__html: tableOfContents
}}
2018-11-08 13:21:39 +01:00
/>
2018-11-08 10:44:46 +01:00
</div>
2018-11-23 13:49:53 +01:00
) : (
sidebarfile.map((group, i) => (
<div key={i}>
<h4 className={styles.groupTitle}>
{group.items[0].link ? (
<SidebarLink
link={group.items[0].link}
title={group.group}
linkClasses={styles.groupTitleLink}
/>
) : (
group.group
)}
</h4>
<SidebarList
key={i}
items={group.items}
location={location}
/>
</div>
))
)}
2018-11-08 10:44:46 +01:00
</nav>
)
}
}
SidebarLink.propTypes = {
link: PropTypes.string.isRequired,
title: PropTypes.string.isRequired,
linkClasses: PropTypes.string
}
SidebarList.propTypes = {
items: PropTypes.array.isRequired,
location: PropTypes.object.isRequired
}