-
-
Save gajus/0bbc78135d88a02c18366f12237011a5 to your computer and use it in GitHub Desktop.
Making the anchor links work in SPA applications. https://medium.com/@gajus/making-the-anchor-links-work-in-spa-applications-618ba2c6954a
This file contains 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
// @flow | |
/** | |
* @param history {@see https://www.npmjs.com/package/history} | |
* @param timeout A number of milliseconds to wait for the element to appear after PUSH action has been received. | |
*/ | |
export default (history: *, timeout: number = 1000) => { | |
let observer; | |
let timeoutId; | |
if (!window.MutationObserver) { | |
return; | |
} | |
const reset = () => { | |
if (timeoutId) { | |
clearTimeout(timeoutId); | |
timeoutId = null; | |
} | |
if (observer) { | |
observer.disconnect(); | |
} | |
}; | |
const createScrollToElement = (id: string) => { | |
return () => { | |
const element = document.getElementById(id); | |
if (element) { | |
element.scrollIntoView(); | |
reset(); | |
return true; | |
} | |
return false; | |
}; | |
}; | |
history.listen((location: *, action: *) => { | |
if (timeoutId) { | |
reset(); | |
} | |
if (action !== 'PUSH') { | |
return; | |
} | |
if (typeof location.hash !== 'string') { | |
return; | |
} | |
const elementId = location.hash.slice(1); | |
if (!elementId) { | |
return; | |
} | |
const scrollToElement = createScrollToElement(elementId); | |
setTimeout(() => { | |
if (scrollToElement()) { | |
return; | |
} | |
observer = new MutationObserver(scrollToElement); | |
observer.observe(document, { | |
attributes: true, | |
childList: true, | |
subtree: true | |
}); | |
timeoutId = setTimeout(reset, timeout); | |
}); | |
}); | |
}; |
@aditya-padhi-kbl it's simple
import React from 'react';
import { useHistory } from 'react-router-dom';
import Adventure from './Adventure';
// I named it `useAnchors`
import { useAnchors } from './useAnchors';
export const App = withContainer(() => {
const history = useHistory();
useAnchors(history, 750);
return (
<Adventure/>
);
});
export default App;
Thankyou for sharing the details. It worked.
Kind Regards,
Aditya
Sent from Outlook Mobile<https://aka.ms/blhgte>
…________________________________
From: Patrick Michaelsen <notifications@github.com>
Sent: Wednesday, June 10, 2020 11:27:15 PM
To: gajus <gajus@noreply.github.com>
Cc: Aditya Padhi <aditya.padhi@outlook.com>; Mention <mention@noreply.github.com>
Subject: Re: gajus/createHistoryHashObserver.js
@prmichaelsen commented on this gist.
________________________________
This almost worked for me, it would scroll to correct location of my app but then scroll again just afterwards, leaving it just off by a bit.
I didn't understand what the code was doing and ended up re-writing it such that it doesn't even use history. It simply waits for the element to appear then scrolls to it. Perhaps it's not the best approach but it seems to work.
export const useAnchors = (timeout: number = 100) => {
const getElement = async (id: string): Promise<HTMLElement> => {
return new Promise((resolve) => {
const intervalId = setInterval(() => {
const el = document.getElementById(id)
if (el) {
clearInterval(intervalId);
resolve(el);
}
}, timeout);
});
}
async function scroll(location) {
if (typeof location.hash !== 'string') {
return;
}
const elementId = location.hash.slice(1)
if (!elementId) {
return;
}
const element = await getElement(elementId);
element.scrollIntoView();
}
requestAnimationFrame(() => {
scroll(location)
})
}
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<https://gist.github.com/0bbc78135d88a02c18366f12237011a5#gistcomment-3337408>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AGZ6AU5M4MNAQXAQUAHOL73RV7CPXANCNFSM4N2S2ZFA>.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice information. But can you please guide me how to implement this in a react app ?