Skip to content

Commit 6639684

Browse files
rfbottosapegin
authored andcommitted
Fix: Highlight currently selected page on sidebar (styleguidist#1195)
1 parent 3aa6ff1 commit 6639684

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

src/rsg-components/ComponentsList/ComponentsListRenderer.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
33
import cx from 'classnames';
44
import Link from 'rsg-components/Link';
55
import Styled from 'rsg-components/Styled';
6+
import { hasInHash, getHash } from '../../utils/handleHash';
67

78
const styles = ({ color, fontFamily, fontSize, space, mq }) => ({
89
list: {
@@ -31,6 +32,9 @@ const styles = ({ color, fontFamily, fontSize, space, mq }) => ({
3132
fontFamily: fontFamily.base,
3233
fontWeight: 'bold',
3334
},
35+
isSelected: {
36+
fontWeight: 'bold',
37+
},
3438
});
3539

3640
export function ComponentsListRenderer({ classes, items }) {
@@ -40,23 +44,31 @@ export function ComponentsListRenderer({ classes, items }) {
4044
return null;
4145
}
4246

47+
const windowHash = window.location.pathname + getHash(window.location.hash);
4348
return (
4449
<ul className={classes.list}>
45-
{items.map(({ heading, visibleName, href, content, external }) => (
46-
<li
47-
className={cx(classes.item, (!content || !content.props.items.length) && classes.isChild)}
48-
key={href}
49-
>
50-
<Link
51-
className={cx(heading && classes.heading)}
52-
href={href}
53-
target={external ? '_blank' : undefined}
50+
{items.map(({ heading, visibleName, href, content, external }) => {
51+
const isItemSelected = hasInHash(windowHash, href);
52+
return (
53+
<li
54+
className={cx(
55+
classes.item,
56+
(!content || !content.props.items.length) && classes.isChild,
57+
isItemSelected && classes.isSelected
58+
)}
59+
key={href}
5460
>
55-
{visibleName}
56-
</Link>
57-
{content}
58-
</li>
59-
))}
61+
<Link
62+
className={cx(heading && classes.heading)}
63+
href={href}
64+
target={external ? '_blank' : undefined}
65+
>
66+
{visibleName}
67+
</Link>
68+
{content}
69+
</li>
70+
);
71+
})}
6072
</ul>
6173
);
6274
}

0 commit comments

Comments
 (0)