Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: collapsible categories #1128

Merged
merged 13 commits into from
Jan 23, 2019
4 changes: 4 additions & 0 deletions docs/api-site-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ An option to disable showing the title in the header next to the header icon. Ex

An option to disable showing the tagline in the title of main pages. Exclude this field to keep page titles as `Title • Tagline`. Set to `true` to make page titles just `Title`.

#### `docsSideNavCollapsible` [boolean]

Set this to `true` if you want to be able to expand/collapse the links and subcategories in the sidebar.

#### `editUrl` [string]

URL for editing docs, usage example: `editUrl + 'en/doc1.md'`. If this field is omitted, there will be no "Edit this Doc" button for each document.
Expand Down
11 changes: 11 additions & 0 deletions docs/guides-navigation.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,14 @@ We support secondary on-page navigation so you can more easily see the topics as
```

Currently, `'separate'` is the only option available for this field. This provides a separate navigation on the right side of the page.

## Collapsible Categories

For sites with a sizable amount of content, we support the option to expand/collapse the links and subcategories under categories. To enable this feature, set the `docsSideNavCollapsible` site configuration [option](api-site-config.md#optional-fields) in `siteConfig.js` to true.

```js
{
docsSideNavCollapsible: true,
...
}
```
74 changes: 55 additions & 19 deletions v1/lib/core/nav/SideNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,38 @@ class SideNav extends React.Component {
return null;
}

renderCategory = categoryItem => (
<div className="navGroup" key={categoryItem.title}>
<h3 className="navGroupCategoryTitle">
{this.getLocalizedCategoryString(categoryItem.title)}
</h3>
<ul>
{categoryItem.children.map(item => {
switch (item.type) {
case 'LINK':
return this.renderItemLink(item);
case 'SUBCATEGORY':
return this.renderSubcategory(item);
default:
return null;
}
})}
</ul>
</div>
);
renderCategory = categoryItem => {
let ulClassName = '';
let categoryClassName = 'navGroupCategoryTitle';
let arrow;

if (siteConfig.docsSideNavCollapsible) {
categoryClassName += ' collapsible';
ulClassName = 'hide';
arrow = <span className="arrow">&#8963;</span>;
}

return (
<div className="navGroup" key={categoryItem.title}>
<h3 className={categoryClassName}>
{this.getLocalizedCategoryString(categoryItem.title)}
{arrow}
</h3>
<ul className={ulClassName}>
{categoryItem.children.map(item => {
switch (item.type) {
case 'LINK':
return this.renderItemLink(item);
case 'SUBCATEGORY':
return this.renderSubcategory(item);
default:
return null;
}
})}
</ul>
</div>
);
};

renderSubcategory = subcategoryItem => (
<div className="navGroup subNavGroup" key={subcategoryItem.title}>
Expand Down Expand Up @@ -133,6 +146,29 @@ class SideNav extends React.Component {
<script
dangerouslySetInnerHTML={{
__html: `
var coll = document.getElementsByClassName('collapsible');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heyhey, is there any particular reason you're using var in favor of const/let?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code within <script> is shipped as-is to browsers, and not all browsers understand let. We stick to ES5 in such situations.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx for the clarification!

var checkActiveCategory = true;
for (var i = 0; i < coll.length; i++) {
var links = coll[i].nextElementSibling.getElementsByTagName('*');
if (checkActiveCategory){
for (var j = 0; j < links.length; j++) {
if (links[j].classList.contains('navListItemActive')){
coll[i].nextElementSibling.classList.toggle('hide');
coll[i].childNodes[1].classList.toggle('rotate');
checkActiveCategory = false;
break;
}
}
}

coll[i].addEventListener('click', function() {
var arrow = this.childNodes[1];
arrow.classList.toggle('rotate');
var content = this.nextElementSibling;
content.classList.toggle('hide');
});
}

document.addEventListener('DOMContentLoaded', function() {
createToggler('#navToggler', '#docsNav', 'docsSliderActive');
createToggler('#tocToggler', 'body', 'tocActive');
Expand Down
16 changes: 16 additions & 0 deletions v1/lib/static/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -1595,6 +1595,22 @@ input::placeholder {
/* End of Docs Navigation */

/* Start of Docs Sidebar */
.hide {
display: none;
}

.collapsible .arrow {
cursor: pointer;
float: right;
margin-right: 5px;
transform: rotate(90deg);
transition: transform 200ms linear;
}

.collapsible .arrow.rotate {
transform: rotate(180deg);
}

@media only screen and (max-width: 1023px) {
.docsNavContainer {
background: #fff;
Expand Down
1 change: 1 addition & 0 deletions v1/website/siteConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const siteConfig = {
enableUpdateTime: true,
enableUpdateBy: true,
customDocsPath: '../docs',
docsSideNavCollapsible: true,
};

module.exports = siteConfig;