-
Notifications
You must be signed in to change notification settings - Fork 525
feat: add dark mode #2322
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
Merged
Merged
feat: add dark mode #2322
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8fcc565
feat: add theme provider
kaf-lamed-beyt e8d30c3
Update src/index.css
kaf-lamed-beyt 4a61e2a
Update src/index.css
kaf-lamed-beyt 09ec2ca
Update src/index.css
kaf-lamed-beyt 180dd54
Update src/hooks/theme.ts
kaf-lamed-beyt 80ae8f2
Update src/context/theme-provider.tsx
kaf-lamed-beyt ea53fa6
Update src/hooks/theme.ts
kaf-lamed-beyt 86e3fee
Update src/hooks/theme.ts
kaf-lamed-beyt f0c5ad4
refactor: useTheme hook based on feedback provided
kaf-lamed-beyt b4b1c95
chore: refactor the theme hoook
kaf-lamed-beyt 06cb705
fix: theme uses system settings and listens for changes
SgtPooki 17a8263
fix: storybook testing
SgtPooki f8ecade
fix: theme hook and provider context values
SgtPooki 27920ce
Merge branch 'main' into dark-mode
SgtPooki 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
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
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
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,53 @@ | ||
.theme-toggle { | ||
--size: 1.55rem; | ||
--icon-fill: hsl(210, 22%, 22%); | ||
--icon-fill-hover: hsl(210, 22%, 12%); | ||
|
||
background: none; | ||
border: none; | ||
padding: 0; | ||
inline-size: var(--size); | ||
block-size: var(--size); | ||
aspect-ratio: 1; | ||
border-radius: 50%; | ||
cursor: pointer; | ||
touch-action: manipulation; | ||
-webkit-tap-highlight-color: transparent; | ||
outline-offset: 5px; | ||
} | ||
|
||
.theme-toggle > svg { | ||
inline-size: 100%; | ||
block-size: 100%; | ||
stroke-linecap: round; | ||
color: #378085; | ||
} | ||
|
||
[data-theme='dark'] .theme-toggle { | ||
--icon-fill: hsl(25, 100%, 50%); | ||
--icon-fill-hover: hsl(25, 100%, 40%); | ||
|
||
svg { | ||
color: #fff; | ||
} | ||
} | ||
|
||
.theme-toggle:hover, | ||
.theme-toggle:focus-visible { | ||
background: hsl(0 0% 50% / 0.1); | ||
} | ||
|
||
@media (prefers-reduced-motion: no-preference) { | ||
.theme-toggle { | ||
transition: background-color 0.3s ease; | ||
} | ||
|
||
.theme-toggle > svg { | ||
transition: transform 0.5s ease; | ||
} | ||
|
||
.theme-toggle:hover > svg, | ||
.theme-toggle:focus-visible > svg { | ||
transform: scale(1.1); | ||
} | ||
} |
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,50 @@ | ||
import React from 'react' | ||
import './theme-toggle.css' | ||
import { useTheme } from '../../hooks/theme' | ||
|
||
export const ThemeToggle = () => { | ||
const { darkTheme: isDarkTheme, toggleTheme } = useTheme() | ||
return ( | ||
<button | ||
className="theme-toggle" | ||
onClick={() => toggleTheme()} | ||
onKeyDown={toggleTheme} | ||
tabIndex={0} | ||
aria-label={`Toggle ${isDarkTheme ? 'dark' : 'light'} mode`} | ||
role="switch" | ||
aria-checked={isDarkTheme} | ||
> | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
width="24" | ||
height="24" | ||
viewBox="0 0 24 24" | ||
fill="none" | ||
stroke="currentColor" | ||
strokeWidth="2" | ||
strokeLinecap="round" | ||
strokeLinejoin="round" | ||
aria-hidden="true" | ||
color="#fff" | ||
> | ||
{isDarkTheme | ||
? ( | ||
<> | ||
<circle cx="12" cy="12" r="5" /> | ||
<line x1="12" y1="1" x2="12" y2="3" /> | ||
<line x1="12" y1="21" x2="12" y2="23" /> | ||
<line x1="4.22" y1="4.22" x2="5.64" y2="5.64" /> | ||
<line x1="18.36" y1="18.36" x2="19.78" y2="19.78" /> | ||
<line x1="1" y1="12" x2="3" y2="12" /> | ||
<line x1="21" y1="12" x2="23" y2="12" /> | ||
<line x1="4.22" y1="19.78" x2="5.64" y2="18.36" /> | ||
<line x1="18.36" y1="5.64" x2="19.78" y2="4.22" /> | ||
</> | ||
) | ||
: ( | ||
<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" /> | ||
)} | ||
</svg> | ||
</button> | ||
) | ||
} |
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,54 @@ | ||
import React from 'react' | ||
|
||
export interface ThemeProviderProps { | ||
children: React.ReactNode | ||
} | ||
SgtPooki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export type Theme = 'light' | 'dark' | ||
|
||
export type ThemeContextValues = { | ||
darkTheme: boolean, | ||
toggleTheme: (event?: React.KeyboardEvent<HTMLButtonElement> | React.MouseEvent<HTMLButtonElement>) => void; | ||
} | ||
|
||
export const ThemeContext = React.createContext<ThemeContextValues | null>(null) | ||
|
||
export const ThemeProvider = ({ children }: ThemeProviderProps) => { | ||
SgtPooki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const [isDarkTheme, setDarkTheme] = React.useState<boolean>(() => { | ||
const savedTheme = | ||
typeof window !== 'undefined' && localStorage.getItem('theme') | ||
if (savedTheme) return savedTheme === 'dark' | ||
return window.matchMedia('prefers-color-scheme: dark').matches | ||
}) | ||
React.useEffect(() => { | ||
const htmlElem = document.documentElement | ||
const currentTheme = isDarkTheme ? 'dark' : 'light' | ||
htmlElem.setAttribute('data-theme', currentTheme) | ||
localStorage.setItem('theme', currentTheme) | ||
htmlElem.setAttribute('aria-label', `Current theme: ${currentTheme}`) | ||
}, [isDarkTheme]) | ||
const toggleTheme = (event: React.KeyboardEvent<HTMLButtonElement> | React.MouseEvent<HTMLButtonElement>) => { | ||
if (event.type === 'keydown') { | ||
const isKeyboardEvent = (event: React.KeyboardEvent | React.MouseEvent): event is React.KeyboardEvent<HTMLButtonElement> => { | ||
return event.type === 'keydown' | ||
} | ||
if (isKeyboardEvent(event)) { | ||
if (event.key === ' ') { | ||
event.preventDefault() | ||
setDarkTheme((prevTheme) => !prevTheme) | ||
} | ||
} | ||
} | ||
} | ||
const values: ThemeContextValues = { | ||
darkTheme: isDarkTheme, | ||
toggleTheme: (event) => { | ||
event ? toggleTheme(event) : setDarkTheme(prevTheme => !prevTheme) | ||
} | ||
} | ||
return ( | ||
<ThemeContext.Provider value={values}> | ||
{children} | ||
</ThemeContext.Provider> | ||
) | ||
} |
Oops, something went wrong.
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.