diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index 04be8ab..efbc27e 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -38,7 +38,6 @@ class AccountController extends Controller Rule::unique('users')->ignoreModel($user), ], 'new_password' => ['nullable', 'confirmed', VALI_PASSWORD], - 'dark_mode' => 'nullable', ]); if ($input->email != $user->email) { diff --git a/app/Models/User.php b/app/Models/User.php index fb28b3c..d661bbe 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -33,7 +33,6 @@ use MightyPork\Utils\Str; * @property string $email - unique, for login and social auth chaining * @property string $password - hashed pw * @property bool $confirmed - user e-mail is confirmed - * @property bool $dark_mode - user prefers dark mode * @property-read Proposal[]|Collection $proposals * @property-read Table[]|Collection $tables * @property-read OAuthIdentity[]|Collection $socialIdentities @@ -63,7 +62,7 @@ class User extends BaseModel implements * @var array */ protected $fillable = [ - 'name', 'title', 'email', 'password', 'bio', 'website', 'confirmed', 'dark_mode' + 'name', 'title', 'email', 'password', 'bio', 'website', 'confirmed' ]; /** diff --git a/app/helpers.php b/app/helpers.php index 00b3c92..7eaafe0 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -33,7 +33,7 @@ function faker() { } function dark_mode() { - return user() !== NULL && user()->dark_mode; + return isset($_COOKIE["dark_mode"]); } /** diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index 8d64c31..e0e432f 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -23,7 +23,6 @@ class CreateUsersTable extends Migration $table->string('email')->unique(); // would have to be nullable if we supported login via providers that don't give us e-mail $table->string('password')->nullable(); // null if registered via provider and not set a pw yet $table->boolean('confirmed')->default(false); // set to 1 after e-mail is verified - $table->boolean('dark_mode')->default(false); $table->rememberToken(); }); } diff --git a/resources/assets/js/app.js b/resources/assets/js/app.js index 866d344..aad708c 100644 --- a/resources/assets/js/app.js +++ b/resources/assets/js/app.js @@ -65,6 +65,44 @@ $(document).on('input keypress paste keyup', 'input[data-autoalias]', function ( } }) +$(function () { + // theme switcher without reloading + + let themeStyle = document.querySelector('#theme-style'); + const lightURL = themeStyle.getAttribute('light-url'); + const darkURL = themeStyle.getAttribute('dark-url'); + const navbar = document.querySelector('.page-navbar'); + const logo = document.querySelector('#navbar-logo'); + + window.toggleDarkMode = function () { + let newStyle = document.createElement('link'); + newStyle.rel = 'stylesheet'; + if (themeStyle.href === lightURL) { + newStyle.href = darkURL; + navbar.classList.remove('navbar-light'); + navbar.classList.add('navbar-dark'); + logo.src = logo.getAttribute('src-dark'); + + document.cookie = "dark_mode=1"; + } else { + newStyle.href = lightURL; + navbar.classList.remove('navbar-dark'); + navbar.classList.add('navbar-light'); + logo.src = logo.getAttribute('src-light'); + + document.cookie = "dark_mode=0;expires=" + new Date().toUTCString(); + } + + // remove old css after new css has loaded to prevent FOUC + let oldThemeStyle = themeStyle; + themeStyle = newStyle; + newStyle.addEventListener('load', () => { + document.head.removeChild(oldThemeStyle); + }); + document.head.appendChild(newStyle); + }; +}); + window.Vue = require('vue'); Vue.component('column-editor', require('./components/ColumnEditor.vue')); diff --git a/resources/assets/sass/bootstrap-customizations/_navbar.scss b/resources/assets/sass/bootstrap-customizations/_navbar.scss index 7ba9c54..f096883 100644 --- a/resources/assets/sass/bootstrap-customizations/_navbar.scss +++ b/resources/assets/sass/bootstrap-customizations/_navbar.scss @@ -17,3 +17,23 @@ .dropdown-menu { box-shadow: 0 3px 4px 0 rgba(black, .1); } + +.dropdown-menu .dropdown-item.nav-link { + cursor: pointer; +} + +@if $theme == 'dark' { + .dropdown-menu .dark-mode-switch-on { + display: block; + } + .dropdown-menu .dark-mode-switch-off { + display: none; + } +} @else { + .dropdown-menu .dark-mode-switch-on { + display: none; + } + .dropdown-menu .dark-mode-switch-off { + display: block; + } +} diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index c1bee10..31ff3af 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -14,7 +14,12 @@ - + diff --git a/resources/views/layouts/main-nav.blade.php b/resources/views/layouts/main-nav.blade.php index 415efad..ecf804f 100644 --- a/resources/views/layouts/main-nav.blade.php +++ b/resources/views/layouts/main-nav.blade.php @@ -3,7 +3,16 @@