-
Notifications
You must be signed in to change notification settings - Fork 609
feat: Implement dynamic theme switching and dark mode support #3323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DSingh0304
wants to merge
21
commits into
apache:master
Choose a base branch
from
DSingh0304:proposal/dark-mode
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
135bf55
feat: prevent FOUC with inline color scheme script
DSingh0304 1910c72
feat: enable auto color scheme on MantineProvider
DSingh0304 610e9cb
feat: add dark mode support to Ant Design ConfigProvider
DSingh0304 690fd91
feat: add dynamic theme switching for Monaco Editor
DSingh0304 0e7d7cb
feat: add theme toggle component to header
DSingh0304 a962ebf
style: add smooth theme transition on body
DSingh0304 346d718
test: add E2E tests for dark mode
DSingh0304 07b31f5
style: remove temporary transition for theme switching
DSingh0304 973eee2
Update src/components/Header/ThemeToggle.tsx
DSingh0304 936f54e
Update src/components/Header/ThemeToggle.tsx
DSingh0304 5a42c3c
Update src/styles/global.css
DSingh0304 cf066f2
Update index.html
DSingh0304 9ad3df8
Fixed the css selectors
DSingh0304 8870467
Potential fix for pull request finding
DSingh0304 ecbd121
Refactor color scheme initialization script for improved readability
DSingh0304 4d11542
Potential fix for pull request finding
DSingh0304 759ff89
fix(theme): guard color scheme toggle and localize labels
DSingh0304 bae6e4b
Potential fix for pull request finding
DSingh0304 bd40076
test(dark-mode): use accessible role-based theme selectors
DSingh0304 b3d009b
fix(i18n): correct Turkish translations for theme settings
DSingh0304 9fb2032
CI fix ( failed visibility check )
DSingh0304 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { test } from '@e2e/utils/test'; | ||
| import type { Page } from '@playwright/test'; | ||
| import { expect } from '@playwright/test'; | ||
|
|
||
| const themeOptionNameByValue = { | ||
| auto: 'System theme', | ||
| dark: 'Dark theme', | ||
| light: 'Light theme', | ||
| } as const; | ||
|
|
||
| const themeOption = (page: Page, value: keyof typeof themeOptionNameByValue) => | ||
| page.getByRole('radio', { name: themeOptionNameByValue[value], exact: true }); | ||
|
|
||
| test.describe('Dark Mode', () => { | ||
| test( | ||
| 'auto mode resolves to dark when OS prefers dark', | ||
| { tag: '@dark-mode' }, | ||
| async ({ page }) => { | ||
| await test.step('emulate OS dark preference', async () => { | ||
| await page.emulateMedia({ colorScheme: 'dark' }); | ||
| }); | ||
|
|
||
| await test.step('activate auto mode', async () => { | ||
| await themeOption(page, 'auto').dispatchEvent('click'); | ||
| await page.reload(); | ||
| }); | ||
|
|
||
| await test.step('verify dark scheme applied', async () => { | ||
| await expect(page.locator('html')).toHaveAttribute( | ||
| 'data-mantine-color-scheme', | ||
| 'dark' | ||
| ); | ||
| }); | ||
| } | ||
| ); | ||
|
|
||
| test( | ||
| 'theme preference persists across reload', | ||
| { tag: '@dark-mode' }, | ||
| async ({ page }) => { | ||
| await test.step('switch to dark mode', async () => { | ||
| await themeOption(page, 'dark').dispatchEvent('click'); | ||
| await expect(page.locator('html')).toHaveAttribute( | ||
| 'data-mantine-color-scheme', | ||
| 'dark' | ||
| ); | ||
| }); | ||
|
|
||
| await test.step('reload and verify persistence', async () => { | ||
| await page.reload(); | ||
| await expect(page.locator('html')).toHaveAttribute( | ||
| 'data-mantine-color-scheme', | ||
| 'dark' | ||
| ); | ||
| }); | ||
|
|
||
| await test.step('verify localStorage value', async () => { | ||
| const stored = await page.evaluate(() => | ||
| localStorage.getItem('mantine-color-scheme-value') | ||
| ); | ||
| expect(stored).toBe('dark'); | ||
| }); | ||
|
|
||
| await test.step('clean up: restore auto mode', async () => { | ||
| await themeOption(page, 'auto').dispatchEvent('click'); | ||
| }); | ||
| } | ||
| ); | ||
|
|
||
| test( | ||
| 'can cycle through all theme modes', | ||
| { tag: '@dark-mode' }, | ||
| async ({ page }) => { | ||
| await test.step('switch to light mode', async () => { | ||
| await themeOption(page, 'light').dispatchEvent('click'); | ||
| await expect(page.locator('html')).toHaveAttribute( | ||
| 'data-mantine-color-scheme', | ||
| 'light' | ||
| ); | ||
| }); | ||
|
|
||
| await test.step('switch to dark mode', async () => { | ||
| await themeOption(page, 'dark').dispatchEvent('click'); | ||
| await expect(page.locator('html')).toHaveAttribute( | ||
| 'data-mantine-color-scheme', | ||
| 'dark' | ||
| ); | ||
| }); | ||
|
|
||
| await test.step('switch back to auto mode', async () => { | ||
| await themeOption(page, 'auto').dispatchEvent('click'); | ||
| }); | ||
|
|
||
| await test.step('verify auto mode resolves correctly', async () => { | ||
| const stored = await page.evaluate(() => | ||
| localStorage.getItem('mantine-color-scheme-value') | ||
| ); | ||
| expect(stored).toBe('auto'); | ||
| }); | ||
| } | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| import { SegmentedControl, useMantineColorScheme, VisuallyHidden } from '@mantine/core'; | ||
| import { useCallback, useEffect, useRef } from 'react'; | ||
| import { useTranslation } from 'react-i18next'; | ||
|
|
||
| import IconDarkMode from '~icons/material-symbols/dark-mode'; | ||
| import IconDesktop from '~icons/material-symbols/desktop-windows'; | ||
| import IconLightMode from '~icons/material-symbols/light-mode'; | ||
|
|
||
| const TRANSITION_DURATION_MS = 200; | ||
| const COLOR_SCHEMES = ['light', 'dark', 'auto'] as const; | ||
|
|
||
| type ColorSchemeValue = (typeof COLOR_SCHEMES)[number]; | ||
|
|
||
| const isColorSchemeValue = (value: string): value is ColorSchemeValue => | ||
| (COLOR_SCHEMES as readonly string[]).includes(value); | ||
|
|
||
| const iconStyle = { width: 14, height: 14 } as const; | ||
|
|
||
| export const ThemeToggle = () => { | ||
| const { t } = useTranslation(); | ||
| const { colorScheme, setColorScheme } = useMantineColorScheme({ | ||
| keepTransitions: true, | ||
| }); | ||
| const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null); | ||
|
|
||
| const segmentData = [ | ||
| { | ||
| value: 'light', | ||
| label: ( | ||
| <> | ||
| <VisuallyHidden>{t('settings.theme.light')}</VisuallyHidden> | ||
| <IconLightMode style={iconStyle} /> | ||
| </> | ||
| ), | ||
| }, | ||
| { | ||
| value: 'dark', | ||
| label: ( | ||
| <> | ||
| <VisuallyHidden>{t('settings.theme.dark')}</VisuallyHidden> | ||
| <IconDarkMode style={iconStyle} /> | ||
| </> | ||
| ), | ||
| }, | ||
| { | ||
| value: 'auto', | ||
| label: ( | ||
| <> | ||
| <VisuallyHidden>{t('settings.theme.auto')}</VisuallyHidden> | ||
| <IconDesktop style={iconStyle} /> | ||
| </> | ||
| ), | ||
| }, | ||
| ]; | ||
|
|
||
| // Clean up pending transition timer on unmount | ||
| useEffect(() => { | ||
| return () => { | ||
| if (timerRef.current) { | ||
| clearTimeout(timerRef.current); | ||
| document.body.classList.remove('theme-transitioning'); | ||
| } | ||
| }; | ||
| }, []); | ||
|
|
||
| const handleChange = useCallback( | ||
| (value: string) => { | ||
| if (!isColorSchemeValue(value)) { | ||
| return; | ||
| } | ||
|
|
||
| // Clear any pending transition timer from a previous rapid toggle | ||
| if (timerRef.current) { | ||
| clearTimeout(timerRef.current); | ||
| } | ||
|
|
||
| document.body.classList.add('theme-transitioning'); | ||
| setColorScheme(value); | ||
|
|
||
| timerRef.current = setTimeout(() => { | ||
| document.body.classList.remove('theme-transitioning'); | ||
| timerRef.current = null; | ||
| }, TRANSITION_DURATION_MS); | ||
| }, | ||
| [setColorScheme] | ||
| ); | ||
|
|
||
| return ( | ||
| <SegmentedControl | ||
| size="xs" | ||
| value={colorScheme} | ||
| onChange={handleChange} | ||
| data={segmentData} | ||
| /> | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.