Skip to content

Instantly share code, notes, and snippets.

@robclancy
Last active December 8, 2020 14:43
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 robclancy/be27cdbd0f4d7c10999191a1858c0776 to your computer and use it in GitHub Desktop.
Save robclancy/be27cdbd0f4d7c10999191a1858c0776 to your computer and use it in GitHub Desktop.
route-scroll.js
import Route from '@ember/routing/route';
import { next } from '@ember/runloop';
import { scrollToTarget } from 'postedin/helpers/scroll-to-target';
export function initialize() {
Route.reopen({
activate() {
this._super();
window.scrollTo(0, 0);
next(() => {
let hash = window.location.hash;
if (! hash) {
// esa = `ember sucks anchor` because you can't link to anchors in embers link-to so we have to use it to hack around
const esa = new URLSearchParams(window.location.search).get('esa');
if (esa) {
hash = `#${esa}`;
// now remove the hack and add the hash
this.controllerFor('application').set('esa');
// for some reason it runs this twice and if I do it in current runloop it doesn't apply at all,
// probably because of the query param change still applying on the controller
next(() => {
if (! window.location.hash) {
history.replaceState({}, '', `${window.location.href}${hash}`);
}
});
}
}
scrollToTarget(hash);
});
},
});
}
export default {
initialize,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment