Ver código fonte

theme selector

Daniel Bohry 9 meses atrás
pai
commit
3036d4c147
3 arquivos alterados com 64 adições e 107 exclusões
  1. 10 0
      src/app.css
  2. 27 15
      src/app.html
  3. 27 92
      src/routes/profile/+page.svelte

+ 10 - 0
src/app.css

@@ -1,2 +1,12 @@
 @import "tailwindcss";
 @import '@fontsource/fira-mono';
+
+@custom-variant dark (&:where(.dark, .dark *));
+
+html[data-theme="loading"] {
+    display: none;
+}
+
+body {
+    @apply bg-white text-gray-900 dark:bg-gray-900 dark:text-white;
+}

+ 27 - 15
src/app.html

@@ -1,16 +1,28 @@
 <!doctype html>
-<html lang="en">
-	<head>
-		<meta charset="utf-8" />
-		<link rel="icon" href="%sveltekit.assets%/favicon.png" />
-		<link
-			href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap"
-			rel="stylesheet"
-		/>
-		<meta name="viewport" content="width=device-width, initial-scale=1" />
-		%sveltekit.head%
-	</head>
-	<body data-sveltekit-preload-data="hover">
-		<div style="display: contents">%sveltekit.body%</div>
-	</body>
-</html>
+<html lang="en" data-theme="loading">
+<head>
+	<script>
+		(() => {
+			const theme = localStorage.getItem('theme') || 'light';
+			if (theme === 'dark') {
+				document.documentElement.classList.add('dark');
+			} else {
+				document.documentElement.classList.remove('dark');
+			}
+			document.documentElement.dataset.theme = 'ready';
+		})();
+	</script>
+
+	<meta charset="utf-8" />
+	<link rel="icon" href="%sveltekit.assets%/favicon.png" />
+	<link
+		href="https://fonts.googleapis.com/css2?family=Roboto:ital,wght@0,100;0,300;0,400;0,500;0,700;0,900;1,100;1,300;1,400;1,500;1,700;1,900&display=swap"
+		rel="stylesheet"
+	/>
+	<meta name="viewport" content="width=device-width, initial-scale=1" />
+	%sveltekit.head%
+</head>
+<body data-sveltekit-preload-data="hover">
+<div style="display: contents">%sveltekit.body%</div>
+</body>
+</html>

+ 27 - 92
src/routes/profile/+page.svelte

@@ -11,6 +11,7 @@
 	let savePreferenceDisabled = true;
 	let hasChanges = false;
 	let saveMessage = '';
+	let theme = 'light';
 
 	$: authentication.subscribe((value) => {
 		isAuthenticated = !!value;
@@ -39,6 +40,11 @@
 				localStorage.setItem('defaultOrder', 'total');
 			}
 
+			const storedTheme = localStorage.getItem('theme');
+			if (storedTheme) {
+				theme = storedTheme;
+				applyTheme(theme);
+			}
 		} catch (error) {
 			console.error('Error parsing stored auth:', error);
 		} finally {
@@ -46,6 +52,14 @@
 		}
 	});
 
+	function applyTheme(selected) {
+		if (selected === 'dark') {
+			document.documentElement.classList.add('dark');
+		} else {
+			document.documentElement.classList.remove('dark');
+		}
+	}
+
 	function logout() {
 		window.location.href = '/logout';
 	}
@@ -65,6 +79,9 @@
 	function savePreferences() {
 		localStorage.setItem('defaultCurrency', currency);
 		localStorage.setItem('defaultOrder', orderBy);
+		localStorage.setItem('theme', theme);
+		applyTheme(theme);
+
 		savePreferenceDisabled = true;
 		saveMessage = 'Preferences saved successfully.';
 
@@ -73,6 +90,12 @@
 		}, 2000);
 	}
 
+	function updateTheme(event) {
+		theme = event.target.value;
+		hasChanges = true;
+		savePreferenceDisabled = false;
+	}
+
 </script>
 
 <svelte:head>
@@ -127,9 +150,11 @@
 					<select
 						id="theme"
 						class="w-full px-3 py-2 rounded-lg border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-800 text-gray-800 dark:text-gray-100 focus:outline-none focus:ring-2 focus:ring-blue-500"
+						bind:value={theme}
+						on:change={updateTheme}
 					>
 						<option value="light">Light</option>
-						<!-- <option value="dark">Dark</option> -->
+						<option value="dark">Dark</option>
 					</select>
 				</div>
 			</div>
@@ -157,94 +182,4 @@
 			</div>
 		{/if}
 	{/if}
-</div>
-
-
-<style>
-    .profile-container {
-        max-width: 600px;
-        margin: 2rem auto;
-        padding: 1rem;
-    }
-
-    .profile-header {
-        display: flex;
-        justify-content: space-between;
-        align-items: center;
-        margin-bottom: 1.5rem;
-    }
-
-    button {
-        color: white;
-        border: none;
-        border-radius: 8px;
-        padding: 0.5rem 1rem;
-        cursor: pointer;
-    }
-
-    .save-preferences {
-        background-color: #4caf50;
-    }
-
-    .logout-button {
-        background-color: #f44336;
-    }
-
-    .logout-button:hover {
-        background-color: #d32f2f;
-    }
-
-    .settings-card {
-        border: 1px solid #ddd;
-        border-radius: 10px;
-        padding: 1.5rem;
-        background-color: #fefefe;
-        box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
-    }
-
-    .settings-card h2 {
-        margin-top: 0;
-        margin-bottom: 1rem;
-        font-size: 1.25rem;
-    }
-
-    .settings-group {
-        display: flex;
-        flex-direction: column;
-        margin-bottom: 1rem;
-    }
-
-    .settings-group label {
-        margin-bottom: 0.5rem;
-        font-weight: 500;
-        width: 260px;
-    }
-
-    .form-control {
-        width: 100%;
-        padding: 0.5rem;
-        border-radius: 8px;
-        border: 1px solid #ccc;
-        background-color: #fff;
-        font-size: 1rem;
-    }
-
-    button:disabled {
-        pointer-events: none;
-        opacity: 0.6;
-        cursor: not-allowed;
-    }
-
-    .save-message {
-        color: green;
-        height: 2rem;
-        overflow: hidden;
-        position: relative;
-        margin-top: 1rem;
-        text-align: center;
-        display: flex;
-        justify-content: center;
-        align-items: center;
-    }
-
-</style>
+</div>