Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

feat(theme): cross-fade theme changes as one transition

spacesdrive7b3ea12172c0
diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx
index f182173..3214a41 100644
--- a/frontend/src/App.jsx
+++ b/frontend/src/App.jsx
@@ -61,7 +61,7 @@ const router = createBrowserRouter([
export default function App() {
return (
- <ThemeProvider attribute="class" defaultTheme="system" enableSystem>
+ <ThemeProvider attribute="class" defaultTheme="system" enableSystem disableTransitionOnChange>
<TooltipProvider>
<AuthProvider>
<Suspense fallback={null}>
diff --git a/frontend/src/components/CommandPalette.jsx b/frontend/src/components/CommandPalette.jsx
index 7e1805a..1ec644d 100644
--- a/frontend/src/components/CommandPalette.jsx
+++ b/frontend/src/components/CommandPalette.jsx
@@ -1,6 +1,6 @@
import React, { useMemo, useState } from 'react';
import { useNavigate } from 'react-router-dom';
-import { useTheme } from 'next-themes';
+import { useSmoothTheme } from '@/hooks/use-smooth-theme';
import {
LayoutDashboard, PlaySquare, Scissors, Clapperboard, MessagesSquare, Bookmark, Send,
Users, Settings, RefreshCw, Sun, Moon, Monitor, LogOut, Tag, Plus,
@@ -22,7 +22,7 @@ const PAGE_ICONS = {
export default function CommandPalette({ open, onOpenChange, accounts, categories, onRefreshAll, onSignOut }) {
const navigate = useNavigate();
- const { setTheme } = useTheme();
+ const { setTheme } = useSmoothTheme();
const [query, setQuery] = useState('');
const handleOpenChange = next => {
diff --git a/frontend/src/hooks/use-smooth-theme.js b/frontend/src/hooks/use-smooth-theme.js
new file mode 100644
index 0000000..bdc0978
--- /dev/null
+++ b/frontend/src/hooks/use-smooth-theme.js
@@ -0,0 +1,32 @@
+import { useCallback } from 'react';
+import { useTheme } from 'next-themes';
+
+function resolve(theme) {
+ if (theme !== 'system') return theme;
+ return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
+}
+
+// Switches theme as one cross-fade of the whole page instead of every element easing at its own
+// speed. The class is applied synchronously inside the transition so the new snapshot is complete.
+export function useSmoothTheme() {
+ const { theme, resolvedTheme, setTheme } = useTheme();
+
+ const setThemeSmooth = useCallback(next => {
+ const apply = () => {
+ const resolved = resolve(next);
+ const root = document.documentElement;
+ root.classList.toggle('dark', resolved === 'dark');
+ root.classList.toggle('light', resolved === 'light');
+ root.style.colorScheme = resolved;
+ setTheme(next);
+ };
+ const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
+ if (typeof document.startViewTransition !== 'function' || reduceMotion) {
+ apply();
+ return;
+ }
+ document.startViewTransition(apply);
+ }, [setTheme]);
+
+ return { theme, resolvedTheme, setTheme: setThemeSmooth };
+}
diff --git a/frontend/src/index.css b/frontend/src/index.css
index c33e241..c519b07 100644
--- a/frontend/src/index.css
+++ b/frontend/src/index.css
@@ -292,6 +292,13 @@
to { opacity: 1; transform: none; }
}
+/* Theme changes cross-fade the whole page as one image (see hooks/use-smooth-theme.js) */
+::view-transition-old(root),
+::view-transition-new(root) {
+ animation-duration: 260ms;
+ animation-timing-function: var(--ease-standard);
+}
+
@layer base {
* {
@apply border-border outline-ring/50;
diff --git a/frontend/src/layout/Header.jsx b/frontend/src/layout/Header.jsx
index 36b6627..dfb0b93 100644
--- a/frontend/src/layout/Header.jsx
+++ b/frontend/src/layout/Header.jsx
@@ -1,6 +1,6 @@
import React from 'react';
import { useLocation } from 'react-router-dom';
-import { useTheme } from 'next-themes';
+import { useSmoothTheme } from '@/hooks/use-smooth-theme';
import { RefreshCw, Sun, Moon, Search } from 'lucide-react';
import { SidebarTrigger } from '@/components/ui/sidebar';
import { Button } from '@/components/ui/button';
@@ -12,7 +12,7 @@ import { cn } from '@/lib/utils';
export default function TopBar({ onOpenPalette }) {
const location = useLocation();
const { loading, refreshAll, accounts } = useAppContext();
- const { resolvedTheme, setTheme } = useTheme();
+ const { resolvedTheme, setTheme } = useSmoothTheme();
const isDark = resolvedTheme === 'dark';
const title = routeTitle(location.pathname);
diff --git a/frontend/src/layout/Sidebar.jsx b/frontend/src/layout/Sidebar.jsx
index 8f604b6..01b0663 100644
--- a/frontend/src/layout/Sidebar.jsx
+++ b/frontend/src/layout/Sidebar.jsx
@@ -1,6 +1,6 @@
import React from 'react';
import { Link, useLocation, useNavigate } from 'react-router-dom';
-import { useTheme } from 'next-themes';
+import { useSmoothTheme } from '@/hooks/use-smooth-theme';
import { Search, Tag, Settings, LogOut, Sun, Moon, Monitor, ChevronsUpDown } from 'lucide-react';
import {
Sidebar, SidebarContent, SidebarFooter, SidebarGroup, SidebarGroupLabel, SidebarHeader,
@@ -126,7 +126,7 @@ export default function AppSidebar({ categories, onOpenPalette, onSignOut }) {
function UserMenu({ onSignOut }) {
const navigate = useNavigate();
const { user } = useAuth();
- const { theme, setTheme } = useTheme();
+ const { theme, setTheme } = useSmoothTheme();
const email = user?.email ?? '';
const initial = email[0]?.toUpperCase() ?? 'T';