-
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(DarkModeToggler): Migrate and add stories to theme toggler 🎉 (#5236
) Co-authored-by: Claudio Wunder <cwunder@gnome.org> Co-authored-by: Claudio Wunder <cwunder@hubspot.com>
- Loading branch information
1 parent
79b5134
commit bb46dfe
Showing
12 changed files
with
218 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
build | ||
public | ||
storybook-static |
53 changes: 53 additions & 0 deletions
53
components/Common/DarkModeToggle/__tests__/__snapshots__/index.test.tsx.snap
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,53 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`DarkModeToggle Component render dark mode toggle 1`] = ` | ||
<div> | ||
<button | ||
aria-pressed="false" | ||
class="darkModeToggle" | ||
type="button" | ||
> | ||
<span | ||
class="sr-only" | ||
> | ||
components.header.buttons.toggleDarkMode | ||
</span> | ||
<svg | ||
class="light-mode-only" | ||
fill="currentColor" | ||
height="1em" | ||
stroke="currentColor" | ||
stroke-width="0" | ||
viewBox="0 0 24 24" | ||
width="1em" | ||
xmlns="http://www.w3.org/2000/svg" | ||
> | ||
<path | ||
d="M0 0h24v24H0V0z" | ||
fill="none" | ||
/> | ||
<path | ||
d="M14 2c1.82 0 3.53.5 5 1.35-2.99 1.73-5 4.95-5 8.65s2.01 6.92 5 8.65A9.973 9.973 0 0114 22C8.48 22 4 17.52 4 12S8.48 2 14 2z" | ||
/> | ||
</svg> | ||
<svg | ||
class="dark-mode-only" | ||
fill="currentColor" | ||
height="1em" | ||
stroke="currentColor" | ||
stroke-width="0" | ||
viewBox="0 0 24 24" | ||
width="1em" | ||
xmlns="http://www.w3.org/2000/svg" | ||
> | ||
<path | ||
d="M0 0h24v24H0z" | ||
fill="none" | ||
/> | ||
<path | ||
d="M12 7c-2.76 0-5 2.24-5 5s2.24 5 5 5 5-2.24 5-5-2.24-5-5-5zM2 13h2c.55 0 1-.45 1-1s-.45-1-1-1H2c-.55 0-1 .45-1 1s.45 1 1 1zm18 0h2c.55 0 1-.45 1-1s-.45-1-1-1h-2c-.55 0-1 .45-1 1s.45 1 1 1zM11 2v2c0 .55.45 1 1 1s1-.45 1-1V2c0-.55-.45-1-1-1s-1 .45-1 1zm0 18v2c0 .55.45 1 1 1s1-.45 1-1v-2c0-.55-.45-1-1-1s-1 .45-1 1zM5.99 4.58a.996.996 0 00-1.41 0 .996.996 0 000 1.41l1.06 1.06c.39.39 1.03.39 1.41 0s.39-1.03 0-1.41L5.99 4.58zm12.37 12.37a.996.996 0 00-1.41 0 .996.996 0 000 1.41l1.06 1.06c.39.39 1.03.39 1.41 0a.996.996 0 000-1.41l-1.06-1.06zm1.06-10.96a.996.996 0 000-1.41.996.996 0 00-1.41 0l-1.06 1.06c-.39.39-.39 1.03 0 1.41s1.03.39 1.41 0l1.06-1.06zM7.05 18.36a.996.996 0 000-1.41.996.996 0 00-1.41 0l-1.06 1.06c-.39.39-.39 1.03 0 1.41s1.03.39 1.41 0l1.06-1.06z" | ||
/> | ||
</svg> | ||
</button> | ||
</div> | ||
`; |
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,53 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import DarkModeToggle from '../index'; | ||
import { IntlProvider } from 'react-intl'; | ||
|
||
let mockCurrentTheme = ''; | ||
|
||
const mockToggleTheme = jest.fn().mockImplementation(() => { | ||
mockCurrentTheme = mockCurrentTheme === 'dark' ? 'light' : 'dark'; | ||
}); | ||
|
||
// Mock dark mode module for controlling dark mode HOC behaviour | ||
jest.mock('next-themes', () => ({ | ||
useTheme: () => { | ||
return { theme: mockCurrentTheme, setTheme: mockToggleTheme }; | ||
}, | ||
})); | ||
|
||
describe('DarkModeToggle Component', () => { | ||
it('render dark mode toggle', () => { | ||
const { container } = render( | ||
<IntlProvider locale="en" onError={() => {}}> | ||
<DarkModeToggle /> | ||
</IntlProvider> | ||
); | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
|
||
it('switches dark theme to light theme', () => { | ||
mockCurrentTheme = 'dark'; | ||
render( | ||
<IntlProvider locale="en" onError={() => {}}> | ||
<DarkModeToggle /> | ||
</IntlProvider> | ||
); | ||
const toggle = screen.getByRole('button'); | ||
userEvent.click(toggle); | ||
expect(mockCurrentTheme).toBe('light'); | ||
}); | ||
|
||
it('switches light theme to dark theme', () => { | ||
mockCurrentTheme = 'light'; | ||
render( | ||
<IntlProvider locale="en" onError={() => {}}> | ||
<DarkModeToggle /> | ||
</IntlProvider> | ||
); | ||
const toggle = screen.getByRole('button'); | ||
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,8 @@ | ||
.darkModeToggle { | ||
background: none; | ||
border: none; | ||
color: var(--color-text-accent); | ||
cursor: pointer; | ||
line-height: 0; | ||
padding: 0; | ||
} |
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 @@ | ||
import DarkModeToggle from '.'; | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
const meta: Meta<typeof DarkModeToggle> = { | ||
title: 'DarkModeToggle', | ||
component: DarkModeToggle, | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<typeof DarkModeToggle>; | ||
|
||
export const Default: Story = {}; |
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,37 @@ | ||
import React from 'react'; | ||
import { useTheme } from 'next-themes'; | ||
import { FormattedMessage } from 'react-intl'; | ||
import { MdLightMode, MdNightlight } from 'react-icons/md'; | ||
import styles from './index.module.scss'; | ||
|
||
const DarkModeToggle = () => { | ||
const { theme, setTheme } = useTheme(); | ||
|
||
const isDark = theme === 'dark'; | ||
|
||
const toggleTheme = (isKeyPress?: boolean) => { | ||
if (isKeyPress) { | ||
return; | ||
} | ||
|
||
setTheme(isDark ? 'light' : 'dark'); | ||
}; | ||
|
||
return ( | ||
<button | ||
type="button" | ||
className={styles.darkModeToggle} | ||
onClick={() => toggleTheme()} | ||
onKeyPress={() => toggleTheme(true)} | ||
aria-pressed={isDark} | ||
> | ||
<span className="sr-only"> | ||
<FormattedMessage id="components.header.buttons.toggleDarkMode" /> | ||
</span> | ||
<MdNightlight className="light-mode-only" /> | ||
<MdLightMode className="dark-mode-only" /> | ||
</button> | ||
); | ||
}; | ||
|
||
export default DarkModeToggle; |
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
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
bb46dfe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
nodejs-org-stories – ./
nodejs-org-stories-git-major-website-redesign-openjs.vercel.app
nodejs-org-stories-openjs.vercel.app
nodejs-org-storybook.vercel.app