Skip to content

Commit d1e819e

Browse files
committed
pure components for docs
1 parent 5e47b46 commit d1e819e

File tree

1 file changed

+26
-35
lines changed

1 file changed

+26
-35
lines changed

site/_core/DocsSidebar.js

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,62 +9,55 @@
99
var React = require('react');
1010
import { toSlug } from './Header';
1111

12-
// thisPageID is the id of the rendering page
12+
export default ({ site, page, firstURL }) =>
13+
<div className="nav-docs">
14+
{getCategories(site, page.dir, firstURL).map(category =>
15+
<SidebarForCategory pageID={page.id} category={category} key={category.name} />
16+
)}
17+
</div>
18+
19+
// pageID is the id of the rendering page
1320
// category is the category object to render a sidebar for
14-
function sidebarForCategory(thisPageID, category) {
15-
var listItems = [];
16-
for (var page of category.links) {
17-
21+
function SidebarForCategory({ pageID, category }) {
22+
const listItems = category.links.map(page => {
1823
const shouldOpenInNewWindow = page.url.slice(0, 4) === 'http';
1924
const target = shouldOpenInNewWindow ? '_blank' : null;
2025
const rel = shouldOpenInNewWindow ? 'noopener noreferrer' : null;
2126

22-
var marginLeft = page.indent ? 20 : 0;
23-
24-
// Sublinks to any page sub-parts
25-
var sublinkUL = page.sublinks &&
26-
<ul>{page.sublinks.split(',').map(sublink =>
27-
<li key={sublink}>
28-
<a target={target} rel={rel} href={page.url + '#' + toSlug(sublink)}>
29-
{sublink}
30-
</a>
31-
</li>
32-
)}</ul>;
33-
3427
// Link for the main page overall
35-
listItems.push(
28+
return (
3629
<li key={page.permalink}>
3730
<a
3831
target={target}
3932
rel={rel}
40-
style={{marginLeft: marginLeft}}
41-
className={page.id === thisPageID ? 'active' : ''}
33+
style={{ marginLeft: page.indent ? 20 : 0 }}
34+
className={page.id === pageID ? 'active' : null}
4235
href={page.url}>
4336
{page.sidebarTitle || page.title}
4437
</a>
45-
{sublinkUL}
38+
{page.sublinks && // Sublinks to any page sub-parts
39+
<ul>
40+
{page.sublinks.split(',').map(sublink =>
41+
<li key={sublink}>
42+
<a target={target} rel={rel} href={page.url + '#' + toSlug(sublink)}>
43+
{sublink}
44+
</a>
45+
</li>
46+
)}
47+
</ul>
48+
}
4649
</li>
4750
);
48-
}
51+
});
4952

5053
return (
51-
<div key={category.name}>
54+
<div>
5255
<h3>{category.name}</h3>
5356
<ul>{listItems}</ul>
5457
</div>
5558
);
5659
}
5760

58-
var DocsSidebar = React.createClass({
59-
render: function() {
60-
return <div className="nav-docs">
61-
{getCategories(this.props.site, this.props.page.dir, this.props.firstURL).map((category) =>
62-
sidebarForCategory(this.props.page.id, category)
63-
)}
64-
</div>;
65-
}
66-
});
67-
6861
// If firstURL is provided, it's the URL (starting with /) of the
6962
// first page to put on the sidebar.
7063
function getCategories(site, dir, firstURL) {
@@ -127,5 +120,3 @@ function getCategories(site, dir, firstURL) {
127120

128121
return categories;
129122
}
130-
131-
module.exports = DocsSidebar;

0 commit comments

Comments
 (0)