feat: add dark mode and theme switching support#6766
Conversation
Signed-off-by: ansita20 <ansitasingh20@gmail.com>
66afaa0 to
bc61417
Compare
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds dark mode support with centralized theme state management, persistence, and a header toggle, plus a dev proxy tweak for auth endpoints.
Changes:
- Introduces a
ThemeContextprovider/hook to manage light/dark mode with persistence and system preference support. - Splits MUI theme into
lightTheme/darkThemeand adds aThemeTogglein the header. - Updates dev webpack proxy to include
/authroutes.
Reviewed changes
Copilot reviewed 7 out of 9 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| web/webpack.config.dev.js | Proxies /auth alongside /api for dev auth routing. |
| web/src/theme.ts | Adds light/dark theme variants and a helper to select by mode. |
| web/src/index.tsx | Switches app wiring to use the custom ThemeProvider (context-managed). |
| web/src/contexts/ThemeContext.tsx | New context/provider implementing theme mode, persistence, and system detection. |
| web/src/components/theme/ThemeToggle.tsx | New header-friendly theme toggle button component. |
| web/src/components/header/index.tsx | Adds the toggle to the header actions area. |
| web/package.json | Adds ajv and ajv-keywords dependencies. |
| // Persist theme mode to localStorage | ||
| useEffect(() => { | ||
| localStorage.setItem(THEME_STORAGE_KEY, mode); | ||
| // Update HTML attribute for CSS-based styling if needed | ||
| if (typeof window !== "undefined") { | ||
| document.documentElement.setAttribute("data-theme", mode); | ||
| } | ||
| }, [mode]); | ||
|
|
||
| // Listen to system theme changes | ||
| useEffect(() => { | ||
| const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)"); | ||
| const handleChange = (e: MediaQueryListEvent) => { | ||
| const stored = localStorage.getItem(THEME_STORAGE_KEY); | ||
| // Only update if user hasn't explicitly set a preference | ||
| if (!stored) { | ||
| setMode(e.matches ? "dark" : "light"); | ||
| } | ||
| }; |
| useEffect(() => { | ||
| const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)"); | ||
| const handleChange = (e: MediaQueryListEvent) => { | ||
| const stored = localStorage.getItem(THEME_STORAGE_KEY); | ||
| // Only update if user hasn't explicitly set a preference | ||
| if (!stored) { | ||
| setMode(e.matches ? "dark" : "light"); | ||
| } | ||
| }; | ||
|
|
||
| mediaQuery.addEventListener("change", handleChange); | ||
| return () => mediaQuery.removeEventListener("change", handleChange); | ||
| }, []); |
|
|
||
| return ( | ||
| <Tooltip title={`Switch to ${mode === "light" ? "dark" : "light"} mode`}> | ||
| <IconButton onClick={toggleTheme} size="small" color="inherit"> |
| export const useTheme = (): ThemeContextType => { | ||
| const context = useContext(ThemeContext); | ||
| if (context === undefined) { | ||
| throw new Error("useTheme must be used within a ThemeProvider"); |
| "ajv": "^8.20.0", | ||
| "ajv-keywords": "^5.1.0", |
Ayushmore1214
left a comment
There was a problem hiding this comment.
@Ansita20 this is a huge change, can you run this locally and share screen recording and screenshots ?
yess I will share the image...the mode changes according to the system settings. |
|
go with system setting one first if it works well we can look ahead! |
Pull Request: Implement Dark Mode Theme Switching
Overview
This PR adds comprehensive dark mode support to the PipeCD web application with a centralized theme management system, automatic system preference detection, and localStorage persistence.
Changes Made
New Files Created
1. web/src/contexts/ThemeContext.tsx
ThemeContextwith TypeScript supportThemeProvidercomponent that wraps the appuseTheme()hook for accessing theme stateprefers-color-schemepipecd-theme-mode2. web/src/components/theme/ThemeToggle.tsx
ThemeTogglecomponentModified Files
1. web/src/theme.ts
lightThemeobject with light color palettedarkThemeobject with dark color palettegetTheme()helper function for dynamic theme selectioncommonComponentsconstantthemeexport2. web/src/index.tsx
ThemeProviderwith customThemeProviderfrom ThemeContextCssBaselineimport (now handled by ThemeContext)ThemeProviderfrom contexts3. web/src/components/header/index.tsx
import ThemeToggle from "~/components/theme/ThemeToggle";<ThemeToggle />component in the header's right-side action box4. web/webpack.config.dev.js
/authto proxy context alongside/apiCannot POST /auth/loginerror during local developmentTechnical Details
Theme Architecture
Data Flow
toggleTheme()updates state in ThemeContextgetTheme()returns appropriate MUI themedata-themeattribute updated for CSS hooksBrowser API Usage
localStoragefor persistencewindow.matchMedia('(prefers-color-scheme: dark)')for system detectiondocument.documentElement.setAttribute('data-theme', mode)for CSS integrationTesting Checklist
/authproxy works in dev environmentBrowser Compatibility
Performance Impact
Accessibility
Documentation
Usage for Developers
For End Users
Breaking Changes
None. The
themeexport fromtheme.tsis maintained for backward compatibility.Related Issues
Closes #[Issue Number - Dark Mode Support]
Screenshots
Reviewers
@pipe-cd/maintainers
Deployment Notes
Future Improvements
fixes issue : #6765