Skip to content

Commit 1369261

Browse files
authored
Cleaned up Sidebar code a little (#10841)
1 parent 4455c3d commit 1369261

File tree

12 files changed

+187
-131
lines changed

12 files changed

+187
-131
lines changed

www/src/components/MarkdownPage/MarkdownPage.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {urlRoot} from 'constants';
2424

2525
const MarkdownPage = ({
2626
authors,
27+
createLink,
2728
date,
2829
ogDescription,
2930
location,
@@ -93,6 +94,7 @@ const MarkdownPage = ({
9394

9495
<div css={sharedStyles.articleLayout.sidebar}>
9596
<StickyResponsiveSidebar
97+
createLink={createLink}
9698
defaultActiveSection={findSectionForPath(
9799
location.pathname,
98100
sectionList,
@@ -123,6 +125,7 @@ MarkdownPage.defaultProps = {
123125
// TODO Better types
124126
MarkdownPage.propTypes = {
125127
authors: PropTypes.array.isRequired,
128+
createLink: PropTypes.func.isRequired,
126129
date: PropTypes.string,
127130
location: PropTypes.object.isRequired,
128131
markdownRemark: PropTypes.object.isRequired,

www/src/pages/docs/error-decoder.html.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import MarkdownHeader from 'components/MarkdownHeader';
1717
import React from 'react';
1818
import StickyResponsiveSidebar from 'components/StickyResponsiveSidebar';
1919
import {colors, sharedStyles} from 'theme';
20+
import {createLinkDocs} from 'utils/createLink';
2021
import findSectionForPath from 'utils/findSectionForPath';
21-
22-
import sectionList from '../../../../docs/_data/nav_docs.yml';
22+
import {sectionListDocs} from 'utils/sectionList';
2323

2424
const ErrorPage = ({data, location}) => (
2525
<Flex
@@ -76,12 +76,13 @@ const ErrorPage = ({data, location}) => (
7676

7777
<div css={sharedStyles.articleLayout.sidebar}>
7878
<StickyResponsiveSidebar
79+
createLink={createLinkDocs}
7980
defaultActiveSection={findSectionForPath(
8081
location.pathname,
81-
sectionList,
82+
sectionListDocs,
8283
)}
8384
location={location}
84-
sectionList={sectionList}
85+
sectionList={sectionListDocs}
8586
title={data.markdownRemark.frontmatter.title}
8687
/>
8788
</div>

www/src/templates/blog.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99

1010
'use strict';
1111

12-
import MarkdownPage from 'components/MarkdownPage';
1312
import PropTypes from 'prop-types';
1413
import React from 'react';
14+
import MarkdownPage from 'components/MarkdownPage';
15+
import {createLinkBlog} from 'utils/createLink';
1516

16-
// TODO This is hacky
1717
const toSectionList = allMarkdownRemark => [
1818
{
1919
title: 'Recent Posts',
@@ -32,6 +32,7 @@ const toSectionList = allMarkdownRemark => [
3232
const Blog = ({data, location}) => (
3333
<MarkdownPage
3434
authors={data.markdownRemark.frontmatter.author}
35+
createLink={createLinkBlog}
3536
date={data.markdownRemark.fields.date}
3637
location={location}
3738
ogDescription={data.markdownRemark.excerpt}

www/src/templates/community.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,15 @@
1212
import MarkdownPage from 'components/MarkdownPage';
1313
import PropTypes from 'prop-types';
1414
import React from 'react';
15-
16-
import sectionList from '../../../docs/_data/nav_community.yml';
17-
18-
sectionList.forEach(item => {
19-
item.directory = 'community';
20-
});
15+
import {createLinkCommunity} from 'utils/createLink';
16+
import {sectionListCommunity} from 'utils/sectionList';
2117

2218
const Community = ({data, location}) => (
2319
<MarkdownPage
20+
createLink={createLinkCommunity}
2421
location={location}
2522
markdownRemark={data.markdownRemark}
26-
sectionList={sectionList}
23+
sectionList={sectionListCommunity}
2724
titlePostfix=" - React"
2825
/>
2926
);

www/src/templates/components/Sidebar/Section.js

Lines changed: 4 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,19 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
* @emails react-core
8-
*/
8+
*/
99

1010
'use strict';
1111

12-
import isItemActive from 'utils/isItemActive';
13-
import Link from 'gatsby-link';
1412
import React from 'react';
15-
import slugify from 'utils/slugify';
1613
import {colors, media} from 'theme';
1714
import MetaTitle from '../MetaTitle';
18-
import ExternalLinkSvg from '../ExternalLinkSvg';
1915
import ChevronSvg from '../ChevronSvg';
2016

2117
// TODO Update isActive link as document scrolls past anchor tags
2218
// Maybe used 'hashchange' along with 'scroll' to set/update active links
2319

24-
const Section = ({isActive, location, onClick, section}) => (
20+
const Section = ({createLink, isActive, location, onClick, section}) => (
2521
<div>
2622
<MetaTitle
2723
onClick={onClick}
@@ -63,13 +59,13 @@ const Section = ({isActive, location, onClick, section}) => (
6359
css={{
6460
marginTop: 5,
6561
}}>
66-
{CreateLink(location, section, item)}
62+
{createLink(location, section, item)}
6763

6864
{item.subitems &&
6965
<ul css={{marginLeft: 20}}>
7066
{item.subitems.map(subitem => (
7167
<li key={subitem.id}>
72-
{CreateLink(location, section, subitem)}
68+
{createLink(location, section, subitem)}
7369
</li>
7470
))}
7571
</ul>}
@@ -79,76 +75,4 @@ const Section = ({isActive, location, onClick, section}) => (
7975
</div>
8076
);
8177

82-
const activeLinkCss = {
83-
fontWeight: 'bold',
84-
};
85-
86-
const activeLinkBefore = {
87-
width: 4,
88-
height: 25,
89-
borderLeft: `4px solid ${colors.brand}`,
90-
paddingLeft: 16,
91-
position: 'absolute',
92-
left: 0,
93-
marginTop: -3,
94-
95-
[media.greaterThan('largerSidebar')]: {
96-
left: 15,
97-
},
98-
};
99-
100-
const linkCss = {
101-
color: colors.text,
102-
display: 'inline-block',
103-
borderBottom: '1px solid transparent',
104-
transition: 'border 0.2s ease',
105-
marginTop: 5,
106-
107-
'&:hover': {
108-
color: colors.subtle,
109-
},
110-
};
111-
112-
const CreateLink = (location, section, item) => {
113-
const isActive = isItemActive(location, item);
114-
if (item.id.includes('.html')) {
115-
return (
116-
<Link css={[linkCss, isActive && activeLinkCss]} to={item.id}>
117-
{isActive && <span css={activeLinkBefore} />}
118-
{item.title}
119-
</Link>
120-
);
121-
} else if (item.forceInternal) {
122-
return (
123-
<Link css={[linkCss, isActive && activeLinkCss]} to={item.href}>
124-
{isActive && <span css={activeLinkBefore} />}
125-
{item.title}
126-
</Link>
127-
);
128-
} else if (item.href) {
129-
return (
130-
<a css={[linkCss]} href={item.href}>
131-
{item.title}
132-
<ExternalLinkSvg
133-
cssProps={{
134-
verticalAlign: -2,
135-
display: 'inline-block',
136-
marginLeft: 5,
137-
color: colors.subtle,
138-
}}
139-
/>
140-
</a>
141-
);
142-
} else {
143-
return (
144-
<Link
145-
css={[linkCss, isActive && activeLinkCss]}
146-
to={slugify(item.id, section.directory)}>
147-
{isActive && <span css={activeLinkBefore} />}
148-
{item.title}
149-
</Link>
150-
);
151-
}
152-
};
153-
15478
export default Section;

www/src/templates/components/Sidebar/Sidebar.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*
77
* @emails react-core
8-
*/
8+
*/
99

1010
'use strict';
1111

@@ -24,7 +24,7 @@ class Sidebar extends Component {
2424
}
2525

2626
render() {
27-
const {location, sectionList} = this.props;
27+
const {createLink, location, sectionList} = this.props;
2828
const {activeSection} = this.state;
2929

3030
return (
@@ -47,6 +47,7 @@ class Sidebar extends Component {
4747
}}>
4848
{sectionList.map((section, index) => (
4949
<Section
50+
createLink={createLink}
5051
isActive={activeSection === section || sectionList.length === 1}
5152
key={index}
5253
location={location}

www/src/templates/docs.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,15 @@
1212
import MarkdownPage from 'components/MarkdownPage';
1313
import PropTypes from 'prop-types';
1414
import React from 'react';
15-
16-
import sectionListA from '../../../docs/_data/nav_docs.yml';
17-
import sectionListB from '../../../docs/_data/nav_contributing.yml';
18-
19-
const sectionList = sectionListA
20-
.map(item => {
21-
item.directory = 'docs';
22-
return item;
23-
})
24-
.concat(
25-
sectionListB.map(item => {
26-
item.directory = 'contributing';
27-
return item;
28-
}),
29-
);
15+
import {createLinkDocs} from 'utils/createLink';
16+
import {sectionListDocs} from 'utils/sectionList';
3017

3118
const Docs = ({data, location}) => (
3219
<MarkdownPage
20+
createLink={createLinkDocs}
3321
location={location}
3422
markdownRemark={data.markdownRemark}
35-
sectionList={sectionList}
23+
sectionList={sectionListDocs}
3624
titlePostfix=" - React"
3725
/>
3826
);

www/src/templates/installation.js

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,12 @@ import PropTypes from 'prop-types';
1717
import React, {Component} from 'react';
1818
import StickyResponsiveSidebar from 'components/StickyResponsiveSidebar';
1919
import TitleAndMetaTags from 'components/TitleAndMetaTags';
20+
import {createLinkDocs} from 'utils/createLink';
2021
import findSectionForPath from 'utils/findSectionForPath';
22+
import {sectionListDocs} from 'utils/sectionList';
2123
import {sharedStyles} from 'theme';
2224
import {urlRoot} from 'constants';
2325

24-
import sectionListA from '../../../docs/_data/nav_docs.yml';
25-
import sectionListB from '../../../docs/_data/nav_contributing.yml';
26-
27-
const sectionList = sectionListA
28-
.map(item => {
29-
item.directory = 'docs';
30-
return item;
31-
})
32-
.concat(
33-
sectionListB.map(item => {
34-
item.directory = 'contributing';
35-
return item;
36-
}),
37-
);
38-
3926
// HACK: copied from 'installation.md'
4027
// TODO: clean this up.
4128
function setSelected(value) {
@@ -180,12 +167,13 @@ class InstallationPage extends Component {
180167

181168
<div css={sharedStyles.articleLayout.sidebar}>
182169
<StickyResponsiveSidebar
170+
createLink={createLinkDocs}
183171
defaultActiveSection={findSectionForPath(
184172
location.pathname,
185-
sectionList,
173+
sectionListDocs,
186174
)}
187175
location={location}
188-
sectionList={sectionList}
176+
sectionList={sectionListDocs}
189177
title="Installation"
190178
/>
191179
</div>

www/src/templates/tutorial.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
import MarkdownPage from 'components/MarkdownPage';
1313
import PropTypes from 'prop-types';
1414
import React from 'react';
15-
16-
import sectionList from '../../../docs/_data/nav_tutorial.yml';
15+
import {createLinkTutorial} from 'utils/createLink';
16+
import {sectionListTutorial} from 'utils/sectionList';
1717

1818
const Tutorial = ({data, location}) => (
1919
<MarkdownPage
20+
createLink={createLinkTutorial}
2021
location={location}
2122
markdownRemark={data.markdownRemark}
22-
sectionList={sectionList}
23+
sectionList={sectionListTutorial}
2324
titlePostfix=" - React"
2425
/>
2526
);

0 commit comments

Comments
 (0)