Skip to content

Commit

Permalink
Improve <ReplacementSwap> documentation and optimize how old astro-ro…
Browse files Browse the repository at this point in the history
…ute-announcer are removed
  • Loading branch information
martrapp committed Dec 25, 2023
1 parent 6ab6f38 commit 0f1e1dd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
10 changes: 6 additions & 4 deletions components/ReplacementSwap.astro
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@
originalSwap();
return;
}
customSwap(event.newDocument, () =>
customSwap(event.newDocument, () => {
intersection.forEach((name) => {
const oldEl = oldEls.find((el) => (el as HTMLElement).dataset.vtbotReplace === name);
const newEl = newEls.find((el) => (el as HTMLElement).dataset.vtbotReplace === name);
if (oldEl && newEl) {
oldEl.replaceWith(newEl);
}
document.querySelector("[class*='astro-route-announcer']")?.remove();
})
);
});
[...document.querySelectorAll("[class*='astro-route-announcer']")].forEach((e) =>
e.remove()
);
});
};
}
});
Expand Down
7 changes: 7 additions & 0 deletions example/src/pages/library/ReplacementSwap.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,10 @@ The elements with the same identifier do not have to be the same type of HTML el

If the old page and the new page have no replacement identifiers in common, `<ReplacementSwap>` falls back to the normal swap behavior of astro view transitions.

## An important note on styling

The `<ReplacementSwap/>` component copies DOM trees from the new `<body>` into the current `<body>`. It also changes the `<head>` of the current page to reflect the `<head>` of the new page. This sounds good, but may have unexpected side effects:

* The part of the page that remains untouched could lose its styling if the necessary CSS is not included in the `<head>` of the new page or in the remaining DOM of the current page.
* In particular, if you use scoped styles in the untouched part of your page, they may not be provided by the new page.
* If the tree being copied uses inline styles, these may need to be copied explicitly using appropriate `data-vtbot-replace` attributes.

0 comments on commit 0f1e1dd

Please sign in to comment.