Skip to content

Commit

Permalink
fix: remove logs, add scrollToTop support
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re committed Aug 1, 2022
1 parent 28445b9 commit f472e4f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/gentle-waves-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"micromorph": patch
---

remove logs, support scrollToTop option
11 changes: 10 additions & 1 deletion src/nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,28 @@ export default function listen(opts: Options = {}) {
}
const src = new URL(location.toString());
const dest = new URL(e.destination.url);
console.log(e, { src, dest });
if (src.toString() === dest.toString()) {
if (opts.scrollToTop ?? true) {
window.scrollTo({ top: 0 });
}
return;
}
const { beforeDiff = noop, afterDiff = noop } = opts;
p = p || new DOMParser();
async function navigate() {
const contents = await fetch(dest.toString()).then((res) => res.text());
const html = p.parseFromString(contents, "text/html");
normalizeRelativeURLs(html, dest);
beforeDiff(html);

const title = html.querySelector("title");
if (title) {
document.title = title.text;
}
await micromorph(document, html);
if (opts.scrollToTop ?? true) {
window.scrollTo({ top: 0 });
}
afterDiff();
}
e.transitionWhile(navigate())
Expand Down

0 comments on commit f472e4f

Please sign in to comment.