Created
February 27, 2020 15:37
-
-
Save kieran/cd0a7625a7f24197a24b87dab8bb9792 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import debounce from 'underscore-es/debounce' | |
# fixes @reach/router scrolling issue | |
update_scroll_position = debounce -> | |
history.replaceState { history.state..., scrollY }, document.title | |
, 100 | |
export default \ | |
class Layout extends React.Component | |
componentDidMount: -> | |
addEventListener 'scroll', update_scroll_position | |
if y = history.state?.scrollY | |
scrollTo 0, y | |
else | |
scrollTo 0, 0 unless document.querySelector location.hash or null | |
componentWillUnmount: -> | |
removeEventListener 'scroll', update_scroll_position | |
render: -> | |
<> | |
{@props.children} | |
</> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment