-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(themeToggle): introduce (#6007)
* feat(themeToggle): introduce * fix * remove `ThemeProvider` from `preview.tsx` * update to use focus and `onKeyDown` * fix: code format * use classname instead of css module * remove toggling * fix: indentation * test: render put on `beforeEach` * separate icon * update * chore: code review changes * fix: style indentation * meta: corrected designs of themetoggle and languagedropdown --------- Co-authored-by: Claudio Wunder <[email protected]>
- Loading branch information
1 parent
ff41772
commit 3cd2d6d
Showing
9 changed files
with
125 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,38 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
import { LocaleProvider } from '@/providers/localeProvider'; | ||
|
||
import ThemeToggle from '../'; | ||
|
||
let mockCurrentTheme = 'light'; | ||
|
||
const toggleTheme = () => { | ||
mockCurrentTheme = mockCurrentTheme === 'light' ? 'dark' : 'light'; | ||
}; | ||
|
||
describe('ThemeToggle', () => { | ||
let toggle; | ||
|
||
beforeEach(() => { | ||
mockCurrentTheme = 'light'; | ||
|
||
render( | ||
<LocaleProvider> | ||
<ThemeToggle onClick={toggleTheme} /> | ||
</LocaleProvider> | ||
); | ||
toggle = screen.getByRole('button'); | ||
}); | ||
|
||
it('switches dark theme to light theme', async () => { | ||
mockCurrentTheme = 'dark'; | ||
await userEvent.click(toggle); | ||
expect(mockCurrentTheme).toBe('light'); | ||
}); | ||
|
||
it('switches light theme to dark theme', async () => { | ||
await userEvent.click(toggle); | ||
expect(mockCurrentTheme).toBe('dark'); | ||
}); | ||
}); |
This file contains 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,13 @@ | ||
.themeToggle { | ||
@apply h-9 | ||
w-9 | ||
rounded-md | ||
p-2 | ||
text-neutral-700 | ||
dark:text-neutral-300; | ||
|
||
&:hover { | ||
@apply bg-neutral-100 | ||
dark:bg-neutral-900; | ||
} | ||
} |
This file contains 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,10 @@ | ||
import type { Meta as MetaObj, StoryObj } from '@storybook/react'; | ||
|
||
import ThemeToggle from '@/components/Common/ThemeToggle'; | ||
|
||
type Story = StoryObj<typeof ThemeToggle>; | ||
type Meta = MetaObj<typeof ThemeToggle>; | ||
|
||
export const Default: Story = {}; | ||
|
||
export default { component: ThemeToggle } as Meta; |
This file contains 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,31 @@ | ||
import { MoonIcon, SunIcon } from '@heroicons/react/24/outline'; | ||
import type { FC, MouseEvent } from 'react'; | ||
import { useIntl } from 'react-intl'; | ||
|
||
import styles from './index.module.css'; | ||
|
||
type ThemeToggleProps = { | ||
onClick?: (event: MouseEvent<HTMLButtonElement>) => void; | ||
}; | ||
|
||
const ThemeToggle: FC<ThemeToggleProps> = ({ onClick = () => {} }) => { | ||
const { formatMessage } = useIntl(); | ||
|
||
const ariaLabel = formatMessage({ | ||
id: 'components.header.buttons.toggleDarkMode', | ||
}); | ||
|
||
return ( | ||
<button | ||
type="button" | ||
onClick={onClick} | ||
className={styles.themeToggle} | ||
aria-label={ariaLabel} | ||
> | ||
<MoonIcon className="block dark:hidden" height="20" /> | ||
<SunIcon className="hidden dark:block" height="20" /> | ||
</button> | ||
); | ||
}; | ||
|
||
export default ThemeToggle; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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