|
| 1 | +/* |
| 2 | + * This is copied from https://github.com/ncoughlin/scroll-to-hash-element/blob/main/src/index.tsx |
| 3 | + * Unfortunately this package does not contain typescript definitions, so we can not use it directly. |
| 4 | + */ |
| 5 | +import { useEffect, useLayoutEffect, useRef, useState } from 'react'; |
| 6 | + |
| 7 | +interface ScrollToHashElementProps { |
| 8 | + /** On first run do we jump or scroll? */ |
| 9 | + initialBehavior?: ScrollBehavior; |
| 10 | + behavior?: ScrollBehavior; |
| 11 | + inline?: ScrollLogicalPosition; |
| 12 | + block?: ScrollLogicalPosition; |
| 13 | +} |
| 14 | + |
| 15 | +export const ScrollToHashElement = ({ |
| 16 | + behavior = 'auto', |
| 17 | + initialBehavior = 'auto', |
| 18 | + inline = 'nearest', |
| 19 | + block = 'start', |
| 20 | +}: ScrollToHashElementProps): null => { |
| 21 | + const [hash, setHash] = useState(window.location.hash); |
| 22 | + const [count, setCount] = useState(0); |
| 23 | + const originalListeners = useRef<{ [key: string]: Function }>({}); |
| 24 | + |
| 25 | + // We need to know if this is the first run. If it is, we can do an instant jump, no scrolling. |
| 26 | + const [firstRun, setFirstRun] = useState(true); |
| 27 | + useEffect(() => setFirstRun(false), []); |
| 28 | + |
| 29 | + useEffect(() => { |
| 30 | + const handleLocationChange = () => { |
| 31 | + setHash(window.location.hash); |
| 32 | + |
| 33 | + // We increment count just so the layout effect will run if the hash is the same. |
| 34 | + // Otherwise the user might click a hashlink a second time and it won't go anywhere. |
| 35 | + setCount((count: number) => count + 1); |
| 36 | + }; |
| 37 | + |
| 38 | + const onPopState = () => { |
| 39 | + window.dispatchEvent(new Event('locationchange')); |
| 40 | + }; |
| 41 | + |
| 42 | + const addWindowListeners = () => { |
| 43 | + originalListeners.current.pushState = window.history.pushState; |
| 44 | + originalListeners.current.replaceState = window.history.replaceState; |
| 45 | + |
| 46 | + window.history.pushState = function (...args: any) { |
| 47 | + const result = originalListeners.current.pushState.apply(this, args); |
| 48 | + window.dispatchEvent(new Event('pushstate')); |
| 49 | + window.dispatchEvent(new Event('locationchange')); |
| 50 | + return result; |
| 51 | + }; |
| 52 | + |
| 53 | + window.history.replaceState = function (...args: any) { |
| 54 | + const result = originalListeners.current.replaceState.apply(this, args); |
| 55 | + window.dispatchEvent(new Event('replacestate')); |
| 56 | + window.dispatchEvent(new Event('locationchange')); |
| 57 | + return result; |
| 58 | + }; |
| 59 | + |
| 60 | + window.addEventListener('popstate', onPopState); |
| 61 | + window.addEventListener('locationchange', handleLocationChange); |
| 62 | + }; |
| 63 | + |
| 64 | + // Cleanup the event listeners on component unmount |
| 65 | + const removeWindowListeners = () => { |
| 66 | + window.history.pushState = originalListeners.current.pushState as typeof window.history.pushState; |
| 67 | + window.history.replaceState = originalListeners.current.replaceState as typeof window.history.replaceState; |
| 68 | + window.removeEventListener('popstate', onPopState); |
| 69 | + window.removeEventListener('locationchange', handleLocationChange); |
| 70 | + }; |
| 71 | + |
| 72 | + addWindowListeners(); |
| 73 | + return removeWindowListeners; |
| 74 | + }, []); |
| 75 | + |
| 76 | + useLayoutEffect(() => { |
| 77 | + const removeHashCharacter = (str: string) => { |
| 78 | + return str.slice(1); |
| 79 | + }; |
| 80 | + |
| 81 | + if (hash) { |
| 82 | + const element = document.getElementById(removeHashCharacter(hash)); |
| 83 | + |
| 84 | + if (element) { |
| 85 | + element.scrollIntoView({ |
| 86 | + behavior: firstRun ? initialBehavior : behavior, |
| 87 | + inline: inline, |
| 88 | + block: block, |
| 89 | + }); |
| 90 | + } |
| 91 | + } |
| 92 | + // eslint-disable-next-line react-hooks/exhaustive-deps |
| 93 | + }, [hash, count, firstRun]); |
| 94 | + |
| 95 | + return null; |
| 96 | +}; |
0 commit comments