Keep the dark theme across wire:navigate page changes

wire:navigate replaces the <html> attributes with the server's, which
dropped the "dark" class, and the unchanged head script does not run
again. Apply the stored theme again on every Livewire page swap, before
the new page paints, and cover the main-menu navigation with a browser
test.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-22 14:55:03 +02:00
co-authored by Claude Opus 5.5
parent 4b42fd4009
commit cfbabcd268
2 changed files with 29 additions and 4 deletions
+7
View File
@@ -15,13 +15,20 @@
tallstackui_darkTheme() keeps it in localStorage ("dark-theme"), tallstackui_darkTheme() keeps it in localStorage ("dark-theme"),
but only runs once Alpine starts, which would flash the light theme. but only runs once Alpine starts, which would flash the light theme.
Like the package, any value other than dark/light (true/false) means "system". Like the package, any value other than dark/light (true/false) means "system".
wire:navigate replaces the <html> attributes with the server's, which
drops the class, so it is applied again on every page swap.
--}} --}}
<script> <script>
(() => { (() => {
const applyStoredTheme = () => {
const stored = localStorage.getItem('dark-theme'); const stored = localStorage.getItem('dark-theme');
const dark = stored === 'dark' || stored === 'true' const dark = stored === 'dark' || stored === 'true'
|| (stored !== 'light' && stored !== 'false' && window.matchMedia('(prefers-color-scheme: dark)').matches); || (stored !== 'light' && stored !== 'false' && window.matchMedia('(prefers-color-scheme: dark)').matches);
document.documentElement.classList.toggle('dark', dark); document.documentElement.classList.toggle('dark', dark);
};
applyStoredTheme();
document.addEventListener('livewire:navigating', (event) => event.detail.onSwap(applyStoredTheme));
})(); })();
</script> </script>
+18
View File
@@ -37,6 +37,24 @@ it('overrides a dark system theme', function () {
->assertScript('localStorage.getItem("dark-theme")', 'light'); ->assertScript('localStorage.getItem("dark-theme")', 'light');
}); });
it('keeps the chosen theme when moving between pages with wire:navigate', function () {
$this->actingAs(User::factory()->admin()->create());
$page = visit(route('customers.index'))->inLightMode()
->click(THEME_TOGGLE)
->assertScript('document.documentElement.classList.contains("dark")', true);
$page->click('Käyttäjät')
->assertPathIs('/users')
->assertScript('document.documentElement.classList.contains("dark")', true)
->assertAttribute(THEME_TOGGLE, 'aria-pressed', 'true')
->click('Asiakkaat')
->assertPathIs('/customers')
->assertScript('document.documentElement.classList.contains("dark")', true)
->assertAttribute(THEME_TOGGLE, 'aria-pressed', 'true')
->assertNoAccessibilityIssues(level: 2);
});
it('is in the header for signed-in users and works with the keyboard', function () { it('is in the header for signed-in users and works with the keyboard', function () {
$this->actingAs(User::factory()->create()); $this->actingAs(User::factory()->create());