Hotfix for smooth scroll not working on anchors — https://github.com/hotwired/turbo/issues/1255

This commit is contained in:
Kuba Orlik 2026-08-11 14:38:42 +02:00
parent 1105cc947f
commit a17056fb17
2 changed files with 31 additions and 0 deletions

View File

@ -42,6 +42,7 @@
background: none; background: none;
border: none; border: none;
cursor: pointer; cursor: pointer;
svg { svg {
height: 16px; height: 16px;
width: 16px; width: 16px;

View File

@ -31,3 +31,33 @@ export * from "./controllers.js";
shouldPreserveScroll = false; shouldPreserveScroll = false;
}); });
})(); })();
/* fix for https://github.com/hotwired/turbo/issues/1255 */
(() => {
const markHashLinks = (root: ParentNode): void => {
root.querySelectorAll<HTMLAnchorElement>('a[href^="#"]').forEach((link) => {
link.dataset.turbo = "false";
});
};
markHashLinks(document);
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (!(node instanceof Element)) return;
if (node instanceof HTMLAnchorElement && node.matches('a[href^="#"]')) {
node.dataset.turbo = "false";
}
markHashLinks(node);
});
});
});
observer.observe(document.documentElement, {
childList: true,
subtree: true,
});
})();