Last active
December 8, 2020 14:43
-
-
Save robclancy/be27cdbd0f4d7c10999191a1858c0776 to your computer and use it in GitHub Desktop.
route-scroll.js
This file contains hidden or 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
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