Skip to content

Commit 5a46341

Browse files
committed
updating dark mode
1 parent 4190be7 commit 5a46341

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

resources/js/app.tsx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,23 @@ import {
1414
const appName =
1515
import.meta.env.VITE_APP_NAME || 'Laravel';
1616

17-
// window.appearance = 'system';
1817

19-
// // Initialize appearance
20-
// if (typeof window !== 'undefined') {
21-
// const appearance = localStorage.getItem('appearance') || 'system';
22-
// window.appearance = appearance;
18+
/*
19+
* Dark/Light Mode Initialization
20+
* - This needs to be added to every page so that way the correct appearance is loaded
21+
*/
22+
(function() {
23+
const savedAppearance = localStorage.getItem('appearance') || 'system';
24+
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
2325

24-
// const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
25-
// const shouldBeDark = appearance === 'dark' || (appearance === 'system' && prefersDark);
26+
const isDark =
27+
savedAppearance === 'dark' ||
28+
(savedAppearance === 'system' && prefersDark);
2629

27-
// if (shouldBeDark) {
28-
// document.body.classList.add('dark');
29-
// }
30-
// }
30+
if (isDark) {
31+
document.documentElement.classList.add('dark');
32+
}
33+
})();
3134

3235
createInertiaApp({
3336
title: (title) => `${title} - ${appName}`,

resources/js/hooks/use-appearance.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function useAppearance() {
1010

1111
const applyTheme = (mode: Appearance) => {
1212
const isDark = mode === 'dark' || (mode === 'system' && prefersDark());
13-
document.body.classList.toggle('dark', isDark);
13+
document.documentElement.classList.toggle('dark', isDark);
1414
};
1515

1616
const updateAppearance = (mode: Appearance) => {

resources/views/app.blade.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1">
66

77
<title inertia>{{ config('app.name', 'Laravel') }}</title>
8-
9-
<!-- Scripts -->
8+
109
@routes
1110
@viteReactRefresh
1211
@vite(['resources/js/app.tsx', "resources/js/Pages/{$page['component']}.tsx"])

routes/web.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
Route::get('settings/password', [PasswordController::class, 'edit'])->name('password.edit');
3131
Route::put('settings/password', [PasswordController::class, 'update'])->name('password.update');
3232

33-
Route::get('settings/appearance', function(){
33+
Route::get('settings/appearance', function () {
3434
return Inertia::render('Settings/Appearance');
3535
})->name('appearance');
3636

0 commit comments

Comments
 (0)