-
Notifications
You must be signed in to change notification settings - Fork 97
Priyam/dark mode enable #214
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
base: master
Are you sure you want to change the base?
Changes from all commits
cf5041e
244b14b
f274dd2
32c8002
4afcf7e
2eef1f7
86152ec
0439af3
1da2bb3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import React from 'react'; | ||
| import styled from '@emotion/styled'; | ||
| import { IconButton, Tooltip } from '@mui/material'; | ||
| import { Brightness4, Brightness7 } from '@mui/icons-material'; | ||
| import { useTheme } from '@app/contexts/themeContext'; | ||
|
|
||
| const ToggleButton = styled(IconButton)` | ||
| && { | ||
| position: fixed; | ||
| bottom: 2rem; | ||
| right: 2rem; | ||
| width: 56px; | ||
| height: 56px; | ||
| background: ${(props) => props.bgcolor}; | ||
| color: ${(props) => props.color}; | ||
| box-shadow: 0 4px 20px ${(props) => props.shadowcolor}; | ||
| transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1); | ||
| z-index: 1000; | ||
|
|
||
| &:hover { | ||
| background: ${(props) => props.hoverbg}; | ||
| transform: translateY(-4px) rotate(15deg); | ||
| box-shadow: 0 8px 30px ${(props) => props.shadowcolor}; | ||
| } | ||
|
|
||
| &:active { | ||
| transform: translateY(-2px) rotate(0deg); | ||
| } | ||
|
|
||
| @media (max-width: 768px) { | ||
| width: 48px; | ||
| height: 48px; | ||
| bottom: 1.5rem; | ||
| right: 1.5rem; | ||
| } | ||
| } | ||
| `; | ||
|
|
||
| const IconWrapper = styled.div` | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| animation: ${(props) => (props.animate ? 'rotate 0.5s ease-in-out' : 'none')}; | ||
|
|
||
| @keyframes rotate { | ||
| from { | ||
| transform: rotate(0deg) scale(0.8); | ||
| opacity: 0.5; | ||
| } | ||
| to { | ||
| transform: rotate(360deg) scale(1); | ||
| opacity: 1; | ||
| } | ||
| } | ||
| `; | ||
|
|
||
| // dark mode added | ||
| // eslint-disable-next-line complexity | ||
| export const DarkModeToggle = () => { | ||
| const { isDarkMode, toggleTheme, colors } = useTheme(); | ||
| const [isAnimating, setIsAnimating] = React.useState(false); | ||
|
|
||
| const handleToggle = () => { | ||
| setIsAnimating(true); | ||
| toggleTheme(); | ||
| setTimeout(() => setIsAnimating(false), 500); | ||
| }; | ||
|
Comment on lines
+63
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using You can update <IconWrapper animate={isAnimating} onAnimationEnd={() => setIsAnimating(false)}>
<Icon fontSize="large" />
</IconWrapper> const handleToggle = () => {
setIsAnimating(true);
toggleTheme();
};
Comment on lines
+63
to
+67
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clear the animation timeout on unmount to avoid setState after unmount. If the component unmounts within 500ms, the timeout callback will still fire. 🧹 Suggested cleanup export const DarkModeToggle = () => {
const { isDarkMode, toggleTheme, colors } = useTheme();
const [isAnimating, setIsAnimating] = React.useState(false);
+ const timeoutRef = React.useRef();
+ React.useEffect(
+ () => () => {
+ if (timeoutRef.current) clearTimeout(timeoutRef.current);
+ },
+ []
+ );
const handleToggle = () => {
setIsAnimating(true);
toggleTheme();
- setTimeout(() => setIsAnimating(false), 500);
+ if (timeoutRef.current) clearTimeout(timeoutRef.current);
+ timeoutRef.current = setTimeout(() => setIsAnimating(false), 500);
};🤖 Prompt for AI Agents |
||
|
|
||
| const tooltipTitle = isDarkMode ? 'Switch to Light Mode' : 'Switch to Dark Mode'; | ||
| const bgColor = isDarkMode ? colors.surface : colors.primary; | ||
| const textColor = isDarkMode ? colors.accent : colors.text; | ||
| const hoverBgColor = isDarkMode ? colors.hover : colors.secondary; | ||
| const Icon = isDarkMode ? Brightness7 : Brightness4; | ||
|
|
||
| return ( | ||
| <Tooltip title={tooltipTitle} placement="left" arrow> | ||
| <ToggleButton | ||
| onClick={handleToggle} | ||
| aria-label="toggle dark mode" | ||
| bgcolor={bgColor} | ||
| color={textColor} | ||
| hoverbg={hoverBgColor} | ||
| shadowcolor={colors.shadow} | ||
| > | ||
| <IconWrapper animate={isAnimating}> | ||
| <Icon fontSize="large" /> | ||
| </IconWrapper> | ||
| </ToggleButton> | ||
| </Tooltip> | ||
| ); | ||
| }; | ||
|
|
||
| export default DarkModeToggle; | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,70 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import React from 'react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { render, screen, fireEvent } from '@testing-library/react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import '@testing-library/jest-dom'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { DarkModeToggle } from '../index'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| import { ThemeProvider } from '@app/contexts/themeContext'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| describe('DarkModeToggle', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const renderWithTheme = (initialMode = 'light') => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| if (initialMode === 'dark') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| localStorage.setItem('theme', 'dark'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| localStorage.setItem('theme', 'light'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| return render( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| <ThemeProvider> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| <DarkModeToggle /> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| </ThemeProvider> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| beforeEach(() => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| localStorage.clear(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| it('should render the toggle button', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| renderWithTheme(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const button = screen.getByRole('button', { name: /toggle dark mode/i }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(button).toBeInTheDocument(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| it('should show sun icon in dark mode', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| renderWithTheme('dark'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const button = screen.getByRole('button'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(button).toBeInTheDocument(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| it('should show moon icon in light mode', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| renderWithTheme('light'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const button = screen.getByRole('button'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(button).toBeInTheDocument(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+32
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tests only check for the presence of the button, not the specific icon for each theme mode. To make these tests more meaningful, you should assert that the correct icon is rendered for each mode. Material-UI icons have a
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| it('should toggle theme when clicked', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| renderWithTheme(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const button = screen.getByRole('button', { name: /toggle dark mode/i }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| // Click to switch to dark mode | ||||||||||||||||||||||||||||||||||||||||||||||||||
| fireEvent.click(button); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(localStorage.getItem('theme')).toBe('dark'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| // Click to switch back to light mode | ||||||||||||||||||||||||||||||||||||||||||||||||||
| fireEvent.click(button); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(localStorage.getItem('theme')).toBe('light'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+44
to
+55
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make the theme-toggle assertion resilient to effect timing.
✅ Suggested fix-import { render, screen, fireEvent } from '@testing-library/react';
+import { render, screen, fireEvent, waitFor } from '@testing-library/react';
...
- it('should toggle theme when clicked', () => {
+ it('should toggle theme when clicked', async () => {
renderWithTheme();
const button = screen.getByRole('button', { name: /toggle dark mode/i });
// Click to switch to dark mode
fireEvent.click(button);
- expect(localStorage.getItem('theme')).toBe('dark');
+ await waitFor(() => expect(localStorage.getItem('theme')).toBe('dark'));
// Click to switch back to light mode
fireEvent.click(button);
- expect(localStorage.getItem('theme')).toBe('light');
+ await waitFor(() => expect(localStorage.getItem('theme')).toBe('light'));
});📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| it('should have fixed positioning', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| renderWithTheme(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const button = screen.getByRole('button'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const styles = window.getComputedStyle(button); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| expect(styles.position).toBe('fixed'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| it('should show tooltip on hover', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| renderWithTheme(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| const button = screen.getByRole('button'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| fireEvent.mouseOver(button); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // Material-UI tooltips appear after a delay | ||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+64
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test for the tooltip is incomplete. Since Material-UI tooltips appear asynchronously after a delay, you need to use an it('should show tooltip on hover', async () => {
renderWithTheme();
const button = screen.getByRole('button');
fireEvent.mouseOver(button);
const tooltip = await screen.findByRole('tooltip');
expect(tooltip).toBeInTheDocument();
expect(tooltip).toHaveTextContent('Switch to Dark Mode');
});
Comment on lines
+64
to
+69
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tooltip test has no assertion. It currently can pass even if the tooltip is broken. Add an assertion (or remove the test). 🧪 Suggested assertion-import { render, screen, fireEvent } from '@testing-library/react';
+import { render, screen, fireEvent, act } from '@testing-library/react';
...
it('should show tooltip on hover', () => {
+ jest.useFakeTimers();
renderWithTheme();
const button = screen.getByRole('button');
fireEvent.mouseOver(button);
- // Material-UI tooltips appear after a delay
+ act(() => {
+ jest.runAllTimers();
+ });
+ expect(screen.getByRole('tooltip')).toHaveTextContent(/switch to dark mode/i);
+ jest.useRealTimers();
});🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.