fix(docz): restore scroll on history change

This commit is contained in:
Pedro Nauck
2018-09-15 00:48:13 -03:00
parent 94b4467113
commit 905010e90f
2 changed files with 25 additions and 4 deletions

View File

@@ -5,9 +5,10 @@ import { ComponentType as CT } from 'react'
import { HashRouter, BrowserRouter } from 'react-router-dom'
import createContext from 'create-react-context'
import { state, State, ThemeConfig, ImportMap } from './state'
import { ErrorBoundary } from './components/ErrorBoundary'
import { DataServer } from './components/DataServer'
import { state, State, ThemeConfig, ImportMap } from './state'
import { ScrollToTop } from './utils/ScrollToTop'
declare var BASE_URL: string
@@ -49,9 +50,11 @@ export function theme(
<state.Provider initialState={props.db}>
<DataServer websocketUrl={props.websocketUrl}>
<Router basename={BASE_URL}>
<Wrapper>
<WrappedComponent />
</Wrapper>
<ScrollToTop>
<Wrapper>
<WrappedComponent />
</Wrapper>
</ScrollToTop>
</Router>
</DataServer>
</state.Provider>

View File

@@ -0,0 +1,18 @@
import { ReactNode, Component } from 'react'
import { withRouter, RouteComponentProps } from 'react-router'
type ScrollToTopProps = RouteComponentProps<any>
class ScrollToTopBase extends Component<ScrollToTopProps> {
public componentDidUpdate(prevProps: ScrollToTopProps): void {
if (this.props.location !== prevProps.location) {
window.scrollTo(0, 0)
}
}
public render(): ReactNode {
return this.props.children
}
}
export const ScrollToTop = withRouter(ScrollToTopBase)