import React from 'react'
import { withRouter } from 'react-router-dom'
import RAF from 'raf'
//
import { unwrapArray } from '../../utils/shared'
import scrollTo from '../../utils/scrollTo'

const RouterScroller = withRouter(
  class RouterScroller extends React.Component {
    componentDidMount () {
      this.scrollToHash()
    }
    componentDidUpdate (prev) {
      if (prev.location.pathname !== this.props.location.pathname && !this.props.location.hash) {
        if (window.__noScrollTo) {
          window.__noScrollTo = false
          return
        }
        this.scrollToTop()
        return
      }
      if (prev.location.hash !== this.props.location.hash) {
        this.scrollToHash()
      }
    }
    scrollToTop = () => {
      const { autoScrollToTop, scrollToTopDuration } = this.props
      if (autoScrollToTop) {
        scrollTo(0, {
          duration: scrollToTopDuration,
        })
      }
    }
    scrollToHash = () => {
      const {
        scrollToHashDuration,
        autoScrollToHash,
        scrollToHashOffset,
        location: { hash },
      } = this.props
      if (!autoScrollToHash) {
        return
      }
      RAF(() => {
        if (hash) {
          const resolvedHash = hash.substring(1)
          if (resolvedHash) {
            const element = document.getElementById(resolvedHash)
            if (element !== null) {
              scrollTo(element, {
                duration: scrollToHashDuration,
                offset: scrollToHashOffset,
              })
            }
          }
        } else {
          scrollTo(0, {
            duration: scrollToHashDuration,
          })
        }
      })
    }
    render () {
      return unwrapArray(this.props.children)
    }
  }
)

export default RouterScroller

@@ -1151,28 +1151,8 @@
   %7D%0A
-      RAF(() =%3E %7B%0A  
     
@@ -1161,26 +1161,24 @@
 if (hash) %7B%0A
-  
         cons
@@ -1216,26 +1216,24 @@
 (1)%0A        
-  
 if (resolved
@@ -1238,24 +1238,115 @@
 edHash) %7B%0A  
+        // We must attempt to scroll synchronously or we risk the browser scrolling for us%0A
           co
@@ -1403,26 +1403,24 @@
 )%0A          
-  
 if (element 
@@ -1435,34 +1435,32 @@
 ) %7B%0A            
- 
- 
 scrollTo(element
@@ -1459,26 +1459,24 @@
 (element, %7B%0A
-  
             
@@ -1523,18 +1523,16 @@
         
- 
- 
 offset: 
@@ -1567,56 +1567,166 @@
     
-  %7D)%0A            %7D%0A          %7D%0A        %7D else %7B%0A
+%7D)%0A          %7D else %7B%0A            RAF(() =%3E %7B%0A              const element = document.getElementById(resolvedHash)%0A              if (element !== null) %7B%0A      
     
@@ -1732,37 +1732,49 @@
        scrollTo(
-0, %7B%0A
+element, %7B%0A      
             dura
@@ -1793,32 +1793,84 @@
 ToHashDuration,%0A
+                  offset: scrollToHashOffset,%0A      
           %7D)%0A   
@@ -1878,18 +1878,150 @@
     
+  
-%7D%0A      %7D)
+    %7D%0A            %7D)%0A          %7D%0A        %7D%0A      %7D else %7B%0A        scrollTo(0, %7B%0A          duration: scrollToHashDuration,%0A        %7D)%0A      %7D
 %0A   

Fix corner case of hash scroll not happening (#462)