mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2025-01-05 12:57:33 +03:00
Make sure anchor headings are scrolled into view on page render.
This commit is contained in:
parent
1f6dbb8742
commit
3d3588455d
11
index.js
11
index.js
@ -3,6 +3,7 @@ const elmPagesVersion = require("./package.json").version;
|
||||
module.exports = function pagesInit(
|
||||
/** @type { { mainElmModule: { init: any } } } */ { mainElmModule }
|
||||
) {
|
||||
const initialLocationHash = document.location.hash.replace(/^#/, "");
|
||||
let prefetchedPages = [window.location.pathname];
|
||||
|
||||
document.addEventListener("DOMContentLoaded", function() {
|
||||
@ -40,10 +41,20 @@ module.exports = function pagesInit(
|
||||
});
|
||||
}
|
||||
|
||||
function loadNamedAnchor() {
|
||||
if (initialLocationHash !== "") {
|
||||
const namedAnchor = document.querySelector(
|
||||
`[name=${initialLocationHash}]`
|
||||
);
|
||||
namedAnchor && namedAnchor.scrollIntoView();
|
||||
}
|
||||
}
|
||||
|
||||
function observeFirstRender(
|
||||
/** @type {MutationRecord[]} */ mutationList,
|
||||
/** @type {MutationObserver} */ firstRenderObserver
|
||||
) {
|
||||
loadNamedAnchor();
|
||||
for (let mutation of mutationList) {
|
||||
if (mutation.type === "childList") {
|
||||
setupLinkPrefetchingHelp();
|
||||
|
Loading…
Reference in New Issue
Block a user