mirror of
https://github.com/zhigang1992/docz.git
synced 2026-03-26 10:14:33 +08:00
fix(docz): restore scroll on history change
This commit is contained in:
@@ -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>
|
||||
|
||||
18
packages/docz/src/utils/ScrollToTop.tsx
Normal file
18
packages/docz/src/utils/ScrollToTop.tsx
Normal 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)
|
||||
Reference in New Issue
Block a user