-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from gorules/feat/dark-mode
feat: add dark mode
- Loading branch information
Showing
8 changed files
with
144 additions
and
28 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React, { createContext, useContext, useEffect, useMemo, useState } from 'react'; | ||
import { JdmConfigProvider } from '@gorules/jdm-editor'; | ||
import { ConfigProvider, theme } from 'antd'; | ||
import { match } from 'ts-pattern'; | ||
|
||
const colorMediaQuery = () => window.matchMedia('(prefers-color-scheme: dark)'); | ||
|
||
export enum ThemePreference { | ||
Automatic = 'automatic', | ||
Dark = 'dark', | ||
Light = 'light', | ||
} | ||
|
||
type ThemeContextState = { | ||
themePreference: ThemePreference; | ||
setThemePreference: (preference: ThemePreference) => void; | ||
}; | ||
|
||
// eslint-disable-next-line | ||
export const ThemeContext = createContext<ThemeContextState>({} as any); | ||
|
||
export const ThemeContextProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => { | ||
const [themePreference, setThemePreferenceInternal] = useState<ThemePreference>(() => { | ||
return match(localStorage.getItem('themePreference')) | ||
.with('dark', () => ThemePreference.Dark) | ||
.with('light', () => ThemePreference.Light) | ||
.otherwise(() => ThemePreference.Automatic); | ||
}); | ||
|
||
const [isAutomaticDark, setIsAutomaticDark] = useState(() => colorMediaQuery().matches); | ||
|
||
const isDarkTheme = useMemo<boolean>(() => { | ||
return match(themePreference) | ||
.with(ThemePreference.Dark, () => true) | ||
.with(ThemePreference.Light, () => false) | ||
.otherwise(() => isAutomaticDark); | ||
}, [themePreference, isAutomaticDark]); | ||
|
||
useEffect(() => { | ||
const eventTarget = colorMediaQuery(); | ||
const listener = (event: MediaQueryListEvent) => { | ||
setIsAutomaticDark(event.matches); | ||
}; | ||
|
||
eventTarget.addEventListener('change', listener); | ||
return () => { | ||
eventTarget.removeEventListener('change', listener); | ||
}; | ||
}, []); | ||
|
||
useEffect(() => { | ||
document.body.setAttribute('data-theme', isDarkTheme ? 'dark' : 'light'); | ||
}, [isDarkTheme]); | ||
|
||
const setThemePreference = (preference: ThemePreference) => { | ||
setThemePreferenceInternal(preference); | ||
localStorage.setItem('themePreference', preference); | ||
}; | ||
|
||
return ( | ||
<ConfigProvider theme={{ algorithm: isDarkTheme ? theme.darkAlgorithm : theme.defaultAlgorithm }}> | ||
<ThemeContext.Provider value={{ themePreference, setThemePreference }}> | ||
<JdmConfigProvider theme={{ mode: isDarkTheme ? 'dark' : 'light' }}>{children}</JdmConfigProvider> | ||
</ThemeContext.Provider> | ||
</ConfigProvider> | ||
); | ||
}; | ||
|
||
export const useTheme = () => useContext(ThemeContext); | ||
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 |
---|---|---|
|
@@ -33,7 +33,6 @@ | |
overflow-y: auto; | ||
display: flex; | ||
gap: 8px; | ||
max-height: calc(100vh - 80px); | ||
} | ||
|
||
.content { | ||
|
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