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

fix(docs): Fixing doc navigation link issues #2760

Merged
merged 8 commits into from
May 19, 2018
Merged
32 changes: 27 additions & 5 deletions docs/app/Components/ComponentDoc/ComponentDoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,16 @@ class ComponentDoc extends Component {
).isRequired,
suiLink: PropTypes.string,
}

state = {}
constructor(props) {
super(props)
let activePath
if (location.hash) {
activePath = _.last((location.hash || '').split('#'))
}
this.state = {
activePath,
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Prefer property initializer for state unless props are needed to calculate state:

state = {
  activePath: _.last((location.hash || '').split('#'))
}


getChildContext() {
return {
Expand All @@ -68,14 +76,28 @@ class ComponentDoc extends Component {

handleSidebarItemClick = (e, { path }) => {
const { history } = this.props
const aPath = _.kebabCase(_.last(path.split('/')))
const aPath = _.kebabCase(path.split('/').join(' '))
Copy link
Member

Choose a reason for hiding this comment

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

Let's pull this into a utility function in docs/apps/utils. Something like createComponentHash(). It would be ideal to be able to pass an example filename or file path to a single function that knows how to create the hash. This way it can be used everywhere we are constructing the hash.

Also, we need to add redirects from the old hashes to the new hashes. A utility function could be used for transforming the old hashes to the new hashes as well.


history.replace(`${location.pathname}#${aPath}`)
scrollToAnchor()
// set active hash path
this.setState(
{
activePath: aPath,
},
scrollToAnchor,
)
}

render() {
const { componentGroup, componentName, description, ghLink, path, seeItems, suiLink } = this.props
const {
componentGroup,
componentName,
description,
ghLink,
path,
seeItems,
suiLink,
} = this.props
const { activePath, examplesRef } = this.state

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class ComponentExample extends Component {
const { examplePath, location } = this.props
const sourceCode = this.getOriginalSourceCode()

this.anchorName = _.kebabCase(_.last(examplePath.split('/')))
this.anchorName = _.kebabCase(examplePath.split('/').join(' '))
Copy link
Member

Choose a reason for hiding this comment

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

This would be the utility function as well.


// show code for direct links to examples
const showCode = this.anchorName === location.hash.replace('#', '')
Expand Down Expand Up @@ -441,7 +441,7 @@ class ComponentExample extends Component {
}

return (
<Visibility once={false} onTopPassed={this.handlePass} onTopPassedReverse={this.handlePass}>
Copy link
Member

Choose a reason for hiding this comment

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

The right menu no longer updates as the user scrolls. These handlers enabled that functionality.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh! I didn't check that. Thank you for pointing it out. I'll look into it.
And, all your comments make sense to me, I'll fix them.

<Visibility once={false}>
<Grid
className='docs-example'
id={this.anchorName}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,25 @@ import ComponentSidebarItem from './ComponentSidebarItem'
class ComponentSidebarSection extends Component {
static propTypes = {
activePath: PropTypes.string,
examples: PropTypes.arrayOf(PropTypes.shape({
title: PropTypes.string,
path: PropTypes.string,
})),
examples: PropTypes.arrayOf(
PropTypes.shape({
title: PropTypes.string,
path: PropTypes.string,
}),
),
name: PropTypes.string,
onItemClick: PropTypes.func,
onTitleClick: PropTypes.func,
}

state = {}
constructor(props) {
super(props)
this.state = {
isActiveByProps: this.isActiveAccordion(),
}
}

componentWillReceiveProps(nextProps) {
const { activePath, examples } = nextProps
const isActiveByProps = !!_.find(examples, { path: activePath })

const isActiveByProps = this.isActiveAccordion(nextProps)
const didCloseByProps = this.state.isActiveByProps && !isActiveByProps

// We allow the user to open accordions, but we close them when we scroll passed them
Expand All @@ -35,7 +39,13 @@ class ComponentSidebarSection extends Component {

handleItemClick = (e, itemProps) => _.invoke(this.props, 'onItemClick', e, itemProps)

handleTitleClick = () => this.setState(prevState => ({ isActiveByUser: !prevState.isActiveByUser }))
handleTitleClick = () =>
this.setState(prevState => ({ isActiveByUser: !prevState.isActiveByUser }))

isActiveAccordion = (props = this.props) =>
(props.examples || []).findIndex(
item => _.kebabCase(item.path.split('/').join(' ')) === props.activePath,
Copy link
Member

Choose a reason for hiding this comment

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

The utility function would be used here.

) !== -1

render() {
const { activePath, examples, name } = this.props
Expand All @@ -52,7 +62,7 @@ class ComponentSidebarSection extends Component {
<Accordion.Content as={Menu.Menu} active={active}>
{_.map(examples, ({ title, path }) => (
<ComponentSidebarItem
active={activePath === path}
active={activePath === _.kebabCase(path.split('/').join(' '))}
Copy link
Member

Choose a reason for hiding this comment

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

The utility function would be used here as well.

key={path}
onClick={this.handleItemClick}
path={path}
Expand Down