-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Possible issue with JS and ViewTransition #8482
Comments
@Alecyrus I tried the solution in #8466, but it didn't work in my case. <script is:inline>
// Use a global flag to track whether the code has been initialized
let initialized = false;
// Function to set up your event listeners and perform initial checks
function initialize() {
// Check theme and menu
themeCheck();
menuCheck();
// Attach event listeners
attachEvent('[data-aw-toggle-menu]', 'click', function (elem) {
elem.classList.toggle('expanded');
document.body.classList.toggle('overflow-hidden');
document.getElementById('header')?.classList.toggle('h-screen');
document.querySelector('#header nav')?.classList.toggle('hidden');
});
attachEvent('[data-aw-toggle-theme]', 'click', function () {
document.documentElement.classList.toggle('dark');
localStorage.theme = document.documentElement.classList.contains('dark') ? 'dark' : 'light';
});
// Set the initialized flag to true
initialized = true;
}
// Define the attachEvent function (same as before)
// Define the menuCheck function (same as before)
// Define the themeCheck function (same as before)
// Check theme and menu on window load
window.addEventListener("DOMContentLoaded", () => {
if (!initialized) {
initialize();
}
});
// Check theme and menu on after swap
document.addEventListener("astro:after-swap", () => {
if (!initialized) {
initialize();
}
});
</script> then my dark mode toggle will only work once, which is unexpected. |
@sparxastronomy you want to run your dark mode checks in |
@matthewp Having a global flag to check whether menuCheck has been executed before leaves the navigation menu open after page swap, and also shows the same error. |
@sparxastronomy yes, but you have it so that it only runs once. It needs to run after every page swap (the dark mode stuff does). |
#8603 will fix this. |
Astro Info
If this issue only occurs in one browser, which browser is a problem?
No response
Describe the Bug
I'm trying out the new ViewTransition update.
I've two basic JS function, one to toggle navbar menu and one to toggle themes.
The function
menuCheck()
checks if my navigation menu is open. I've wrapped the entire<nav><nav />
intransition:persist
so that on switching between pages, it is behaving as expected and thedocument.addEventListener("astro:after-swap"...)
is rechecking the menu and themes after the content swap.The nav bar menu is only activated on small devices.
Error:
On using the navbar to go to any of the pages throws a console error:
This error is not present for
themeCheck
. I've also checked that menuCheck is not defined anywhere else in my code.What's the expected result?
This error shouldn't have appeared, or it should have come for both menuCheck and themeCheck
Link to Minimal Reproducible Example
https://migrate-3--sparxastronomy.netlify.app/
Participation
The text was updated successfully, but these errors were encountered: