-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(theme): add support for light theme (#130)
- Loading branch information
1 parent
716ccb1
commit 0ef841f
Showing
26 changed files
with
270 additions
and
69 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
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,74 @@ | ||
import { ConfigProvider, theme as antdTheme } from 'antd'; | ||
import { | ||
ReactNode, | ||
createContext, | ||
useContext, | ||
useEffect, | ||
useState, | ||
} from 'react'; | ||
|
||
type Theme = 'light' | 'dark'; | ||
|
||
interface ThemeContextProps { | ||
theme: Theme; | ||
toggleTheme: () => void; | ||
} | ||
|
||
export const ThemeContext = createContext<ThemeContextProps | undefined>( | ||
undefined, | ||
); | ||
|
||
export const useTheme = () => { | ||
const context = useContext(ThemeContext); | ||
if (!context) { | ||
throw new Error('useTheme must be used within a ThemeProvider'); | ||
} | ||
return context; | ||
}; | ||
|
||
export const ThemeProvider = ({ children }: { children: ReactNode }) => { | ||
// Initialize theme based on local storage or system preference | ||
const [theme, setTheme] = useState<Theme>(() => { | ||
if (typeof window === 'undefined') { | ||
return 'dark'; | ||
} | ||
const savedTheme = localStorage.getItem('theme'); | ||
if (savedTheme) { | ||
return savedTheme as Theme; | ||
} | ||
const prefersDark = window.matchMedia( | ||
'(prefers-color-scheme: dark)', | ||
).matches; | ||
return prefersDark ? 'dark' : 'light'; | ||
}); | ||
|
||
// Apply the theme to the HTML element and save it in localStorage | ||
useEffect(() => { | ||
document.documentElement.setAttribute('data-theme', theme); | ||
localStorage.setItem('theme', theme); | ||
}, [theme]); | ||
|
||
const toggleTheme = () => { | ||
setTheme((prevTheme) => (prevTheme === 'light' ? 'dark' : 'light')); | ||
}; | ||
|
||
// Ant Design's theme configuration | ||
const antdConfig = { | ||
token: { | ||
colorPrimary: '#0098ea', | ||
colorError: '#C84075', | ||
fontFamily: 'var(--font-body)', | ||
borderRadius: 4, | ||
}, | ||
algorithm: | ||
theme === 'dark' ? antdTheme.darkAlgorithm : antdTheme.defaultAlgorithm, | ||
}; | ||
|
||
return ( | ||
<ThemeContext.Provider value={{ theme, toggleTheme }}> | ||
<ConfigProvider theme={antdConfig}>{children}</ConfigProvider> | ||
</ThemeContext.Provider> | ||
); | ||
}; | ||
|
||
export default ThemeProvider; |
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 @@ | ||
export { ThemeContext, default, useTheme } from './ThemeProvider'; |
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 @@ | ||
export { default as Layout } from './Layout'; | ||
export { default as LogView } from './LogView'; | ||
export { default as ThemeProvider } from './ThemeProvider'; |
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
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
Oops, something went wrong.