Skip to content

Instantly share code, notes, and snippets.

@kieran
Created February 27, 2020 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kieran/cd0a7625a7f24197a24b87dab8bb9792 to your computer and use it in GitHub Desktop.
Save kieran/cd0a7625a7f24197a24b87dab8bb9792 to your computer and use it in GitHub Desktop.
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