Skip to content

Commit

Permalink
fix: scroll to anchor if url has hash
Browse files Browse the repository at this point in the history
add a function in AsyncComponent to perform scrollIntoView in cDM
  • Loading branch information
yaodingyd authored and pedronauck committed Jan 21, 2019
1 parent b3a6094 commit 389577f
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions core/docz/src/components/AsyncComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class AsyncComponent extends Component<Props, State> {

public componentDidMount(): void {
this.fetch()
this.scrollToAnchor()
}

public render(): ReactNode {
Expand Down Expand Up @@ -54,4 +55,16 @@ export class AsyncComponent extends Component<Props, State> {
}
}
}

private scrollToAnchor(): void {
setTimeout(() => {
if (location.hash) {
const id: string = location.hash.substring(1)
const el: HTMLElement | null = document.getElementById(id)
if (el) {
el.scrollIntoView()
}
}
}, 0)
}
}

0 comments on commit 389577f

Please sign in to comment.