Skip to content

Commit

Permalink
fix: focus body after navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re committed Mar 17, 2023
1 parent e46b663 commit 410d14c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/brave-maps-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"micromorph": patch
---

Focus body after navigation
3 changes: 3 additions & 0 deletions src/nav.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export default function createRouter(opts: Options = {}) {
await micromorph(document, html);
if (opts.scrollToTop ?? true) {
window.scrollTo({ top: 0 });
if (!document.activeElement?.closest('[data-persist]')) {
document.body.focus();
}
}
await afterDiff();
delete announcer.dataset.persist;
Expand Down
5 changes: 4 additions & 1 deletion src/spa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ async function navigate(url: URL, isBack: boolean = false, opts: Options) {
if (document.startViewTransition) {
await document.startViewTransition(() => micromorph(document, html))
} else {

micromorph(document, html);
}
if (!document.activeElement?.closest('[data-persist]')) {
document.body.focus();
}
await afterDiff();
delete announcer.dataset.persist;
Expand Down

0 comments on commit 410d14c

Please sign in to comment.