mirror of
https://github.com/dillonkearns/elm-pages-v3-beta.git
synced 2025-01-07 06:53:46 +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(
|
module.exports = function pagesInit(
|
||||||
/** @type { { mainElmModule: { init: any } } } */ { mainElmModule }
|
/** @type { { mainElmModule: { init: any } } } */ { mainElmModule }
|
||||||
) {
|
) {
|
||||||
|
const initialLocationHash = document.location.hash.replace(/^#/, "");
|
||||||
let prefetchedPages = [window.location.pathname];
|
let prefetchedPages = [window.location.pathname];
|
||||||
|
|
||||||
document.addEventListener("DOMContentLoaded", function() {
|
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(
|
function observeFirstRender(
|
||||||
/** @type {MutationRecord[]} */ mutationList,
|
/** @type {MutationRecord[]} */ mutationList,
|
||||||
/** @type {MutationObserver} */ firstRenderObserver
|
/** @type {MutationObserver} */ firstRenderObserver
|
||||||
) {
|
) {
|
||||||
|
loadNamedAnchor();
|
||||||
for (let mutation of mutationList) {
|
for (let mutation of mutationList) {
|
||||||
if (mutation.type === "childList") {
|
if (mutation.type === "childList") {
|
||||||
setupLinkPrefetchingHelp();
|
setupLinkPrefetchingHelp();
|
||||||
|
Loading…
Reference in New Issue
Block a user