-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Changes from 1 commit
3c453f3
a800e84
55cb9b2
46ec7dc
21069f4
684082f
561ef58
1484d39
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
} | ||
} | ||
|
||
getChildContext() { | ||
return { | ||
|
@@ -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(' ')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's pull this into a utility function in 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 ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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(' ')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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('#', '') | ||
|
@@ -441,7 +441,7 @@ class ComponentExample extends Component { | |
} | ||
|
||
return ( | ||
<Visibility once={false} onTopPassed={this.handlePass} onTopPassedReverse={this.handlePass}> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
<Visibility once={false}> | ||
<Grid | ||
className='docs-example' | ||
id={this.anchorName} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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(' '))} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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} | ||
|
There was a problem hiding this comment.
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: