Browse Source

Scroll to top on route change

master
meriadec 7 years ago
parent
commit
16270c2ab1
No known key found for this signature in database GPG Key ID: 1D2FC2305E2CB399
  1. 20
      src/components/layout/Default.js

20
src/components/layout/Default.js

@ -1,9 +1,11 @@
// @flow // @flow
import React, { Fragment, Component } from 'react' import React, { Fragment, Component } from 'react'
import { compose } from 'redux'
import styled from 'styled-components' import styled from 'styled-components'
import { Route } from 'react-router' import { Route, withRouter } from 'react-router'
import { translate } from 'react-i18next' import { translate } from 'react-i18next'
import type { Location } from 'react-router'
import * as modals from 'components/modals' import * as modals from 'components/modals'
import Box from 'components/base/Box' import Box from 'components/base/Box'
@ -25,11 +27,21 @@ const Container = styled(GrowScroll).attrs({
padding-top: ${p => p.theme.sizes.topBarHeight + p.theme.space[7]}px; padding-top: ${p => p.theme.sizes.topBarHeight + p.theme.space[7]}px;
` `
class Default extends Component<{}> { type Props = {
location: Location,
}
class Default extends Component<Props> {
componentDidMount() { componentDidMount() {
window.requestAnimationFrame(() => (this._timeout = setTimeout(() => window.onAppReady(), 300))) window.requestAnimationFrame(() => (this._timeout = setTimeout(() => window.onAppReady(), 300)))
} }
componentDidUpdate(prevProps) {
if (this.props.location !== prevProps.location) {
this._scrollContainer._scrollbar.scrollTo(0, 0)
}
}
componentWillUnmount() { componentWillUnmount() {
clearTimeout(this._timeout) clearTimeout(this._timeout)
} }
@ -52,7 +64,7 @@ class Default extends Component<{}> {
<Box shrink grow bg="cream" color="grey" relative> <Box shrink grow bg="cream" color="grey" relative>
<TopBar /> <TopBar />
<UpdateNotifier /> <UpdateNotifier />
<Container> <Container innerRef={n => (this._scrollContainer = n)}>
<Route path="/" exact component={DashboardPage} /> <Route path="/" exact component={DashboardPage} />
<Route path="/settings" component={SettingsPage} /> <Route path="/settings" component={SettingsPage} />
<Route path="/account/:id" component={AccountPage} /> <Route path="/account/:id" component={AccountPage} />
@ -65,4 +77,4 @@ class Default extends Component<{}> {
} }
} }
export default translate()(Default) export default compose(withRouter, translate())(Default)

Loading…
Cancel
Save