Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: try and prevent style flash for dark mode on init

Richie McIlroyaec400eb6ada
diff --git a/apps/desktop/src/App.tsx b/apps/desktop/src/App.tsx
index 54bee2470..46b2563b8 100644
--- a/apps/desktop/src/App.tsx
+++ b/apps/desktop/src/App.tsx
@@ -11,6 +11,29 @@ import "./styles/theme.css";
import { commands } from "./utils/tauri";
import { themeStore } from "./store/theme";
+const darkModeMediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
+const localStorageDarkMode = localStorage.getItem("darkMode");
+
+// Check stored preference first, then system preference
+if (
+ localStorageDarkMode === "true" ||
+ (localStorageDarkMode === null && darkModeMediaQuery.matches)
+) {
+ document.documentElement.classList.add("dark");
+}
+
+// Add base background color to prevent flash
+const style = document.createElement("style");
+style.textContent = `
+ html.dark {
+ background-color: #1E1E1E;
+ }
+ html.dark body {
+ background-color: #1E1E1E;
+ }
+`;
+document.head.appendChild(style);
+
const queryClient = new QueryClient({
defaultOptions: {
mutations: {
diff --git a/apps/desktop/src/store/theme.ts b/apps/desktop/src/store/theme.ts
index 79a89512f..56587597b 100644
--- a/apps/desktop/src/store/theme.ts
+++ b/apps/desktop/src/store/theme.ts
@@ -4,7 +4,9 @@ import type { GeneralSettingsStore } from "~/utils/tauri";
import { emit, listen } from "@tauri-apps/api/event";
const THEME_CHANGE_EVENT = "theme-change";
-const [isDarkMode, setIsDarkMode] = createSignal(false);
+const [isDarkMode, setIsDarkMode] = createSignal(
+ document.documentElement.classList.contains('dark')
+);
const applyTheme = (darkMode: boolean) => {
setIsDarkMode(darkMode);
diff --git a/apps/desktop/src/styles/theme.css b/apps/desktop/src/styles/theme.css
index 7c3dc1f0e..5dfb6a5db 100644
--- a/apps/desktop/src/styles/theme.css
+++ b/apps/desktop/src/styles/theme.css
@@ -1,3 +1,13 @@
+@media (prefers-color-scheme: dark) {
+ [data-tauri-drag-region] {
+ background: var(--gray-50);
+ }
+}
+
+.dark [data-tauri-drag-region] {
+ background: var(--gray-50);
+}
+
:root {
/* Light theme variables */
--gray-50: #FFFFFF;