From 7b6a909677f6181600631f3f901d0d22c8bf9f73 Mon Sep 17 00:00:00 2001 From: Petri Saarinen Date: Sun, 22 Nov 2026 11:25:01 +0200 Subject: [PATCH] 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 --- lang/fi/layout.php | 1 + resources/views/layouts/app.blade.php | 6 +++ .../views/partials/theme-toggle.blade.php | 19 ++++++++ tests/Browser/ThemeToggleTest.php | 48 +++++++++++++++++++ tests/Feature/HomePageTest.php | 3 +- 5 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 resources/views/partials/theme-toggle.blade.php create mode 100644 tests/Browser/ThemeToggleTest.php diff --git a/lang/fi/layout.php b/lang/fi/layout.php index d7af331..5d4eb78 100644 --- a/lang/fi/layout.php +++ b/lang/fi/layout.php @@ -8,4 +8,5 @@ return [ 'users' => 'Käyttäjät', 'signed_in_as' => 'Kirjautuneena: :name', 'logout' => 'Kirjaudu ulos', + 'theme_toggle' => 'Tumma teema', ]; diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index 47d5c65..6cbf9be 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -44,6 +44,8 @@
{{ __('layout.signed_in_as', ['name' => auth()->user()->name]) }} + @include('partials.theme-toggle') +
@csrf @@ -53,6 +55,10 @@
+ @else +
+ @include('partials.theme-toggle') +
@endauth
diff --git a/resources/views/partials/theme-toggle.blade.php b/resources/views/partials/theme-toggle.blade.php new file mode 100644 index 0000000..a833c66 --- /dev/null +++ b/resources/views/partials/theme-toggle.blade.php @@ -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. +--}} + + diff --git a/tests/Browser/ThemeToggleTest.php b/tests/Browser/ThemeToggleTest.php new file mode 100644 index 0000000..969a406 --- /dev/null +++ b/tests/Browser/ThemeToggleTest.php @@ -0,0 +1,48 @@ +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'); +}); diff --git a/tests/Feature/HomePageTest.php b/tests/Feature/HomePageTest.php index 0d3b706..3559e99 100644 --- a/tests/Feature/HomePageTest.php +++ b/tests/Feature/HomePageTest.php @@ -17,7 +17,8 @@ it('renders the home page with the main menu', function () { ->assertSee('Siirry pääsisältöön') ->assertSee('id="main-content"', 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 () {