Skip to content

Commit 2a4f0f7

Browse files
committed
feat(site) Fixed Sidebar
1 parent 0284cf2 commit 2a4f0f7

File tree

6 files changed

+24
-91
lines changed

6 files changed

+24
-91
lines changed

DISCUSSION.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ this branch:
1010

1111
- [ ] Finish porting the markdown process to `remark` (may require new plugins)
1212
- [ ] Rethink external population process
13-
- [ ] Extract anchors into `_content.json` via `DirectoryTreePlugin`
13+
- [x] Extract anchors into `_content.json` via `DirectoryTreePlugin` (Fernando)
1414
- [ ] Finish re-incorporating mobile sidebar
1515
- [ ] Re-integrate google-analytics
1616
- [ ] Re-incorporate `redirects.json`
17-
- [x] Populate page title in `server.jsx` (fernando)
17+
- [x] Populate page title in `server.jsx` (Fernando)
1818
- [ ] Finish `Navigation` component (greg)
1919
- [x] Add custom route for landing page (greg)
20+
- [x] Fixed sidebar (Fernando)
2021

2122
Some of these should be fairly quick adds now that the site works. The two
2223
toughest ones are most likely the markdown parsing and external population. The

src/components/Sidebar/Sidebar.jsx

Lines changed: 10 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -4,104 +4,37 @@ import SidebarItem from '../SidebarItem/SidebarItem';
44
import '../Sidebar/Sidebar.scss';
55

66
export default class Sidebar extends Component {
7-
state = {
8-
fixed: false,
9-
availableHeight: null,
10-
maxWidth: null
11-
};
12-
137
render() {
148
let { className = '', pages, currentPage } = this.props;
15-
let { fixed, availableHeight, maxWidth } = this.state;
169
let group;
1710

18-
console.log(pages);
19-
2011
return (
21-
<nav
22-
className={ `sidebar ${className}` }
23-
ref={ ref => this._container = ref }
24-
style={{
25-
position: fixed ? 'fixed' : null,
26-
top: fixed ? 0 : null,
27-
width: fixed ? maxWidth : null,
28-
maxHeight: availableHeight
29-
}}>
30-
12+
<nav className={`sidebar ${className}`}>
3113
<div className="sidebar__inner">
3214
<a href="https://github.com/webpack/webpack/releases">
3315
<Shield content="npm/v/webpack" label="webpack" />
3416
</a>
3517

36-
{ pages.map((page, index) => {
18+
{pages.map((page, index) => {
3719
let displayGroup = group !== page.group && page.group !== '-';
3820
group = page.group;
3921

4022
return (
41-
<div key={ `sidebar-item-${index}` }>
42-
{ displayGroup ? (
43-
<h4 className="sidebar__group">
44-
{ group }
45-
</h4>
46-
) : null }
23+
<div key={`sidebar-item-${index}`}>
24+
{displayGroup ? <h4 className="sidebar__group">{group}</h4> : null}
4725

4826
<SidebarItem
49-
index={ index }
50-
url={ page.url }
51-
title={ page.title }
52-
anchors={ page.anchors }
53-
currentPage= { currentPage }
54-
onToggle={ this._recalculate.bind(this) } />
27+
index={index}
28+
url={page.url}
29+
title={page.title}
30+
anchors={page.anchors}
31+
currentPage={currentPage}
32+
/>
5533
</div>
5634
);
5735
})}
5836
</div>
59-
6037
</nav>
6138
);
6239
}
63-
64-
componentDidMount() {
65-
// TODO: Try using `position: sticky` instead
66-
// setTimeout(
67-
// this._recalculate.bind(this),
68-
// 250
69-
// );
70-
71-
// document.addEventListener(
72-
// 'scroll',
73-
// this._recalculate.bind(this)
74-
// );
75-
}
76-
77-
componentWillUnmount() {
78-
document.removeEventListener(
79-
'scroll',
80-
this._recalculate.bind(this)
81-
);
82-
}
83-
84-
/**
85-
* Re-calculate fixed state and position
86-
*
87-
*/
88-
_recalculate() {
89-
let { scrollY, innerHeight } = window;
90-
let { scrollHeight } = document.body;
91-
let { offsetHeight: sidebarHeight } = this._container;
92-
let { offsetWidth: parentWidth, offsetHeight: parentHeight } = this._container.parentNode;
93-
let headerHeight = document.querySelector('header').offsetHeight + document.querySelector('.notification-bar').offsetHeight;
94-
let footerHeight = document.querySelector('footer').offsetHeight;
95-
let distToBottom = scrollHeight - scrollY - innerHeight;
96-
97-
// Calculate the space that the footer and header are actually occupying
98-
let headerSpace = scrollY > headerHeight ? 0 : headerHeight - scrollY;
99-
let footerSpace = distToBottom > footerHeight ? 0 : footerHeight - distToBottom;
100-
101-
this.setState({
102-
fixed: scrollY >= headerHeight && sidebarHeight < parentHeight,
103-
availableHeight: innerHeight - headerSpace - footerSpace,
104-
maxWidth: parentWidth
105-
});
106-
}
10740
}

src/components/Sidebar/Sidebar.scss

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
display: none;
77
width: 100%;
88
max-height: 100%;
9-
overflow: auto;
109
will-change: transform;
1110

1211
@include break {
@@ -16,6 +15,8 @@
1615

1716
.sidebar__inner {
1817
padding: 1.5em;
18+
position: sticky;
19+
top: -1px;
1920
overflow: hidden;
2021
}
2122

src/components/SidebarItem/SidebarItem.jsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,8 @@ export default class SidebarItem extends React.Component {
7373
* @param {object} e - Click event
7474
*/
7575
_toggle(e) {
76-
let { onToggle } = this.props;
77-
7876
this.setState({
7977
open: !this.state.open
80-
}, () => {
81-
if (typeof onToggle === 'function') {
82-
onToggle();
83-
}
8478
});
8579
}
8680

md.js renamed to src/utilities/remark.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ var markdown = require('remark');
22
var slug = require('remark-slug');
33
var visit = require('unist-util-visit');
44

5-
module.exports = function getAnchors(content) {
5+
function getAnchors(content) {
66
let anchors = [];
77

88
markdown()
99
.use(slug)
10-
.use(pl)
10+
.use(headings)
1111
.process(content, (err, file) => {
1212
if (err) {
1313
throw err;
1414
}
1515
});
1616

17-
function pl() {
17+
function headings() {
1818
return function transformer(ast) {
1919
visit(ast, 'heading', visitor);
2020
};
@@ -28,4 +28,8 @@ module.exports = function getAnchors(content) {
2828
}
2929

3030
return anchors;
31-
};
31+
}
32+
33+
module.exports = {
34+
getAnchors
35+
}

webpack.common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const fs = require('fs');
22
const path = require('path');
33
const webpack = require('webpack');
44
const FrontMatter = require('front-matter');
5-
const getAnchors = require('./md');
5+
const { getAnchors } = require('./src/utilities/remark');
66
const CleanPlugin = require('clean-webpack-plugin');
77
const CopyWebpackPlugin = require('copy-webpack-plugin');
88
const ExtractTextPlugin = require('extract-text-webpack-plugin');

0 commit comments

Comments
 (0)