open-swe/ui/src/routes/__root.tsx
Johannes du Plessis 30d5d55f0f
feat: add dark mode support to web UI (#1436)
* feat: add dark mode support to web UI

Add a persisted, system-aware theme with a Light/Dark/System toggle in
the sidebar user menu. An inline head script applies the stored theme
before paint to avoid a flash, and the agents-ui surface gets dark
overrides for its --ui-* variables.

Co-authored-by: open-swe[bot] <open-swe@users.noreply.github.com>

* fix: use static theme init script to satisfy CodeQL

Build the no-FOUC theme script from a fully static string literal
instead of interpolating THEME_STORAGE_KEY, so no value flows into
code construction (CodeQL "Improper code sanitization").

Co-authored-by: open-swe[bot] <open-swe@users.noreply.github.com>

* fix: avoid theme flip on hydration for persisted preferences

Only apply a theme after the stored preference is read, instead of
eagerly applying the default "system" theme on first effect. This
prevented a brief flip to the OS theme (then back) on load for users
with a saved preference that differs from their OS setting.

Co-authored-by: open-swe[bot] <open-swe@users.noreply.github.com>

---------

Co-authored-by: open-swe[bot] <open-swe@users.noreply.github.com>
2026-06-06 12:06:31 -07:00

73 lines
2.3 KiB
TypeScript

import {
HeadContent,
Outlet,
Scripts,
createRootRoute,
} from "@tanstack/react-router"
import { TanStackRouterDevtoolsPanel } from "@tanstack/react-router-devtools"
import { TanStackDevtools } from "@tanstack/react-devtools"
import { QueryClientProvider } from "@tanstack/react-query"
import { ReactQueryDevtools } from "@tanstack/react-query-devtools"
import { useState } from "react"
import appCss from "../styles.css?url"
import { makeQueryClient } from "@/lib/query"
const themeInitScript = `(function(){try{var t=localStorage.getItem("open-swe-theme");var d=t==="dark"||((!t||t==="system")&&window.matchMedia("(prefers-color-scheme: dark)").matches);var r=document.documentElement;r.classList.toggle("dark",d);r.style.colorScheme=d?"dark":"light";}catch(e){}})();`
export const Route = createRootRoute({
head: () => ({
meta: [
{ charSet: "utf-8" },
{
name: "viewport",
content: "width=device-width, initial-scale=1, maximum-scale=1",
},
{ name: "theme-color", content: "#000000" },
{ title: "open-swe" },
],
links: [
{ rel: "stylesheet", href: appCss },
{ rel: "manifest", href: "/manifest.webmanifest" },
{ rel: "icon", type: "image/png", href: "/favicon.png" },
{ rel: "apple-touch-icon", href: "/apple-touch-icon.png" },
],
}),
notFoundComponent: () => (
<main className="container mx-auto p-4 pt-16">
<h1 className="text-2xl font-medium">404</h1>
<p className="text-muted-foreground">
The requested page could not be found.
</p>
</main>
),
shellComponent: RootDocument,
})
function RootDocument({ children }: { children: React.ReactNode }) {
const [queryClient] = useState(() => makeQueryClient())
return (
<html lang="en">
<head>
<script dangerouslySetInnerHTML={{ __html: themeInitScript }} />
<HeadContent />
</head>
<body>
<QueryClientProvider client={queryClient}>
{children ?? <Outlet />}
<TanStackDevtools
config={{ position: "bottom-right" }}
plugins={[
{
name: "Tanstack Router",
render: <TanStackRouterDevtoolsPanel />,
},
]}
/>
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
<Scripts />
</body>
</html>
)
}