Skip to content
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

Bug Dark Mode add <ClientRouter /> #12898

Closed
1 task
alisanoelia opened this issue Jan 5, 2025 · 1 comment
Closed
1 task

Bug Dark Mode add <ClientRouter /> #12898

alisanoelia opened this issue Jan 5, 2025 · 1 comment
Labels
needs triage Issue needs to be triaged

Comments

@alisanoelia
Copy link

alisanoelia commented Jan 5, 2025

Astro Info

Astro                    v5.1.2
Node                     v20.16.0
System                   Linux (x64)
Package Manager          bun
Output                   static
Adapter                  none
Integrations             @astrojs/tailwind

If this issue only occurs in one browser, which browser is a problem?

All browser

Describe the Bug

When I add <ClientRouter /> for animations, there are errors when loading the theme on different pages, and the button to change the theme is also disabled.

Everything works correctly if I don't use <ClientRouter />.

What's the expected result?

Animations without bugs

Link to Minimal Reproducible Example

https://stackblitz.com/~/github.com/alisanoelia/error-clientrouter

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Jan 5, 2025
@martrapp
Copy link
Member

martrapp commented Jan 6, 2025

Hi @alisanoelia 👋🏼,

without the client Router, the script inside the ToggleTheme component is executed on every full page load.
The ClientRouter replaces full page loads with soft navigation. To ensure that the script is still executed on each navigation, you have to wrap it in an astro:page-load listener like this

<script>
import "astro:transitions/client"
document.addEventListener("astro:page-load",()=>{
	document.getElementById('theme-selector')?.addEventListener('click', () => {
		if (localStorage.theme === 'dark') {
			localStorage.theme = 'light'
			document.documentElement.classList.remove('dark')
		} else {
			localStorage.theme = 'dark'
			document.documentElement.classList.add('dark')
		}
	})
})
</script>

https://docs.astro.build/en/guides/view-transitions/#astropage-load

The import statement at the start of the script is normally not necessary, but i added it due to open bug #12804.

For your other script in the Layout.astro file, you should be aware of
https://docs.astro.build/en/guides/view-transitions/#data-astro-rerun

@martrapp martrapp closed this as completed Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

2 participants