Add light/dark theme toggle remembered in the browser

- New partial resources/views/partials/theme-toggle.blade.php: a Flux
  button that flips $flux.dark. Flux stores the choice in localStorage
  (flux.appearance) and applies it before the page paints; until the
  user chooses, the system theme is followed.
- Shown in the header for signed-in users and at the top right of the
  guest pages.
- Accessible: stable name "Tumma teema" with a tooltip, state in
  aria-pressed, decorative moon/sun icons.
- Browser tests cover toggling, persistence across pages, overriding a
  dark system theme, keyboard use and axe checks in both themes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-22 11:25:01 +02:00
co-authored by Claude Opus 5.5
parent c9a7f970ab
commit 7b6a909677
5 changed files with 76 additions and 1 deletions
+1
View File
@@ -8,4 +8,5 @@ return [
'users' => 'Käyttäjät', 'users' => 'Käyttäjät',
'signed_in_as' => 'Kirjautuneena: :name', 'signed_in_as' => 'Kirjautuneena: :name',
'logout' => 'Kirjaudu ulos', 'logout' => 'Kirjaudu ulos',
'theme_toggle' => 'Tumma teema',
]; ];
+6
View File
@@ -44,6 +44,8 @@
<div class="flex flex-wrap items-center gap-3"> <div class="flex flex-wrap items-center gap-3">
<flux:text size="sm">{{ __('layout.signed_in_as', ['name' => auth()->user()->name]) }}</flux:text> <flux:text size="sm">{{ __('layout.signed_in_as', ['name' => auth()->user()->name]) }}</flux:text>
@include('partials.theme-toggle')
<form method="POST" action="{{ route('logout') }}"> <form method="POST" action="{{ route('logout') }}">
@csrf @csrf
<flux:button type="submit" variant="ghost" size="sm" icon="arrow-right-start-on-rectangle"> <flux:button type="submit" variant="ghost" size="sm" icon="arrow-right-start-on-rectangle">
@@ -53,6 +55,10 @@
</div> </div>
</div> </div>
</header> </header>
@else
<div class="flex justify-end px-4 pt-3 sm:px-6">
@include('partials.theme-toggle')
</div>
@endauth @endauth
<main id="main-content" tabindex="-1" class="focus:outline-none"> <main id="main-content" tabindex="-1" class="focus:outline-none">
@@ -0,0 +1,19 @@
{{--
Light/dark theme toggle. Flux stores the choice in localStorage
("flux.appearance") and @fluxAppearance applies it before the page paints.
The name stays "Tumma teema"; aria-pressed tells whether it is on.
--}}
<flux:button
x-data
x-on:click="$flux.dark = ! $flux.dark"
x-bind:aria-pressed="$flux.dark ? 'true' : 'false'"
aria-label="{{ __('layout.theme_toggle') }}"
:tooltip="__('layout.theme_toggle')"
variant="ghost"
size="sm"
square
data-theme-toggle
>
<flux:icon.moon variant="mini" class="dark:hidden" aria-hidden="true" />
<flux:icon.sun variant="mini" class="hidden dark:block" aria-hidden="true" />
</flux:button>
+48
View File
@@ -0,0 +1,48 @@
<?php
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
const THEME_TOGGLE = '[data-theme-toggle]';
it('switches to the dark theme and back, and remembers the choice', function () {
$page = visit(route('login'))->on()->desktop()->inLightMode();
$page->assertScript('document.documentElement.classList.contains("dark")', false)
->assertAttribute(THEME_TOGGLE, 'aria-pressed', 'false')
->assertAttribute(THEME_TOGGLE, 'aria-label', 'Tumma teema')
->click(THEME_TOGGLE)
->assertScript('document.documentElement.classList.contains("dark")', true)
->assertAttribute(THEME_TOGGLE, 'aria-pressed', 'true')
->assertAttribute(THEME_TOGGLE, 'aria-label', 'Tumma teema')
->assertScript('localStorage.getItem("flux.appearance")', 'dark')
->assertNoAccessibilityIssues(level: 2);
$page->navigate(route('password.request'))
->assertScript('document.documentElement.classList.contains("dark")', true)
->click(THEME_TOGGLE)
->assertScript('document.documentElement.classList.contains("dark")', false)
->assertScript('localStorage.getItem("flux.appearance")', 'light')
->assertNoAccessibilityIssues(level: 2);
});
it('overrides a dark system theme', function () {
visit(route('login'))->inDarkMode()
->assertScript('document.documentElement.classList.contains("dark")', true)
->assertAttribute(THEME_TOGGLE, 'aria-pressed', 'true')
->click(THEME_TOGGLE)
->assertScript('document.documentElement.classList.contains("dark")', false)
->assertScript('localStorage.getItem("flux.appearance")', 'light');
});
it('is in the header for signed-in users and works with the keyboard', function () {
$this->actingAs(User::factory()->create());
visit(route('customers.index'))->inLightMode()
->assertPresent('header '.THEME_TOGGLE)
->keys(THEME_TOGGLE, 'Enter')
->assertScript('document.documentElement.classList.contains("dark")', true)
->assertScript('localStorage.getItem("flux.appearance")', 'dark');
});
+2 -1
View File
@@ -17,7 +17,8 @@ it('renders the home page with the main menu', function () {
->assertSee('Siirry pääsisältöön') ->assertSee('Siirry pääsisältöön')
->assertSee('id="main-content"', false) ->assertSee('id="main-content"', false)
->assertSee('aria-label="Päävalikko"', false) ->assertSee('aria-label="Päävalikko"', false)
->assertSee('Kirjautuneena: Eero Esimerkki'); ->assertSee('Kirjautuneena: Eero Esimerkki')
->assertSee('aria-label="Tumma teema"', false);
}); });
it('increments the click count', function () { it('increments the click count', function () {