Skip to content

Commit 4dfbb86

Browse files
fix: fixed theme transition being applied incorrectly (TECH-132) (#38)
1 parent eca3c0a commit 4dfbb86

File tree

5 files changed

+12
-8
lines changed

5 files changed

+12
-8
lines changed

apps/web/index.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<title>TODO: Change page title</title>
99
<script>
1010
// This script is used to prevent flashing on page load, as it loads the apporiate theme first before loading the css
11-
const theme = localStorage.getItem("swamphacks-ui-theme") || "system";
11+
const theme = localStorage.getItem("ui-theme") || "system";
1212
const root = window.document.documentElement;
1313

1414
root.classList.remove("light", "dark");
@@ -23,6 +23,11 @@
2323
} else {
2424
root.classList.add(theme);
2525
}
26+
27+
// Only apply this class only after the theme has been applied, hence the calling the onload function
28+
window.onload = () => {
29+
document.body.classList.add("enable-theme-transition");
30+
};
2631
</script>
2732
</head>
2833
<body>

apps/web/src/components/ThemeProvider.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const ThemeProviderContext = createContext<ThemeProviderState>(initialState);
2727
export function ThemeProvider({
2828
children,
2929
defaultTheme = "system",
30-
storageKey = "vite-ui-theme",
30+
storageKey = "ui-theme",
3131
...props
3232
}: ThemeProviderProps) {
3333
const [theme, setTheme] = useState<Theme>(
@@ -74,7 +74,7 @@ export function ThemeProvider({
7474
);
7575
}
7676

77-
export const ThemeSwitch = () => {
77+
export const DevThemeSwitch = () => {
7878
const { theme, setTheme } = useTheme();
7979

8080
return (

apps/web/src/index.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ body,
1818
}
1919

2020
/* https://stackoverflow.com/questions/68400074/how-to-apply-transition-effects-when-switching-from-light-mode-to-dark-in-tailwi */
21-
body * {
21+
body.enable-theme-transition * {
2222
@apply transition-colors duration-500;
2323
}

apps/web/src/main.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable react-refresh/only-export-components */
21
import { StrictMode } from "react";
32
import { createRoot } from "react-dom/client";
43
import { RouterProvider, createRouter } from "@tanstack/react-router";
@@ -25,7 +24,7 @@ declare module "@tanstack/react-router" {
2524

2625
function App() {
2726
return (
28-
<ThemeProvider defaultTheme="system" storageKey="swamphacks-ui-theme">
27+
<ThemeProvider defaultTheme="system">
2928
<QueryClientProvider client={queryClient}>
3029
<InnerApp />
3130
</QueryClientProvider>

apps/web/src/routes/__root.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ThemeSwitch } from "@/components/ThemeProvider";
1+
import { DevThemeSwitch } from "@/components/ThemeProvider";
22
import type { auth } from "@/lib/authClient";
33
import { createRootRouteWithContext, Outlet } from "@tanstack/react-router";
44

@@ -14,7 +14,7 @@ export const Route = createRootRouteWithContext<RouterContext>()({
1414
<Outlet />
1515
{IS_DEV && (
1616
<div className="fixed inline-flex w-fit z-[999] bottom-3 left-3 text-white">
17-
<ThemeSwitch />
17+
<DevThemeSwitch />
1818
</div>
1919
)}
2020
</>

0 commit comments

Comments
 (0)