-
-
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); | |
}); | |
}); | |
}; |
Great snippet!
Btw I adjusted this a littlebit to our needs and just wanted to share my solution with you.
2 adjustments I made:
- Scroll Up when page changed - we have a long docs site where you don't want that effect. We do this "in the main area"
- Scroll to Hash immediately after the page loaded (
requetAnimationFrame
) - Conversion to untyped/basic Typescript
export default (history, timeout: number = 1000, mainSelector: string = 'right-side') => {
let observer
let timeoutId
if (!(window as any).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
}
}
function scroll(location) {
if (typeof location.hash !== 'string') {
return
}
const elementId = location.hash.slice(1)
if (!elementId) {
const contentArea = document.getElementById(mainSelector)
if (contentArea) {
contentArea.scrollTop = 0
}
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)
})
}
history.listen((location, action) => {
if (timeoutId) {
reset()
}
if (action !== 'PUSH') {
return
}
scroll(location)
})
requestAnimationFrame(() => {
scroll(location)
})
}
I'm getting window is not defined
error. Am I doing something wrong?
Nice information. But can you please guide me how to implement this in a react app ?
@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
Very nice piece of code. Seems much less hacky than the other solutions.
I had a problem though, that nothing was happening, because of following check against
action
I use your script like this
createHistoryHashObserver(browserHistory)
, but action is alwaysundefined
.location.action
is defined though. So following code works for meAnother problem is, that there is no scroll on page load. So if I go directly to a page with a hash it doesn't scroll to the expected element. If I introduce a flag for initial page load
let initialPageLoad = true;
});
Everything works as expected. The second part might not be necessary for server side rendering though. I didn't test that yet