]> BookStack Code Mirror - bookstack/commitdiff
Added dark/light mode toggle to profile dropdown menu
authorDan Brown <redacted>
Sat, 11 Apr 2020 19:37:51 +0000 (20:37 +0100)
committerDan Brown <redacted>
Sat, 11 Apr 2020 19:37:51 +0000 (20:37 +0100)
- Also fixed some remaining areas which needed dark mode support.

app/Http/Controllers/UserController.php
resources/icons/dark-mode.svg [new file with mode: 0644]
resources/icons/light-mode.svg [new file with mode: 0644]
resources/sass/_lists.scss
resources/views/base.blade.php
resources/views/common/header.blade.php
routes/web.php

index 43cbad1fb6414baa33c83c1b02a60e2ac75e6ce7..775e2a984e6e8edcc8fb65906db7c208076e90dd 100644 (file)
@@ -312,6 +312,16 @@ class UserController extends Controller
         return $this->changeListSort($id, $request, $type);
     }
 
+    /**
+     * Toggle dark mode for the current user.
+     */
+    public function toggleDarkMode()
+    {
+        $enabled = setting()->getForCurrentUser('dark-mode-enabled', false);
+        setting()->putUser(user(), 'dark-mode-enabled', $enabled ? 'false' : 'true');
+        return redirect()->back();
+    }
+
     /**
      * Update the stored section expansion preference for the given user.
      */
diff --git a/resources/icons/dark-mode.svg b/resources/icons/dark-mode.svg
new file mode 100644 (file)
index 0000000..8b00d72
--- /dev/null
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M10 2c-1.82 0-3.53.5-5 1.35C7.99 5.08 10 8.3 10 12s-2.01 6.92-5 8.65C6.47 21.5 8.18 22 10 22c5.52 0 10-4.48 10-10S15.52 2 10 2z"/></svg>
\ No newline at end of file
diff --git a/resources/icons/light-mode.svg b/resources/icons/light-mode.svg
new file mode 100644 (file)
index 0000000..cf2961d
--- /dev/null
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 15.31L23.31 12 20 8.69V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69zM12 18c-3.31 0-6-2.69-6-6s2.69-6 6-6 6 2.69 6 6-2.69 6-6 6z"/></svg>
\ No newline at end of file
index 8165d532d39d55eab6af76355a650091980a7cd5..5eb672697ab52e7c935b82f149ffb8808dafc701 100644 (file)
@@ -603,6 +603,9 @@ ul.pagination {
   li.border-bottom {
     border-bottom: 1px solid #DDD;
   }
+  li hr {
+    margin: $-xs 0;
+  }
 }
 
 // Books grid view
index 0f43532fb8f9b7464b35a5fd5a7bf3f609331ca2..a5404a36506ae9d70225034a6eeef749eeb86d28 100644 (file)
@@ -1,5 +1,7 @@
 <!DOCTYPE html>
-<html lang="{{ config('app.lang') }}" dir="{{ config('app.rtl') ? 'rtl' : 'ltr' }}" class="@yield('body-class')">
+<html lang="{{ config('app.lang') }}"
+      dir="{{ config('app.rtl') ? 'rtl' : 'ltr' }}"
+      class="{{ setting()->getForCurrentUser('dark-mode-enabled') ? 'dark-mode ' : '' }}@yield('body-class')">
 <head>
     <title>{{ isset($pageTitle) ? $pageTitle . ' | ' : '' }}{{ setting('app-name') }}</title>
 
 
     <!-- Translations for JS -->
     @stack('translations')
-
-    <script>
-        if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
-            document.documentElement.classList.add('dark-mode');
-        }
-    </script>
 </head>
 <body class="@yield('body-class')">
 
index ec90739ee497f8dfd41155ab90cae76f547f153a..af7aaeeb0ee8ec3cb498867c3084ac91b2d6621b 100644 (file)
                                     <a href="{{ url('/logout') }}">@icon('logout'){{ trans('auth.logout') }}</a>
                                 @endif
                             </li>
+                            <li><hr></li>
+                            <li>
+                                <form action="{{ url('/settings/users/toggle-dark-mode') }}" method="post">
+                                    {{ csrf_field() }}
+                                    {{ method_field('patch') }}
+                                    @if(setting()->getForCurrentUser('dark-mode-enabled'))
+                                        <button>@icon('light-mode')Light Mode</button>
+                                    @else
+                                        <button>@icon('dark-mode')Dark Mode</button>
+                                    @endif
+                                </form>
+                            </li>
                         </ul>
                     </div>
                 @endif
index 4dfccdf367e9156509cfe72c9f65856491700c93..3e05e394d29698498e19f2ec739e17dd0d1a4a1c 100644 (file)
@@ -183,6 +183,7 @@ Route::group(['middleware' => 'auth'], function () {
         Route::patch('/users/{id}/switch-shelf-view', 'UserController@switchShelfView');
         Route::patch('/users/{id}/change-sort/{type}', 'UserController@changeSort');
         Route::patch('/users/{id}/update-expansion-preference/{key}', 'UserController@updateExpansionPreference');
+        Route::patch('/users/toggle-dark-mode', 'UserController@toggleDarkMode');
         Route::post('/users/create', 'UserController@store');
         Route::get('/users/{id}', 'UserController@edit');
         Route::put('/users/{id}', 'UserController@update');